1 #include "wakeup_settings.h"
2 #include "wakeup_manager_main.h"
8 namespace multiassistant
13 /* Utility function for checking if an element exists in a container */
14 template<class C, class T>
15 static auto contains(const C& v, const T& x) -> decltype(end(v), true)
17 return end(v) != find(begin(v), end(v), x);
20 static string prev_wake_word_enabled;
22 CWakeupSettings::CWakeupSettings()
26 CWakeupSettings::~CWakeupSettings()
30 static void wakeup_setting_input_language_changed_cb(keynode_t* node, void* data)
33 if (nullptr == node) return;
35 CWakeupSettings* settings = static_cast<CWakeupSettings*>(data);
36 if (nullptr == settings) return;
38 if (VCONF_TYPE_STRING == node->type) {
39 const char* value = static_cast<const char*>(node->value.s);
40 const auto& observers = settings->get_observers();
41 for (const auto& observer : observers) {
43 if (!observer->on_voice_input_language_changed(value)) {
44 LOGW("[Settings WARNING] One of the observer returned false");
49 LOGE("[Settings ERROR] the value type is not string : %d", node->type);
53 static void wakeup_setting_enabled_assistants_changed_cb(keynode_t* node, void* data)
56 if (nullptr == node) return;
58 CWakeupSettings* settings = static_cast<CWakeupSettings*>(data);
59 if (nullptr == settings) return;
61 if (VCONF_TYPE_STRING == node->type) {
62 vector<string> newlyAddedAssistants;
63 vector<string> newlyRemovedAssistants;
64 const char* value = static_cast<const char*>(node->value.s);
66 vector<string> previouslyEnabledAssistants = settings->get_enabled_assistants();
67 vector<string> currentlyEnabledAssistants;
69 istringstream iss(value);
70 currentlyEnabledAssistants.clear();
71 while (getline(iss, token, ';')) {
72 currentlyEnabledAssistants.push_back(token);
73 MWR_LOGD("enabled_assistants : %s", token.c_str());
76 for (const auto& assistant : currentlyEnabledAssistants) {
77 if (!contains(previouslyEnabledAssistants, assistant)) {
78 newlyAddedAssistants.push_back(assistant);
81 for (const auto& assistant : previouslyEnabledAssistants) {
82 if (!contains(currentlyEnabledAssistants, assistant)) {
83 newlyRemovedAssistants.push_back(assistant);
88 const auto& observers = settings->get_observers();
89 for (const auto& observer : observers) {
91 for (const auto& assistant : newlyAddedAssistants) {
92 if (!observer->on_assistant_enabled_info_changed(assistant.c_str(), true)) {
93 LOGW("[Settings WARNING] One of the observer returned false");
96 for (const auto& assistant : newlyRemovedAssistants) {
97 if (!observer->on_assistant_enabled_info_changed(assistant.c_str(), false)) {
98 LOGW("[Settings WARNING] One of the observer returned false");
104 LOGE("[Settings ERROR] the value type is not string : %d", node->type);
108 static void wakeup_setting_default_assistant_appid_changed_cb(keynode_t* node, void* data)
111 if (nullptr == node) return;
113 CWakeupSettings* settings = static_cast<CWakeupSettings*>(data);
114 if (nullptr == settings) return;
116 if (VCONF_TYPE_STRING == node->type) {
117 const char* value = static_cast<const char*>(node->value.s);
118 const auto& observers = settings->get_observers();
119 for (const auto& observer : observers) {
121 if (!observer->on_default_assistant_appid_changed(value)) {
122 LOGW("[Settings WARNING] One of the observer returned false");
127 LOGE("[Settings ERROR] the value type is not string : %d", node->type);
131 static void wakeup_setting_multiple_mode_changed_cb(keynode_t* node, void* data)
135 CWakeupSettings* settings = static_cast<CWakeupSettings*>(data);
136 if (nullptr == settings) return;
138 const auto& observers = settings->get_observers();
139 for (const auto& observer : observers) {
141 if (!observer->on_multiple_mode_changed()) {
142 LOGW("[Settings WARNING] One of the observer returned false");
148 static void wakeup_setting_enabled_wake_word_detection_changed_cb(keynode_t* node, void* data)
152 char *vconf_str = vconf_keynode_get_str(node);
153 if (!vconf_str) return;
155 MWR_LOGI("vconf value : %s", vconf_str);
156 if (0 == prev_wake_word_enabled.compare(string(vconf_str))) {
160 prev_wake_word_enabled = string(vconf_str);
162 CWakeupSettings* settings = static_cast<CWakeupSettings*>(data);
163 if (nullptr == settings) return;
165 const auto& observers = settings->get_observers();
166 for (const auto& observer : observers) {
168 if (!observer->on_wake_word_detection_enabled_info_changed()) {
169 LOGW("[Settings WARNING] One of the observer returned false");
175 void CWakeupSettings::initialize(map<string, string> custom_keys)
181 mCustomVconfKeys.clear();
182 mCustomVconfKeys = custom_keys;
184 char *default_assistant_appid = vconf_get_str(mCustomVconfKeys[WAKEUP_SETTINGS_KEY_DEFAULT_ASSISTANT_APPID].c_str());
185 if (default_assistant_appid) {
186 mDefaultAssistantAppid = default_assistant_appid;
187 MWR_LOGD("default_assistant_appid : %s", mDefaultAssistantAppid.c_str());
188 for (const auto& observer : mObservers) {
190 if (!observer->on_default_assistant_appid_changed(default_assistant_appid)) {
191 LOGW("[Settings WARNING] One of the observer returned false");
195 free(default_assistant_appid);
196 default_assistant_appid = nullptr;
198 vconf_ret = vconf_get_bool(WAKEUP_SETTINGS_KEY_UI_PANEL_ENABLED, &vconf_bool);
199 if (0 == vconf_ret) {
200 mUiPanelEnabled = vconf_bool;
201 MWR_LOGD("ui_panel_enabled : %s", (mUiPanelEnabled ? "true" : "false"));
203 vconf_ret = vconf_get_dbl(WAKEUP_SETTINGS_KEY_CONVERSATION_TIMEOUT, &vconf_double);
204 if (0 == vconf_ret) {
205 mConversationTimeout = vconf_double;
206 MWR_LOGD("conversation_timeout : %f", mConversationTimeout);
208 vconf_ret = vconf_get_bool(WAKEUP_SETTINGS_KEY_MULTIPLE_MODE, &vconf_bool);
209 if (0 == vconf_ret) {
210 mMultipleMode = vconf_bool;
211 MWR_LOGD("multiple_mode : %s", (mMultipleMode ? "true" : "false"));
213 if (true == mMultipleMode) {
214 char *enabled_assistants = vconf_get_str(mCustomVconfKeys[WAKEUP_SETTINGS_KEY_ENABLED_ASSISTANTS].c_str());
215 if (enabled_assistants) {
217 istringstream iss(enabled_assistants);
218 mEnabledAssistants.clear();
219 while (getline(iss, token, ';')) {
220 mEnabledAssistants.push_back(token);
221 MWR_LOGD("enabled_assistants : %s", token.c_str());
222 for (const auto& observer : mObservers) {
224 if (!observer->on_assistant_enabled_info_changed(token.c_str(), true)) {
225 LOGW("[Settings WARNING] One of the observer returned false");
230 free(enabled_assistants);
231 enabled_assistants = nullptr;
234 char *wakeword_detection_enabled = vconf_get_str(mCustomVconfKeys[WAKEUP_SETTINGS_KEY_WAKE_WORD_DETECTION_ENABLED].c_str());
235 if (wakeword_detection_enabled) {
238 mWakeWordDisabledAssistants.clear();
239 if (!reader.parse(wakeword_detection_enabled, root)) {
240 LOGW("[Settings WARNING] Failed to parse Json : %s", reader.getFormattedErrorMessages().c_str());
242 auto member = root.getMemberNames();
243 for (string m : member) {
244 if (0 == root[m].asString().compare("off")) {
245 mWakeWordDisabledAssistants.push_back(m);
246 MWR_LOGD("wake_word_detection_disabled assistant : %s", m.c_str());
250 free(wakeword_detection_enabled);
251 wakeword_detection_enabled = nullptr;
253 vconf_ret = vconf_get_dbl(WAKEUP_SETTINGS_KEY_WAKEUP_POLICY_DELAY, &vconf_double);
254 if (0 == vconf_ret) {
255 mWakeupPolicyDelay = vconf_double;
256 MWR_LOGD("conversation_timeout : %f", mWakeupPolicyDelay);
258 char *wakeup_policy_priority = vconf_get_str(WAKEUP_SETTINGS_KEY_WAKEUP_POLICY_PRIORITY);
259 if (wakeup_policy_priority) {
261 istringstream iss(wakeup_policy_priority);
262 mWakeupPolicyPriority.clear();
263 while (getline(iss, token, ';')) {
264 mWakeupPolicyPriority.push_back(token);
265 MWR_LOGD("wakeup_policy_priority : %s", token.c_str());
267 free(wakeup_policy_priority);
268 wakeup_policy_priority = nullptr;
270 vconf_ret = vconf_get_dbl(WAKEUP_SETTINGS_KEY_STREAMING_DURATION_MAX, &vconf_double);
271 if (0 == vconf_ret) {
272 mStreamingDurationMax = vconf_double;
273 MWR_LOGD("streaming_duration_max : %f", mStreamingDurationMax);
275 #ifdef ENABLE_VOICE_INPUT_LANGUAGE_SETTINGS
276 char *voice_input_language = vconf_get_str(WAKEUP_SETTINGS_KEY_VOICE_INPUT_LANGUAGE);
277 if (voice_input_language) {
278 mVoiceInputLanguage = voice_input_language;
279 MWR_LOGD("voice input language : %s", mVoiceInputLanguage.c_str());
280 for (const auto& observer : mObservers) {
282 if (!observer->on_voice_input_language_changed(voice_input_language)) {
283 LOGW("[Settings WARNING] One of the observer returned false");
287 free(voice_input_language);
288 voice_input_language = nullptr;
292 #ifdef ENABLE_VOICE_INPUT_LANGUAGE_SETTINGS
293 vconf_notify_key_changed(WAKEUP_SETTINGS_KEY_VOICE_INPUT_LANGUAGE,
294 wakeup_setting_input_language_changed_cb, this);
296 vconf_notify_key_changed(mCustomVconfKeys[WAKEUP_SETTINGS_KEY_DEFAULT_ASSISTANT_APPID].c_str(),
297 wakeup_setting_default_assistant_appid_changed_cb, this);
298 if (true == mMultipleMode) {
299 vconf_notify_key_changed(mCustomVconfKeys[WAKEUP_SETTINGS_KEY_ENABLED_ASSISTANTS].c_str(),
300 wakeup_setting_enabled_assistants_changed_cb, this);
302 vconf_notify_key_changed(WAKEUP_SETTINGS_KEY_MULTIPLE_MODE,
303 wakeup_setting_multiple_mode_changed_cb, this);
304 vconf_notify_key_changed(mCustomVconfKeys[WAKEUP_SETTINGS_KEY_WAKE_WORD_DETECTION_ENABLED].c_str(),
305 wakeup_setting_enabled_wake_word_detection_changed_cb, this);
308 void CWakeupSettings::deinitialize()
310 #ifdef ENABLE_VOICE_INPUT_LANGUAGE_SETTINGS
311 vconf_ignore_key_changed(WAKEUP_SETTINGS_KEY_VOICE_INPUT_LANGUAGE,
312 wakeup_setting_input_language_changed_cb);
314 vconf_ignore_key_changed(mCustomVconfKeys[WAKEUP_SETTINGS_KEY_DEFAULT_ASSISTANT_APPID].c_str(),
315 wakeup_setting_default_assistant_appid_changed_cb);
316 if (true == mMultipleMode) {
317 vconf_ignore_key_changed(mCustomVconfKeys[WAKEUP_SETTINGS_KEY_ENABLED_ASSISTANTS].c_str(),
318 wakeup_setting_enabled_assistants_changed_cb);
320 vconf_ignore_key_changed(WAKEUP_SETTINGS_KEY_MULTIPLE_MODE,
321 wakeup_setting_multiple_mode_changed_cb);
322 vconf_ignore_key_changed(mCustomVconfKeys[WAKEUP_SETTINGS_KEY_WAKE_WORD_DETECTION_ENABLED].c_str(),
323 wakeup_setting_enabled_wake_word_detection_changed_cb);
326 void CWakeupSettings::subscribe(ISettingsEventObserver *observer)
328 mObservers.push_back(observer);
331 void CWakeupSettings::unsubscribe(ISettingsEventObserver *observer)
333 auto iter = find(mObservers.begin(), mObservers.end(), observer);
334 if (iter != mObservers.end()) {
335 mObservers.erase(iter);
339 const vector<ISettingsEventObserver*>& CWakeupSettings::get_observers()
344 string CWakeupSettings::get_default_assistant_appid()
347 vconf_str = vconf_get_str(mCustomVconfKeys[WAKEUP_SETTINGS_KEY_DEFAULT_ASSISTANT_APPID].c_str());
348 MWR_LOGD("vconf default_assistant_appid : %s", vconf_str);
350 mDefaultAssistantAppid = vconf_str;
351 MWR_LOGD("member default_assistant_appid : %s", mDefaultAssistantAppid.c_str());
356 return mDefaultAssistantAppid;
359 void CWakeupSettings::set_default_assistant_appid(std::string appid)
362 MWR_LOGE("appid is NULL");
365 if (appid.compare(get_default_assistant_appid()) == 0) {
366 MWR_LOGE("Default assistant appid not changed, ignoring...");
369 int ret = vconf_set_str(mCustomVconfKeys[WAKEUP_SETTINGS_KEY_DEFAULT_ASSISTANT_APPID].c_str(), appid.c_str());
370 MWR_LOGD("default_assistant_appid : %s, %d", appid.c_str(), ret);
371 mDefaultAssistantAppid = appid;
374 bool CWakeupSettings::get_ui_panel_enabled()
376 return mUiPanelEnabled;
379 float CWakeupSettings::get_conversation_timeout()
381 return mConversationTimeout;
384 bool CWakeupSettings::get_multiple_mode()
386 return mMultipleMode;
389 vector<string> CWakeupSettings::get_enabled_assistants()
391 return mEnabledAssistants;
394 float CWakeupSettings::get_wakeup_policy_delay()
396 return mWakeupPolicyDelay;
399 vector<string> CWakeupSettings::get_wakeup_policy_priority()
401 return mWakeupPolicyPriority;
404 float CWakeupSettings::get_streaming_duration_max()
406 return mStreamingDurationMax;
409 std::string CWakeupSettings::get_current_language(void)
411 std::string result{"en_US"};
413 #ifdef ENABLE_VOICE_INPUT_LANGUAGE_SETTINGS
414 char* language = vconf_get_str(WAKEUP_SETTINGS_KEY_VOICE_INPUT_LANGUAGE);
424 vector<string> CWakeupSettings::get_wake_word_disabled_assistants()
426 return mWakeWordDisabledAssistants;