3381e9b9468ae71b9df9779c6d586993be781188
[apps/native/ug-wifi-efl.git] / sources / ui-gadget / wifi-efl-UG.c
1 /*
2  * Wi-Fi
3  *
4  * Copyright 2012 Samsung Electronics Co., Ltd
5  *
6  * Licensed under the Flora License, Version 1.1 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.tizenopensource.org/license
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #ifndef UG_MODULE_API
21 #define UG_MODULE_API __attribute__ ((visibility("default")))
22 #endif
23
24 #include <vconf-keys.h>
25 //#include <setting-cfg.h>
26 #include "ug_wifi.h"
27 #include "view_detail.h"
28 #include "i18nmanager.h"
29 #include "wlan_manager.h"
30 #include "winset_popup.h"
31 #include "common_utils.h"
32 #include "viewer_manager.h"
33 #include "view_ime_hidden.h"
34 #include "view_advanced.h"
35 #include "wifi-engine-callback.h"
36
37 #define STR_ATOM_PANEL_SCROLLABLE_STATE "_E_MOVE_PANEL_SCROLLABLE_STATE"
38
39 static int wifi_exit_end = FALSE;
40 static bool is_scan_reqd = false;
41 wifi_appdata *ug_app_state = NULL;
42
43 UG_MODULE_API int UG_MODULE_INIT(struct ug_module_ops *ops);
44 UG_MODULE_API void UG_MODULE_EXIT(struct ug_module_ops *ops);
45 UG_MODULE_API int setting_plugin_reset(bundle *data, void *priv);
46 UG_MODULE_API int setting_plugin_search_init(app_control_h app_control, void *priv, char **domainname);
47
48 static gboolean __wifi_efl_ug_del_found_ap_noti(void *data)
49 {
50         common_utils_send_message_to_net_popup(NULL, NULL,
51                         "del_found_ap_noti", NULL);
52
53         return FALSE;
54 }
55
56 static void _set_rotation(Evas_Object *win)
57 {
58         int rots[4] = { 0, 90, 180, 270 };
59
60         if (!elm_win_wm_rotation_supported_get(win))
61                 return;
62
63         elm_win_wm_rotation_available_rotations_set(win, (const int *)(&rots), 4);
64 }
65
66 static void *on_create(ui_gadget_h ug, enum ug_mode mode,
67                 app_control_h app_control, void *priv)
68 {
69         __COMMON_FUNC_ENTER__;
70
71         const char *uri = NULL;
72         int state;
73
74         if (!ug || !priv) {
75                 INFO_LOG(UG_NAME_ERR, "UG and PRIV should not be NULL");
76
77                 __COMMON_FUNC_EXIT__;
78                 return NULL;
79         }
80
81         if (ug_app_state) {
82                 INFO_LOG(UG_NAME_NORMAL, "ug_app_state already exists");
83                 return ug_app_state->layout_main;
84         }
85
86         ug_app_state = g_new0(wifi_appdata, 1);
87         retvm_if(NULL == ug_app_state, NULL);
88
89         struct ug_data *ugd;
90         ugd = (struct ug_data*)priv;
91         ugd->ug = ug;
92
93         if (NULL != app_control) {
94                 INFO_LOG(UG_NAME_NORMAL, "message load from caller");
95
96                 char *caller = NULL;
97                 app_control_get_extra_data(app_control, UG_CALLER, &caller);
98
99                 if (app_control_get_uri(app_control, (char **)&uri) < 0)
100                         ERROR_LOG(UG_NAME_NORMAL, "Failed to get app_control URI");
101
102                 if (uri)
103                         free((void *)uri);
104
105                 if (caller != NULL) {
106                         SECURE_INFO_LOG(UG_NAME_NORMAL, "caller: %s", caller);
107
108                         if (g_strcmp0(caller, "pwlock") == 0) {
109                                 ugd->elm_conform = ug_get_conformant();
110                                 ug_app_state->ug_type = UG_VIEW_OOBE;
111                         } else if (g_strcmp0(caller, "notification") == 0) {
112                                 /* Remove the "WiFi networks found" from the notification tray.*/
113                                 common_util_managed_idle_add(__wifi_efl_ug_del_found_ap_noti, NULL);
114                                 ug_app_state->ug_type = UG_VIEW_DEFAULT;
115                         } else if (g_strcmp0(caller, "lbhome") == 0) {
116                                 ug_app_state->ug_type = UG_VIEW_DEFAULT;
117                                 ug_app_state->app_control = app_control;
118                                 ug_app_state->is_lbhome = EINA_TRUE;
119                         } else {
120                                 ug_app_state->ug_type = UG_VIEW_DEFAULT;
121                         }
122
123                         free(caller);
124                 } else {
125                         INFO_LOG(UG_NAME_NORMAL, "caller is not defined");
126                         ug_app_state->ug_type = UG_VIEW_DEFAULT;
127                 }
128
129
130         } else {
131                 INFO_LOG(UG_NAME_NORMAL, "caller is not defined");
132                 ug_app_state->ug_type = UG_VIEW_DEFAULT;
133         }
134
135         Evas_Object *parent_layout = ug_get_parent_layout(ug);
136         if (parent_layout == NULL) {
137                 ERROR_LOG(UG_NAME_NORMAL, "Failed to get parent layout");
138
139                 __COMMON_FUNC_EXIT__;
140                 return NULL;
141         }
142
143         ugd->win_main = ug_get_window();
144         ug_app_state->parent_layout = parent_layout;
145         ug_app_state->gadget = ugd;
146         ug_app_state->ug = ug;
147         ug_app_state->conformant = ug_get_conformant();
148
149         _set_rotation(ugd->win_main);
150
151         common_util_set_system_registry(VCONFKEY_WIFI_UG_RUN_STATE,
152                         VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND);
153
154         Evas_Object *layout_main = viewer_manager_create(parent_layout, ugd->win_main);
155         if (layout_main == NULL) {
156                 INFO_LOG(UG_NAME_ERR, "Failed to create viewer_manager");
157
158                 __COMMON_FUNC_EXIT__;
159                 return NULL;
160         }
161
162         if (app_control != NULL) {
163                 char *zorder = NULL;
164                 app_control_get_extra_data(app_control, "z_order", &zorder);
165                 INFO_LOG(UG_NAME_NORMAL, "zorder [%s]", zorder);
166                 if (zorder != NULL && 0 == g_strcmp0(zorder, "highest")) {
167                         efl_util_set_notification_window_level(ugd->win_main, EFL_UTIL_NOTIFICATION_LEVEL_DEFAULT);
168                         g_free(zorder);
169                         zorder = NULL;
170                 }
171         }
172
173 #if defined TIZEN_TETHERING_ENABLE
174         ug_app_state->popup_manager = winset_popup_manager_create(layout_main, PACKAGE);
175 #endif
176
177         ugd->base = layout_main;
178         ug_app_state->layout_main = layout_main;
179         ug_app_state->bAlive = EINA_TRUE;
180
181         wlan_manager_create();
182         wlan_manager_set_message_callback(wlan_engine_callback);
183         wlan_manager_set_refresh_callback(wlan_engine_refresh_callback);
184
185         switch (wlan_manager_start()) {
186         case WLAN_MANAGER_ERR_NONE:
187                 break;
188
189         case WLAN_MANAGER_ERR_ALREADY_REGISTERED:
190                 break;
191
192         default:
193                 ERROR_LOG(UG_NAME_NORMAL, "Cannot start wlan_manager");
194
195                 __COMMON_FUNC_EXIT__;
196                 return ugd->base;
197         }
198
199         state = wlan_manager_state_get();
200         switch (state) {
201         case WLAN_MANAGER_OFF:
202                 if (ug_app_state->ug_type == UG_VIEW_SETUP_WIZARD) {
203                         viewer_manager_header_mode_set(HEADER_MODE_ACTIVATING);
204                         power_control();
205                 } else {
206                         viewer_manager_hide(VIEWER_WINSET_SUB_CONTENTS);
207                         viewer_manager_header_mode_set(HEADER_MODE_OFF);
208                 }
209                 break;
210
211         case WLAN_MANAGER_CONNECTING:
212         case WLAN_MANAGER_UNCONNECTED:
213         case WLAN_MANAGER_CONNECTED:
214                 viewer_manager_hide(VIEWER_WINSET_SEARCHING);
215                 viewer_manager_header_mode_set(HEADER_MODE_ON);
216                 viewer_manager_show(VIEWER_WINSET_SUB_CONTENTS);
217                 INFO_LOG(UG_NAME_NORMAL, "Issue the WiFi Scan");
218                 viewer_manager_request_scan();
219                 break;
220
221         case WLAN_MANAGER_ERROR:
222         default:
223                 return ugd->base;
224         }
225
226         evas_object_show(layout_main);
227
228         __COMMON_FUNC_EXIT__;
229         return ugd->base;
230 }
231
232 static gboolean load_initial_ap_list(gpointer data)
233 {
234         __COMMON_FUNC_ENTER__;
235
236         /* Because of transition effect performance,
237          * Wi-Fi lists might be better to be updated at maximum delayed
238          */
239         wlan_manager_scanned_profile_refresh();
240
241         __COMMON_FUNC_EXIT__;
242         return FALSE;
243 }
244
245 static void on_start(ui_gadget_h ug, app_control_h app_control, void *priv)
246 {
247         __COMMON_FUNC_ENTER__;
248
249         common_util_managed_idle_add(load_initial_ap_list, NULL);
250         wlan_manager_set_autoscan_mode(WLAN_MANAGER_AUTOSCAN_MODE_PERIODIC);
251
252         __COMMON_FUNC_EXIT__;
253 }
254
255 static void on_pause(ui_gadget_h ug, app_control_h app_control, void *priv)
256 {
257         __COMMON_FUNC_ENTER__;
258
259         INFO_LOG(UG_NAME_NORMAL, "Wi-Fi UG paused");
260
261         wlan_manager_set_autoscan_mode(WLAN_MANAGER_AUTOSCAN_MODE_EXPONENTIAL);
262         common_util_set_system_registry(VCONFKEY_WIFI_UG_RUN_STATE,
263                         VCONFKEY_WIFI_UG_RUN_STATE_ON_BACKGROUND);
264
265         __COMMON_FUNC_EXIT__;
266 }
267
268 static void on_resume(ui_gadget_h ug, app_control_h app_control, void *priv)
269 {
270         __COMMON_FUNC_ENTER__;
271
272         INFO_LOG(UG_NAME_NORMAL, "Wi-Fi UG resumed");
273
274         wlan_manager_set_autoscan_mode(WLAN_MANAGER_AUTOSCAN_MODE_PERIODIC);
275         common_util_set_system_registry(VCONFKEY_WIFI_UG_RUN_STATE,
276                         VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND);
277
278         view_manager_view_type_t top_viewID = viewer_manager_view_type_get();
279         if (top_viewID == VIEW_MANAGER_VIEW_TYPE_MAIN) {
280                 viewer_manager_request_scan();
281                 is_scan_reqd = false;
282         } else {
283                 is_scan_reqd = true;
284         }
285
286         __COMMON_FUNC_EXIT__;
287 }
288
289 static void on_destroy(ui_gadget_h ug, app_control_h app_control, void *priv)
290 {
291         __COMMON_FUNC_ENTER__;
292
293         int ret;
294         struct ug_data* ugd = priv;
295
296         common_util_set_system_registry(VCONFKEY_WIFI_UG_RUN_STATE,
297                         VCONFKEY_WIFI_UG_RUN_STATE_OFF);
298
299         if (!ug || !priv) {
300                 __COMMON_FUNC_EXIT__;
301                 return;
302         }
303
304         /*Added to handle incase of force closure*/
305         passwd_popup_free(ug_app_state->passpopup);
306         ug_app_state->passpopup = NULL;
307
308         if (ug_app_state->timeout) {
309                 g_source_remove(ug_app_state->timeout);
310                 ug_app_state->timeout = 0;
311         }
312
313 #if defined TIZEN_TETHERING_ENABLE
314         winset_popup_manager_destroy(ug_app_state->popup_manager);
315         ug_app_state->popup_manager = NULL;
316         DEBUG_LOG(UG_NAME_NORMAL, "* popup manager destroy complete");
317 #endif
318
319         if (wifi_exit_end == FALSE) {
320                 wlan_manager_set_autoscan_mode(WLAN_MANAGER_AUTOSCAN_MODE_EXPONENTIAL);
321                 common_util_managed_idle_cleanup();
322                 common_util_managed_ecore_scan_update_timer_del();
323
324                 ret = wlan_manager_destroy();
325                 if (ret != WLAN_MANAGER_ERR_NONE)
326                         ERROR_LOG(UG_NAME_NORMAL, "Failed to destroy wlan manager: %d", ret);
327                 else
328                         INFO_LOG(UG_NAME_NORMAL, "* wlan manager destroy complete");
329         }
330
331         viewer_manager_destroy();
332         INFO_LOG(UG_NAME_NORMAL, "* viewer manager destroy complete");
333
334         if (ug_app_state->rbutton_setup_wizard_next != NULL) {
335                 g_free(ug_app_state->rbutton_setup_wizard_next);
336                 ug_app_state->rbutton_setup_wizard_next = NULL;
337         }
338
339         if (ug_app_state->rbutton_setup_wizard_skip != NULL) {
340                 g_free(ug_app_state->rbutton_setup_wizard_skip);
341                 ug_app_state->rbutton_setup_wizard_skip = NULL;
342         }
343
344         if (ug_app_state->lbutton_setup_wizard_prev != NULL) {
345                 g_free(ug_app_state->lbutton_setup_wizard_prev);
346                 ug_app_state->lbutton_setup_wizard_prev = NULL;
347         }
348
349         if (ug_app_state != NULL) {
350                 g_free(ug_app_state);
351                 ug_app_state = NULL;
352         }
353
354         if (ugd->base != NULL) {
355                 evas_object_del(ugd->base);
356                 ugd->base = NULL;
357         }
358
359         __COMMON_FUNC_EXIT__;
360 }
361
362 static void on_message(ui_gadget_h ug, app_control_h msg, app_control_h app_control, void *priv)
363 {
364 }
365
366 static void _language_changed(void)
367 {
368
369         __COMMON_FUNC_ENTER__;
370
371         if (NULL != ug_app_state->rbutton_setup_wizard_next) {
372                 g_free(ug_app_state->rbutton_setup_wizard_next);
373                 ug_app_state->rbutton_setup_wizard_next = NULL;
374         }
375
376         if (NULL != ug_app_state->rbutton_setup_wizard_skip) {
377                 g_free(ug_app_state->rbutton_setup_wizard_skip);
378                 ug_app_state->rbutton_setup_wizard_skip = NULL;
379         }
380
381         if (NULL != ug_app_state->lbutton_setup_wizard_prev) {
382                 g_free(ug_app_state->lbutton_setup_wizard_prev);
383                 ug_app_state->lbutton_setup_wizard_prev = NULL;
384         }
385
386         ug_app_state->rbutton_setup_wizard_next = g_strdup(sc(PACKAGE, I18N_TYPE_Next));
387         ug_app_state->rbutton_setup_wizard_skip = g_strdup(sc(PACKAGE, I18N_TYPE_Skip));
388         ug_app_state->lbutton_setup_wizard_prev = g_strdup(sc(PACKAGE, I18N_TYPE_Prev));
389
390         viewer_manager_setup_wizard_button_controller();
391
392         language_changed_refresh();
393
394         __COMMON_FUNC_EXIT__;
395 }
396
397 static void on_event(ui_gadget_h ug, enum ug_event event, app_control_h app_control, void *priv)
398 {
399         __COMMON_FUNC_ENTER__;
400
401         switch (event) {
402         case UG_EVENT_LOW_MEMORY:
403                 break;
404         case UG_EVENT_LOW_BATTERY:
405                 break;
406         case UG_EVENT_LANG_CHANGE:
407                 INFO_LOG(UG_NAME_NORMAL, "LANGUAGE");
408                 if (UG_VIEW_SETUP_WIZARD == ug_app_state->ug_type)
409                         _language_changed();
410                 break;
411         case UG_EVENT_ROTATE_PORTRAIT:
412         case UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN:
413                 INFO_LOG(UG_NAME_NORMAL, "PORTRAIT");
414                 viewer_manager_rotate_top_setupwizard_layout();
415                 break;
416         case UG_EVENT_ROTATE_LANDSCAPE:
417         case UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN:
418                 INFO_LOG(UG_NAME_NORMAL, "LANSCAPE");
419                 viewer_manager_rotate_top_setupwizard_layout();
420                 break;
421         default:
422                 break;
423         }
424
425         __COMMON_FUNC_EXIT__;
426 }
427
428 static void on_key_event(ui_gadget_h ug, enum ug_key_event event, app_control_h app_control, void *priv)
429 {
430         __COMMON_FUNC_ENTER__;
431
432         if (!ug) {
433                 __COMMON_FUNC_EXIT__;
434                 return;
435         }
436
437         switch (event) {
438         case UG_KEY_EVENT_END:
439                 INFO_LOG(UG_NAME_NORMAL, "UG_KEY_EVENT_END");
440
441 #if defined TIZEN_TETHERING_ENABLE
442                 /* popup key event determine */
443                 winset_popup_hide_popup(ug_app_state->popup_manager);
444 #endif
445                 view_manager_view_type_t top_view_id = viewer_manager_view_type_get();
446                 if (top_view_id == VIEW_MANAGER_VIEW_TYPE_MAIN) {
447                         INFO_LOG(UG_NAME_NORMAL, "same");
448                 } else {
449                         INFO_LOG(UG_NAME_NORMAL, "differ");
450                         elm_naviframe_item_pop(viewer_manager_get_naviframe());
451
452                         __COMMON_FUNC_EXIT__;
453                         return;
454                 }
455
456                 wifi_exit();
457                 break;
458         default:
459                 INFO_LOG(UG_NAME_NORMAL, "UG_KEY_EVENT [%d]", event);
460                 break;
461         }
462
463         __COMMON_FUNC_EXIT__;
464 }
465
466 UG_MODULE_API int UG_MODULE_INIT(struct ug_module_ops *ops)
467 {
468         __COMMON_FUNC_ENTER__;
469
470         wifi_exit_end = FALSE;
471
472         retvm_if(NULL == ops, 0);
473
474         struct ug_data *ugd;
475         ugd = calloc(1, sizeof(struct ug_data));
476
477         assertm_if(NULL == ugd, "Err!! calloc fail");
478
479         ops->create = on_create;
480         ops->start = on_start;
481         ops->pause = on_pause;
482         ops->resume = on_resume;
483         ops->destroy = on_destroy;
484         ops->message = on_message;
485         ops->event = on_event;
486         ops->key_event = on_key_event;
487         ops->priv = ugd;
488         ops->opt = UG_OPT_INDICATOR_ENABLE;
489
490         __COMMON_FUNC_EXIT__;
491         return 0;
492 }
493
494 UG_MODULE_API void UG_MODULE_EXIT(struct ug_module_ops *ops)
495 {
496         __COMMON_FUNC_ENTER__;
497
498         retm_if(NULL == ops);
499
500         struct ug_data *ugd;
501
502         ugd = ops->priv;
503
504         if (ugd)
505                 free(ugd);
506
507         __COMMON_FUNC_EXIT__;
508 }
509
510 static bool setting_plugin_wifi_found_ap_cb(wifi_manager_ap_h ap, void* user_data)
511 {
512         bool favorite = false;
513
514         wifi_manager_ap_is_favorite(ap, &favorite);
515
516         if (true == favorite)
517                 wlan_manager_forget(ap);
518
519         return true;
520 }
521
522 UG_MODULE_API int setting_plugin_reset(bundle *data, void *priv)
523 {
524         __COMMON_FUNC_ENTER__;
525
526         int return_value = 0;
527
528         return_value = wlan_manager_start();
529         if (return_value != WLAN_MANAGER_ERR_NONE) {
530                 ERROR_LOG(UG_NAME_NORMAL, "Failed to register : %d", return_value);
531                 return_value = -1;
532                 goto error;
533         }
534
535         wlan_manager_foreach_found_ap(setting_plugin_wifi_found_ap_cb, NULL);
536         return_value = wlan_manager_power_off();
537         if (return_value != WLAN_MANAGER_ERR_NONE) {
538                 ERROR_LOG(UG_NAME_NORMAL, "Failed to power_off: %d", return_value);
539                 return_value = -1;
540                 goto error;
541         }
542
543         common_util_set_system_registry(VCONFKEY_WIFI_ENABLE_QS,
544                                                                         VCONFKEY_WIFI_QS_ENABLE);
545
546 error:
547         wlan_manager_destroy();
548
549         __COMMON_FUNC_EXIT__;
550         return return_value;
551 }
552
553 int wifi_exit(void)
554 {
555         __COMMON_FUNC_ENTER__;
556
557         if (wifi_exit_end == TRUE) {
558                 __COMMON_FUNC_EXIT__;
559                 return FALSE;
560         }
561         wifi_exit_end = TRUE;
562
563         int ret = WLAN_MANAGER_ERR_NONE;
564         struct ug_data *ugd;
565         ugd = ug_app_state->gadget;
566         ug_app_state->bAlive = EINA_FALSE;
567
568         wlan_manager_set_autoscan_mode(WLAN_MANAGER_AUTOSCAN_MODE_EXPONENTIAL);
569         common_util_managed_idle_cleanup();
570         common_util_managed_ecore_scan_update_timer_del();
571
572         ret = wlan_manager_destroy();
573         if (ret != WLAN_MANAGER_ERR_NONE)
574                 ERROR_LOG(UG_NAME_NORMAL, "Failed to destroy wlan manager: %d", ret);
575         else
576                 DEBUG_LOG(UG_NAME_NORMAL, "* wlan manager destroy complete");
577
578         DEBUG_LOG(UG_NAME_NORMAL, "* ug_destroying...");
579         ug_destroy_me(ugd->ug);
580
581         __COMMON_FUNC_EXIT__;
582         return TRUE;
583 }
584
585 bool wifi_is_scan_required(void)
586 {
587         return is_scan_reqd;
588 }
589
590 UG_TYPE wifi_get_ug_type(void)
591 {
592         return ug_app_state->ug_type;
593 }
594
595 #if 0
596 UG_MODULE_API int setting_plugin_search_init(app_control_h app_control, void *priv, char **domainname)
597 {
598         void *node = NULL;
599         *domainname = strdup(PACKAGE);
600         Eina_List **pplist = (Eina_List **)priv;
601
602         node = setting_plugin_search_item_add("IDS_ST_BODY_NETWORK_NOTIFICATION",
603                 "viewtype:advancedsetting", NULL, 5, NULL);
604         *pplist = eina_list_append(*pplist, node);
605         node = setting_plugin_search_item_add("IDS_WIFI_TMBODY_SMART_NETWORK_SWITCH",
606                 "viewtype:mainview", NULL, 5, NULL);
607         *pplist = eina_list_append(*pplist, node);
608         node = setting_plugin_search_item_add("IDS_WIFI_HEADER_PASSPOINT",
609                 "viewtype:advancedsetting", NULL, 5, NULL);
610         *pplist = eina_list_append(*pplist, node);
611         node = setting_plugin_search_item_add("IDS_ST_BODY_KEEP_WI_FI_ON_DURING_SLEEP",
612                 "viewtype:advancedsetting", NULL, 5, NULL);
613         *pplist = eina_list_append(*pplist, node);
614         node = setting_plugin_search_item_add("IDS_ST_MBODY_ALWAYS_ALLOW_SCANNING",
615                 "viewtype:advancedsetting", NULL, 5, NULL);
616         *pplist = eina_list_append(*pplist, node);
617         node = setting_plugin_search_item_add("IDS_WIFI_BODY_ADVANCED_SETTINGS",
618                 "viewtype:advancedsetting", NULL, 5, NULL);
619         *pplist = eina_list_append(*pplist, node);
620
621         return 0;
622 }
623 #endif