Mobile & Wearable profile migration for NFC setting UI
[apps/native/ug-nfc-efl.git] / wearable / app / src / nsa-view-main.c
1 /*
2  * Copyright (c) 2000-2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * PROPRIETARY/CONFIDENTIAL
5  *
6  * This software is the confidential and proprietary information of
7  * SAMSUNG ELECTRONICS ("Confidential Information").
8  * You shall not disclose such Confidential Information and shall
9  * use it only in accordance with the terms of the license agreement
10  * you entered into with SAMSUNG ELECTRONICS.
11  * SAMSUNG make no representations or warranties about the suitability
12  * of the software, either express or implied, including but not
13  * limited to the implied warranties of merchantability, fitness for
14  * a particular purpose, or non-infringement.
15  * SAMSUNG shall not be liable for any damages suffered by licensee as
16  * a result of using, modifying or distributing this software or its derivatives.
17
18  */
19
20
21 #include <app.h>
22 #include <nfc.h>
23 #include <vconf.h>
24 #include <pkgmgr-info.h>
25 #include "nsa-view.h"
26 #include "nsa-main.h"
27 #include "nsa-debug.h"
28 #include "nsa-util.h"
29 #include "nsa-ui-widget.h"
30 #include "nsa-popup.h"
31 #include "nsa-string.h"
32
33
34 typedef enum {
35         T_NFC = 0,
36         T_TAP_N_PAY,
37
38         T_MAX
39 } nfc_setting_type_e;
40
41 typedef struct {
42         Elm_Object_Item *obj_item[T_MAX];
43         Evas_Object *nf;
44         bool isProgressing;
45
46         struct appdata *ad;
47 } main_view_data_t;
48
49 static main_view_data_t *view_data;
50 static char *payment_handler;
51
52 ///////////////////////////////////////////////////////////////////////////////
53
54 static Eina_Bool __back_clicked_cb(void *data, Elm_Object_Item *it)
55 {
56         main_view_data_t *vd = (main_view_data_t *)data;
57
58         NSA_DEBUG("__back_clicked_cb");
59
60         if (vd != NULL) {
61                 struct appdata *ad = vd->ad;
62
63                 _nsa_view_main_destroy(ad);
64         }
65
66         ui_app_exit();
67
68         return EINA_FALSE;
69 }
70
71 #if 0
72 static void __MDM_restriced_popup_response_cb(void *data,
73         Evas_Object *obj, void *event_info)
74 {
75         main_view_data_t *vd = (main_view_data_t *)data;
76         struct appdata *ad;
77
78         ret_if(vd == NULL);
79
80         ad = (struct appdata *)vd->ad;
81         ret_if(ad == NULL);
82
83         ret_if(ad == NULL);
84         ret_if(ad->popup == NULL);
85
86         NSA_DEBUG("__MDM_restriced_popup_response_cb");
87
88         elm_popup_dismiss(ad->popup);
89 }
90 #endif
91
92 static main_view_data_t *__get_view_data()
93 {
94         return view_data;
95 }
96
97 static void __set_view_data(main_view_data_t *data)
98 {
99         view_data  = data;
100 }
101
102 static void __get_payment_handler(char **handler)
103 {
104         char *appid;
105
106         NSA_MEM_FREE(*handler);
107
108         appid = vconf_get_str(VCONFKEY_NFC_PAYMENT_HANDLERS);
109
110         if (appid != NULL) {
111                 pkgmgrinfo_pkginfo_h pkginfo = NULL;
112                 char *label = NULL;
113                 int ret;
114
115                 ret = pkgmgrinfo_pkginfo_get_pkginfo(appid, &pkginfo);
116                 if (ret != 0) {
117                         return;
118                 }
119
120                 ret = pkgmgrinfo_pkginfo_get_label(pkginfo, &label);
121                 if (ret != 0) {
122                         pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
123                         return;
124                 } else {
125                         NSA_MEM_STRDUP(*handler, label);
126                         if (*handler != NULL)
127                                 NSA_DEBUG("text: %s", *handler);
128
129                         pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
130                 }
131         }
132 }
133
134 static void __update_onoff_obj(void *data)
135 {
136         main_view_data_t *vd = (main_view_data_t *)data;
137         Elm_Object_Item *it;
138
139         ret_if(vd == NULL);
140
141         it = vd->obj_item[T_NFC];
142         ret_if(it == NULL);
143
144         NSA_DEBUG("__update_onoff_obj");
145
146         /* V1.9 : instead of toast popup, disable item during operation */
147         elm_object_item_disabled_set(it, false);
148         elm_genlist_item_fields_update(it, "*",
149                 ELM_GENLIST_ITEM_FIELD_CONTENT);
150 }
151
152 static void __update_tap_and_pay_obj(bool activated, void *data)
153 {
154         main_view_data_t *vd = (main_view_data_t *)data;
155         Elm_Object_Item *it;
156
157         ret_if(vd == NULL);
158
159         it = vd->obj_item[T_TAP_N_PAY];
160         ret_if(it == NULL);
161
162         /* V2.1 */
163         if (activated) {
164                 elm_object_item_disabled_set(it, false);
165         } else {
166                 elm_object_item_disabled_set(it, true);
167         }
168         elm_genlist_item_fields_update(it, "*",
169                 ELM_GENLIST_ITEM_FIELD_TEXT);
170 }
171
172 static void __handle_nfc_activation_completed_event(bool activated,
173         main_view_data_t *data)
174 {
175         main_view_data_t *vd = (main_view_data_t *)data;
176
177         ret_if(vd == NULL);
178
179
180         NSA_DEBUG("activated [%d]", activated);
181
182         /* update ui */
183         __update_onoff_obj(vd);
184         __update_tap_and_pay_obj(activated, vd);
185 }
186
187 static void __nfc_vconf_key_changed (keynode_t *key, void *data)
188 {
189         main_view_data_t *vd = (main_view_data_t *)data;
190         int boolval = 0;
191         bool activated;
192
193         g_assert(vd != NULL);
194
195         if (VCONF_OK != vconf_get_bool(VCONFKEY_NFC_STATE, &boolval)) {
196                 activated = false;
197         } else {
198                 if (boolval) {
199                         activated = true;
200                 } else {
201                         activated = false;
202                 }
203         }
204
205         NSA_DEBUG("boolval %d", boolval);
206         NSA_DEBUG("nfc mode %s ", activated ? "ON" : "OFF");
207
208         if (!vd->isProgressing) {
209                 __handle_nfc_activation_completed_event(activated, vd);
210         }
211 }
212
213 static void __payment_handler_vconf_key_changed (keynode_t *key,
214         void *data)
215 {
216         main_view_data_t *vd = (main_view_data_t *)data;
217
218         NSA_DEBUG("__payment_handler_vconf_key_changed");
219
220         /* Get changed payment handler */
221         __get_payment_handler(&payment_handler);
222
223         /* update tap and pay */
224         if (nfc_manager_is_activated()) {
225                 __update_tap_and_pay_obj(true, vd);
226         } else {
227                 __update_tap_and_pay_obj(false, vd);
228 }
229 }
230
231 /* called when ifself performs activation       */
232 /* vd->isProgressing is set by itself   */
233 static void __nfc_activation_completed_cb(nfc_error_e error,
234         void *user_data)
235 {
236         main_view_data_t *vd = (main_view_data_t *)user_data;;
237         bool activated;
238
239         g_assert(vd != NULL);
240
241         /* handle error cases */
242         if (error != NFC_ERROR_NONE) {
243                 NSA_DEBUG_ERR("__nfc_activation_completed_cb failed");
244         }
245
246         /* update ui after activation is completed */
247         if (vd->isProgressing) {
248                 vd->isProgressing = false;
249                 activated = nfc_manager_is_activated();
250
251                 __handle_nfc_activation_completed_event(activated, vd);
252         }
253 }
254
255 /* called when someone performs activation except for itself */
256 static void __nfc_activation_changed_cb(bool activated , void *user_data)
257 {
258         main_view_data_t *vd = (main_view_data_t *)user_data;
259
260         g_assert(vd != NULL);
261
262         NSA_DEBUG("nfc mode %s ", activated ? "ON" : "OFF");
263
264         if (!vd->isProgressing) {
265                 __handle_nfc_activation_completed_event(activated, vd);
266         }
267 }
268
269 static void __change_nfc_onoff_setting(void *data)
270 {
271         main_view_data_t *vd = (main_view_data_t *)data;
272         int result;
273         bool activated;
274         Elm_Object_Item *it;
275
276         ret_if(vd == NULL);
277
278         NSA_BEGIN();
279
280         /* V1.9 : instead of toast popup, disable item during operation */
281         it = vd->obj_item[T_NFC];
282         if(it != NULL)
283                 elm_object_item_disabled_set(it, true);
284
285         activated = nfc_manager_is_activated();
286
287         result = nfc_manager_set_activation(!activated,
288                 __nfc_activation_completed_cb, vd);
289         ret_if(result != NFC_ERROR_NONE);
290
291         vd->isProgressing = true;
292
293         NSA_END();
294 }
295
296 static void __gl_list_clicked_cb(void *data, Evas_Object *obj,
297         void *event_info)
298 {
299         Elm_Object_Item* it = (Elm_Object_Item *)event_info;
300         struct appdata *ad;
301         main_view_data_t *vd;
302         int index;
303
304         index = (int) data;
305
306         vd = __get_view_data();
307         ret_if(vd == NULL);
308
309         ad = vd->ad;
310         ret_if(ad == NULL);
311
312         elm_genlist_item_selected_set(it, EINA_FALSE);
313
314         NSA_DEBUG("__gl_list_clicked_cb");
315
316 #ifdef MDM_PHASE_2
317         if(!__is_nfc_allowed(ad)) {
318                 __update_onoff_obj(vd);
319
320                 /* show popup */
321 #if 0
322                 ad->popup = nsa_create_popup_text_1button(
323                         ad->ly_main,
324                         IDS_SECURITY_POLICY_PREVENTS_USE_OF_NFC_ABB,
325                         IMAGES_PATH"/tw_ic_popup_btn_check.png",
326                         __MDM_restriced_popup_response_cb,
327                         (void *)vd);
328 #else
329                 nsa_create_toast_popup(ad->ly_main,
330                         IDS_SECURITY_POLICY_PREVENTS_USE_OF_NFC_ABB);
331 #endif
332                 return;
333         }
334 #endif
335
336         if (index == T_NFC) {
337                 __change_nfc_onoff_setting(vd);
338         } else if (index == T_TAP_N_PAY){
339                 _nsa_view_tap_n_pay_create(ad);
340         } else {
341                 return ;
342         }
343 }
344
345 static Evas_Object *__gl_content_get_on_off(void *data, Evas_Object *obj,
346         const char *part)
347 {
348         main_view_data_t *vd;
349         Evas_Object *check;
350         int boolval = 0;
351         int index;
352
353         index = (int) data;
354
355         vd = __get_view_data();
356         retv_if(vd == NULL, NULL);
357
358         if (!strcmp(part, "elm.icon")) {
359                 check = elm_check_add(obj);
360                 retv_if(check == NULL, NULL);
361                 elm_access_object_unregister(check);
362                 elm_object_style_set(check, "on&off/list");
363
364                 if (index == T_NFC) {
365                         if (VCONF_OK != vconf_get_bool(VCONFKEY_NFC_STATE, &boolval)) {
366                                 elm_check_state_set(check, EINA_FALSE);
367                         } else {
368                                 if (boolval) {
369                                         elm_check_state_set(check, EINA_TRUE);
370                                 } else {
371                                         elm_check_state_set(check, EINA_FALSE);
372                                 }
373                         }
374                 }
375                 evas_object_size_hint_align_set(check, EVAS_HINT_FILL,
376                         EVAS_HINT_FILL);
377                 evas_object_size_hint_weight_set(check, EVAS_HINT_EXPAND,
378                         EVAS_HINT_EXPAND);
379                 evas_object_propagate_events_set(check, EINA_FALSE);
380                 evas_object_repeat_events_set(check, EINA_TRUE);
381
382                 return check;
383         } else {
384                 return NULL;
385         }
386 }
387
388 static char *__gl_text_get_title(void *data, Evas_Object *obj,
389         const char *part)
390 {
391         //NSA_DEBUG("part:%s", part);
392
393         if (!strcmp(part, "elm.text")) {
394                 return strdup(IDS_NFC_NFC);
395         } else {
396                 return NULL;
397         }
398 }
399
400 static char *__gl_text_get(void *data, Evas_Object *obj,
401         const char *part)
402 {
403         int index = (int)data;
404
405         //NSA_DEBUG("index:%d", index);
406         //NSA_DEBUG("part:%s", part);
407
408         if (!strcmp(part, "elm.text")) {
409                 if (index == T_NFC) {
410                         return strdup(IDS_NFC_NFC);
411                 } else if (index == T_TAP_N_PAY){
412                         return strdup(IDS_TAP_AND_PAY);
413                 } else {
414                         return NULL;
415                 } // elm.text.2
416         } else if (!strcmp(part, "elm.text.1")) {
417                 if (index == T_TAP_N_PAY) {
418                         if (payment_handler != NULL)
419                                 return strdup(payment_handler);
420                         else
421                                 return NULL;
422                 } else {
423                         return NULL;
424                 }
425         } else {
426                 return NULL;
427         }
428 }
429
430 static Evas_Object *__create_nfc_setting_list(void *data)
431 {
432         main_view_data_t *vd = (main_view_data_t *)data;
433         struct appdata *ad;
434         Evas_Object *genlist;
435         Elm_Genlist_Item_Class *itc_title, *itc_on_off, *itc_list;
436         Evas_Object *circle_obj;
437         int i;
438
439         retv_if(vd == NULL, NULL);
440
441         ad = vd->ad;
442         retv_if(ad == NULL, NULL);
443
444         /* make genlist */
445         genlist = elm_genlist_add(vd->nf);
446         retv_if(genlist == NULL, NULL);
447
448         /* set genlist property */
449         elm_genlist_mode_set(genlist, ELM_LIST_COMPRESS);
450 //      elm_genlist_homogeneous_set(genlist, EINA_TRUE);
451 //      uxt_genlist_set_bottom_margin_enabled(genlist, EINA_TRUE);
452
453         circle_obj = eext_circle_object_genlist_add(genlist,
454                 ad->circle_surface);
455         eext_circle_object_genlist_scroller_policy_set(circle_obj,
456                 ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
457         eext_rotary_object_event_activated_set(circle_obj, EINA_TRUE);
458         g_array_append_val(ad->circle_obj_list, circle_obj);
459         ad->circle_obj_list_idx ++;
460         NSA_DEBUG("ad->circle_obj_list_idx %d", ad->circle_obj_list_idx);
461         NSA_DEBUG("circle_obj 0x%p", circle_obj);
462
463         /* create genlist class + */
464         itc_title = elm_genlist_item_class_new();
465         itc_title->item_style = "title";
466         itc_title->func.text_get = __gl_text_get_title;
467         itc_title->func.content_get = NULL;
468         itc_title->func.del = NULL;
469
470         itc_on_off = elm_genlist_item_class_new();
471         itc_on_off->item_style = "1text.1icon.1";
472         itc_on_off->func.text_get = __gl_text_get;
473         itc_on_off->func.content_get = __gl_content_get_on_off;
474         itc_on_off->func.state_get = NULL;
475         itc_on_off->func.del = NULL;
476
477         itc_list = elm_genlist_item_class_new();
478         __get_payment_handler(&payment_handler);
479         if (payment_handler != NULL)
480                 itc_list->item_style = "2text";
481         else
482                 itc_list->item_style = "1text";
483         itc_list->func.text_get = __gl_text_get;
484         itc_list->func.content_get = NULL;
485         itc_list->func.state_get = NULL;
486         itc_list->func.del = NULL;
487         /* create genlist class - */
488
489         /* ADD: ITEM */
490         elm_genlist_item_append(genlist,
491                 itc_title, NULL,
492                 NULL, ELM_GENLIST_ITEM_NONE,
493                 NULL, NULL);
494
495         for (i = 0; i < T_MAX; i++) {
496                 Elm_Genlist_Item_Class *itc;
497
498                 if (i == T_NFC)
499                         itc = itc_on_off;
500                 else
501                         itc = itc_list;
502
503                 vd->obj_item[i] = elm_genlist_item_append(genlist,
504                         itc, (void *)i,
505                         NULL, ELM_GENLIST_ITEM_NONE,
506                         __gl_list_clicked_cb, (void *)i);
507         }
508
509         elm_genlist_item_class_free(itc_title);
510         elm_genlist_item_class_free(itc_on_off);
511         elm_genlist_item_class_free(itc_list);
512
513         /* V2.1 */
514         if (!nfc_manager_is_activated())
515                 __update_tap_and_pay_obj(false, vd);
516
517         evas_object_show(genlist);
518
519         return genlist;
520 }
521
522 int _nsa_view_main_create(void *data)
523 {
524         struct appdata *ad = (struct appdata *)data;
525         main_view_data_t *vd;
526         Evas_Object *list;
527
528         /* create data */
529         NSA_MEM_MALLOC(vd, 1, main_view_data_t);
530         retv_if(vd == NULL, NSA_ERROR);
531         __set_view_data(vd);
532         vd->ad = ad;
533
534         /* set appdata to main view data */
535         vd->nf = ad->navi_frame;
536
537         /* Draw UI */
538         list = __create_nfc_setting_list(vd);
539         retv_if(list == NULL, NSA_ERROR);
540
541         /* Push navifreme + */
542         eext_object_event_callback_add(vd->nf, EEXT_CALLBACK_BACK,
543                 eext_naviframe_back_cb, NULL);
544
545         ad->base_navi_it = elm_naviframe_item_push(
546                 vd->nf,
547                 NULL,
548                 NULL,
549                 NULL,
550                 list,
551                 "empty");
552         elm_naviframe_item_pop_cb_set(ad->base_navi_it, __back_clicked_cb,
553                 vd);
554         /* Push navifreme - */
555
556         /* register nfc callback */
557         nfc_manager_set_activation_changed_cb(
558                                 __nfc_activation_changed_cb, vd);
559
560         if (VCONF_OK != vconf_notify_key_changed(VCONFKEY_NFC_STATE,
561                 __nfc_vconf_key_changed, vd)) {
562                 NSA_DEBUG_ERR("vconf_notify_key_changed error");
563         }
564
565         if (VCONF_OK != vconf_notify_key_changed(VCONFKEY_NFC_PAYMENT_HANDLERS,
566                 __payment_handler_vconf_key_changed, vd)) {
567                 NSA_DEBUG_ERR("vconf_notify_key_changed error");
568         }
569
570         return NSA_OK;
571 }
572
573 void _nsa_view_main_destroy(void *data)
574 {
575         struct appdata *ad;
576
577         __set_view_data(NULL);
578
579         /* unregister nfc callback */
580         nfc_manager_unset_activation_changed_cb();
581
582         if (VCONF_OK != vconf_ignore_key_changed(VCONFKEY_NFC_STATE,
583                 __nfc_vconf_key_changed)) {
584                 NSA_DEBUG_ERR("vconf_notify_key_changed error");
585         }
586
587         if (VCONF_OK != vconf_ignore_key_changed(VCONFKEY_NFC_PAYMENT_HANDLERS,
588                 __payment_handler_vconf_key_changed)) {
589                 NSA_DEBUG_ERR("vconf_ignore_key_changed error");
590         }
591
592         ad = (struct appdata *)data;
593         ret_if(ad == NULL);
594 }
595
596