[Service] Provide webapis.isRunningApp() API 55/282855/4
authorDongHyun Song <dh81.song@samsung.com>
Wed, 12 Oct 2022 08:58:44 +0000 (17:58 +0900)
committerDongHyun Song <dh81.song@samsung.com>
Thu, 20 Oct 2022 04:06:46 +0000 (13:06 +0900)
Conventionally, to know an app if it is alive with webapis, it should
get all alive packages info and check if the subject app ID is included
on them. This is very inefficient way.

So, WRT did provide below API recently.
 - wrt.isRunningApp(appId: string): bool

This patch will webapis.isRunningApp(appId: strig) also for the service
apps.

Change-Id: If20751d6b47913ebb9836463b307d5aa97f76e65
Signed-off-by: DongHyun Song <dh81.song@samsung.com>
wrt_app/service/device_api_router.ts

index cde4134..7754a63 100644 (file)
@@ -76,14 +76,43 @@ export class DeviceAPIRouter {
     global.webapis.getAppIdsByMetadata = (metadata: string) => {
       return wrt.getAppIdsByMetadata(metadata);
     }
+    global.webapis.isRunningApp = async (appId: string) => {
+      if (wrt.isRunningApp) {
+        return new Promise((resolve) => {
+          resolve(wrt.isRunningApp(appId));
+        });
+      } else {
+        const getAppsContext = async (appId: string): Promise<boolean> => {
+          return new Promise((resolve) => {
+            try {
+              global.tizen.application.getAppsContext((contexts: any) => {
+                let isRunningApp = false;
+                for (let i = 0; i < contexts.length; i++) {
+                  if (appId === contexts[i].appId) {
+                    isRunningApp = true;
+                    break;
+                  }
+                }
+                resolve(isRunningApp);
+              });
+            } catch (e) {
+              console.log(`tizen.application.getAppsContext() has failed with ${e}`);
+              resolve(false);
+            }
+          });
+        }
+        return await getAppsContext(appId);
+      }
+    }
     Object.defineProperties(global.webapis, {
       getAppIdsByMetadata: { writable: false, enumerable: true },
       getCallerAppId: { writable: false, enumerable: true },
       getPackageId: { writable: false, enumerable: true },
-      getServiceId: { writable: false, enumerable: true },
-      postPlainNotification: { writable: false, enumerable: true },
       getPreviewData: { writable: false, enumerable: true },
       getProfile: { writable: false, enumerable: true },
+      getServiceId: { writable: false, enumerable: true },
+      isRunningApp: { writable: false, enumerable: true },
+      postPlainNotification: { writable: false, enumerable: true },
     });
     this.initMDEWebapis();
     this.initEdgeWebapis();