304b37306aded376eb3ab93f4c2f4f7a3dd8e109
[apps/native/ug-mobile-ap.git] / src / mh_main_ug.c
1 /*
2 * ug-mobile-ap
3 *
4 * Copyright 2012  Samsung Electronics Co., Ltd
5
6 * Licensed under the Flora License, Version 1.0 (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 "mobile_hotspot.h"
25 #include "mh_view_main.h"
26 #include "mh_func_onoff.h"
27 #include "mh_common_utility.h"
28 #include "mh_popup.h"
29 #include "mh_string.h"
30 #include "mh_view_wifi_setup.h"
31
32 static Evas_Object *create_content(mh_appdata_t *ad)
33 {
34         __MOBILE_AP_FUNC_ENTER__;
35
36         ad->naviframe = _create_naviframe(ad->layout);
37         _main_draw_contents(ad);
38
39         __MOBILE_AP_FUNC_EXIT__;
40
41         return ad->naviframe;
42 }
43
44 static void __set_callbacks(tethering_h handle, void *user_data)
45 {
46         DBG("+\n");
47
48         int ret;
49
50         ret = tethering_set_enabled_cb(handle, TETHERING_TYPE_ALL,
51                         _enabled_cb, user_data);
52         if (ret != TETHERING_ERROR_NONE)
53                 ERR("tethering_set_enabled_cb [0x%X]\n", ret);
54
55         ret = tethering_wifi_set_passphrase_changed_cb(handle,
56                         _passphrase_changed_cb, user_data);
57         if (ret != TETHERING_ERROR_NONE)
58                 ERR("tethering_wifi_set_passphrase_changed_cb [0x%X]\n", ret);
59
60         ret = tethering_wifi_set_ssid_visibility_changed_cb(handle,
61                         _visibility_changed_cb, user_data);
62         if (ret != TETHERING_ERROR_NONE)
63                 ERR("tethering_wifi_set_ssid_visibility_changed_cb [0x%X]\n", ret);
64
65         ret = tethering_wifi_set_security_type_changed_cb(handle,
66                         _security_type_changed_cb, user_data);
67         if (ret != TETHERING_ERROR_NONE)
68                 ERR("tethering_wifi_set_security_type_changed_cb [0x%X]\n", ret);
69
70         ret = tethering_set_disabled_cb(handle, TETHERING_TYPE_ALL,
71                         _disabled_cb, user_data);
72         if (ret != TETHERING_ERROR_NONE)
73                 ERR("tethering_set_disabled_cb [0x%X]\n", ret);
74
75         ret = tethering_set_connection_state_changed_cb(handle,
76                         TETHERING_TYPE_ALL,
77                         _connection_changed_cb, user_data);
78         if (ret != TETHERING_ERROR_NONE)
79                 ERR("tethering_set_connection_state_changed_cb [0x%X]\n", ret);
80
81         ret = wifi_set_device_state_changed_cb(_wifi_state_changed_cb, user_data);
82         if (ret != WIFI_ERROR_NONE)
83                 ERR("wifi_set_device_state_changed_cb [0x%X]\n", ret);
84         vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
85                                 _device_name_changed_cb, user_data);
86
87         vconf_notify_key_changed(VCONFKEY_NETWORK_CELLULAR_STATE,
88                         _handle_network_cellular_state_changed_cb, user_data);
89
90         vconf_notify_key_changed(VCONFKEY_SYSMAN_USB_STATUS,
91                         _handle_usb_status_change, (void *)user_data);
92
93         vconf_notify_key_changed(VCONF_KEY_MOBILEAP_SYSPOPUP_RESPONSE,
94                         _handle_mobileap_syspopup_popup_response, (void *)user_data);
95
96         DBG("-\n");
97 }
98
99 static void __unset_callbacks(tethering_h handle)
100 {
101         DBG("+\n");
102
103         int ret;
104
105         ret = wifi_unset_device_state_changed_cb();
106         if (ret != WIFI_ERROR_NONE)
107                 ERR("wifi_unset_device_state_changed_cb [0x%X]\n", ret);
108
109         ret = tethering_unset_connection_state_changed_cb(handle,
110                         TETHERING_TYPE_ALL);
111         if (ret != TETHERING_ERROR_NONE)
112                 ERR("tethering_unset_connection_state_changed_cb[0x%X]\n", ret);
113
114         ret = tethering_unset_disabled_cb(handle, TETHERING_TYPE_ALL);
115         if (ret != TETHERING_ERROR_NONE)
116                 ERR("tethering_unset_disabled_cb [0x%X]\n", ret);
117
118         ret = tethering_wifi_unset_security_type_changed_cb(handle);
119         if (ret != TETHERING_ERROR_NONE)
120                 ERR("tethering_wifi_unset_security_type_changed_cb [0x%X]\n", ret);
121
122         ret = tethering_wifi_unset_ssid_visibility_changed_cb(handle);
123         if (ret != TETHERING_ERROR_NONE)
124                 ERR("tethering_wifi_unset_ssid_visibility_changed_cb [0x%X]\n", ret);
125
126         ret = tethering_wifi_unset_passphrase_changed_cb(handle);
127         if (ret != TETHERING_ERROR_NONE)
128                 ERR("tethering_wifi_unset_passphrase_changed_cb [0x%X]\n", ret);
129
130         ret = tethering_unset_enabled_cb(handle, TETHERING_TYPE_ALL);
131         if (ret != TETHERING_ERROR_NONE)
132                 ERR("tethering_unset_enabled [0x%X]\n", ret);
133
134         vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
135                         _device_name_changed_cb);
136
137         vconf_ignore_key_changed(VCONFKEY_NETWORK_CELLULAR_STATE,
138                         _handle_network_cellular_state_changed_cb);
139
140         vconf_ignore_key_changed(VCONFKEY_SYSMAN_USB_STATUS, _handle_usb_status_change);
141
142         vconf_ignore_key_changed(VCONF_KEY_MOBILEAP_SYSPOPUP_RESPONSE,
143                         _handle_mobileap_syspopup_popup_response);
144
145         DBG("-\n");
146 }
147
148 static void *on_create(ui_gadget_h ug, enum ug_mode mode,
149                 app_control_h app_control, void *priv)
150 {
151         DBG("+\n");
152
153         if (!ug || !priv) {
154                 ERR("The param is NULL\n");
155                 return NULL;
156         }
157
158         if (mode != UG_MODE_FULLVIEW) {
159                 ERR("Only Fullview is supported\n");
160                 return NULL;
161         }
162
163         Evas_Object *layout;
164         Evas_Object *content;
165         mh_ugdata_t *ugd;
166         mh_appdata_t *ad;
167         int ret;
168
169         bindtextdomain(MH_TEXT_DOMAIN, MH_LOCALEDIR);
170         dgettext(PACKAGE, LOCALEDIR);
171
172         ad = (mh_appdata_t *)malloc(sizeof(mh_appdata_t));
173         if (ad == NULL) {
174                 ERR("Error!!! failed to allocate memory()\n");
175                 return NULL;
176         }
177         memset(ad, 0x0, sizeof(mh_appdata_t));
178
179         ugd = (mh_ugdata_t *)priv;
180         ugd->ad = ad;
181         ugd->ug = ug;
182         ad->gadget = ugd;
183
184         _send_signal_qp(QP_SIGNAL_PROGRESS_RESET);
185
186         ecore_imf_init();
187
188         ret = tethering_create(&ad->handle);
189 #ifndef TIZEN_FEATURE_EMULATOR
190         if (ret != TETHERING_ERROR_NONE) {
191                 ERR("tethering_create is failed : %d\n", ret);
192                 free(ad);
193                 ugd->ad = NULL;
194                 return NULL;
195         }
196 #endif
197
198         ad->win = ug_get_parent_layout(ug);
199         if (!ad->win) {
200                 ERR("ad->win is NULL\n");
201                 free(ad);
202                 ugd->ad = NULL;
203                 return NULL;
204         }
205
206         /* set rotation */
207         if (elm_win_wm_rotation_supported_get(ad->win)) {
208                 int rots[4] = {0, 90, 180, 270};
209                 elm_win_wm_rotation_available_rotations_set(ad->win, (const int *)(&rots), 4);
210         }
211
212         layout = _create_win_layout(ad);
213         if (layout == NULL) {
214                 ERR("_create_win_layout is failed\n");
215                 free(ad);
216                 ugd->ad = NULL;
217                 return NULL;
218         }
219
220         content = create_content(ad);
221         if (content == NULL) {
222                 ERR("create_content is failed\n");
223                 free(ad);
224                 ugd->ad = NULL;
225                 return NULL;
226         }
227
228         elm_object_part_content_set(layout, "elm.swallow.content", content);
229         evas_object_show(layout);
230         ret = connection_create(&ad->conn_handle);
231         if (ret != CONNECTION_ERROR_NONE)
232                 ERR("connection_create() is failed : %d\n", ret);
233
234         ret = wifi_initialize();
235         if (ret != WIFI_ERROR_NONE)
236                 ERR("wifi_initialize() is failed : %d\n", ret);
237
238         __set_callbacks(ad->handle, (void *)ad);
239
240         DBG("-\n");
241         return layout;
242 }
243
244 static void on_start(ui_gadget_h ug, app_control_h app_control, void *priv)
245 {
246         DBG("+\n");
247
248         char * viewtype = NULL;
249
250         mh_ugdata_t *ugd = (mh_ugdata_t *)priv;
251         mh_appdata_t *ad = ugd->ad;
252
253         ad->is_foreground = true;
254
255         if (_get_list_clients_count(ad) > 0) {
256 #ifdef TETHERING_DATA_USAGE_SUPPORT
257                 _start_update_data_packet_usage(ad);
258 #endif
259                 if (ad->navi_item)
260                         _start_update_device_conn_time(ad);
261         }
262
263         app_control_get_extra_data(app_control, "viewtype", &viewtype);
264
265         if (viewtype != NULL) {
266                 if (strcmp(viewtype, "wifisettings") == 0)
267                         mh_draw_wifi_setup_view(ad);
268                 g_free(viewtype);
269         }
270
271         DBG("-\n");
272         return;
273 }
274
275 static void on_pause(ui_gadget_h ug, app_control_h app_control, void *priv)
276 {
277         DBG("+\n");
278
279         mh_ugdata_t *ugd = (mh_ugdata_t *)priv;
280         mh_appdata_t *ad = ugd->ad;
281
282 #ifdef TETHERING_DATA_USAGE_SUPPORT
283         _stop_update_data_packet_usage(ad);
284 #endif
285         _stop_update_device_conn_time(ad);
286         ad->is_foreground = false;
287
288         DBG("-\n");
289 }
290
291 static void on_resume(ui_gadget_h ug, app_control_h app_control, void *priv)
292 {
293         DBG("+\n");
294
295         mh_ugdata_t *ugd = (mh_ugdata_t *)priv;
296         mh_appdata_t *ad = ugd->ad;
297         Elm_Object_Item *item = ad->main.device_item;
298         GSList *l = NULL;
299
300         ad->is_foreground = true;
301
302         if (item && elm_genlist_item_expanded_get(item)) {
303                 for (l = ad->client_list; l != NULL; l = g_slist_next(l)) {
304                         item = elm_genlist_item_next_get(item);
305                         elm_genlist_item_fields_update(item, "elm.text", ELM_GENLIST_ITEM_FIELD_TEXT);
306                 }
307         }
308
309         if (_get_list_clients_count(ad) > 0) {
310 #ifdef TETHERING_DATA_USAGE_SUPPORT
311                 _start_update_data_packet_usage(ad);
312 #endif
313                 if (ad->navi_item)
314                         _start_update_device_conn_time(ad);
315         }
316         DBG("-\n");
317 }
318
319 static void on_destroy(ui_gadget_h ug, app_control_h app_control, void *priv)
320 {
321         DBG("+\n");
322
323         if (priv == NULL) {
324                 ERR("The param is NULL\n");
325                 return;
326         }
327
328         mh_ugdata_t *ugd = priv;
329         mh_appdata_t *ad = ugd->ad;
330         int ret = 0;
331
332         if (ad == NULL) {
333                 ERR("The param is NULL\n");
334                 return;
335         }
336
337         __unset_callbacks(ad->handle);
338
339 #ifdef TETHERING_DATA_USAGE_SUPPORT
340         _stop_update_data_packet_usage(ad);
341 #endif
342         _stop_update_device_conn_time(ad);
343
344         if (vconf_set_int(VCONF_MOBILE_AP_CONNECT_USB_POPUP_STATUS, 0) < 0)
345                 ERR("vconf_set_int is failed\n");
346
347         ret = wifi_deinitialize();
348         if (ret != WIFI_ERROR_NONE)
349                 ERR("wifi_deinitialize() is failed : %d\n", ret);
350
351         ret = connection_destroy(ad->conn_handle);
352         if (ret != CONNECTION_ERROR_NONE)
353                 ERR("connection_destroy() is failed : %d\n", ret);
354
355         ret = tethering_destroy(ad->handle);
356         if (ret != TETHERING_ERROR_NONE)
357                 ERR("tethering_destroy() is failed : %d\n", ret);
358
359         if (ad->layout == NULL) {
360                 ERR("ad->layout is NULL\n");
361                 free(ugd->ad);
362                 ugd->ad = NULL;
363                 return;
364         }
365
366         if (ad->popup) {
367                 evas_object_del(ad->popup);
368                 ad->popup = NULL;
369         }
370
371         if (ad->ps_recheck_timer_id > 0) {
372                 g_source_remove(ad->ps_recheck_timer_id);
373                 ad->ps_recheck_timer_id = 0;
374         }
375
376         evas_object_del(ad->bg);
377         ad->bg = NULL;
378
379         evas_object_del(ad->layout);
380         ad->layout = NULL;
381
382         _main_free_genlist_itc(ad);
383
384         ecore_imf_shutdown();
385
386         free(ugd->ad);
387         ugd->ad = NULL;
388
389         DBG("-\n");
390         return;
391 }
392
393 static void on_message(ui_gadget_h ug, app_control_h msg,
394                 app_control_h app_control, void *priv)
395 {
396 }
397
398 static void on_event(ui_gadget_h ug, enum ug_event event,
399                 app_control_h app_control, void *priv)
400 {
401         __MOBILE_AP_FUNC_ENTER__;
402
403         if (!ug || !priv) {
404                 ERR("The param is NULL\n");
405                 return;
406         }
407
408         switch (event) {
409         case UG_EVENT_LOW_MEMORY:
410                 break;
411         case UG_EVENT_LOW_BATTERY:
412                 break;
413         case UG_EVENT_LANG_CHANGE:
414                 break;
415         case UG_EVENT_ROTATE_PORTRAIT:
416         case UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN:
417                 DBG("UG_EVENT_ROTATE_PORTRAIT[_UPSIDEDOWN]\n");
418                 break;
419         case UG_EVENT_ROTATE_LANDSCAPE:
420         case UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN:
421                 DBG("UG_EVENT_ROTATE_LANDSCAPE[_UPSIDEDOWN]\n");
422                 break;
423         default:
424                 break;
425         }
426
427         __MOBILE_AP_FUNC_EXIT__;
428 }
429
430 static void on_key_event(ui_gadget_h ug, enum ug_key_event event,
431                 app_control_h app_control, void *priv)
432 {
433         __MOBILE_AP_FUNC_ENTER__;
434
435         if (priv == NULL || ug == NULL) {
436                 ERR("The param is NULL\n");
437                 return;
438         }
439
440         mh_ugdata_t *ugd = (mh_ugdata_t *)priv;
441         mh_appdata_t *ad = ugd->ad;
442
443         if (ad == NULL) {
444                 ERR("ad is NULL\n");
445                 return;
446         }
447
448         switch (event) {
449         case UG_KEY_EVENT_END:
450                 DBG("UG_KEY_EVENT_END is received : %p\n", ad->popup);
451                 if (NULL == ad->popup)
452                         ug_destroy_me(ug);
453                 else
454                         _destroy_popup(ad);
455                 break;
456
457         default:
458                 break;
459         }
460
461         __MOBILE_AP_FUNC_EXIT__;
462 }
463
464 UG_MODULE_API int UG_MODULE_INIT(struct ug_module_ops *ops)
465 {
466         DBG("+\n");
467
468         if (!ops) {
469                 ERR("The param is NULL\n");
470                 return -1;
471         }
472
473         mh_ugdata_t *ugd;
474         ugd = calloc(1, sizeof(mh_ugdata_t));
475         if (!ugd) {
476                 ERR("Quit : calloc failed(ugd)\n");
477                 return -1;
478         }
479
480         ops->create = on_create;
481         ops->start = on_start;
482         ops->pause = on_pause;
483         ops->resume = on_resume;
484         ops->destroy = on_destroy;
485         ops->message = on_message;
486         ops->event = on_event;
487         ops->key_event = on_key_event;
488         ops->priv = ugd;
489         ops->opt = UG_OPT_INDICATOR_ENABLE;
490
491         return 0;
492 }
493
494 UG_MODULE_API void UG_MODULE_EXIT(struct ug_module_ops *ops)
495 {
496         DBG("+\n");
497
498         if (!ops) {
499                 ERR("The param is NULL\n");
500                 return;
501         }
502
503         mh_ugdata_t *ugd = (mh_ugdata_t *)ops->priv;
504
505         if (ugd)
506                 free(ugd);
507 }
508
509 static void __mh_reset_vconf(tethering_h handle)
510 {
511         int ret = 0;
512
513         ret = vconf_set_int(VCONF_MOBILE_AP_WIFI_POPUP_CHECKBOX_STATUS, 0);
514         if (ret != 0)
515                 ERR("vconf_set_int failed\n");
516
517         ret = vconf_set_int(VCONF_MOBILE_AP_BT_POPUP_CHECKBOX_STATUS, 0);
518         if (ret != 0)
519                 ERR("vconf_set_int failed\n");
520
521         ret = vconf_set_int(VCONF_MOBILE_AP_USB_POPUP_CHECKBOX_STATUS, 0);
522         if (ret != 0)
523                 ERR("vconf_set_int failed\n");
524
525         ret = vconf_set_int(VCONF_MOBILE_AP_PREV_WIFI_STATUS, 0);
526         if (ret != 0)
527                 ERR("vconf_set_int failed\n");
528
529         ret = vconf_set_int(VCONF_MOBILE_AP_CONNECT_USB_POPUP_STATUS, 0);
530         if (ret != 0)
531                 ERR("vconf_set_int is failed\n");
532
533         ret = tethering_wifi_set_ssid_visibility(handle, true);
534         if (ret != TETHERING_ERROR_NONE)
535                 ERR("tethering_wifi_set_ssid_visibility failed\n");
536
537         ret = tethering_wifi_set_security_type(handle, TETHERING_WIFI_SECURITY_TYPE_WPA2_PSK);
538         if (ret != TETHERING_ERROR_NONE)
539                 ERR("tethering_wifi_set_security_type failed\n");
540
541         return;
542 }
543
544 UG_MODULE_API int setting_plugin_reset(app_control_h app_control, void *priv)
545 {
546         DBG("+\n");
547
548         int ret = -1;
549         tethering_h handle = NULL;
550
551         if (tethering_create(&handle) != TETHERING_ERROR_NONE) {
552                 ERR("tethering_create failed\n");
553                 return -1;
554         }
555
556         if (tethering_is_enabled(handle, TETHERING_TYPE_USB) ||
557                         tethering_is_enabled(handle, TETHERING_TYPE_WIFI) ||
558                         tethering_is_enabled(handle, TETHERING_TYPE_BT)) {
559                 ret = tethering_disable(handle, TETHERING_TYPE_ALL);
560                 if (ret != TETHERING_ERROR_NONE) {
561                         ERR("tethering_disable failed : %d\n", ret);
562                         tethering_destroy(handle);
563                         return -1;
564                 }
565
566         }
567
568         __mh_reset_vconf(handle);
569
570         tethering_destroy(handle);
571
572         return 0;
573 }