Remove efl-assist dependency
[apps/core/preloaded/lockscreen.git] / src / default_lock.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 <Elementary.h>
18 #include <efl_extension.h>
19 #include <app.h>
20 #include <minicontrol-viewer.h>
21 #include <bundle.h>
22
23 #include "lockscreen.h"
24 #include "log.h"
25 #include "default_lock.h"
26 #include "property.h"
27 #include "window.h"
28 #include "background_view.h"
29 #include "battery.h"
30 #include "lock_time.h"
31 #include "sim_state.h"
32
33 #define INDICATOR_HEIGHT 38
34 #define UNLOCK_DISTANCE 140
35
36 #define MINICONTROL_BUNDLE_KEY_WIDTH "width"
37 #define MINICONTROL_BUNDLE_KEY_HEIGHT "height"
38
39 static struct _s_info {
40         Evas_Object *conformant;
41         Evas_Object *layout;
42         Evas_Object *swipe_layout;
43
44         Ecore_Event_Handler *mouse_down_handler;
45         Ecore_Event_Handler *mouse_move_handler;
46         Ecore_Event_Handler *mouse_up_handler;
47
48         Eina_Bool is_mouse_down;
49
50         int lcd_off_count;
51
52         int clicked_x;
53         int clicked_y;
54
55         lock_exit_state_e exit_state;
56 } s_info = {
57         .conformant = NULL,
58         .layout= NULL,
59         .swipe_layout = NULL,
60
61         .mouse_down_handler = NULL,
62         .mouse_move_handler = NULL,
63         .mouse_up_handler = NULL,
64
65         .is_mouse_down = EINA_FALSE,
66         .clicked_x = 0,
67         .clicked_y = 0,
68
69         .exit_state = LOCK_EXIT_STATE_NORMAL,
70 };
71
72 Evas_Object *lock_default_conformant_get(void)
73 {
74         return s_info.conformant;
75 }
76
77 Evas_Object *lock_default_lock_layout_get(void)
78 {
79         return s_info.layout;
80 }
81
82 Evas_Object *lock_default_swipe_layout_get(void)
83 {
84         return s_info.swipe_layout;
85 }
86
87 void _default_lock_hw_back_cb(void *data, Evas_Object *obj, void *event_info)
88 {
89         _I("%s", __func__);
90 }
91
92 static Eina_Bool _default_lock_mouse_down_cb(void *data, int type, void *event)
93 {
94         _D("%s", __func__);
95
96         Ecore_Event_Mouse_Button *m = event;
97
98         int touch_upper_y = 0;
99
100         retv_if(!m, ECORE_CALLBACK_CANCEL);
101         retv_if(!s_info.swipe_layout, ECORE_CALLBACK_CANCEL);
102
103         /* (Up to 3 times, 30 seconds) is extended by 10 seconds Control panel area when tap */
104         lockscreen_lcd_off_count_raise();
105
106         s_info.clicked_x = m->root.x;
107         s_info.clicked_y = m->root.y;
108         _D("clicked x(%d), y(%d)", s_info.clicked_x, s_info.clicked_y);
109
110         touch_upper_y = INDICATOR_HEIGHT;
111         _D("touch upper y : %d", touch_upper_y);
112
113         if (m->root.y <= touch_upper_y) {
114                 _D("ignore touch event(%d > %d)", m->root.y, touch_upper_y);
115                 s_info.is_mouse_down = EINA_FALSE;
116         } else {
117                 elm_object_signal_emit(s_info.swipe_layout, "vi_effect_start", "padding.top");
118                 s_info.is_mouse_down = EINA_TRUE;
119         }
120
121         return ECORE_CALLBACK_PASS_ON;
122 }
123
124 static Eina_Bool _default_lock_mouse_move_cb(void *data, int type, void *event)
125 {
126         Ecore_Event_Mouse_Move *m = event;
127         retv_if(!m, ECORE_CALLBACK_CANCEL);
128         retv_if(m->multi.device != 0, ECORE_CALLBACK_CANCEL);
129
130         int const dx = m->x - s_info.clicked_x;
131         int const dy = s_info.clicked_y - m->y;
132         int scaled_unlock_distance = _X(UNLOCK_DISTANCE);
133         int distance = sqrt(dx*dx + dy*dy) + _X(20);
134
135         if (distance >= scaled_unlock_distance) {
136                 s_info.exit_state = LOCK_EXIT_STATE_EXIT;
137         } else {
138                 s_info.exit_state = LOCK_EXIT_STATE_NORMAL;
139         }
140
141         return ECORE_CALLBACK_DONE;
142 }
143
144 static Eina_Bool _default_lock_mouse_up_cb(void *data, int type, void *event)
145 {
146         _D("%s", __func__);
147
148         Ecore_Event_Mouse_Button *m = event;
149
150         retv_if(!m, ECORE_CALLBACK_CANCEL);
151         retv_if(m->multi.device != 0, ECORE_CALLBACK_CANCEL);
152         retv_if(!s_info.layout, ECORE_CALLBACK_CANCEL);
153         retv_if(!s_info.swipe_layout, ECORE_CALLBACK_CANCEL);
154
155
156         if (s_info.is_mouse_down == EINA_FALSE) {
157                 _I("ignore touch event");
158                 return ECORE_CALLBACK_CANCEL;
159         }
160
161         s_info.is_mouse_down = EINA_FALSE;
162
163         switch(s_info.exit_state) {
164         case LOCK_EXIT_STATE_NORMAL :
165                 _D("cancel unlock");
166                 break;
167         case LOCK_EXIT_STATE_EXIT :
168                 _D("exit lockscreen");
169
170                 elm_object_signal_emit(s_info.swipe_layout, "vi_effect", "padding.top");
171                 elm_object_signal_emit(s_info.layout, "vi_effect", "vi_clipper");
172                 return ECORE_CALLBACK_CANCEL;
173         default :
174                 _E("type error : %d", s_info.exit_state);
175                 break;
176         }
177
178         s_info.exit_state = LOCK_EXIT_STATE_NORMAL;
179
180         elm_object_signal_emit(s_info.swipe_layout, "vi_effect_stop", "padding.top");
181         elm_object_signal_emit(s_info.layout, "vi_effect_stop", "vi_clipper");
182         elm_object_signal_emit(s_info.swipe_layout, "show,txt,plmn", "txt.plmn");
183
184         return ECORE_CALLBACK_PASS_ON;
185 }
186
187 static void _vi_effect_end_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
188 {
189         _D("%s", __func__);
190
191         ui_app_exit();
192 }
193
194 static lock_error_e _unlock_panel_create(void)
195 {
196         retv_if(!s_info.swipe_layout, LOCK_ERROR_FAIL);
197
198         s_info.mouse_down_handler = ecore_event_handler_add(ECORE_EVENT_MOUSE_BUTTON_DOWN, _default_lock_mouse_down_cb, NULL);
199         if (!s_info.mouse_down_handler) {
200                 _E("Failed to add mouse down handler");
201         }
202
203         s_info.mouse_move_handler = ecore_event_handler_add(ECORE_EVENT_MOUSE_MOVE, _default_lock_mouse_move_cb, NULL);
204         if (!s_info.mouse_move_handler) {
205                 _E("Failed to add mouse move handler");
206         }
207
208         s_info.mouse_up_handler = ecore_event_handler_add(ECORE_EVENT_MOUSE_BUTTON_UP, _default_lock_mouse_up_cb, NULL);
209         if (!s_info.mouse_up_handler) {
210                 _E("Failed to add mouse up handler");
211         }
212
213         elm_object_signal_callback_add(s_info.swipe_layout, "vi_effect_end", "vi_clipper", _vi_effect_end_cb, NULL);
214
215         return LOCK_ERROR_OK;
216 }
217
218 static Evas_Object *_swipe_layout_create(Evas_Object *parent)
219 {
220         Evas_Object *swipe_layout = NULL;
221
222         retv_if(!parent, NULL);
223
224         swipe_layout = elm_layout_add(parent);
225         retv_if(!swipe_layout, NULL);
226
227         if (!elm_layout_file_set(swipe_layout, LOCK_EDJE_FILE, "swipe-lock")) {
228                 _E("Failed to set edje file for swipe lock");
229                 goto ERROR;
230         }
231
232         evas_object_size_hint_weight_set(swipe_layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
233         evas_object_size_hint_align_set(swipe_layout, EVAS_HINT_FILL, EVAS_HINT_FILL);
234
235         evas_object_show(swipe_layout);
236         s_info.swipe_layout = swipe_layout;
237
238         /* initialize time & date information */
239         lock_time_init();
240
241         /* initialize battery information */
242         if (LOCK_ERROR_OK != lock_battery_init()) {
243                 _E("Failed to initialize battery information");
244         }
245
246         /* initialize PLMN-SPN information */
247         if (LOCK_ERROR_OK != lock_sim_state_init()) {
248                 _E("Failed to initialize sim state");
249         }
250
251         return swipe_layout;
252
253 ERROR:
254         _E("Failed to create swipe layout");
255
256         if(swipe_layout) {
257                 evas_object_del(swipe_layout);
258                 swipe_layout = NULL;
259         }
260
261         return NULL;
262 }
263
264 static Evas_Object *_layout_create(void)
265 {
266         Evas_Object *layout = NULL;
267         Evas_Object *swipe_layout = NULL;
268         Evas_Object *win = NULL;
269
270         win = lock_window_win_get();
271         retv_if(!win, NULL);
272
273         layout = elm_layout_add(win);
274         retv_if(!layout, NULL);
275
276         evas_object_show(layout);
277
278         if (!elm_layout_file_set(layout, LOCK_EDJE_FILE, "lockscreen")) {
279                 _E("Failed to set edje file");
280                 goto ERROR;
281         }
282
283         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
284         evas_object_size_hint_align_set(layout, EVAS_HINT_FILL, EVAS_HINT_FILL);
285
286         swipe_layout = _swipe_layout_create(layout);
287         if (!swipe_layout) {
288                 _E("Failed to create swipe layout");
289                 goto ERROR;
290         }
291
292         elm_object_part_content_set(layout, "sw.swipe_layout", swipe_layout);
293         if (!elm_object_part_content_get(layout, "sw.swipe_layout")) {
294                 _E("Failed to set swipe layout");
295                 goto ERROR;
296         }
297
298         elm_win_resize_object_add(win, layout);
299
300         return layout;
301
302 ERROR:
303         _E("Failed to create layout");
304
305         if (layout) {
306                 evas_object_del(layout);
307                 layout = NULL;
308         }
309
310         if (swipe_layout) {
311                 evas_object_del(swipe_layout);
312                 swipe_layout = NULL;
313         }
314
315         return NULL;
316 }
317
318 static Evas_Object *_comformant_create(void)
319 {
320         Evas_Object *conformant = NULL;
321         Evas_Object *win = NULL;
322
323         win = lock_window_win_get();
324         retv_if(!win, NULL);
325
326         conformant = elm_conformant_add(win);
327         retv_if(!conformant, NULL);
328
329         evas_object_size_hint_weight_set(conformant, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
330         elm_win_resize_object_add(win, conformant);
331
332         elm_win_indicator_mode_set(win, ELM_WIN_INDICATOR_SHOW);
333         elm_object_signal_emit(conformant, "elm,state,indicator,overlap", "elm");
334
335         evas_object_show(conformant);
336
337         return conformant;
338 }
339
340 lock_error_e lock_default_lock_init(void)
341 {
342         Evas_Object *conformant = NULL;
343         Evas_Object *layout = NULL;
344         Evas_Object *bg = NULL;
345
346         int ret = 0;
347
348         layout = _layout_create();
349         goto_if(!layout, ERROR);
350
351         s_info.layout = layout;
352
353         conformant = _comformant_create();
354         goto_if(!conformant, ERROR);
355         s_info.conformant = conformant;
356
357         eext_object_event_callback_add(layout, EEXT_CALLBACK_BACK, _default_lock_hw_back_cb, NULL);
358
359         bg = lock_background_view_bg_get();
360         if (!bg) {
361                 _E("Failed to get BG");
362         } else {
363                 elm_object_part_content_set(layout, "sw.bg", bg);
364         }
365
366         ret = _unlock_panel_create();
367         goto_if(LOCK_ERROR_OK != ret, ERROR);
368
369         return LOCK_ERROR_OK;
370
371 ERROR:
372         _E("Failed to initialize default lock");
373
374         if (conformant) {
375                 evas_object_del(conformant);
376                 conformant = NULL;
377         }
378
379         if (layout) {
380                 evas_object_del(layout);
381                 layout = NULL;
382         }
383
384         return LOCK_ERROR_FAIL;
385 }
386
387 void lock_default_lock_fini(void)
388 {
389         /* delete network status */
390         lock_sim_state_deinit();
391
392         /* delete batteyr information */
393         lock_battery_fini();
394
395         /* delete data&time information */
396         lock_time_fini();
397
398         /* delete wallpaper */
399         lock_background_view_bg_del();
400
401         if (s_info.mouse_down_handler) {
402                 ecore_event_handler_del(s_info.mouse_down_handler);
403                 s_info.mouse_down_handler = NULL;
404         }
405
406         if (s_info.mouse_move_handler) {
407                 ecore_event_handler_del(s_info.mouse_move_handler);
408                 s_info.mouse_move_handler = NULL;
409         }
410
411         if (s_info.mouse_up_handler) {
412                 ecore_event_handler_del(s_info.mouse_up_handler);
413                 s_info.mouse_up_handler = NULL;
414         }
415
416         if (s_info.swipe_layout) {
417                 evas_object_del(s_info.swipe_layout);
418                 s_info.swipe_layout = NULL;
419         }
420
421         if (s_info.conformant) {
422                 evas_object_del(s_info.conformant);
423                 s_info.conformant = NULL;
424         }
425
426         if (s_info.layout) {
427                 eext_object_event_callback_del(s_info.layout, EEXT_CALLBACK_BACK, _default_lock_hw_back_cb);
428                 evas_object_del(s_info.layout);
429                 s_info.layout = NULL;
430         }
431 }