From 21a78dbd123b7149b4471297aac5f0f71063bd08 Mon Sep 17 00:00:00 2001 From: Yelin Jeong Date: Mon, 1 Jul 2024 17:28:09 +0900 Subject: [PATCH] [Tizen/Web] Launch native service in web application This patch launchs image classification offloading service in web application. Signed-off-by: Yelin Jeong --- .../ImageClassificationOffloading/js/main.js | 3 +- .../ImageClassificationOffloading/js/utils.js | 60 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/Tizen.web/ImageClassificationOffloading/js/main.js b/Tizen.web/ImageClassificationOffloading/js/main.js index 9f36bec5..b411788f 100644 --- a/Tizen.web/ImageClassificationOffloading/js/main.js +++ b/Tizen.web/ImageClassificationOffloading/js/main.js @@ -13,6 +13,7 @@ import { GetMaxIdx, GetImgPath, loadLabelInfo, + startHybridService, } from "./utils.js"; let fHandle = null; @@ -160,7 +161,7 @@ window.onload = async function () { const networkType = await getNetworkType(); ip = await getIpAddress(networkType); labels = loadLabelInfo(); - + startHybridService(); document.getElementById("start_local").addEventListener("click", function () { runLocal(); }); diff --git a/Tizen.web/ImageClassificationOffloading/js/utils.js b/Tizen.web/ImageClassificationOffloading/js/utils.js index 783a53ca..d7696263 100644 --- a/Tizen.web/ImageClassificationOffloading/js/utils.js +++ b/Tizen.web/ImageClassificationOffloading/js/utils.js @@ -7,6 +7,8 @@ * @author Yelin Jeong */ +let gServiceAppId = "EQmf4iSfpX.imageclassificationoffloadingservice"; + /** * Get currently used network type * @returns the network type @@ -72,3 +74,61 @@ export function loadLabelInfo() { const labelList = fHandle.readString(); return labelList.split("\n"); } + +function launchServiceApp() { + function onSuccess() { + console.log("Service App launched successfully"); + } + + function onError(err) { + console.error("Service App launch failed", err); + } + + try { + console.log("Launching [" + gServiceAppId + "] ..."); + tizen.application.launch(gServiceAppId, onSuccess, onError); + } catch (exc) { + console.error("Exception while launching HybridServiceApp: " + exc.message); + } +} + +function onGetAppsContextSuccess(contexts) { + let i = 0; + let appInfo = null; + + for (i = 0; i < contexts.length; i = i + 1) { + try { + appInfo = tizen.application.getAppInfo(contexts[i].appId); + } catch (exc) { + console.error("Exception while getting application info " + exc.message); + } + + if (appInfo.id === gServiceAppId) { + break; + } + } + + if (i >= contexts.length) { + console.log("Service App not found, Trying to launch service app"); + launchServiceApp(); + } +} + +function onGetAppsContextError(err) { + console.error("getAppsContext exc", err); +} + +/** + * Starts obtaining information about applications + * that are currently running on a device. + */ +export function startHybridService() { + try { + tizen.application.getAppsContext( + onGetAppsContextSuccess, + onGetAppsContextError, + ); + } catch (e) { + console.log("Get AppContext failed, " + e); + } +}