2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "view_pwd_popup.h"
21 #include "settings_provider.h"
23 #define PWD_DATA_ID "change_passcode_data"
24 #define CTXPOPUP_WIDTH 688
25 #define CTXPOPUP_HEIGHT 411
28 #define BOX_HPADDING 10
29 #define BOX_VPADDING 0
31 #define MSGID_PWD_TITLE "Change passcode"
32 #define MSGID_UPTEXT "Enter your old passcode"
33 #define MSGID_DOWNTEXT "Default passcode is 0000"
34 #define MSGID_INVALID_PWD "Failed passcode attempt"
35 #define MSGID_NEW_PWD "Enter your new passcode"
36 #define MSGID_REENTER_PWD "Re-enter your new passcode"
37 #define MSGID_DIFFER_PWD "Not same passcode. Enter again"
38 #define MSGID_CANCEL "Cancel"
39 #define PWD_SYMBOL "*"
40 #define PWD_RANGE "0123456789"
42 #define SIG_CLICKED "clicked"
43 #define SIG_DISMISSED "dismissed"
57 Evas_Object *cancel_btn;
58 Evas_Object *ctxpopup;
64 char entered_pwd[PWD_SIZE];
65 char reentered_pwd[PWD_SIZE];
71 struct setting_mgr *mgr;
72 struct settingview_data *view;
75 static void _hide(Evas_Object *base);
80 * @param[in]: data : the user data of change passcode view.
84 static void _free_pwd_data(struct _pwd_data *data)
90 evas_object_del(data->bg);
93 evas_object_del(data->base);
96 evas_object_del(data->ctxpopup);
99 eina_array_free(data->array);
105 * Create background of change passcode view.
107 * @param[in]: win : the window this view created on.
109 * @return: the created background of type evas object.
111 static Evas_Object *_add_pwd_bg(Evas_Object *win)
118 bg = utils_add_base(win);
120 _ERR("elm layout add failed.");
124 elm_layout_file_set(bg, EDJ_FILE, GRP_PWD_BG);
130 * Create the base layout of change passcode view.
132 * @param[in]: ctxpopup : the ctxpopup which contains change passcode view.
134 * @return: the created base layout of change passcode view.
136 static Evas_Object *_add_pwd_base(Evas_Object *ctxpopup)
143 base = utils_add_base(ctxpopup);
145 _ERR("elm layout add failed.");
149 elm_layout_file_set(base, EDJ_FILE, GRP_PWD_VIEW);
151 elm_object_part_text_set(base, PART_TITLE,
152 utils_get_translation_str(MSGID_PWD_TITLE));
153 elm_object_part_text_set(base, PART_UPTEXT,
154 utils_get_translation_str(MSGID_UPTEXT));
155 elm_object_part_text_set(base, PART_DOWNTEXT,
156 utils_get_translation_str(MSGID_DOWNTEXT));
162 * Evas_Smart_Cb type callback for handling click event.
164 * @param[in]: priv : the user data.
165 * @param[in]: obj : the corresponding object which the click event occurred.
166 * @param[in]: ev : event info.
170 static void _cancelbtn_clicked_cb(void *priv, Evas_Object *obj, void *ev)
172 struct _pwd_data *data;
177 data = (struct _pwd_data *) priv;
179 settingmgr_view_pop(data->mgr);
183 * Create cancel button of change passcode view.
185 * @param[in]: data : the user data of change passcode view.
187 * @return: the created cancel button or null if error occurred.
189 static Evas_Object *_add_pwd_cancel_btn(struct _pwd_data *data)
193 if (!data || !data->base)
196 btn = utils_add_btn(data->base, PWD_BASIC_BTN,
197 utils_get_translation_str((const char *) MSGID_CANCEL), EINA_TRUE);
199 _ERR("utils add button failed.");
203 evas_object_smart_callback_add(btn, SIG_CLICKED,
204 _cancelbtn_clicked_cb, data);
206 elm_object_part_content_set(data->base, SWALLOW_CANCEL_BTN, btn);
208 elm_object_focus_allow_set(btn, EINA_TRUE);
214 * Set the move directions of focus between entries and cancel button.
216 * @param[in]: array : eina array containing entries.
217 * @param[in]: cancel_btn : the cancel button.
221 static void _set_entry_directions(Eina_Array *array, Evas_Object *cancel_btn)
227 if (!array || !cancel_btn)
230 for (i = ENTRY_0; i < ENTRY_3; i++) {
231 en = (Evas_Object *) eina_array_data_get(array, i);
235 elm_object_focus_next_object_set(en,
236 (Evas_Object *) eina_array_data_get(array, i + 1),
238 elm_object_focus_next_object_set(en, en, ELM_FOCUS_LEFT);
239 elm_object_focus_next_object_set(en, en, ELM_FOCUS_RIGHT);
240 elm_object_focus_next_object_set(en, en, ELM_FOCUS_UP);
241 elm_object_focus_next_object_set(en, cancel_btn,
245 lasten = (Evas_Object *) eina_array_data_get(array, ENTRY_3);
246 elm_object_focus_next_object_set(lasten, lasten, ELM_FOCUS_UP);
247 elm_object_focus_next_object_set(lasten, cancel_btn, ELM_FOCUS_DOWN);
248 elm_object_focus_next_object_set(lasten, lasten, ELM_FOCUS_LEFT);
249 elm_object_focus_next_object_set(lasten, lasten, ELM_FOCUS_RIGHT);
251 elm_object_focus_next_object_set(cancel_btn, cancel_btn,
253 elm_object_focus_next_object_set(cancel_btn, cancel_btn,
255 elm_object_focus_next_object_set(cancel_btn,
256 (Evas_Object *) eina_array_data_get(array, ENTRY_0), ELM_FOCUS_UP);
257 elm_object_focus_next_object_set(cancel_btn, cancel_btn,
264 * @param[in]: vdata : view data.
266 * @return: the passcode.
268 static char *_get_pwd(struct settingview_data *vdata)
270 struct settingitem *item;
275 item = viewdata_get_parentitem(vdata);
277 _ERR("get parent item failed.");
281 return provider_get_list_value(item);
285 * Check if the input passcode is right.
287 * @param[in]: data : the user data of change passcode view.
289 * @return: 0 - wrong passcode, 1 - right passcode.
291 static int _check_pwd(struct _pwd_data *data)
295 if (!data || !data->view)
298 pwd = _get_pwd(data->view);
300 _ERR("get passcode failed.");
304 if (strncmp(data->pwd, pwd, strlen(pwd))) {
317 * @param[in]: pwd : the passcode.
321 static void _clear_pwd(char *pwd)
338 * This function clears displayed passcode in entries when user
339 * inputs wrong passcode.
341 * @param[in]: data : the user data of change passcode view.
345 static void _reset_pwd(struct _pwd_data *data)
347 Evas_Object *btn, *first;
351 if (!data || !data->array || !data->base)
356 first = (Evas_Object *) eina_array_data_get(array, ENTRY_0);
360 elm_object_focus_set(first, EINA_TRUE);
361 elm_object_focus_next_object_set(data->cancel_btn, first, ELM_FOCUS_UP);
363 for (i = ENTRY_0; i < ENTRY_MAX; i++) {
364 btn = (Evas_Object *)eina_array_data_get(array, i);
368 elm_object_text_set(btn, "");
373 * Set passcode to system.
375 * @param[in]: data : the user data of change passcode view.
377 * @return: 0 - Success, -1 - Fail.
379 static int _set_pwd(struct _pwd_data *data)
382 struct settingitem *item;
384 if (!data || !data->view)
387 item = viewdata_get_parentitem(data->view);
389 _ERR("get parent item failed.");
393 ret = provider_set_list_value(item, data->pwd);
395 _ERR("set passcode failed.");
403 * Check if the reentered passcode is right.
405 * @param[in]: data : the user data of change passcode view.
407 * @return: 0 - Success, -1 - Fail.
409 static int _check_reentered_pwd(struct _pwd_data *data)
414 if (strncmp(data->entered_pwd, data->reentered_pwd,
422 * Update text of change passcode view.
424 * @param[in]: data : the user data of change passcode view.
425 * @param[in]: part : edje part of text.
426 * @param[in]: msgid : the message id of text.
430 static void _update_pwd_ui(struct _pwd_data *data,
431 const char *part, const char *msgid)
435 if (!data || !data->base || !msgid || !part)
440 text = utils_get_translation_str(msgid);
441 elm_object_part_text_set(data->base, part, text);
445 * Evas_Object_Event_Cb type callback for handling key press event.
447 * @param[in]: priv : the user data.
448 * @param[in]: e : the evas canvas.
449 * @param[in]: obj : the corresponding object which the key press event occurred.
450 * @param[in]: ei : event info.
454 static void _entry_keypress_cb(void *priv, Evas *e,
455 Evas_Object *obj, void *ei)
457 Evas_Event_Key_Down *event;
459 Evas_Object *next_entry;
460 struct _pwd_data *data;
464 if (!priv || !ei || !obj)
467 data = (struct _pwd_data *) priv;
472 event = (Evas_Event_Key_Down *) ei;
473 keyname = event->keyname;
478 for (i = ENTRY_0; i < ENTRY_MAX; i++) {
479 if (obj == eina_array_data_get(array, i)) {
485 if (!strncmp(keyname, KEY_BACK, strlen(keyname)) || !strncmp(keyname, KEY_BACK_REMOTE, strlen(keyname))) {
486 settingmgr_view_pop(data->mgr);
487 } else if (!strncmp(keyname, KEY_DOWN, strlen(keyname))) {
488 elm_object_focus_set(data->cancel_btn, EINA_TRUE);
489 } else if (!strncmp(keyname, KEY_0, strlen(keyname)) ||
490 !strncmp(keyname, KEY_1, strlen(keyname)) ||
491 !strncmp(keyname, KEY_2, strlen(keyname)) ||
492 !strncmp(keyname, KEY_3, strlen(keyname)) ||
493 !strncmp(keyname, KEY_4, strlen(keyname)) ||
494 !strncmp(keyname, KEY_5, strlen(keyname)) ||
495 !strncmp(keyname, KEY_6, strlen(keyname)) ||
496 !strncmp(keyname, KEY_7, strlen(keyname)) ||
497 !strncmp(keyname, KEY_8, strlen(keyname)) ||
498 !strncmp(keyname, KEY_9, strlen(keyname))) {
499 elm_entry_entry_set(obj, PWD_SYMBOL);
501 data->pwd[entry_idx] = keyname[0];
503 if (data->new_flag && !data->renew_flag)
504 data->entered_pwd[entry_idx] = keyname[0];
506 if (data->renew_flag)
507 data->reentered_pwd[entry_idx] = keyname[0];
509 if (entry_idx < ENTRY_3) {
510 elm_object_focus_next(obj, ELM_FOCUS_NEXT);
511 next_entry = elm_object_focus_next_object_get(obj,
514 _ERR("next entry is null.");
518 elm_object_focus_next_object_set(data->cancel_btn,
519 next_entry, ELM_FOCUS_UP);
521 if (!data->new_flag) {
522 if (_check_pwd(data)) {
523 _update_pwd_ui(data, PART_UPTEXT,
528 _update_pwd_ui(data, PART_UPTEXT,
531 } else if (!data->renew_flag) {
532 _update_pwd_ui(data, PART_UPTEXT,
535 data->renew_flag = 1;
537 if (_check_reentered_pwd(data)) {
538 if (_set_pwd(data) != 0) {
539 _ERR("set passcode failed.");
544 data->renew_flag = 0;
546 settingmgr_view_pop(data->mgr);
548 _update_pwd_ui(data, PART_UPTEXT,
551 data->renew_flag = 0;
562 * @param[in]: parent : object the entry will be created on.
564 * @return: the created entry or null if error occurred.
566 static Evas_Object *_add_entry(Evas_Object *parent)
569 Elm_Entry_Filter_Limit_Size length = {
572 Elm_Entry_Filter_Accept_Set type = {
573 .accepted = PWD_RANGE
579 entry = utils_add_entry(parent);
581 _ERR("utils add entry failed.");
585 elm_entry_markup_filter_append(entry,
586 elm_entry_filter_limit_size, &length);
587 elm_entry_markup_filter_append(entry,
588 elm_entry_filter_accept_set, &type);
590 elm_object_style_set(entry, STYLE_PWD_ENTRY);
592 elm_entry_text_style_user_push(entry, STYLE_ENTRY_TEXT);
593 elm_object_part_content_set(parent, SWALLOW_ENTRY, entry);
599 * Create entries for inputting passcode.
601 * @param[in]: data : the user data of change passcode view.
603 * @return: 0 - Success, -1 - Fail.
605 static int _add_pwd_entry(struct _pwd_data *data)
610 Evas_Object *entry_base;
613 if (!data || !data->base) {
614 _ERR("invalid arguments.");
618 box = utils_add_box(data->base);
620 _ERR("add box failed.");
624 elm_box_horizontal_set(box, EINA_TRUE);
625 elm_box_padding_set(box, BOX_HPADDING, BOX_VPADDING);
627 array = eina_array_new(1);
629 _ERR("new eina array failed.");
630 evas_object_del(box);
634 for (i = ENTRY_0; i < ENTRY_MAX; i++) {
635 entry_base = utils_add_base(data->base);
637 _ERR("entry base created failed.");
638 evas_object_del(box);
639 eina_array_free(array);
643 elm_layout_file_set(entry_base, EDJ_FILE, GRP_PWD_ENTRY);
645 entry = _add_entry(entry_base);
647 _ERR("add entry failed.");
648 evas_object_del(box);
649 evas_object_del(entry_base);
650 eina_array_free(array);
654 eina_array_push(array, entry);
655 elm_box_pack_end(box, entry_base);
657 evas_object_event_callback_add(entry, EVAS_CALLBACK_KEY_DOWN,
658 _entry_keypress_cb, data);
661 elm_object_part_content_set(data->base, SWALLOW_CONTENT_AREA, box);
663 elm_object_focus_allow_set(
664 (Evas_Object *) eina_array_data_get(array, ENTRY_0), EINA_TRUE);
665 elm_object_focus_set(
666 (Evas_Object *)eina_array_data_get(array, ENTRY_0), EINA_TRUE);
668 _set_entry_directions(array, data->cancel_btn);
677 * Evas_Smart_Cb type callback for handling ctxpopup dismissed event.
679 * @param[in]: priv : the user data.
680 * @param[in]: obj : the corresponding object which the dismissed event occurred.
681 * @param[in]: ev : event info.
685 static void _ctxpopup_dismissed(void *priv, Evas_Object *obj,
688 struct _pwd_data *data;
693 data = (struct _pwd_data *) priv;
695 if (!data->base || !data->mgr)
700 settingmgr_view_pop(data->mgr);
704 * Create all the UI components of change passcode view.
706 * @param[in]: mgr : view manager of settings views.
707 * @param[in]: view : data of view.
708 * @prev[in]: prev : the user data.
710 * @return: the base layout of change passcode view or null
713 static Evas_Object *_create(struct setting_mgr *mgr,
714 struct settingview_data *view, void *prev)
716 Evas_Object *base, *bg;
718 Evas_Object *cancel_btn;
719 Evas_Object *ctxpopup;
720 struct _pwd_data *data;
726 _ERR("invalid argument.");
730 win = settingmgr_get_win(mgr);
732 _ERR("settingmgr get window failed.");
736 data = (struct _pwd_data *)calloc(1, sizeof(*data));
738 _ERR("calloc _change_passcode_data failed.");
744 _clear_pwd(data->pwd);
746 bg = _add_pwd_bg(win);
748 _ERR("add change passcode background failed.");
752 ctxpopup = utils_add_ctxpopup(win, (char *) CHANGE_PWD_CTXPOPUP_STYLE);
754 _ERR("add ctxpopup failed.");
758 data->ctxpopup = ctxpopup;
760 base = _add_pwd_base(data->ctxpopup);
762 _ERR("add change passcode base layout failed.");
768 elm_object_content_set(ctxpopup, base);
773 xpos = (width - CTXPOPUP_WIDTH) / 2;
774 ypos = (height - CTXPOPUP_HEIGHT) / 2;
775 evas_object_move(data->ctxpopup, xpos * ELM_SCALE, ypos * ELM_SCALE);
777 cancel_btn = _add_pwd_cancel_btn(data);
779 _ERR("add change passcode cancel button failed.");
783 data->cancel_btn = cancel_btn;
785 ret = _add_pwd_entry(data);
787 _ERR("add change passcode input buttons failed.");
795 evas_object_smart_callback_add(data->ctxpopup,
796 SIG_DISMISSED, _ctxpopup_dismissed, data);
798 evas_object_data_set(base, PWD_DATA_ID, data);
803 _free_pwd_data(data);
808 * Show the change passcode view.
810 * @param[in]: base : the base layout of change passcode view.
814 static void _show(Evas_Object *base)
816 struct _pwd_data *data;
821 data = (struct _pwd_data *)evas_object_data_get(base, PWD_DATA_ID);
823 _ERR("get data from base failed. ID: %s.", PWD_DATA_ID);
827 evas_object_show(data->ctxpopup);
831 * Destroy the change passcode view.
833 * @param[in]: base : the base layout of change passcode view.
837 static void _destroy(Evas_Object *base)
839 struct _pwd_data *data;
844 data = (struct _pwd_data *)evas_object_data_get(base, PWD_DATA_ID);
846 _ERR("get data from base failed. ID: %s.", PWD_DATA_ID);
850 _free_pwd_data(data);
854 * Hide the change passcode view.
856 * @param[in]: base : the base layout of change passcode view.
860 static void _hide(Evas_Object *base)
862 struct _pwd_data *data;
867 data = (struct _pwd_data *)evas_object_data_get(base, PWD_DATA_ID);
869 _ERR("get data from base failed. ID: %s.",
874 evas_object_hide(data->bg);
875 evas_object_hide(data->ctxpopup);
879 * view class of change passcode view.
881 static struct setting_class _vclass = {
882 VCLASS_TITLE_CHANGE_PASSCODE,
898 * Return view class of change passcode view.
902 * @return: the view class of change passcode view.
904 struct setting_class *view_passcode_popup_get_vclass(void)