|
7 | 7 | * @author Yelin Jeong <[email protected]> |
8 | 8 | */ |
9 | 9 |
|
| 10 | +let gServiceAppId = "lfebC6UrMY.nsdservice"; |
| 11 | +export let gRemoteServices = new Map(); |
| 12 | + |
10 | 13 | /** |
11 | 14 | * Get currently used network type |
12 | 15 | * @returns the network type |
@@ -34,3 +37,97 @@ export async function getIpAddress(networkType) { |
34 | 37 | ); |
35 | 38 | }); |
36 | 39 | } |
| 40 | + |
| 41 | +function launchServiceApp() { |
| 42 | + function onSuccess() { |
| 43 | + console.log("Service App launched successfully"); |
| 44 | + } |
| 45 | + |
| 46 | + function onError(err) { |
| 47 | + console.error("Service App launch failed", err); |
| 48 | + } |
| 49 | + |
| 50 | + try { |
| 51 | + console.log("Launching [" + gServiceAppId + "] ..."); |
| 52 | + tizen.application.launch(gServiceAppId, onSuccess, onError); |
| 53 | + } catch (exc) { |
| 54 | + console.error("Exception while launching HybridServiceApp: " + exc.message); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +function onGetAppsContextSuccess(contexts) { |
| 59 | + let i = 0; |
| 60 | + let appInfo = null; |
| 61 | + |
| 62 | + for (i = 0; i < contexts.length; i = i + 1) { |
| 63 | + try { |
| 64 | + appInfo = tizen.application.getAppInfo(contexts[i].appId); |
| 65 | + } catch (exc) { |
| 66 | + console.error("Exception while getting application info " + exc.message); |
| 67 | + } |
| 68 | + |
| 69 | + if (appInfo.id === gServiceAppId) { |
| 70 | + break; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if (i >= contexts.length) { |
| 75 | + console.log("Service App not found, Trying to launch service app"); |
| 76 | + launchServiceApp(); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +function onGetAppsContextError(err) { |
| 81 | + console.error("getAppsContext exc", err); |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Starts obtaining information about applications |
| 86 | + * that are currently running on a device. |
| 87 | + */ |
| 88 | +export function startHybridService() { |
| 89 | + try { |
| 90 | + tizen.application.getAppsContext( |
| 91 | + onGetAppsContextSuccess, |
| 92 | + onGetAppsContextError, |
| 93 | + ); |
| 94 | + } catch (e) { |
| 95 | + console.log("Get AppContext failed, " + e); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +export function startMessagePort() { |
| 100 | + try { |
| 101 | + const addService = |
| 102 | + tizen.messageport.requestLocalMessagePort("STATE_AVAILABLE"); |
| 103 | + addService.addMessagePortListener(function (data) { |
| 104 | + let remoteService = new Object(); |
| 105 | + let serviceName = ""; |
| 106 | + for (var i = 0; i < data.length; i++) { |
| 107 | + if (data[i].key == "name") { |
| 108 | + var name = data[i].value.split(" ")[0]; |
| 109 | + serviceName = name; |
| 110 | + } else remoteService[data[i].key] = data[i].value; |
| 111 | + } |
| 112 | + if (gRemoteServices.has(serviceName)) { |
| 113 | + gRemoteServices.delete(serviceName); |
| 114 | + } |
| 115 | + gRemoteServices.set(serviceName, remoteService); |
| 116 | + }); |
| 117 | + |
| 118 | + const removeService = |
| 119 | + tizen.messageport.requestLocalMessagePort("STATE_UNAVAILABLE"); |
| 120 | + removeService.addMessagePortListener(function (data) { |
| 121 | + let serviceName = ""; |
| 122 | + for (var i = 0; i < data.length; i++) { |
| 123 | + if (data[i].key == "name") { |
| 124 | + var name = data[i].value.split("(")[0]; |
| 125 | + serviceName = name; |
| 126 | + } |
| 127 | + } |
| 128 | + gRemoteServices.delete(serviceName); |
| 129 | + }); |
| 130 | + } catch (e) { |
| 131 | + console.log("Failed to create local message port " + e.name); |
| 132 | + } |
| 133 | +} |
0 commit comments