1 #include "wakeup_settings.h"
2 #include "wakeup_manager_main.h"
7 namespace multiassistant
12 /* Utility function for checking if an element exists in a container */
13 template<class C, class T>
14 static auto contains(const C& v, const T& x) -> decltype(end(v), true)
16 return end(v) != find(begin(v), end(v), x);
19 CWakeupSettings::CWakeupSettings()
23 CWakeupSettings::~CWakeupSettings()
27 static void wakeup_setting_input_language_changed_cb(keynode_t* node, void* data)
30 if (nullptr == node) return;
32 CWakeupSettings* settings = static_cast<CWakeupSettings*>(data);
33 if (nullptr == settings) return;
35 if (VCONF_TYPE_STRING == node->type) {
36 const char* value = static_cast<const char*>(node->value.s);
37 vector<ISettingsEventObserver*> observers = settings->get_observers();
38 for (const auto& observer : observers) {
40 if (!observer->on_voice_input_language_changed(value)) {
41 LOGW("[Settings WARNING] One of the observer returned false");
46 LOGE("[Settings ERROR] the value type is not string : %d", node->type);
50 static void wakeup_setting_enabled_assistants_changed_cb(keynode_t* node, void* data)
53 if (nullptr == node) return;
55 CWakeupSettings* settings = static_cast<CWakeupSettings*>(data);
56 if (nullptr == settings) return;
58 if (VCONF_TYPE_STRING == node->type) {
59 vector<string> newlyAddedAssistants;
60 vector<string> newlyRemovedAssistants;
61 const char* value = static_cast<const char*>(node->value.s);
63 vector<string> previouslyEnabledAssistants = settings->get_enabled_assistants();
64 vector<string> currentlyEnabledAssistants;
66 istringstream iss(value);
67 currentlyEnabledAssistants.clear();
68 while (getline(iss, token, ';')) {
69 currentlyEnabledAssistants.push_back(token);
70 MWR_LOGD("enabled_assistants : %s", token.c_str());
73 for (const auto& assistant : currentlyEnabledAssistants) {
74 if (!contains(previouslyEnabledAssistants, assistant)) {
75 newlyAddedAssistants.push_back(assistant);
78 for (const auto& assistant : previouslyEnabledAssistants) {
79 if (!contains(currentlyEnabledAssistants, assistant)) {
80 newlyRemovedAssistants.push_back(assistant);
85 vector<ISettingsEventObserver*> observers = settings->get_observers();
86 for (const auto& observer : observers) {
88 for (const auto& assistant : newlyAddedAssistants) {
89 if (!observer->on_assistant_enabled_info_changed(assistant.c_str(), true)) {
90 LOGW("[Settings WARNING] One of the observer returned false");
93 for (const auto& assistant : newlyRemovedAssistants) {
94 if (!observer->on_assistant_enabled_info_changed(assistant.c_str(), false)) {
95 LOGW("[Settings WARNING] One of the observer returned false");
101 LOGE("[Settings ERROR] the value type is not string : %d", node->type);
105 static void wakeup_setting_default_assistant_appid_changed_cb(keynode_t* node, void* data)
108 if (nullptr == node) return;
110 CWakeupSettings* settings = static_cast<CWakeupSettings*>(data);
111 if (nullptr == settings) return;
113 if (VCONF_TYPE_STRING == node->type) {
114 const char* value = static_cast<const char*>(node->value.s);
115 vector<ISettingsEventObserver*> observers = settings->get_observers();
116 for (const auto& observer : observers) {
118 if (!observer->on_default_assistant_appid_changed(value)) {
119 LOGW("[Settings WARNING] One of the observer returned false");
124 LOGE("[Settings ERROR] the value type is not string : %d", node->type);
128 void CWakeupSettings::initialize()
135 vconf_str = vconf_get_str(WAKEUP_SETTINGS_KEY_DEFAULT_ASSISTANT_APPID);
137 mDefaultAssistantAppid = vconf_str;
138 MWR_LOGD("default_assistant_appid : %s", mDefaultAssistantAppid.c_str());
139 for (const auto& observer : mObservers) {
141 if (!observer->on_default_assistant_appid_changed(vconf_str)) {
142 LOGW("[Settings WARNING] One of the observer returned false");
149 vconf_ret = vconf_get_bool(WAKEUP_SETTINGS_KEY_UI_PANEL_ENABLED, &vconf_bool);
150 if (0 == vconf_ret) {
151 mUiPanelEnabled = vconf_bool;
152 MWR_LOGD("ui_panel_enabled : %s", (mUiPanelEnabled ? "true" : "false"));
154 vconf_ret = vconf_get_dbl(WAKEUP_SETTINGS_KEY_CONVERSATION_TIMEOUT, &vconf_double);
155 if (0 == vconf_ret) {
156 mConversationTimeout = vconf_double;
157 MWR_LOGD("conversation_timeout : %f", mConversationTimeout);
159 vconf_ret = vconf_get_bool(WAKEUP_SETTINGS_KEY_MULTIPLE_MODE, &vconf_bool);
160 if (0 == vconf_ret) {
161 mMultipleMode = vconf_bool;
162 MWR_LOGD("multiple_mode : %s", (mMultipleMode ? "true" : "false"));
164 vconf_str = vconf_get_str(WAKEUP_SETTINGS_KEY_ENABLED_ASSISTANTS);
167 istringstream iss(vconf_str);
168 mEnabledAssistants.clear();
169 while (getline(iss, token, ';')) {
170 mEnabledAssistants.push_back(token);
171 MWR_LOGD("enabled_assistants : %s", token.c_str());
172 for (const auto& observer : mObservers) {
174 if (!observer->on_assistant_enabled_info_changed(token.c_str(), true)) {
175 LOGW("[Settings WARNING] One of the observer returned false");
183 vconf_ret = vconf_get_dbl(WAKEUP_SETTINGS_KEY_WAKEUP_POLICY_DELAY, &vconf_double);
184 if (0 == vconf_ret) {
185 mWakeupPolicyDelay = vconf_double;
186 MWR_LOGD("conversation_timeout : %f", mWakeupPolicyDelay);
188 vconf_str = vconf_get_str(WAKEUP_SETTINGS_KEY_WAKEUP_POLICY_PRIORITY);
191 istringstream iss(vconf_str);
192 mWakeupPolicyPriority.clear();
193 while (getline(iss, token, ';')) {
194 mWakeupPolicyPriority.push_back(token);
195 MWR_LOGD("wakeup_policy_priority : %s", token.c_str());
200 vconf_ret = vconf_get_dbl(WAKEUP_SETTINGS_KEY_STREAMING_DURATION_MAX, &vconf_double);
201 if (0 == vconf_ret) {
202 mStreamingDurationMax = vconf_double;
203 MWR_LOGD("streaming_duration_max : %f", mStreamingDurationMax);
205 vconf_str = vconf_get_str(WAKEUP_SETTINGS_KEY_VOICE_INPUT_LANGUAGE);
207 mVoiceInputLanguage = vconf_str;
208 MWR_LOGD("voice input language : %s", mVoiceInputLanguage.c_str());
209 for (const auto& observer : mObservers) {
211 if (!observer->on_voice_input_language_changed(vconf_str)) {
212 LOGW("[Settings WARNING] One of the observer returned false");
220 vconf_notify_key_changed(WAKEUP_SETTINGS_KEY_VOICE_INPUT_LANGUAGE,
221 wakeup_setting_input_language_changed_cb, this);
222 vconf_notify_key_changed(WAKEUP_SETTINGS_KEY_ENABLED_ASSISTANTS,
223 wakeup_setting_enabled_assistants_changed_cb, this);
224 vconf_notify_key_changed(WAKEUP_SETTINGS_KEY_DEFAULT_ASSISTANT_APPID,
225 wakeup_setting_default_assistant_appid_changed_cb, this);
228 void CWakeupSettings::deinitialize()
230 vconf_ignore_key_changed(WAKEUP_SETTINGS_KEY_VOICE_INPUT_LANGUAGE, NULL);
231 vconf_ignore_key_changed(WAKEUP_SETTINGS_KEY_ENABLED_ASSISTANTS, NULL);
232 vconf_ignore_key_changed(WAKEUP_SETTINGS_KEY_DEFAULT_ASSISTANT_APPID, NULL);
235 void CWakeupSettings::subscribe(ISettingsEventObserver *observer)
237 mObservers.push_back(observer);
240 void CWakeupSettings::unsubscribe(ISettingsEventObserver *observer)
242 auto iter = find(mObservers.begin(), mObservers.end(), observer);
243 if (iter != mObservers.end()) {
244 mObservers.erase(iter);
248 vector<ISettingsEventObserver*> CWakeupSettings::get_observers()
253 string CWakeupSettings::get_default_assistant_appid()
256 vconf_str = vconf_get_str(WAKEUP_SETTINGS_KEY_DEFAULT_ASSISTANT_APPID);
257 MWR_LOGD("default_assistant_appid : %s", vconf_str);
259 mDefaultAssistantAppid = vconf_str;
260 MWR_LOGD("default_assistant_appid : %s", mDefaultAssistantAppid.c_str());
265 return mDefaultAssistantAppid;
268 bool CWakeupSettings::get_ui_panel_enabled()
270 return mUiPanelEnabled;
273 float CWakeupSettings::get_conversation_timeout()
275 return mConversationTimeout;
278 bool CWakeupSettings::get_multiple_mode()
280 return mMultipleMode;
283 vector<string> CWakeupSettings::get_enabled_assistants()
285 return mEnabledAssistants;
288 float CWakeupSettings::get_wakeup_policy_delay()
290 return mWakeupPolicyDelay;
293 vector<string> CWakeupSettings::get_wakeup_policy_priority()
295 return mWakeupPolicyPriority;
298 float CWakeupSettings::get_streaming_duration_max()
300 return mStreamingDurationMax;
303 std::string CWakeupSettings::get_current_language(void)
305 std::string result{"en_US"};
306 char* language = vconf_get_str(WAKEUP_SETTINGS_KEY_VOICE_INPUT_LANGUAGE);