change vconf key for single mode actived assistant 05/203305/9
authorzg84.zhang <zg84.zhang@samsung.com>
Fri, 12 Apr 2019 09:51:46 +0000 (17:51 +0800)
committerzg84.zhang <zg84.zhang@samsung.com>
Sat, 20 Apr 2019 01:54:02 +0000 (09:54 +0800)
Change-Id: Icf29b970694e8727f625edbc465e032828eb460a

common/multi_assistant_settings.c

index 3655fab54f55308b3977c6bfd81a820c7402c304..798b4b73cec9780aa8cdd2c629f3815df7a5721b 100644 (file)
 #endif
 #define LOG_TAG "ma-assistant-setting"
 
-#define APPID_URL "db/multi-assistant/enabled_status/%s"
-
 #define ENABLED_ASSISTANT_URL "db/multi-assistant/enabled_assistants"
 
 #define DEFAULT_ASSISTANT_URL "db/multi-assistant/default_assistant_appid"
 
 #define MODE_URL "db/multi-assistant/multiple_mode"
 
-#define MAX_LEN 100
+#define MAX_LEN 256
 
 bool ma_settings_get_multiple_mode() {
-    int res;
-    vconf_get_bool(MODE_URL, &res);
-    return res;
+       int res;
+       vconf_get_bool(MODE_URL, &res);
+       return res;
 }
 
 int ma_settings_set_multiple_mode(bool multiple) {
-    if (!ma_settings_get_multiple_mode()) {
-
-        if (vconf_set_bool(MODE_URL, multiple)) {
-            return MA_ERROR_NONE;
-        } else {
-            return MA_ERROR_OPERATION_FAILED;
-        }
-
-    } else {
-        return MA_ERROR_OPERATION_FAILED;
-    }
+       if (multiple != ma_settings_get_multiple_mode()) {
+               if (vconf_set_bool(MODE_URL, multiple)) {
+                       return MA_ERROR_NONE;
+               } else {
+                       return MA_ERROR_OPERATION_FAILED;
+               }
+       } else {
+               return MA_ERROR_OPERATION_FAILED;
+       }
 }
 
 int ma_settings_change_voice_assistant(const char* app_id) {
-    int res = MA_ERROR_NONE;
-    char* previous_assistant = vconf_get_str(ENABLED_ASSISTANT_URL);
-    if (!ma_settings_get_multiple_mode() &&
-        previous_assistant &&
-        app_id) {
-
-        if(strncmp(app_id, previous_assistant, MAX_LEN) != 0) {
-            char new_assistant_url[MAX_LEN] = "", previous_assistant_url[MAX_LEN] = "";
-            snprintf(new_assistant_url, MAX_LEN, APPID_URL, app_id);
-            snprintf(previous_assistant_url, MAX_LEN, APPID_URL, previous_assistant);
-
-            if (vconf_set_bool(new_assistant_url, true) &&
-                vconf_set_bool(previous_assistant_url, false) &&
-                vconf_set_str(ENABLED_ASSISTANT_URL, app_id)) {
-                res = MA_ERROR_NONE;
-            } else {
-                res = MA_ERROR_OPERATION_FAILED;
-            }
-
-        } else {
-            res = MA_ERROR_NOT_SUPPORTED;
-        }
-
-    } else {
-        res = MA_ERROR_OPERATION_FAILED;
-    }
-
-    if (previous_assistant) {
-        free(previous_assistant);
-    }
-    return res;
+       int res = MA_ERROR_NONE;
+       char* previous_assistant = vconf_get_str(DEFAULT_ASSISTANT_URL);
+       if (!ma_settings_get_multiple_mode() &&
+               previous_assistant &&
+               app_id) {
+
+               if(strncmp(app_id, previous_assistant, MAX_LEN) != 0) {
+                       if (vconf_set_str(DEFAULT_ASSISTANT_URL, app_id)) {
+                               res = MA_ERROR_NONE;
+                       } else {
+                               res = MA_ERROR_OPERATION_FAILED;
+                       }
+
+               } else {
+                       res = MA_ERROR_NOT_SUPPORTED;
+               }
+
+       } else {
+               res = MA_ERROR_OPERATION_FAILED;
+       }
+
+       if (previous_assistant) {
+               free(previous_assistant);
+       }
+       return res;
 }
 
 int ma_settings_set_voice_assistant_enabled(const char* app_id, bool enabled) {
-    if (ma_settings_get_multiple_mode() && app_id != NULL) {
-        char assistant_url[MAX_LEN] = "";
-        snprintf(assistant_url, MAX_LEN, APPID_URL, app_id);
-
-        if (vconf_set_bool(assistant_url, enabled)) {
-            return MA_ERROR_NONE;
-        } else {
-            return MA_ERROR_OPERATION_FAILED;
-        }
-
-    } else {
-        return MA_ERROR_ENGINE_NOT_FOUND;
-    }
+       if (!ma_settings_get_multiple_mode()) {
+               return MA_ERROR_NOT_SUPPORTED;
+       }
+       int res = MA_ERROR_NONE;
+       if (NULL != app_id) {
+               char* enabled_assistants = vconf_get_str(ENABLED_ASSISTANT_URL);
+               if (enabled) {
+                       if (NULL != enabled_assistants) {
+                               char* tmp = strstr(enabled_assistants, app_id);
+                               if (tmp) {
+                                       res = MA_ERROR_NOT_SUPPORTED;
+                               } else {
+                                       char assistants[MAX_LEN] = "";
+                                       snprintf(assistants, MAX_LEN, "%s%s;", enabled_assistants, app_id);
+                                       if (vconf_set_str(ENABLED_ASSISTANT_URL, assistants)) {
+                                               res = MA_ERROR_NONE;
+                                       } else {
+                                               res = MA_ERROR_OPERATION_FAILED;
+                                       }
+                               }
+                       } else {
+                               res = MA_ERROR_OPERATION_FAILED;
+                       }
+               } else {
+                       if (NULL != enabled_assistants) {
+                               char* tmp = strstr(enabled_assistants, app_id);
+                               if (tmp) {
+                                       char assistants[MAX_LEN] = "";
+                                       char* outptr = NULL;
+                                       char* as = strtok_r(enabled_assistants, ";", &outptr);
+                                       while (as) {
+                                               if (strncmp(app_id, as, MAX_LEN)) {
+                                                       if ((strlen(as) + strlen(assistants) + 1) <= MAX_LEN) {
+                                                               strncat(assistants, as, strlen(as));
+                                                               strncat(assistants, ";", 1);
+                                                       } else {
+                                                               res = MA_ERROR_OPERATION_FAILED;
+                                                               if (enabled_assistants) {
+                                                                       free(enabled_assistants);
+                                                               }
+                                                               return res;
+                                                       }
+                                               }
+                                               as = strtok_r(NULL, ";", &outptr);
+                                       }
+                                       if (vconf_set_str(ENABLED_ASSISTANT_URL, assistants)) {
+                                               res = MA_ERROR_NONE;
+                                       } else {
+                                               res = MA_ERROR_OPERATION_FAILED;
+                                       }
+                               } else {
+                                       res = MA_ERROR_NOT_SUPPORTED;
+                               }
+                       } else {
+                               res = MA_ERROR_OPERATION_FAILED;
+                       }
+
+               }
+               if (enabled_assistants) {
+                       free(enabled_assistants);
+               }
+       } else {
+               res = MA_ERROR_ENGINE_NOT_FOUND;
+       }
+       return res;
 }
 
 int ma_settings_set_default_voice_assistant(const char* app_id) {
-    int res = MA_ERROR_NONE;
-    char* previous_assistant = vconf_get_str(DEFAULT_ASSISTANT_URL);
-    if (ma_settings_get_multiple_mode() &&
-        previous_assistant &&
-        app_id) {
-
-       if(strncmp(app_id, previous_assistant, MAX_LEN) != 0) {
-
-            if (vconf_set_str(DEFAULT_ASSISTANT_URL, app_id)) {
-                res = MA_ERROR_NONE;
-            } else {
-                res = MA_ERROR_OPERATION_FAILED;
-            }
-
-        } else {
-            res = MA_ERROR_NOT_SUPPORTED;
-        }
-
-    } else {
-        res = MA_ERROR_OPERATION_FAILED;
-    }
-
-    if (previous_assistant) {
-        free(previous_assistant);
-    }
-    return res;
-}
\ No newline at end of file
+       int res = MA_ERROR_NONE;
+       char* previous_assistant = vconf_get_str(DEFAULT_ASSISTANT_URL);
+       if (ma_settings_get_multiple_mode() &&
+               previous_assistant &&
+               app_id) {
+               if(strncmp(app_id, previous_assistant, MAX_LEN) != 0) {
+                       if (vconf_set_str(DEFAULT_ASSISTANT_URL, app_id)) {
+                               res = MA_ERROR_NONE;
+                       } else {
+                               res = MA_ERROR_OPERATION_FAILED;
+                       }
+               } else {
+                       res = MA_ERROR_NOT_SUPPORTED;
+               }
+       } else {
+               res = MA_ERROR_OPERATION_FAILED;
+       }
+       if (previous_assistant) {
+               free(previous_assistant);
+       }
+       return res;
+}