Merge 2.4
[apps/native/volume-app.git] / src / timer.c
1 /*
2  * Copyright (c) 2009-2014 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 #include <vconf.h>
18 #include <vconf-keys.h>
19
20 #include "main.h"
21 #include "_util_log.h"
22 #include "view.h"
23 #include "control.h"
24 #include "sound.h"
25 #include "timer.h"
26
27 #define DEL_TIMER(x) \
28         if (x) {\
29                 _D("DELTIMER x : %p\n", x);\
30                 ecore_timer_del(x);\
31                 x = NULL;\
32         }
33 #define ADD_TIMER(x, time, _timer_cb, data) \
34         if(x != NULL) DEL_TIMER(x); \
35         x = ecore_timer_add(time, _timer_cb, data);\
36         _D("ADDTIMER x : %p\n", x);\
37
38
39 struct _timer_s_info {
40         Ecore_Timer *popup_timer; /* pop-up timer */
41         Ecore_Timer *slider_timer; /* slider timer */
42         Ecore_Timer *su_timer; /* short up timer */
43         Ecore_Timer *sd_timer; /* short down timer */
44         Ecore_Timer *lu_timer; /* long up timer */
45         Ecore_Timer *ld_timer; /* long down timer */
46         Ecore_Timer *bt_timer; /* long down timer */
47 };
48
49 static struct _timer_s_info timer_info = {
50         .popup_timer = NULL,
51         .slider_timer = NULL,
52         .su_timer = NULL,
53         .sd_timer = NULL,
54         .lu_timer = NULL,
55         .ld_timer = NULL,
56         .bt_timer = NULL,
57 };
58
59 static Eina_Bool _timer_short_down_cb(void *data);
60 static Eina_Bool _timer_short_up_cb(void *data);
61 static Eina_Bool _timer_slider_cb(void *data);
62 static Eina_Bool _timer_popup_cb(void *data);
63 static Eina_Bool _timer_bt_cb(void *data);
64
65 Ecore_Timer *volume_timer_bt_timer_get(void)
66 {
67         return timer_info.bt_timer;
68 }
69
70 Ecore_Timer *volume_timer_popup_timer_get(void)
71 {
72         return timer_info.popup_timer;
73 }
74
75 Ecore_Timer *volume_timer_slider_timer_get(void)
76 {
77         return timer_info.slider_timer;
78 }
79
80 Ecore_Timer *volume_timer_su_timer_get(void)
81 {
82         return timer_info.su_timer;
83 }
84
85 Ecore_Timer *volume_timer_sd_timer_get(void)
86 {
87         return timer_info.sd_timer;
88 }
89
90 void volume_timer_add(double time, volume_timer_type type)
91 {
92         _D("VOLUME TIMER ADD");
93         if (type == TYPE_TIMER_POPUP) {
94                 ADD_TIMER(timer_info.popup_timer, time, _timer_popup_cb, NULL);
95         } else if (type == TYPE_TIMER_SLIDER) {
96                 ADD_TIMER(timer_info.slider_timer, time, _timer_slider_cb, NULL);
97         } else if (type == TYPE_TIMER_SD) {
98                 ADD_TIMER(timer_info.sd_timer, time, _timer_short_down_cb, NULL);
99         } else if (type == TYPE_TIMER_SU) {
100                 ADD_TIMER(timer_info.su_timer, time, _timer_short_up_cb, NULL);
101         } else if (type == TYPE_TIMER_BT) {
102                 ADD_TIMER(timer_info.bt_timer, time, _timer_bt_cb, NULL);
103         } else {
104                 _E("Failed to get type : type error(%d)", type);
105                 return;
106         }
107 }
108
109 void volume_timer_del(volume_timer_type type)
110 {
111         if (type == TYPE_TIMER_POPUP) {
112                 DEL_TIMER(timer_info.popup_timer);
113         } else if (type == TYPE_TIMER_SLIDER) {
114                 DEL_TIMER(timer_info.slider_timer);
115         } else if (type == TYPE_TIMER_SD) {
116                 DEL_TIMER(timer_info.sd_timer);
117         } else if (type == TYPE_TIMER_SU) {
118                 DEL_TIMER(timer_info.su_timer);
119         } else if (type == TYPE_TIMER_BT) {
120                 DEL_TIMER(timer_info.bt_timer);
121         } else {
122                 _E("Failed to get type : type error(%d)", type);
123                 return;
124         }
125 }
126
127 static Eina_Bool _timer_bt_cb(void *data)
128 {
129         if (VOLUME_ERROR_OK != volume_control_close_bt_display())
130                 _E("Failed to close volume");
131         if (VOLUME_ERROR_OK != volume_control_cache_flush())
132                 _E("Failed to flush cache");
133
134         return ECORE_CALLBACK_CANCEL;
135 }
136
137 static Eina_Bool _timer_popup_cb(void *data)
138 {
139         if (VOLUME_ERROR_OK != volume_control_hide_view())
140                 _E("Failed to close volume");
141         if (VOLUME_ERROR_OK != volume_control_cache_flush())
142                 _E("Failed to flush cache");
143
144         return ECORE_CALLBACK_CANCEL;
145 }
146
147 static Eina_Bool _timer_slider_cb(void *data)
148 {
149         Evas_Object *slider = volume_view_slider_get();
150         if (slider == NULL) {
151                 timer_info.slider_timer = NULL;
152                 return ECORE_CALLBACK_CANCEL;
153         }
154
155         double val = 0;
156
157         int sound = volume_sound_vconf_status_get(TYPE_VCONF_SOUND_STATUS);
158         _D("sound status : %d", sound);
159
160 #if 0
161         int vibration = volume_sound_vconf_status_get(TYPE_VCONF_VIBRATION_STATUS);
162         _D("vibration : %d", vibration);
163 #endif
164
165         sound_type_e sound_type = volume_control_get_sound_type_at_show();
166         _D("sound type at show : %d", sound_type);
167
168         val = elm_slider_value_get(slider);
169         val += 0.5;
170         _D("slider value : %d", (int)val);
171
172         if ((int)val != 0) {
173                 if (sound_type != SOUND_TYPE_MEDIA) {
174                         volume_sound_vconf_status_set(TYPE_VCONF_SOUND_STATUS, 1);
175                         volume_sound_vconf_status_set(TYPE_VCONF_VIBRATION_STATUS, 0);
176                 }
177                 volume_sound_is_vibration_set(EINA_FALSE);
178         }
179         if (val < 1) {
180                 if (sound_type != SOUND_TYPE_MEDIA) {
181                         volume_sound_vconf_status_set(TYPE_VCONF_SOUND_STATUS, 0);
182
183                         if (sound) {
184                                 volume_sound_vconf_status_set(TYPE_VCONF_VIBRATION_STATUS, 1);
185                                 volume_sound_vib_play();
186                         }
187                 }
188                 elm_slider_value_set(slider, 0);
189                 volume_sound_level_set(sound_type, 1);
190         }
191
192         return ECORE_CALLBACK_RENEW;
193 }
194
195 static Eina_Bool _timer_short_up_cb(void *data)
196 {
197         _D("volume is in LongPress");
198         Evas_Object *win = volume_view_win_get();
199         if (!win) {
200                 _E("Window is NULL");
201                 return ECORE_CALLBACK_CANCEL;
202         }
203
204         sound_type_e sound_type = volume_control_get_sound_type_at_show();
205         _D("sound type at show : %d", sound_type);
206
207         if (!evas_object_visible_get(win) && sound_type == SOUND_TYPE_RINGTONE) {
208                 _E("Window is hidden");
209                 return ECORE_CALLBACK_CANCEL;
210         }
211
212         if (volume_view_is_slider_touching_get())
213                 return ECORE_CALLBACK_RENEW;
214
215         ecore_timer_interval_set(timer_info.su_timer, 0.1);
216         volume_timer_del(TYPE_TIMER_SLIDER);
217
218         int sound_step = volume_sound_step_get();
219         _D("sound step : %d", sound_step);
220
221         int sound = volume_sound_vconf_status_get(TYPE_VCONF_SOUND_STATUS);
222         _D("sound status : %d", sound);
223
224         int val = volume_sound_level_get(sound_type);
225         if (val == -1) {
226                 _E("Failed to get volume level");
227                 return ECORE_CALLBACK_CANCEL;
228         }
229         _D("sound value : %d", val);
230
231         if (val == sound_step) {
232                 _D("already sound value : %d", sound_step);
233                 return ECORE_CALLBACK_RENEW;
234         }
235
236         if (!sound && sound_type == SOUND_TYPE_NOTIFICATION) {
237                 _D("Do not adjust the noti type in no sound.");
238                 return ECORE_CALLBACK_RENEW;
239         }
240
241         if (!sound && sound_type == SOUND_TYPE_RINGTONE) {
242                 volume_sound_vconf_status_set(TYPE_VCONF_SOUND_STATUS, 1);
243                 volume_sound_vconf_status_set(TYPE_VCONF_VIBRATION_STATUS, 0);
244         }
245
246         if (volume_sound_level_set(sound_type, val+1 > sound_step ? sound_step : val+1)) {
247                 _D("[SAFETY_SOUND] release timer");
248                 volume_timer_add(3.0, TYPE_TIMER_POPUP);
249         }
250         val = volume_sound_level_get(sound_type);
251
252         return ECORE_CALLBACK_RENEW;
253 }
254
255 static Eina_Bool _timer_short_down_cb(void *data)
256 {
257         Evas_Object *win = volume_view_win_get();
258         if (!win) {
259                 _E("Window is NULL");
260                 return ECORE_CALLBACK_CANCEL;
261         }
262
263         sound_type_e sound_type = volume_control_get_sound_type_at_show();
264         _D("sound type at show : %d", sound_type);
265
266         if (!evas_object_visible_get(win) && sound_type == SOUND_TYPE_RINGTONE) {
267                 _E("Window is hidden");;
268                 return ECORE_CALLBACK_CANCEL;
269         }
270
271         if (volume_view_is_slider_touching_get())
272                 return ECORE_CALLBACK_RENEW;
273
274         ecore_timer_interval_set(timer_info.sd_timer, 0.1);
275         volume_timer_del(TYPE_TIMER_SLIDER);
276
277         int sound = volume_sound_vconf_status_get(TYPE_VCONF_SOUND_STATUS);
278         _D("sound status : %d", sound);
279
280         int val = volume_sound_level_get(sound_type);
281         if (val == -1) {
282                 _E("Failed to get volume level");
283                 return ECORE_CALLBACK_CANCEL;
284         }
285
286         if (!sound && sound_type == SOUND_TYPE_NOTIFICATION) {
287                 _D("Do not adjust the noti type in no sound.");
288                 return ECORE_CALLBACK_RENEW;
289         }
290         _D("sound value : %d", val);
291
292         if (val == 1) {
293                 if (sound && sound_type == SOUND_TYPE_RINGTONE) {
294                         _D("Set sound status to vibration in long press");
295                         volume_sound_vconf_status_set(TYPE_VCONF_SOUND_STATUS, 0);
296                         volume_sound_vconf_status_set(TYPE_VCONF_VIBRATION_STATUS, 1);
297                         volume_sound_vib_play();
298                 }
299         } else if (!val) {
300                 _D("already sound value : 0");
301                 return ECORE_CALLBACK_RENEW;
302         }
303
304         if (sound || sound_type != SOUND_TYPE_RINGTONE)
305                 volume_sound_level_set(sound_type, val-1 <= 0 ? 0 : val-1);
306
307         return ECORE_CALLBACK_RENEW;
308 }
309