從內容中心和網址調度程式中導入

"hooks": {
    "openstore": {
        "apparmor": "openstore/openstore.apparmor",
        "desktop": "openstore/openstore.desktop",
        "urls": "openstore/openstore.url-dispatcher",
        "content-hub": "openstore/openstore-contenthub.json"
    }
},

In the previous guide we have seen a little bit about how Content Hub works. In this guide we will see how URLdispatcher works and how to handle imported data from the Content Hub.

處理 Content Hub 中的數據

../../_images/02ichu.png

open-store.io 中的 OpenStore 應用程式

測試應用程式的最簡單方法之一是在 Telegram 上向自己發送測試 Click,並透過內容中心使用 OpenStore 打開該 Click 文件:

../../_images/03ichu.png

如果我們點擊 OpenStore 應用程式,它將被打開,它將詢問我們是否要安裝 Click 檔案。讓我們來看看 「main. qml 應用程式的代碼 <https://github.com/UbuntuOpenStore/openstore-app/blob/master/openstore/Main.qml#L85>」_ 它是如何完成的:

Connections {
        target: ContentHub

        onImportRequested: {
            var filePath = String(transfer.items[0].url).replace('file://', '')
            print("Should import file", filePath)
            var fileName = filePath.split("/").pop();
            var popup = PopupUtils.open(installQuestion, root, {fileName: fileName});
            popup.accepted.connect(function() {
                contentHubInstallInProgress = true;
                PlatformIntegration.clickInstaller.installPackage(filePath)
            })
        }
}

Do you see that Connections element that targets the ContentHub? When it receives the signal onImportRequested, it will take the url of the data sent from the Content Hub (transfer.items[0].url is the url of the first data sent) and open a PopUp element to let the user install the click.

那個 URLdispatcher 怎麼樣?

URL dispatcher 程序是一個類似於內容中心的軟體,它使我們的生活更輕鬆,嘗試為特定協議選擇正確的應用程式。 例如:我們可能希望在點擊 http 協議時打開網路瀏覽器。 如果我們點擊地圖鏈接,使用 uNav 打開它或在 Twitter 應用程式中打開推特鏈接是很方便的! 這是如何運作的?

The URLdispatcher selects which app (according to their manifest.json) will open a certain link.

../../_images/05ichu.png

Let’s see how our navigation app looks inside. uNav’s manifest shows special hooks for the URLdispatcher in its manifest.json code:

1  [
2     {
3         "protocol": "http",
4         "domain-suffix": "map.unav.me"
5     },
6     {
7         "protocol": "http",
8         "domain-suffix": "unav-go.github.io"
9     },
10    {
11        "protocol": "geo"
12    },
13    {
14        "protocol": "http",
15        "domain-suffix": "www.openstreetmap.org"
16    },
17    {
18        "protocol": "http",
19        "domain-suffix": "www.opencyclemap.org"
20    },
21    {
22        "protocol": "https",
23        "domain-suffix": "maps.google.com"
24    }
25 ]

這意味著一個看起來像 http://map.unav.me/xxxxx,xxxxx 的連結將在 uNav 中打開。這在第2行和第3行中得到了定義,它在其中查找協定 http,然後是 map.unav.me。

此外,網址格式的 geo:xxx,xxx 應在 uNav 中打開,因為它在第11行中定義了。

我們如何管理收到的網址?

After the URLdispatcher sends the link to the correspondent app, we need to handle that URL or URI in the targeted app. Let’s see how to do that:

In the main qml file, we need to add some code to know what to do with the dispatched URL. Let’s check how Linphone app manages this adding a connection to the URI Handler with a Connections element setting UriHandler as a target.

Connections {
    target: UriHandler

    onOpened: {
        console.log('Open from UriHandler')

        if (uris.length > 0) {
            console.log('Incoming call from UriHandler ' + uris[0]);
            showIncomingCall(uris[0]);
        }
    }
}

This code will manage an URI in the form linphone://sip:xxx@xxx.xx when the app is opened. But what do we need to do to handle this link when the app is closed?

We need to add a little bit extra code that will cover two cases: 1) We receive one URL 2) We receive more than one URL

Let’s check if Qt.application.arguments is not empty and if not, if any argument matches our URI format.

Component.onCompleted: {
    //Check if opened the app because we have an incoming call
    if (Qt.application.arguments && Qt.application.arguments.length > 0) {

        for (var i = 0; i < Qt.application.arguments.length; i++) {
            if (Qt.application.arguments[i].match(/^linphone/)) {
                console.log("Incoming Call on Closed App")
                showIncomingCall(Qt.application.arguments[i]);
            }
        }
    }

    //Start timer for Registering Status
    checkStatus.start()
}

Remember to check that %u (to receive 1 URL) or %U (to receive 1 or more URLs) is present under the Exec line in the .desktop file of the app.

工具

From command line, lomiri-url-dispatcher-dump command will give you the full list of registered protocol schemes and their corresponding app.

Another usefull tool, but not installed by default on devices is lomiri-url-dispatcher-tools, it allows you to simulate a call from a third party app. e.g: lomiri-url-dispatcher https://youtu.be/CIX-a-i6B1w will launch youtube webapp.

To install, it make your partition writable (sudo mount -o rw,remount /) and install it via sudo apt install lomiri-url-dispatcher-tools

如果多個應用程式定義了相同的網址類型,會發生什麼?

A very good question. What happens if we tap on a Twitter link? How is such a URL handled by the URLdispatcher as protocol http or the protocol http://twitter?

如果兩個應用程式具有相同的定義協議會發生什麼?

現在是時候進行一些測試並在下一個指南中分享結果。