tizen 2.4 release
[apps/home/starter.git] / lock_pwd / src / lock_pwd_util.c
1 /*
2  * Copyright (c) 2000 - 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 #include <system_settings.h>
18 #include <efl_extension.h>
19
20 #include "lock_mgr.h"
21 #include "util.h"
22 #include "status.h"
23 #include "status.h"
24 #include "lock_pwd_util.h"
25 #include "lock_pwd_simple.h"
26 #include "lock_pwd_complex.h"
27
28 static struct _s_lock_pwd_util {
29         Evas_Object *lock_pwd_win;
30         Evas_Object *conformant;
31         Evas_Object *layout;
32         Evas_Object *bg;
33
34         int win_w;
35         int win_h;
36 } s_lock_pwd_util = {
37         .lock_pwd_win = NULL,
38         .conformant = NULL,
39         .layout = NULL,
40         .bg = NULL,
41
42         .win_w = 0,
43         .win_h = 0,
44 };
45
46
47
48 int lock_pwd_util_win_width_get(void)
49 {
50         return s_lock_pwd_util.win_w;
51 }
52
53
54
55 int lock_pwd_util_win_height_get(void)
56 {
57         return s_lock_pwd_util.win_h;
58 }
59
60
61
62 Evas_Object *lock_pwd_util_win_get(void)
63 {
64         return s_lock_pwd_util.lock_pwd_win;
65 }
66
67
68
69 Eina_Bool lock_pwd_util_win_visible_get(void)
70 {
71         retv_if(!s_lock_pwd_util.lock_pwd_win, EINA_FALSE);
72         return evas_object_visible_get(s_lock_pwd_util.lock_pwd_win);
73 }
74
75
76
77 static Evas_Object *_pwd_conformant_add(Evas_Object *parent)
78 {
79         Evas_Object *conformant = NULL;
80
81         retv_if(!parent, NULL);
82
83         conformant = elm_conformant_add(parent);
84         retv_if(!conformant, NULL);
85
86         evas_object_size_hint_weight_set(conformant, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
87         elm_win_resize_object_add(parent, conformant);
88
89         elm_object_signal_emit(conformant, "elm,state,indicator,overlap", "elm");
90
91         evas_object_show(conformant);
92
93         return conformant;
94 }
95
96
97 void lock_pwd_util_bg_image_set(Evas_Object *bg, char *file)
98 {
99         const char *old_filename = NULL;
100         char *lock_bg = NULL;
101         int ret = 0;
102
103         ret_if(!bg);
104
105         elm_image_file_get(bg, &old_filename, NULL);
106         if (!old_filename) {
107                 old_filename = LOCK_MGR_DEFAULT_BG_PATH;
108         }
109         _D("old file name : %s", old_filename);
110
111         if (file) {
112                 if (!elm_image_file_set(bg, file, NULL)) {
113                         _E("Failed to set image file : %s", file);
114                         goto ERROR;
115                 }
116         } else {
117                 ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_WALLPAPER_LOCK_SCREEN, &lock_bg);
118                 if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
119                         _E("Failed to get system setting value : %d", SYSTEM_SETTINGS_KEY_WALLPAPER_LOCK_SCREEN);
120                         goto ERROR;
121                 }
122                 goto_if(!lock_bg, ERROR);
123
124                 _D("lock_bg : %s", lock_bg);
125
126                 if (!elm_image_file_set(bg, lock_bg, NULL)) {
127                         _E("Failed to set image file : %s", lock_bg);
128                         goto ERROR;
129                 }
130
131                 free(lock_bg);
132         }
133
134         return;
135
136 ERROR:
137
138         if (!elm_bg_file_set(bg, old_filename, NULL)) {
139                 _E("Failed to set old BG file : %s / Retry to set default BG.", old_filename);
140                 if (!elm_bg_file_set(bg, LOCK_MGR_DEFAULT_BG_PATH, NULL)) {
141                         _E("Failed to set default BG : %s", LOCK_MGR_DEFAULT_BG_PATH);
142                         return;
143                 }
144         }
145
146         return;
147 }
148
149
150
151 static void _wallpaper_lock_screen_changed_cb(system_settings_key_e key, void *data)
152 {
153         Evas_Object *bg = (Evas_Object *)data;
154         ret_if(!bg);
155
156         lock_pwd_util_bg_image_set(bg, NULL);
157 }
158
159
160
161 static Evas_Object *_pwd_bg_add(void *data)
162 {
163         Evas_Object *bg = NULL;
164         Evas_Object *parent = NULL;
165         int ret = 0;
166
167         parent = (Evas_Object *)data;
168         retv_if(!parent, NULL);
169
170         bg = elm_image_add(parent);
171         retv_if(!bg, NULL);
172
173         elm_image_aspect_fixed_set(bg, EINA_TRUE);
174         elm_image_fill_outside_set(bg, EINA_TRUE);
175         elm_image_preload_disabled_set(bg, EINA_TRUE);
176
177         lock_pwd_util_bg_image_set(bg, NULL);
178         evas_object_show(bg);
179
180         ret = system_settings_set_changed_cb(SYSTEM_SETTINGS_KEY_WALLPAPER_LOCK_SCREEN, _wallpaper_lock_screen_changed_cb, bg);
181         if (SYSTEM_SETTINGS_ERROR_NONE != ret) {
182                 _E("Failed to register settings changed cb : %d", SYSTEM_SETTINGS_KEY_WALLPAPER_LOCK_SCREEN);
183         }
184
185         return bg;
186 }
187
188
189
190 static Evas_Object *_pwd_layout_create(Evas_Object *parent)
191 {
192         Evas_Object *layout = NULL;
193
194         retv_if(!parent, NULL);
195
196         layout = elm_layout_add(parent);
197         retv_if(!layout, NULL);
198
199         if (!elm_layout_file_set(layout, LOCK_PWD_EDJE_FILE, "lock_pwd")) {
200                 _E("Failed to set edje file : %s", LOCK_PWD_EDJE_FILE);
201                 goto ERROR;
202         }
203
204         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
205         evas_object_size_hint_align_set(layout, EVAS_HINT_FILL, EVAS_HINT_FILL);
206
207         evas_object_show(layout);
208
209         return layout;
210
211 ERROR:
212         _E("Failed to create password layout");
213
214         if (layout) {
215                 evas_object_del(layout);
216                 layout = NULL;
217         }
218
219         return NULL;
220 }
221
222
223
224 void lock_pwd_util_back_key_relased(void)
225 {
226         _D("%s", __func__);
227
228         ret_if(lock_pwd_simple_is_blocked_get());
229
230         lock_mgr_sound_play(LOCK_SOUND_TAP);
231
232         if (!lock_mgr_lockscreen_launch()) {
233                 _E("Failed to launch lockscreen");
234         }
235
236         lock_pwd_util_view_init();
237 }
238
239
240
241 static void __win_resize_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
242 {
243         Evas_Coord x = 0;
244         Evas_Coord y = 0;
245         Evas_Coord w = 0;
246         Evas_Coord h = 0;
247
248         ret_if(!obj);
249
250         evas_object_geometry_get(obj, &x, &y, &w, &h);
251         _D("win resize : %d, %d(%d*%d)", x, y, w, h);
252 }
253
254
255
256 static void __conformant_resize_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
257 {
258         Evas_Coord x = 0;
259         Evas_Coord y = 0;
260         Evas_Coord w = 0;
261         Evas_Coord h = 0;
262
263         ret_if(!obj);
264
265         evas_object_geometry_get(obj, &x, &y, &w, &h);
266         _D("conformant resize : %d, %d(%d*%d)", x, y, w, h);
267 }
268
269
270
271 static void __layout_resize_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
272 {
273         Evas_Coord x = 0;
274         Evas_Coord y = 0;
275         Evas_Coord w = 0;
276         Evas_Coord h = 0;
277
278         ret_if(!obj);
279
280         evas_object_geometry_get(obj, &x, &y, &w, &h);
281         _D("layout resize : %d, %d(%d*%d)", x, y, w, h);
282 }
283
284
285
286 void lock_pwd_util_create(Eina_Bool is_show)
287 {
288         Evas_Object *win = NULL;
289         Evas_Object *conformant = NULL;
290         Evas_Object *bg = NULL;
291         Evas_Object *layout = NULL;
292         Evas_Object *pwd_layout = NULL;
293         int lock_type = 0;
294         int x = 0, y = 0, w = 0, h = 0;
295
296         if (!s_lock_pwd_util.lock_pwd_win) {
297                 win = window_mgr_pwd_lock_win_create();
298                 ret_if(!win);
299                 s_lock_pwd_util.lock_pwd_win = win;
300
301                 elm_win_screen_size_get(win, &x, &y, &w, &h);
302                 _D("win size : %dx%d(%d, %d)", w, h, x, y);
303                 s_lock_pwd_util.win_w = w;
304                 s_lock_pwd_util.win_h = h;
305         }
306
307         conformant = _pwd_conformant_add(win);
308         goto_if(!conformant, ERROR);
309         s_lock_pwd_util.conformant = conformant;
310
311         layout = _pwd_layout_create(conformant);
312         goto_if(!layout, ERROR);
313         s_lock_pwd_util.layout = layout;
314
315         evas_object_event_callback_add(s_lock_pwd_util.lock_pwd_win, EVAS_CALLBACK_RESIZE, __win_resize_cb, NULL);
316         evas_object_event_callback_add(conformant, EVAS_CALLBACK_RESIZE, __conformant_resize_cb, NULL);
317         evas_object_event_callback_add(conformant, EVAS_CALLBACK_RESIZE, __layout_resize_cb, NULL);
318
319         elm_object_content_set(conformant, layout);
320
321         bg = _pwd_bg_add(win);
322         goto_if(!bg, ERROR);
323         s_lock_pwd_util.bg = bg;
324
325         elm_object_part_content_set(layout, "sw.bg", bg);
326
327         lock_type = status_active_get()->setappl_screen_lock_type_int;
328         _D("lock type : %d", lock_type);
329
330         switch(lock_type) {
331         case SETTING_SCREEN_LOCK_TYPE_SIMPLE_PASSWORD:
332                 pwd_layout = lock_pwd_simple_layout_create(layout);
333                 break;
334         case SETTING_SCREEN_LOCK_TYPE_PASSWORD:
335                 pwd_layout = lock_pwd_complex_layout_create(layout);
336                 break;
337         default:
338                 _E("lock type is not password : %d", lock_type);
339                 goto ERROR;
340         }
341         goto_if(!pwd_layout, ERROR);
342
343         elm_object_part_content_set(layout, "sw.lock_pwd", pwd_layout);
344
345         if (is_show) {
346                 evas_object_show(win);
347         }
348
349         return;
350
351 ERROR:
352         _E("Failed to launch password lockscreen");
353
354         lock_pwd_util_destroy();
355
356         return;
357 }
358
359
360
361 void lock_pwd_util_destroy(void)
362 {
363         int lock_type = status_active_get()->setappl_screen_lock_type_int;
364         _D("lock type : %d", lock_type);
365
366         if (lock_type == SETTING_SCREEN_LOCK_TYPE_SIMPLE_PASSWORD) {
367                 lock_pwd_simple_layout_destroy();
368         } else if (lock_type == SETTING_SCREEN_LOCK_TYPE_PASSWORD) {
369                 lock_pwd_complex_layout_destroy();
370         }
371
372         if (s_lock_pwd_util.layout) {
373                 evas_object_del(s_lock_pwd_util.layout);
374                 s_lock_pwd_util.layout = NULL;
375         }
376
377         if (s_lock_pwd_util.conformant) {
378                 evas_object_del(s_lock_pwd_util.conformant);
379                 s_lock_pwd_util.conformant = NULL;
380         }
381
382         if (s_lock_pwd_util.bg) {
383                 system_settings_unset_changed_cb(SYSTEM_SETTINGS_KEY_WALLPAPER_LOCK_SCREEN);
384                 evas_object_del(s_lock_pwd_util.bg);
385                 s_lock_pwd_util.bg = NULL;
386         }
387
388         if (s_lock_pwd_util.lock_pwd_win) {
389                 evas_object_del(s_lock_pwd_util.lock_pwd_win);
390                 s_lock_pwd_util.lock_pwd_win = NULL;
391         }
392 }
393
394
395
396
397 static void _pwd_popup_cb(void *data, Evas_Object *obj, void *event_info)
398 {
399         ret_if(!data);
400
401         evas_object_del((Evas_Object *)data);
402 }
403
404
405
406 void lock_pwd_util_popup_create(char *title, char *text, Evas_Smart_Cb func, double timeout)
407 {
408         Evas_Object *popup = NULL;
409         Evas_Object *btn = NULL;
410
411         ret_if(!s_lock_pwd_util.lock_pwd_win);
412
413         popup = elm_popup_add(s_lock_pwd_util.lock_pwd_win);
414         ret_if(!popup);
415
416         elm_popup_orient_set(popup, ELM_POPUP_ORIENT_BOTTOM);
417         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
418
419         if (title) {
420                 elm_object_part_text_set(popup, "title,text", title);
421         }
422
423         if (text) {
424                 elm_object_text_set(popup, text);
425         }
426
427         btn = elm_button_add(popup);
428         if (!btn) {
429                 _E("Failed to create lock popup button");
430                 evas_object_del(popup);
431                 return;
432         }
433
434         elm_object_style_set(btn, "popup");
435         elm_object_text_set(btn, _("IDS_COM_BUTTON_OK_ABB"));
436         elm_object_part_content_set(popup, "button1", btn);
437
438         if (timeout > 0.0) {
439                 elm_popup_timeout_set(popup, timeout);
440         }
441
442         if (func) {
443                 evas_object_smart_callback_add(btn, "clicked", func, popup);
444                 eext_object_event_callback_add(popup, EEXT_CALLBACK_BACK, func, popup);
445         } else {
446                 evas_object_smart_callback_add(btn, "clicked", _pwd_popup_cb, popup);
447                 eext_object_event_callback_add(popup, EEXT_CALLBACK_BACK, _pwd_popup_cb, popup);
448         }
449
450         evas_object_show(popup);
451
452         return;
453 }
454
455
456
457 void lock_pwd_util_view_init(void)
458 {
459         _D("initialize password lock values");
460         int lock_type = 0;
461
462         /* clear pwd lockscreen */
463         lock_type = status_active_get()->setappl_screen_lock_type_int;
464         if (lock_type == SETTING_SCREEN_LOCK_TYPE_SIMPLE_PASSWORD) {
465                 lock_pwd_simple_view_init();
466         } else if (lock_type == SETTING_SCREEN_LOCK_TYPE_PASSWORD) {
467                 lock_pwd_complex_view_init();
468         }
469 }
470
471
472
473 void lock_pwd_util_win_show(void)
474 {
475         ret_if(!s_lock_pwd_util.lock_pwd_win);
476         evas_object_show(s_lock_pwd_util.lock_pwd_win);
477 }
478
479
480
481 void lock_pwd_util_win_hide(void)
482 {
483         ret_if(!s_lock_pwd_util.lock_pwd_win);
484         evas_object_hide(s_lock_pwd_util.lock_pwd_win);
485 }