[mobile] Fix logic to launch menu-screen at 64bit binary
[apps/native/starter.git] / src / mobile / lock_pwd_complex.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 <Elementary.h>
18 #include <Ecore.h>
19
20 #include "lock_mgr.h"
21 #include "util.h"
22 #include "lock_pwd_util.h"
23 #include "lock_pwd_complex.h"
24 #include "lock_pwd_control_panel.h"
25
26 #define IME_RESIZED 1
27 #define IME_DOWN 2
28
29 static struct _s_lock_pwd_complex {
30         Evas_Object *pwd_complex_layout;
31         Evas_Object *pwd_complex_entry;
32
33         Eina_Bool is_blocked;
34         Ecore_Timer *timer_pin;
35         int pin_time_remain;
36 } s_lock_pwd_complex = {
37         .pwd_complex_layout = NULL,
38         .pwd_complex_entry = NULL,
39
40         .is_blocked = EINA_FALSE,
41         .timer_pin = NULL,
42         .pin_time_remain = PASSWORD_BLOCK_SECONDS,
43 };
44
45
46
47
48 Eina_Bool lock_pwd_complex_is_blocked_get(void)
49 {
50         return s_lock_pwd_complex.is_blocked;
51 }
52
53 static void _pwd_complex_layout_title_set(const char *title)
54 {
55         ret_if(!s_lock_pwd_complex.pwd_complex_layout);
56         ret_if(!title);
57
58         elm_object_part_text_set(s_lock_pwd_complex.pwd_complex_layout, "title", title);
59 }
60
61
62
63 void lock_pwd_complex_entry_clear(void)
64 {
65         ret_if(!s_lock_pwd_complex.pwd_complex_entry);
66
67         elm_entry_entry_set(s_lock_pwd_complex.pwd_complex_entry, "");
68 }
69
70
71
72 static Eina_Bool _pwd_complex_entry_clear(void *data)
73 {
74         lock_pwd_complex_entry_clear();
75         return ECORE_CALLBACK_CANCEL;
76 }
77
78
79
80 static void _pwd_complex_lock_time_init(void)
81 {
82         if (vconf_set_str(VCONFKEY_SETAPPL_PASSWORD_TIMESTAMP_STR, "") < 0) {
83                 _E("Failed to set vconfkey : %s", VCONFKEY_SETAPPL_PASSWORD_TIMESTAMP_STR);
84         }
85 }
86
87
88
89 static void _pwd_complex_lock_time_save(void)
90 {
91         time_t cur_time = time(NULL);
92         char buf[64] = { 0, };
93         snprintf(buf, sizeof(buf), "%ld", cur_time);
94         if (vconf_set_str(VCONFKEY_SETAPPL_PASSWORD_TIMESTAMP_STR, buf) < 0) {
95                 _E("Failed to set vconfkey : %s", VCONFKEY_SETAPPL_PASSWORD_TIMESTAMP_STR);
96         }
97 }
98
99
100
101 static void _pwd_complex_event_correct(lock_pwd_event_e event)
102 {
103         _D("%s", __func__);
104
105         lock_pwd_util_win_hide();
106         lock_pwd_complex_entry_clear();
107         _pwd_complex_layout_title_set(_("IDS_COM_BODY_ENTER_PIN"));
108
109         lock_mgr_idle_lock_state_set(VCONFKEY_IDLE_UNLOCK);
110         lock_mgr_sound_play(LOCK_SOUND_UNLOCK);
111 }
112
113
114
115 static void _pwd_complex_event_incorrect(lock_pwd_event_e event)
116 {
117         char temp_str[BUF_SIZE_256] = { 0, };
118         char temp_left[BUF_SIZE_256] = { 0, };
119         int remain_attempt = 0;
120
121         remain_attempt = lock_pwd_verification_remain_attempt_get();
122         _D("remain_attempt(%d)", remain_attempt);
123
124         if (remain_attempt == 1) {
125                 strncpy(temp_left, _("IDS_IDLE_BODY_1_ATTEMPT_LEFT"), sizeof(temp_left));
126                 temp_left[sizeof(temp_left) - 1] = '\0';
127         } else {
128                 snprintf(temp_left, sizeof(temp_left), _("IDS_IDLE_BODY_PD_ATTEMPTS_LEFT"), remain_attempt);
129         }
130         snprintf(temp_str, sizeof(temp_str), "%s<br>%s", _("IDS_COM_BODY_INCORRECT_PIN"), temp_left);
131         _pwd_complex_layout_title_set(temp_str);
132
133         ecore_timer_add(0.1, _pwd_complex_entry_clear, NULL);
134
135         lock_pwd_verification_popup_create(event);
136 }
137
138
139
140 static Eina_Bool _wrong_pwd_wait_timer_cb(void *data)
141 {
142         char try_again_buf[BUF_SIZE_256] = { 0, };
143         char incorrect_pass_buf[BUF_SIZE_256] = { 0, };
144
145         retv_if(!s_lock_pwd_complex.pwd_complex_layout, ECORE_CALLBACK_CANCEL);
146
147         snprintf(try_again_buf, sizeof(try_again_buf), _("IDS_LCKSCN_POP_TRY_AGAIN_IN_PD_SECONDS"), s_lock_pwd_complex.pin_time_remain);
148         snprintf(incorrect_pass_buf, sizeof(incorrect_pass_buf), "%s<br>%s", _("IDS_COM_BODY_INCORRECT_PIN"), try_again_buf);
149         _pwd_complex_layout_title_set(incorrect_pass_buf);
150
151         if (s_lock_pwd_complex.pin_time_remain == PASSWORD_BLOCK_SECONDS ||
152                         s_lock_pwd_complex.pin_time_remain > 0) {
153                 s_lock_pwd_complex.pin_time_remain--;
154                 return ECORE_CALLBACK_RENEW;
155         } else {
156                 lock_pwd_complex_view_init();
157
158                 int lcd_state = lock_mgr_lcd_state_get();
159                 if (lcd_state == LCD_STATE_OFF) {
160                         if (!lock_mgr_lockscreen_launch()) {
161                                 _E("Failed to launch lockscreen");
162                         }
163                 }
164         }
165
166         return ECORE_CALLBACK_CANCEL;
167 }
168
169
170
171 static void _pwd_complex_event_input_block(lock_pwd_event_e event)
172 {
173         _D("%s", __func__);
174
175         int block_sec = PASSWORD_BLOCK_SECONDS;
176         char try_again_buf[BUF_SIZE_256] = { 0, };
177         char incorrect_pass_buf[BUF_SIZE_256] = { 0, };
178
179         ret_if(!s_lock_pwd_complex.pwd_complex_layout);
180
181         _pwd_complex_lock_time_save();
182
183         snprintf(try_again_buf, sizeof(try_again_buf), _("IDS_LCKSCN_POP_TRY_AGAIN_IN_PD_SECONDS"), block_sec);
184         snprintf(incorrect_pass_buf, sizeof(incorrect_pass_buf), "%s<br>%s", _("IDS_COM_BODY_INCORRECT_PIN"), try_again_buf);
185         _pwd_complex_layout_title_set(incorrect_pass_buf);
186
187         s_lock_pwd_complex.is_blocked = EINA_TRUE;
188
189         if (s_lock_pwd_complex.timer_pin) {
190                 ecore_timer_del(s_lock_pwd_complex.timer_pin);
191                 s_lock_pwd_complex.timer_pin = NULL;
192         }
193
194         s_lock_pwd_complex.timer_pin = ecore_timer_add(1.0, _wrong_pwd_wait_timer_cb, NULL);
195
196         ecore_timer_add(0.1, _pwd_complex_entry_clear, NULL);
197
198         lock_pwd_verification_popup_create(event);
199
200         lock_pwd_control_panel_cancel_btn_enable_set(EINA_FALSE);
201 }
202
203
204
205 void lock_pwd_complex_event(lock_pwd_event_e event)
206 {
207         switch(event) {
208         case PWD_EVENT_CORRECT:
209                 _pwd_complex_event_correct(event);
210                 break;
211         case PWD_EVENT_INCORRECT_WARNING:
212         case PWD_EVENT_INCORRECT:
213                 _pwd_complex_event_incorrect(event);
214                 break;
215         case PWD_EVENT_INPUT_BLOCK_WARNING:
216         case PWD_EVENT_INPUT_BLOCK:
217                 _pwd_complex_event_input_block(event);
218                 break;
219         case PWD_EVENT_EMPTY:
220                 break;
221         case PWD_EVENT_OVER:
222                 break;
223         default:
224                 break;
225         }
226 }
227
228
229
230 static void _pwd_complex_enter_cb(void *data, Evas_Object *obj, void *event_info)
231 {
232         char buf[BUF_SIZE_256] = { 0, };
233         char *markup_txt = NULL;
234
235         ret_if(!obj);
236
237         const char *password = elm_entry_entry_get(obj);
238         ret_if(!password);
239
240         markup_txt = elm_entry_utf8_to_markup(password);
241         snprintf(buf, sizeof(buf), "%s", markup_txt);
242         free(markup_txt);
243
244         lock_pwd_event_e pwd_event = lock_pwd_verification_verify(buf);
245         lock_pwd_complex_event(pwd_event);
246 }
247
248
249
250 Evas_Object *_pwd_complex_entry_create(void *data)
251 {
252         Evas_Object *parent = NULL;
253         Evas_Object *entry = NULL;
254
255         parent = (Evas_Object *)data;
256         retv_if(!parent, NULL);
257
258         entry = elm_entry_add(parent);
259         retv_if(!entry, NULL);
260
261         elm_entry_single_line_set(entry, EINA_TRUE);
262         elm_entry_password_set(entry, EINA_TRUE);
263         elm_entry_entry_set(entry, "");
264         elm_entry_cursor_end_set(entry);
265         //elm_entry_scrollabled_set(entry, EINA_TRUE);
266         elm_entry_cnp_mode_set(entry, ELM_CNP_MODE_PLAINTEXT);
267         elm_entry_input_panel_layout_set(entry, ELM_INPUT_PANEL_LAYOUT_PASSWORD);
268         elm_entry_input_panel_imdata_set(entry, "type=lockscreen", 15);
269         elm_entry_input_panel_return_key_type_set(entry, ELM_INPUT_PANEL_RETURN_KEY_TYPE_DONE);
270
271         evas_object_smart_callback_add(entry, "activated", _pwd_complex_enter_cb, NULL);
272
273         evas_object_show(entry);
274
275         return entry;
276 }
277
278
279
280 Evas_Object *lock_pwd_complex_layout_create(void *data)
281 {
282         Evas_Object *parent = NULL;
283         Evas_Object *pwd_complex_layout = NULL;
284         Evas_Object *pwd_complex_entry = NULL;
285         Evas_Object *pwd_control_panel = NULL;
286
287         lock_pwd_verification_policy_create();
288
289         parent = (Evas_Object *)data;
290         retv_if(!parent, NULL);
291
292         pwd_complex_layout = elm_layout_add(parent);
293         goto_if(!pwd_complex_layout, ERROR);
294         s_lock_pwd_complex.pwd_complex_layout = pwd_complex_layout;
295
296         if (!elm_layout_file_set(pwd_complex_layout, LOCK_PWD_EDJE_FILE, "lock-complex-password")) {
297                 _E("Failed to set edje file : %s", LOCK_PWD_EDJE_FILE);
298                 goto ERROR;
299         }
300
301         evas_object_size_hint_weight_set(pwd_complex_layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
302         evas_object_size_hint_align_set(pwd_complex_layout, EVAS_HINT_FILL, EVAS_HINT_FILL);
303         evas_object_show(pwd_complex_layout);
304
305         /* create entry */
306         pwd_complex_entry = _pwd_complex_entry_create(pwd_complex_layout);
307         goto_if(!pwd_complex_entry, ERROR);
308         s_lock_pwd_complex.pwd_complex_entry = pwd_complex_entry;
309
310         elm_object_part_content_set(pwd_complex_layout, "entry", pwd_complex_entry);
311
312         _pwd_complex_layout_title_set(_("IDS_COM_BODY_ENTER_PIN"));
313
314         /* create control panel */
315         pwd_control_panel = lock_pwd_control_panel_create(pwd_complex_layout);
316         if (!pwd_control_panel) {
317                 _E("Failed to create password control panel");
318         } else {
319                 elm_object_part_content_set(pwd_complex_layout, "control_panel", pwd_control_panel);
320         }
321
322         evas_object_show(pwd_complex_layout);
323
324         return pwd_complex_layout;
325
326 ERROR:
327         _E("Failed to create complex password layout");
328
329         if (pwd_complex_layout) {
330                 evas_object_del(pwd_complex_layout);
331                 s_lock_pwd_complex.pwd_complex_layout = NULL;
332         }
333
334         return NULL;
335 }
336
337
338 void lock_pwd_complex_layout_del(void)
339 {
340         if (s_lock_pwd_complex.timer_pin) {
341                 ecore_timer_del(s_lock_pwd_complex.timer_pin);
342                 s_lock_pwd_complex.timer_pin = NULL;
343         }
344
345         if (s_lock_pwd_complex.pwd_complex_entry) {
346                 evas_object_del(s_lock_pwd_complex.pwd_complex_entry);
347                 s_lock_pwd_complex.pwd_complex_entry = NULL;
348         }
349
350         if (s_lock_pwd_complex.pwd_complex_layout) {
351                 evas_object_del(s_lock_pwd_complex.pwd_complex_layout);
352                 s_lock_pwd_complex.pwd_complex_layout = NULL;
353         }
354 }
355
356
357
358 void lock_pwd_complex_view_init(void)
359 {
360         _D("initialize complex password values");
361         _pwd_complex_layout_title_set(_("IDS_COM_BODY_ENTER_PIN"));
362         elm_object_signal_emit(s_lock_pwd_complex.pwd_complex_layout, "show_title", "title");
363         s_lock_pwd_complex.is_blocked = EINA_FALSE;
364
365         lock_pwd_complex_entry_clear();
366
367         _pwd_complex_lock_time_init();
368
369         lock_pwd_control_panel_cancel_btn_enable_set(EINA_TRUE);
370 }