From: Ji-hoon Lee Date: Wed, 1 Apr 2020 15:03:59 +0000 (+0900) Subject: Extract aul dependency from service_main.cpp X-Git-Tag: submit/tizen/20200409.014926~7 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=68f452ad396d89e55a647e0025fd7aba0aeecf1e;p=platform%2Fcore%2Fuifw%2Fmulti-assistant-service.git Extract aul dependency from service_main.cpp Change-Id: Ifd796751933776c0153e8c78d345b94542ac37da --- diff --git a/inc/application_manager.h b/inc/application_manager.h index f6733fc..08b2586 100644 --- a/inc/application_manager.h +++ b/inc/application_manager.h @@ -18,6 +18,7 @@ #ifndef __APPLICATION_MANAGER_H__ #define __APPLICATION_MANAGER_H__ +#include #include #ifdef __cplusplus @@ -27,6 +28,10 @@ extern "C" { class IApplicationManager { public: virtual bool is_application_running(int pid) = 0; + virtual bool is_application_running(const std::string& appid) = 0; + virtual bool bring_app_to_foreground(const std::string& appid) = 0; + virtual bool launch_app_async(const std::string& appid, bool background) = 0; + virtual boost::optional get_appid_by_pid(int pid) = 0; }; #ifdef __cplusplus diff --git a/inc/application_manager_aul.h b/inc/application_manager_aul.h index 2c47dc2..b0b3dd3 100644 --- a/inc/application_manager_aul.h +++ b/inc/application_manager_aul.h @@ -20,8 +20,6 @@ #include "application_manager.h" -#include - #ifdef __cplusplus extern "C" { #endif @@ -29,6 +27,10 @@ extern "C" { class CApplicationManagerAul : public IApplicationManager { public: virtual bool is_application_running(int pid) override; + virtual bool is_application_running(const std::string& appid) override; + virtual bool bring_app_to_foreground(const std::string& appid) override; + virtual bool launch_app_async(const std::string& appid, bool background) override; + virtual boost::optional get_appid_by_pid(int pid) override; }; #ifdef __cplusplus diff --git a/inc/service_common.h b/inc/service_common.h index ecef87d..747b57a 100644 --- a/inc/service_common.h +++ b/inc/service_common.h @@ -125,6 +125,8 @@ typedef void (*wakeup_service_voice_key_status_changed_cb)(ma_voice_key_status_e #define MAX_SUPPORTED_LANGUAGES_NUM 255 #define MAX_SUPPORTED_LANGUAGE_LEN 16 +#define MAX_APPID_LEN 255 + typedef enum { VOICE_KEY_SUPPORT_MODE_NONE, VOICE_KEY_SUPPORT_MODE_PUSH_TO_TALK, diff --git a/inc/service_main.h b/inc/service_main.h index 4bfaddd..4a6ddfa 100644 --- a/inc/service_main.h +++ b/inc/service_main.h @@ -37,8 +37,6 @@ extern "C" { #endif -#define MAX_APPID_LEN 255 - typedef enum { CLIENT_LAUNCH_MODE_ACTIVATION, CLIENT_LAUNCH_MODE_PRELAUNCH, diff --git a/src/application_manager_aul.cpp b/src/application_manager_aul.cpp index 2553c93..5cf8855 100644 --- a/src/application_manager_aul.cpp +++ b/src/application_manager_aul.cpp @@ -18,6 +18,7 @@ #include "service_common.h" #include +#include bool CApplicationManagerAul::is_application_running(int pid) { @@ -27,4 +28,77 @@ bool CApplicationManagerAul::is_application_running(int pid) return false; } return true; -} \ No newline at end of file +} + +bool CApplicationManagerAul::is_application_running(const std::string& appid) +{ + return (!!aul_app_is_running(appid.c_str())); +} + +bool CApplicationManagerAul::bring_app_to_foreground(const std::string& appid) +{ + bool ret = true; + + /* Bring MA client to foreground - is there a better way other than launching? */ + bundle *b = NULL; + b = bundle_create(); + if (NULL == b) { + MAS_LOGE("Failed creating bundle for aul operation"); + return -1; + } + + int result = aul_launch_app(appid.c_str(), b); + if (result < AUL_R_OK) { + MAS_LOGE("ERROR : aul_launch_app failed. app_id [%s] bundle[%p] result[%d : %s]", + appid.c_str(), b, result, get_error_message(result)); + ret = false; + } + + if (b) bundle_free(b); + b = NULL; + + return ret; +} + +bool CApplicationManagerAul::launch_app_async(const std::string& appid, bool background) +{ + bool ret = true; + + bundle *b = NULL; + b = bundle_create(); + if (NULL == b) { + MAS_LOGE("Failed creating bundle for aul operation"); + return false; + } + + int result = aul_svc_set_background_launch(b, background); + if (result < AUL_R_OK) { + MAS_LOGE("ERROR : aul_svc_set_background_launch failed. app_id [%s] bundle[%p] result[%d : %s]", + appid.c_str(), b, result, get_error_message(result)); + } + + result = aul_launch_app_async(appid.c_str(), b); + if (result < AUL_R_OK) { + MAS_LOGE("ERROR : aul_launch_app_async failed. app_id [%s] bundle[%p] result[%d : %s]", + appid.c_str(), b, result, get_error_message(result)); + ret = false; + } + + if (b) bundle_free(b); + b = NULL; + + return ret; +} + +boost::optional CApplicationManagerAul::get_appid_by_pid(int pid) +{ + boost::optional ret; + + char appid[MAX_APPID_LEN] = {'\0', }; + if (AUL_R_OK == aul_app_get_appid_bypid(pid, appid, sizeof(appid))) { + appid[MAX_APPID_LEN - 1] = '\0'; + ret = std::string{appid}; + } + + return ret; +} diff --git a/src/service_main.cpp b/src/service_main.cpp index cad2c4a..5e660f9 100644 --- a/src/service_main.cpp +++ b/src/service_main.cpp @@ -18,8 +18,6 @@ #include #include #include -#include -#include #include #include #include @@ -160,13 +158,12 @@ int CServiceMain::mas_client_send_result(int pid, const char* display_text, MAS_LOGE("[ERROR] Fail to send result, ret(%d)", ret); } - const char* pid_appid = NULL; - char buf[MAX_APPID_LEN] = {'\0', }; - if (AUL_R_OK == aul_app_get_appid_bypid(pid, buf, sizeof(buf))) { - buf[MAX_APPID_LEN - 1] = '\0'; - pid_appid = buf; + std::string pid_appid; + boost::optional appid_by_pid = mApplicationManager.get_appid_by_pid(pid); + if (appid_by_pid) { + pid_appid = *appid_by_pid; } - mServicePlugin.update_recognition_result(pid_appid, MA_RECOGNITION_RESULT_EVENT_SUCCESS); + mServicePlugin.update_recognition_result(pid_appid.c_str(), MA_RECOGNITION_RESULT_EVENT_SUCCESS); return ret; } @@ -179,13 +176,12 @@ int CServiceMain::mas_client_send_recognition_result(int pid, int result) MAS_LOGE("[ERROR] Fail to send recognition result, ret(%d)", ret); } - const char* pid_appid = NULL; - char buf[MAX_APPID_LEN] = {'\0', }; - if (AUL_R_OK == aul_app_get_appid_bypid(pid, buf, sizeof(buf))) { - buf[MAX_APPID_LEN - 1] = '\0'; - pid_appid = buf; + std::string pid_appid; + boost::optional appid_by_pid = mApplicationManager.get_appid_by_pid(pid); + if (appid_by_pid) { + pid_appid = *appid_by_pid; } - mServicePlugin.update_recognition_result(pid_appid, result); + mServicePlugin.update_recognition_result(pid_appid.c_str(), result); return ret; } @@ -229,52 +225,48 @@ int CServiceMain::mas_client_stop_streaming_audio_data(int pid, int type) int CServiceMain::mas_client_update_voice_feedback_state(int pid, int state) { - const char* pid_appid = NULL; - char buf[MAX_APPID_LEN] = {'\0', }; - if (AUL_R_OK == aul_app_get_appid_bypid(pid, buf, sizeof(buf))) { - buf[MAX_APPID_LEN - 1] = '\0'; - pid_appid = buf; + std::string pid_appid; + boost::optional appid_by_pid = mApplicationManager.get_appid_by_pid(pid); + if (appid_by_pid) { + pid_appid = *appid_by_pid; } - mServicePlugin.update_voice_feedback_state(pid_appid, state); + mServicePlugin.update_voice_feedback_state(pid_appid.c_str(), state); return 0; } int CServiceMain::mas_client_set_assistant_specific_command(int pid, const char *command) { - const char* pid_appid = NULL; - char buf[MAX_APPID_LEN] = {'\0', }; - if (AUL_R_OK == aul_app_get_appid_bypid(pid, buf, sizeof(buf))) { - buf[MAX_APPID_LEN - 1] = '\0'; - pid_appid = buf; + std::string pid_appid; + boost::optional appid_by_pid = mApplicationManager.get_appid_by_pid(pid); + if (appid_by_pid) { + pid_appid = *appid_by_pid; } - mServicePlugin.set_assistant_specific_command(pid_appid, command); + mServicePlugin.set_assistant_specific_command(pid_appid.c_str(), command); return 0; } int CServiceMain::mas_client_set_background_volume(int pid, double ratio) { - const char* pid_appid = NULL; - char buf[MAX_APPID_LEN] = {'\0', }; - if (AUL_R_OK == aul_app_get_appid_bypid(pid, buf, sizeof(buf))) { - buf[MAX_APPID_LEN - 1] = '\0'; - pid_appid = buf; + std::string pid_appid; + boost::optional appid_by_pid = mApplicationManager.get_appid_by_pid(pid); + if (appid_by_pid) { + pid_appid = *appid_by_pid; } - mServicePlugin.set_background_volume(pid_appid, ratio); + mServicePlugin.set_background_volume(pid_appid.c_str(), ratio); return 0; } int CServiceMain::mas_client_set_preprocessing_allow_mode(int pid, ma_preprocessing_allow_mode_e mode, const char* appid) { - const char* pid_appid = NULL; - char buf[MAX_APPID_LEN] = {'\0', }; - if (AUL_R_OK == aul_app_get_appid_bypid(pid, buf, sizeof(buf))) { - buf[MAX_APPID_LEN - 1] = '\0'; - pid_appid = buf; + std::string pid_appid; + boost::optional appid_by_pid = mApplicationManager.get_appid_by_pid(pid); + if (appid_by_pid) { + pid_appid = *appid_by_pid; } for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) { if (mClientInfo[loop].used) { - if (pid_appid && strncmp(pid_appid, mClientInfo[loop].appid, MAX_APPID_LEN) == 0) { + if (0 == pid_appid.compare(mClientInfo[loop].appid)) { mClientInfo[loop].preprocessing_allow_mode = mode; if (appid) { strncpy(mClientInfo[loop].preprocessing_allow_appid, appid, MAX_APPID_LEN); @@ -293,13 +285,12 @@ int CServiceMain::mas_client_set_preprocessing_allow_mode(int pid, ma_preprocess int CServiceMain::mas_client_send_preprocessing_result(int pid, bool result) { - const char* pid_appid = NULL; - char buf[MAX_APPID_LEN] = {'\0', }; - if (AUL_R_OK == aul_app_get_appid_bypid(pid, buf, sizeof(buf))) { - buf[MAX_APPID_LEN - 1] = '\0'; - pid_appid = buf; + std::string pid_appid; + boost::optional appid_by_pid = mApplicationManager.get_appid_by_pid(pid); + if (appid_by_pid) { + pid_appid = *appid_by_pid; } - if (!is_current_preprocessing_assistant(pid_appid)) return -1; + if (!is_current_preprocessing_assistant(pid_appid.c_str())) return -1; const char *current_maclient_appid = NULL; if (mCurrentClientInfo >= 0 && mCurrentClientInfo < MAX_MACLIENT_INFO_NUM) { @@ -307,7 +298,7 @@ int CServiceMain::mas_client_send_preprocessing_result(int pid, bool result) } if (result) { - MAS_LOGD("Preprocessing succeeded, bring (%s) to foreground", pid_appid); + MAS_LOGD("Preprocessing succeeded, bring (%s) to foreground", pid_appid.c_str()); mas_process_preprocessing_state_event(PREPROCESSING_STATE_EVENT_PREPROCESSING_SUCCEEDED); } else { MAS_LOGD("Preprocessing failed, bring (%s) to foreground", current_maclient_appid); @@ -325,49 +316,44 @@ int CServiceMain::mas_client_send_preprocessing_result(int pid, bool result) int CServiceMain::mas_client_set_wake_word_audio_require_flag(int pid, bool require) { - const char* pid_appid = NULL; - char buf[MAX_APPID_LEN] = {'\0', }; - if (AUL_R_OK == aul_app_get_appid_bypid(pid, buf, sizeof(buf))) { - buf[MAX_APPID_LEN - 1] = '\0'; - pid_appid = buf; + std::string pid_appid; + boost::optional appid_by_pid = mApplicationManager.get_appid_by_pid(pid); + if (appid_by_pid) { + pid_appid = *appid_by_pid; } - mServicePlugin.set_wake_word_audio_require_flag(pid_appid, require); + mServicePlugin.set_wake_word_audio_require_flag(pid_appid.c_str(), require); return 0; } int CServiceMain::mas_client_set_assistant_language(int pid, const char* language) { - const char* pid_appid = NULL; - char buf[MAX_APPID_LEN] = {'\0', }; - int ret = aul_app_get_appid_bypid(pid, buf, sizeof(buf)); - if (AUL_R_OK == ret) { - buf[MAX_APPID_LEN - 1] = '\0'; - pid_appid = buf; + std::string pid_appid; + boost::optional appid_by_pid = mApplicationManager.get_appid_by_pid(pid); + if (appid_by_pid) { + pid_appid = *appid_by_pid; } - mServicePlugin.set_assistant_language(pid_appid, language); + mServicePlugin.set_assistant_language(pid_appid.c_str(), language); return 0; } int CServiceMain::mas_client_add_wake_word(int pid, const char* wake_word, const char* language) { - const char* pid_appid = NULL; - char buf[MAX_APPID_LEN] = {'\0', }; - int ret = aul_app_get_appid_bypid(pid, buf, sizeof(buf)); - if (AUL_R_OK == ret) { - buf[MAX_APPID_LEN - 1] = '\0'; - pid_appid = buf; + std::string pid_appid; + boost::optional appid_by_pid = mApplicationManager.get_appid_by_pid(pid); + if (appid_by_pid) { + pid_appid = *appid_by_pid; } for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) { if (mClientInfo[loop].used && - 0 == strncmp(buf, mClientInfo[loop].appid, MAX_APPID_LEN)) { - ret = mServiceConfig.add_custom_wake_word(wake_word, language, + 0 == pid_appid.compare(mClientInfo[loop].appid)) { + int ret = mServiceConfig.add_custom_wake_word(wake_word, language, mClientInfo[loop].wakeup_word, mClientInfo[loop].wakeup_language); if (0 == ret) { - mServiceConfig.save_custom_wake_words(pid_appid, + mServiceConfig.save_custom_wake_words(pid_appid.c_str(), mClientInfo[loop].wakeup_word, mClientInfo[loop].wakeup_language); } else { @@ -377,35 +363,33 @@ int CServiceMain::mas_client_add_wake_word(int pid, const char* wake_word, const } } - mServicePlugin.add_assistant_wakeup_word(pid_appid, wake_word, language); + mServicePlugin.add_assistant_wakeup_word(pid_appid.c_str(), wake_word, language); return 0; } int CServiceMain::mas_client_remove_wake_word(int pid, const char* wake_word, const char* language) { - const char* pid_appid = NULL; - char buf[MAX_APPID_LEN] = {'\0', }; - int ret = aul_app_get_appid_bypid(pid, buf, sizeof(buf)); - if (AUL_R_OK == ret) { - buf[MAX_APPID_LEN - 1] = '\0'; - pid_appid = buf; + std::string pid_appid; + boost::optional appid_by_pid = mApplicationManager.get_appid_by_pid(pid); + if (appid_by_pid) { + pid_appid = *appid_by_pid; } for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) { if (mClientInfo[loop].used && - 0 == strncmp(buf, mClientInfo[loop].appid, MAX_APPID_LEN)) { - ret = mServiceConfig.remove_custom_wake_word(wake_word, language, + 0 == pid_appid.compare(mClientInfo[loop].appid)) { + int ret = mServiceConfig.remove_custom_wake_word(wake_word, language, mClientInfo[loop].wakeup_word, mClientInfo[loop].wakeup_language); if (0 == ret) { - mServiceConfig.save_custom_wake_words(pid_appid, + mServiceConfig.save_custom_wake_words(pid_appid.c_str(), mClientInfo[loop].wakeup_word, mClientInfo[loop].wakeup_language); } } } - mServicePlugin.remove_assistant_wakeup_word(pid_appid, wake_word, language); + mServicePlugin.remove_assistant_wakeup_word(pid_appid.c_str(), wake_word, language); return 0; } @@ -720,10 +704,8 @@ int CServiceMain::mas_get_client_pid_by_appid(const char *appid) ret = mClientManager.find_client_pid_by_appid(std::string{appid}); } - int status = aul_app_get_status_bypid(ret); - if (-1 != ret && 0 > status) { - MAS_LOGE("The PID for %s was %d, but it seems to be terminated : %d", - (appid ? appid : "NULL"), ret, status); + if (-1 != ret && !mApplicationManager.is_application_running(ret)) { + MAS_LOGE("The PID for %s was %d, but it seems to be terminated", appid, ret); on_deinitialize(ret); ret = -1; } @@ -879,36 +861,22 @@ int CServiceMain::mas_set_current_client_by_appid(const char *appid) int CServiceMain::mas_launch_client_by_appid(const char *appid, CLIENT_LAUNCH_MODE launch_mode) { + int result = 0; + if (NULL == appid || 0 == strlen(appid)) { MAS_LOGE("appid invalid, failed launching MA Client"); return -1; } if (CLIENT_LAUNCH_MODE_PRELAUNCH == launch_mode) { - if (1 == aul_app_is_running(appid)) { + if (mApplicationManager.is_application_running(appid)) { MAS_LOGE("appid %s is already running, no need for a prelaunch", appid); return -1; } - } - - bundle *b = NULL; - b = bundle_create(); - if (NULL == b) { - MAS_LOGE("Failed creating bundle for aul operation"); - return -1; - } - int result = aul_svc_set_background_launch(b, - (CLIENT_LAUNCH_MODE_PRELAUNCH == launch_mode ? TRUE : FALSE)); - if (result < AUL_R_OK) { - MAS_LOGE("ERROR : aul_svc_set_background_launch failed. app_id [%s] bundle[%p] result[%d : %s]", - appid, b, result, get_error_message(result)); - } - - result = aul_launch_app_async(appid, b); - if (result < AUL_R_OK) { - MAS_LOGE("ERROR : aul_launch_app_async failed. app_id [%s] bundle[%p] result[%d : %s]", - appid, b, result, get_error_message(result)); + result = mApplicationManager.launch_app_async(appid, true); + } else { + result = mApplicationManager.launch_app_async(appid, false); } if (CLIENT_LAUNCH_MODE_ACTIVATION == launch_mode) { @@ -925,37 +893,23 @@ int CServiceMain::mas_launch_client_by_appid(const char *appid, CLIENT_LAUNCH_MO MAS_LOGD("mWakeupClientAppId : %s, %d", mWakeupClientAppId.c_str(), found); } - if (b) bundle_free(b); - b = NULL; - return result; } int CServiceMain::mas_bring_client_to_foreground(const char* appid) { - /* Bring MA client to foreground - is there a better way other than launching? */ + int ret = 0; + if (NULL == appid || 0 == strlen(appid)) { MAS_LOGE("appid invalid, failed launching MA Client"); return -1; } - bundle *b = NULL; - b = bundle_create(); - if (NULL == b) { - MAS_LOGE("Failed creating bundle for aul operation"); - return -1; - } - - int result = aul_launch_app(appid, b); - if (result < AUL_R_OK) { - MAS_LOGE("ERROR : aul_launch_app failed. app_id [%s] bundle[%p] result[%d : %s]", - appid, b, result, get_error_message(result)); + if (!mApplicationManager.bring_app_to_foreground(appid)) { + ret = -1; } - if (b) bundle_free(b); - b = NULL; - - return result; + return ret; } int CServiceMain::mas_launch_client_by_wakeup_word(const char *wakeup_word) @@ -972,7 +926,7 @@ int CServiceMain::mas_prelaunch_default_assistant() if (prelaunch_mode && *prelaunch_mode) { const char *default_assistant = NULL; if (0 == mServicePlugin.get_default_assistant(&default_assistant)) { - if (0 == aul_app_is_running(default_assistant)) { + if (!(mApplicationManager.is_application_running(default_assistant))) { MAS_LOGD("prelaunching default_assistant_appid : %s", default_assistant); mas_launch_client_by_appid(default_assistant, CLIENT_LAUNCH_MODE_PRELAUNCH); } @@ -1382,19 +1336,19 @@ void CServiceMain::app_terminate(void *data) int CServiceMain::on_initialize(int pid) { MAS_LOGD("[Enter] pid(%d)", pid); - char appid[MAX_APPID_LEN] = {'\0', }; - if (AUL_R_OK == aul_app_get_appid_bypid(pid, appid, sizeof(appid))) { - appid[MAX_APPID_LEN - 1] = '\0'; - - MAS_LOGD("appid for pid %d is : %s", pid, (appid ? appid : "Unknown")); + std::string pid_appid; + boost::optional appid_by_pid = mApplicationManager.get_appid_by_pid(pid); + if (appid_by_pid) { + pid_appid = *appid_by_pid; + MAS_LOGD("appid for pid %d is : %s", pid, pid_appid.c_str()); /* Remove existing client that has same appid, if there's any */ - mClientManager.destroy_client_by_appid(appid); + mClientManager.destroy_client_by_appid(pid_appid.c_str()); /* And remove a client that has same pid also */ mClientManager.destroy_client_by_pid(pid); - mClientManager.create_client(pid, appid); + mClientManager.create_client(pid, pid_appid.c_str()); const char *current_maclient_appid = NULL; if (mCurrentClientInfo >= 0 && mCurrentClientInfo < MAX_MACLIENT_INFO_NUM) { @@ -1405,16 +1359,16 @@ int CServiceMain::on_initialize(int pid) { if (MA_VOICE_KEY_STATUS_PRESSED == mLastVoiceKeyStatus) { mas_client_send_voice_key_status_change(pid, mLastVoiceKeyStatus); } - if (current_maclient_appid && 0 == strncmp(current_maclient_appid, appid, MAX_APPID_LEN)) { + if (current_maclient_appid && 0 == pid_appid.compare(current_maclient_appid)) { MAS_LOGD("MA client with current maclient appid connected!"); - if (0 == mWakeupClientAppId.compare(appid)) { + if (0 == mWakeupClientAppId.compare(pid_appid)) { mWakeupClientAppId.clear(); mServiceIpc.active_state_change(pid, MA_ACTIVE_STATE_ACTIVE); mas_process_preprocessing_state_event(PREPROCESSING_STATE_EVENT_ASSISTANT_ACTIVATED); } else { MAS_LOGE("[ERROR] mWakeupClientAppId and appid differ : %s %s", - mWakeupClientAppId.c_str(), appid); + mWakeupClientAppId.c_str(), pid_appid.c_str()); } } else { MAS_LOGD("MA client connected, but its appid does not match with current maclient"); diff --git a/tests/utc/client-manager/test_client_manager.cpp b/tests/utc/client-manager/test_client_manager.cpp index e77ded9..ef1357f 100644 --- a/tests/utc/client-manager/test_client_manager.cpp +++ b/tests/utc/client-manager/test_client_manager.cpp @@ -27,6 +27,10 @@ public: virtual ~CDummyApplicationManager() {}; bool is_application_running(int pid) { return true; } + bool is_application_running(const std::string& appid) { return true; } + bool bring_app_to_foreground(const std::string& appid) { return true; } + bool launch_app_async(const std::string& appid, bool background) { return true; } + boost::optional get_appid_by_pid(int pid) { return boost::optional{}; } }; class StorageWithNoClient : public testing::Test