Reload plugin modules depending on pkgmgr event 05/217205/2
authorJi-hoon Lee <dalton.lee@samsung.com>
Thu, 7 Nov 2019 10:07:56 +0000 (19:07 +0900)
committerJi-hoon Lee <dalton.lee@samsung.com>
Fri, 8 Nov 2019 06:05:23 +0000 (15:05 +0900)
Change-Id: I5c12c6600b80f2657e371b15c4eeac13c0044722

CMakeLists.txt
src/multi_assistant_service.c
src/multi_assistant_service_plugin.c

index 1cf3c04..1717bcf 100644 (file)
@@ -28,6 +28,7 @@ pkg_check_modules(pkgs REQUIRED
        libxml-2.0
        multi-assistant
        vconf
+       pkgmgr-info
 )
 
 # SET FLAG ---------------------------------------------------------------------------------------
index 137c7de..fc2f3cb 100644 (file)
@@ -22,6 +22,8 @@
 #include <malloc.h>
 #include <Ecore.h>
 #include <vconf.h>
+#include <package_manager.h>
+#include <pkgmgr-info.h>
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -70,6 +72,7 @@ typedef struct {
 static int g_current_maclient_info = 0;
 static int g_current_preprocessing_maclient_info = -1;
 static const char *g_wakeup_maclient_appid = NULL;
+static package_manager_h g_pkgmgr = NULL;
 
 static PREPROCESSING_STATE g_current_preprocessing_state = PREPROCESSING_STATE_NONE;
 
@@ -738,12 +741,15 @@ static int init_plugin(void)
 
 static int deinit_plugin(void)
 {
+       if (0 != multi_assistant_service_plugin_deactivate()) {
+               MAS_LOGE("Fail to deactivate");
+       }
        if (0 != multi_assistant_service_plugin_deinitialize()) {
                MAS_LOGE("Fail to deinitialize");
        }
 }
 
-static int process_multi_assistant_activated_setting()
+static int process_activated_setting()
 {
        if (0 == vconf_notify_key_changed(MULTI_ASSISTANT_SETTINGS_ACTIVATED, mas_active_state_changed_cb, NULL)) {
                /* Activate / deactivate according to the vconf key setting */
@@ -780,7 +786,7 @@ static int init_wakeup(void)
 
        init_plugin();
 
-       process_multi_assistant_activated_setting();
+       process_activated_setting();
 
        mas_prelaunch_default_assistant();
 
@@ -1239,6 +1245,112 @@ int mas_process_preprocessing_state_event(PREPROCESSING_STATE_EVENT event)
        return 0;
 }
 
+static int pkg_app_list_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
+{
+       char *appid = NULL;
+
+       int ret = pkgmgrinfo_appinfo_get_appid (handle, &appid);
+       if (PMINFO_R_OK == ret && NULL != appid) {
+               int *result = (int*)user_data;
+               if (result) {
+                       for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM;loop++) {
+                               if (g_maclient_info[loop].used) {
+                                       LOGD("comparing appid : %s %s", g_maclient_info[loop].wakeup_engine, appid);
+                                       if (0 == strncmp(g_maclient_info[loop].wakeup_engine, appid, MAX_APPID_LEN)) {
+                                               *result = 1;
+                                       }
+                               }
+                       }
+               }
+       } else {
+               LOGE("pkgmgrinfo_appinfo_get_appid failed! error code=%d", ret);
+               return 0;
+       }
+
+       return 0;
+}
+
+/*
+INFO: Package install/update/uninstall scenario
+Install and Uninstall are obviously simple.
+   Install: just INSTALL
+   Uninstall: just UNINSTALL
+Update package (change the source codes and Run As again), there are four scenarios:
+1. UPDATE
+   Source code change
+2. UNINSTALL -> INSTALL
+   This happens when Tizen IDE Property > Tizen SDK > Rapid Development Support > Check "Enable Project specific settings"
+   and change Application ID in tizen-manifest.xml file and Run As.
+3. UPDATE -> INSTALL
+   This happens when Tizen IDE Property > Tizen SDK > Rapid Development Support > Uncheck "Enable Project specific settings"
+   and change Application ID in tizen-manifest.xml file and Run As.
+   At UPDATE event, pkgid (package parameter) is invalid...
+4. UPDATE
+   Exceptionally, only UPDATE can be called when Application ID in tizen-manifest.xml file is changed.
+   At UPDATE event, pkgid (package parameter) is valid, and only appid is changed; the previous appid is invalid.
+*/
+static void _package_manager_event_cb(const char *type, const char *package, package_manager_event_type_e event_type, package_manager_event_state_e event_state, int progress, package_manager_error_e error, void *user_data)
+{
+       int ret = 0;
+       uid_t uid = getuid ();
+       pkgmgrinfo_pkginfo_h handle = NULL;
+       static bool in_progress = false;
+
+       if (!package || !type)
+               return;
+
+       if (PACKAGE_MANAGER_EVENT_TYPE_UPDATE != event_type &&
+               PACKAGE_MANAGER_EVENT_TYPE_INSTALL != event_type &&
+               PACKAGE_MANAGER_EVENT_TYPE_UNINSTALL != event_type)
+               return;
+
+       if (PACKAGE_MANAGER_EVENT_STATE_STARTED != event_state &&
+               PACKAGE_MANAGER_EVENT_STATE_COMPLETED != event_state)
+               return;
+
+       bool user = false;
+       LOGD("type=%s package=%s event_type=%d event_state=%d progress=%d error=%d",
+               type, package, event_type, event_state, progress, error);
+       ret = pkgmgrinfo_pkginfo_get_pkginfo(package, &handle);
+       if (ret != PMINFO_R_OK || NULL == handle) {
+               LOGW("Failed to call pkgmgrinfo_pkginfo_get_pkginfo & get_usr_pkginfo(\"%s\",~) returned %d, uid : %d", package, ret, getuid ());
+               /* Try to get in user packages */
+               ret = pkgmgrinfo_pkginfo_get_usr_pkginfo (package, uid, &handle);
+               if (ret != PMINFO_R_OK || NULL == handle) {
+                       LOGW("Failed to call pkgmgrinfo_pkginfo_get_pkginfo & get_usr_pkginfo(\"%s\",~) returned %d, uid : %d", package, ret, getuid ());
+                       return;
+               }
+       }
+
+       if (user) {
+               /* Try to get in user packages */
+               pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_ALL_APP, pkg_app_list_cb, (void *)&ret, uid);
+       }  else {
+               /* Try to get in global packages */
+               pkgmgrinfo_appinfo_get_list(handle, PMINFO_ALL_APP, pkg_app_list_cb, (void *)&ret);
+       }
+       if (1 == ret) {
+               if (PACKAGE_MANAGER_EVENT_STATE_STARTED == event_state) {
+                       LOGD("processing PACKAGE_MANAGER_EVENT_STATE_STARTED event");
+                       if (false == in_progress) {
+                               in_progress = true;
+                               deinit_plugin();
+                       }
+               } else if (PACKAGE_MANAGER_EVENT_STATE_COMPLETED == event_state) {
+                       LOGD("processing PACKAGE_MANAGER_EVENT_STATE_COMPLETED event");
+                       if (true == in_progress) {
+                               init_plugin();
+                               process_activated_setting();
+                               in_progress = false;
+                       }
+               }
+       }
+
+       pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
+
+       return;
+}
+
 bool service_app_create(void *data)
 {
        // Todo: add your code here.
@@ -1250,11 +1362,33 @@ bool service_app_create(void *data)
                return false;
        }
 
+       if (!g_pkgmgr) {
+               int ret = package_manager_create(&g_pkgmgr);
+               if (ret == PACKAGE_MANAGER_ERROR_NONE) {
+                       ret = package_manager_set_event_cb(g_pkgmgr, _package_manager_event_cb, NULL);
+                       if (ret == PACKAGE_MANAGER_ERROR_NONE) {
+                               LOGD("package_manager_set_event_cb succeeded.");
+                       }
+                       else {
+                               LOGE("package_manager_set_event_cb failed(%d)", ret);
+                       }
+               }
+               else {
+                       LOGE("package_manager_create failed(%d)", ret);
+               }
+       }
+
        return true;
 }
 
 void service_app_terminate(void *data)
 {
+       if (g_pkgmgr) {
+               package_manager_unset_event_cb(g_pkgmgr);
+               package_manager_destroy(g_pkgmgr);
+               g_pkgmgr = NULL;
+       }
+
        // Todo: add your code here.
        deinit_wakeup();
        return;
@@ -1263,19 +1397,6 @@ void service_app_terminate(void *data)
 void service_app_control(app_control_h app_control, void *data)
 {
        // Todo: add your code here.
-       char *operation = NULL;
-       int ret = app_control_get_operation(app_control, &operation);
-       if (ret == APP_CONTROL_ERROR_NONE && operation) {
-               //strncmp(operation, APP_CONTROL_OPERATION_LAUNCH_ON_EVENT, strlen(operation)) == 0){
-               char* value = NULL;
-               app_control_get_extra_data(app_control, "event", &value);
-               LOGD("operation : %s , %s", operation, value);
-               if (value && 0 == strcmp(value, "reload")) {
-                       deinit_plugin();
-                       init_plugin();
-                       process_multi_assistant_activated_setting();
-               }
-       }
        return;
 }
 
index b9f5f7a..e62a25f 100644 (file)
@@ -511,7 +511,6 @@ int multi_assistant_service_plugin_deinitialize(void)
                MAS_LOGD("[Recorder ERROR] File not found!");
        }
 #endif
-       multi_assistant_service_plugin_deactivate();
 
        int ret = -1;
        if (NULL != g_handle) {