Fixed TSAM-2036 (Device does not vibrate when changing vibrate mode in notification...
[apps/core/preloaded/quickpanel.git] / daemon / settings / modules / sound-profile.c
1 /*
2  * Copyright (c) 2009-2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <Elementary.h>
19
20 #include <vconf.h>
21 #include <tzsh.h>
22 #include <tzsh_quickpanel_service.h>
23 #include <feedback.h>
24 #include <sound_manager.h>
25 #include <E_DBus.h>
26
27 #include "common_uic.h"
28 #include "common.h"
29 #include "quickpanel-ui.h"
30 #include "settings.h"
31 #include "setting_utils.h"
32 #include "setting_module_api.h"
33 #include "settings_icon_common.h"
34
35 #define BUTTON_LABEL _("IDS_QP_BUTTON2_SOUND_ABB")
36 #define BUTTON_ICON_SND_NORMAL "quick_icon_sn_vf.png"
37 #define BUTTON_ICON_SND_HIGHLIGHT "quick_icon_sn_vf.png"
38 #define BUTTON_ICON_MUTE_NORMAL "quick_icon_sf_vf.png"
39 #define BUTTON_ICON_VIB_HIGHLIGHT "quick_icon_sf_vn.png"
40 #define PACKAGE_SETTING_MENU "setting-profile-efl"
41 #define SAM_LOG_FEATURE_SOUND "ST0C"
42
43 static int g_check_press = 0;
44
45 static void _mouse_clicked_cb(void *data, Evas_Object *obj, const char *emission, const char *source);
46
47 static const char *_label_get(void)
48 {
49         return BUTTON_LABEL;
50 }
51
52 static const char *_icon_get(qp_setting_icon_image_type type)
53 {
54         if (type == QP_SETTING_ICON_NORMAL) {
55                 return BUTTON_ICON_SND_NORMAL;
56         } else if (type == QP_SETTING_ICON_HIGHLIGHT) {
57                 return BUTTON_ICON_SND_HIGHLIGHT;
58         } else if (type == QP_SETTING_ICON_DIM) {
59 #ifdef BUTTON_ICON_DIM
60                 return BUTTON_ICON_DIM;
61 #endif
62         }
63
64         return NULL;
65 }
66
67 static void _long_press_cb(void *data)
68 {
69 #ifdef PACKAGE_SETTING_MENU
70         quickpanel_setting_icon_handler_longpress(PACKAGE_SETTING_MENU, NULL);
71 #endif
72 }
73
74 static void _play_snd_job_cb(void *data)
75 {
76         if (feedback_play_type(FEEDBACK_TYPE_SOUND, FEEDBACK_PATTERN_SILENT_OFF) != FEEDBACK_ERROR_NONE) {
77                 ERR("failed to play a sound");
78         }
79 }
80
81 static void _play_vib_job_cb(void *data)
82 {
83         if (feedback_play_type(FEEDBACK_TYPE_VIBRATION, FEEDBACK_PATTERN_VIBRATION_ON) != FEEDBACK_ERROR_NONE) {
84                 ERR("failed to play a vibration");
85         }
86 }
87
88 static void _view_update(Evas_Object *view, int state, int flag_extra_1, int flag_extra_2)
89 {
90         int icon_state;
91         int ret = -1;
92         int sound_status = 1;
93         int vibration_status = 1;
94         Evas_Object *image = NULL;
95         const char *text = NULL;
96         const char *img_path = NULL;
97
98         retif(view == NULL, , "Invalid parameter!");
99
100         /* Get sound status */
101         ret = vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &sound_status);
102         /* If fail, set sound on status */
103         if (ret != 0) {
104                 ERR("failed get VCONFKEY_SETAPPL_SOUND_STATUS_BOOL:%d", ret);
105                 sound_status = 1;
106         }
107
108         /* Get vibration status */
109         ret = vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL,
110                         &vibration_status);
111         /* If fail, set vibration on status */
112         if (ret != 0) {
113                 ERR("failed get VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL:%d", ret);
114                 vibration_status = 1;
115         }
116
117         INFO("sound : %d, vibration : %d", sound_status, vibration_status);
118
119         if (sound_status == 1 && vibration_status == 1) {
120                 /* Sound & vibration profile */
121                 icon_state = ICON_VIEW_STATE_ON;
122                 text = _("IDS_QP_BUTTON2_SOUND_ABB");
123                 img_path = BUTTON_ICON_SND_HIGHLIGHT;
124         } else if (sound_status == 0 && vibration_status == 1) {
125                 /* Vibration profile */
126                 icon_state = ICON_VIEW_STATE_ON;
127                 text = _("IDS_QP_BUTTON2_VIBRATE");
128                 img_path = BUTTON_ICON_VIB_HIGHLIGHT;
129         } else if (sound_status == 1 && vibration_status == 0) {
130                 /*  Sound profile */
131                 icon_state = ICON_VIEW_STATE_ON;
132                 text = _("IDS_QP_BUTTON2_SOUND_ABB");
133                 img_path = BUTTON_ICON_SND_HIGHLIGHT;
134         } else {
135                 /*  Mute profile */
136                 icon_state = ICON_VIEW_STATE_OFF;
137                 text = _("IDS_QP_BUTTON2_MUTE_ABB");
138                 img_path = BUTTON_ICON_MUTE_NORMAL;
139         }
140
141         quickpanel_setting_icon_state_set(view, icon_state);
142         quickpanel_setting_icon_text_set(view, text, icon_state);
143         image = quickpanel_setting_icon_image_new(view, img_path);
144         quickpanel_setting_icon_content_set(view, image);
145
146         if (quickpanel_uic_is_opened() && g_check_press) {
147                 if (sound_status == 1 && vibration_status == 0) {
148                         ecore_job_add(_play_snd_job_cb, NULL);
149                         g_check_press = 0;
150                 } else if (sound_status == 0 && vibration_status == 1) {
151                         ecore_job_add(_play_vib_job_cb, NULL);
152                         g_check_press = 0;
153                 }
154         }
155 }
156
157 static void _status_update(QP_Module_Setting *module, int flag_extra_1, int flag_extra_2)
158 {
159         retif(module == NULL, , "Invalid parameter!");
160
161         quickpanel_setting_module_icon_view_update(module,
162                         quickpanel_setting_module_icon_state_get(module),
163                         FLAG_VALUE_VOID);
164 }
165
166 static void _mouse_clicked_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
167 {
168         int ret = 0;
169         int sound_status = 1;
170         int vibration_status = 1;
171         QP_Module_Setting *module = (QP_Module_Setting *)data;
172         retif(module == NULL, , "Invalid parameter!");
173
174         /* Get sound profile */
175         ret = vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL,
176                         &sound_status);
177         retif(ret != 0, , "failed to get sound status(%d)", ret);
178
179         ret = vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL,
180                         &vibration_status);
181         retif(ret != 0, ,"failed to get vibration status(%d)", ret);
182
183         INFO("sound : %d, vibration : %d", sound_status, vibration_status);
184
185         g_check_press = 1;
186
187         if (sound_status == 1 && vibration_status == 1) {
188                 ret = vconf_set_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, 0);
189                 msgif(ret != 0, "failed set VCONFKEY_SETAPPL_SOUND_STATUS_BOOL:%d", ret);
190         } else  if (sound_status == 1 && vibration_status == 0) {
191                 ret = vconf_set_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, 1);
192                 msgif(ret != 0, "failed set VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL:%d", ret);
193                 ret = vconf_set_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, 0);
194                 msgif(ret != 0, "failed set VCONFKEY_SETAPPL_SOUND_STATUS_BOOL:%d", ret);
195         } else if (sound_status == 0 && vibration_status == 1) {
196                 ret = vconf_set_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, 0);
197                 msgif(ret != 0, "failed set VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL:%d", ret);
198                 /*insert log for mute mode on state */
199                 //_log_manager_insert_log(SAM_LOG_FEATURE_SOUND, "MUTE", NULL);
200         } else if (sound_status == 0 && vibration_status == 0) {
201                 ret = vconf_set_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, 1);
202                 msgif(ret != 0, "failed set VCONFKEY_SETAPPL_SOUND_STATUS_BOOL:%d", ret);
203         }
204 }
205
206 static void _soundprofile_vconf_cb(keynode_t *node, void *data)
207 {
208         _status_update(data, FLAG_VALUE_VOID, FLAG_VALUE_VOID);
209 }
210
211 static int _register_module_event_handler(void *data)
212 {
213         int ret = 0;
214
215         ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL,
216                         _soundprofile_vconf_cb, data);
217         msgif(ret != 0,
218                         "failed to vconf_notify_key_changed [%s] -[%d]",
219                         VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, ret);
220
221         ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL,
222                         _soundprofile_vconf_cb, data);
223         msgif(ret != 0,
224                         "failed to vconf_notify_key_changed [%s] -[%d]",
225                         VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, ret);
226
227         return QP_OK;
228 }
229
230 static int _unregister_module_event_handler(void *data)
231 {
232         int ret = 0;
233
234         ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL,
235                         _soundprofile_vconf_cb);
236         msgif(ret != 0,
237                         "failed to vconf_ignore_key_changed [%s] -[%d]",
238                         VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, ret);
239
240         ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL,
241                         _soundprofile_vconf_cb);
242         msgif(ret != 0,
243                         "failed to vconf_ignore_key_changed [%s] -[%d]",
244                         VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, ret);
245
246         return QP_OK;
247 }
248
249 /****************************************************************************
250  *
251  * Quickpanel Item functions
252  *
253  ****************************************************************************/
254 static int _init(void *data)
255 {
256         int ret = QP_OK;
257
258         ret = _register_module_event_handler(data);
259
260         return ret;
261 }
262
263 static int _fini(void *data)
264 {
265         int ret = QP_OK;
266
267         ret = _unregister_module_event_handler(data);
268
269         return ret;
270 }
271
272 static void _lang_changed(void *data)
273 {
274         QP_Module_Setting *module = (QP_Module_Setting *)data;
275         retif(module == NULL, , "Invalid parameter!");
276
277         quickpanel_setting_module_icon_view_update_text(module);
278         _status_update(module, FLAG_VALUE_VOID, FLAG_VALUE_VOID);
279 }
280
281 static void _refresh(void *data)
282 {
283         QP_Module_Setting *module = (QP_Module_Setting *)data;
284         retif(module == NULL, , "Invalid parameter!");
285
286         quickpanel_setting_module_icon_view_update_text(module);
287         _status_update(module, FLAG_VALUE_VOID, FLAG_VALUE_VOID);
288 }
289
290
291 QP_Module_Setting sound = {
292         .name                           = "sound",
293         .init                           = _init,
294         .fini                           = _fini,
295         .lang_changed           = _lang_changed,
296         .refresh                        = _refresh,
297         .icon_get                       = _icon_get,
298         .label_get                      = _label_get,
299         .view_update        = _view_update,
300         .status_update          = _status_update,
301         .handler_longpress              = _long_press_cb,
302         .handler_press          = _mouse_clicked_cb,
303         .is_disable_feedback = 1,
304 };