Remove the devider of genlist for IOT profile
[apps/native/ug-bluetooth-efl.git] / standard / bt-main-ug.c
1 /*
2 * ug-bluetooth-efl
3 *
4 * Copyright 2012 Samsung Electronics Co., Ltd
5 *
6 * Contact: Hocheol Seo <hocheol.seo@samsung.com>
7 *           GirishAshok Joshi <girish.joshi@samsung.com>
8 *           DoHyun Pyun <dh79.pyun@samsung.com>
9 *
10 * Licensed under the Flora License, Version 1.1 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.tizenopensource.org/license
15 *
16 * Unless required by applicable law or agreed to in writing,
17 * software distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 *
22 */
23
24 #include <aul.h>
25 #include <vconf.h>
26 #include <vconf-internal-setting-keys.h>
27 #include <Evas.h>
28 #include <Edje.h>
29 #include <dbus/dbus.h>
30 #include <notification.h>
31 #include <efl_extension.h>
32 #include <device/power.h>
33 #include <gesture_recognition.h>
34 #include <bundle.h>
35
36 #ifdef TIZEN_REDWOOD
37 #include <setting-cfg.h>
38 #endif
39
40 #include "bt-main-ug.h"
41 #include "bt-util.h"
42 #include "bt-widget.h"
43 #include "bt-main-view.h"
44 #include "bt-ipc-handler.h"
45 #include "bt-debug.h"
46 #include "bt-resource.h"
47 #include "bt-callback.h"
48 #include "bt-string-define.h"
49 #include "bt-net-connection.h"
50 #include "bluetooth_internal.h"
51
52 #define TOGGLE_OFF      "0"
53 #define TOGGLE_ON       "1"
54 #ifndef __TIZEN_OPEN__
55 #define S_FINDER_TOGGLE_DATA    "s_finder_setting_check_value_set"
56 #endif
57
58 #define BT_AUTO_CONNECT_SYSPOPUP_TIMEOUT_FOR_MULTIPLE_POPUPS 200
59
60 static void __on_destroy(ui_gadget_h ug, app_control_h service, void *priv);
61
62 extern int power_wakeup(bool dim);
63
64
65 /**********************************************************************
66 *                                               Static Functions
67 ***********************************************************************/
68
69 static void bt_ug_change_language(bt_ug_data *ugd)
70 {
71         FN_START;
72
73         ret_if(ugd == NULL);
74
75         if (ugd->profile_vd) {
76                 if (ugd->profile_vd->genlist)
77                         elm_genlist_realized_items_update(
78                                         ugd->profile_vd->genlist);
79
80                 if (ugd->profile_vd->navi_it)
81                         elm_object_item_text_set(ugd->profile_vd->navi_it,
82                                         BT_STR_DETAILS);
83         }
84
85         if (ugd->main_genlist)
86                 elm_genlist_realized_items_update(ugd->main_genlist);
87
88         if (ugd->navi_it)
89                 elm_object_item_text_set(ugd->navi_it, BT_STR_BLUETOOTH);
90         if (ugd->scan_btn) {
91                 if (ugd->op_status == BT_SEARCHING)
92                         elm_object_text_set(ugd->scan_btn, BT_STR_STOP);
93                 else
94                         elm_object_text_set(ugd->scan_btn, BT_STR_SCAN);
95         }
96
97         if (ugd->popup)
98                 _bt_set_popup_text(ugd, ugd->popup);
99
100         FN_END;
101 }
102
103 static void __bt_ug_release_memory(bt_ug_data *ugd)
104 {
105         FN_START;
106
107         bt_dev_t *dev = NULL;
108         Eina_List *l = NULL;
109         Eina_List *l_next = NULL;
110
111         ret_if(ugd == NULL);
112         ret_if(ugd->main_genlist == NULL);
113
114         elm_genlist_clear(ugd->main_genlist);
115         evas_object_del(ugd->main_genlist);
116         ugd->main_genlist = NULL;
117
118         /* Release paired device items */
119         EINA_LIST_FOREACH_SAFE(ugd->paired_device, l, l_next, dev) {
120                 ugd->paired_device =
121                         eina_list_remove_list(ugd->paired_device, l);
122                 _bt_util_free_device_item(dev);
123         }
124
125         /* Release searched device items */
126         EINA_LIST_FOREACH_SAFE(ugd->searched_device, l, l_next, dev) {
127                 ugd->searched_device =
128                     eina_list_remove_list(ugd->searched_device, l);
129                 _bt_util_free_device_item(dev);
130         }
131
132         if (ugd->device_name_itc) {
133                 elm_genlist_item_class_free(ugd->device_name_itc);
134                 ugd->device_name_itc = NULL;
135         }
136
137         if (ugd->rename_entry_itc) {
138                 elm_genlist_item_class_free(ugd->rename_entry_itc);
139                 ugd->rename_entry_itc = NULL;
140         }
141
142         if (ugd->rename_desc_itc) {
143                 elm_genlist_item_class_free(ugd->rename_desc_itc);
144                 ugd->rename_desc_itc = NULL;
145         }
146
147         if (ugd->paired_title_itc) {
148                 elm_genlist_item_class_free(ugd->paired_title_itc);
149                 ugd->paired_title_itc = NULL;
150         }
151
152         if (ugd->searched_title_itc) {
153                 elm_genlist_item_class_free(ugd->searched_title_itc);
154                 ugd->searched_title_itc = NULL;
155         }
156
157         if (ugd->paired_device_itc) {
158                 elm_genlist_item_class_free(ugd->paired_device_itc);
159                 ugd->paired_device_itc = NULL;
160         }
161
162         if (ugd->searched_device_itc) {
163                 elm_genlist_item_class_free(ugd->searched_device_itc);
164                 ugd->searched_device_itc = NULL;
165         }
166
167         if (ugd->no_device_itc) {
168                 elm_genlist_item_class_free(ugd->no_device_itc);
169                 ugd->no_device_itc = NULL;
170         }
171
172         if (ugd->on_itc) {
173                 elm_genlist_item_class_free(ugd->on_itc);
174                 ugd->on_itc = NULL;
175         }
176
177         if (ugd->off_itc) {
178                 elm_genlist_item_class_free(ugd->off_itc);
179                 ugd->off_itc = NULL;
180         }
181
182         if (ugd->popup_bundle) {
183                 free(ugd->popup_bundle);
184                 ugd->popup_bundle = NULL;
185         }
186
187         if (ugd->popup)
188                 _bt_main_popup_del_cb(ugd, NULL, NULL);
189
190         if (ugd->popup_timer) {
191                 g_source_remove(ugd->popup_timer);
192                 ugd->popup_timer = 0;
193         }
194
195         if (ugd->base) {
196                 evas_object_del(ugd->base);
197                 ugd->base = NULL;
198         }
199
200         FN_END;
201 }
202
203 #ifndef __TIZEN_OPEN__
204 void __bt_motion_shake_cb(gesture_type_e motion,
205                                         const gesture_data_h event_data,
206                                         double timestamp,
207                                         gesture_error_e error,
208                                         void *data)
209 {
210         FN_START;
211
212         bt_ug_data *ugd = NULL;
213         gesture_event_e event;
214         int ret = 0;
215
216         ret_if(data == NULL);
217         ret_if(event_data == NULL);
218         ret_if(error != GESTURE_ERROR_NONE);
219
220         ugd = (bt_ug_data *)data;
221
222         if (motion == GESTURE_SHAKE) {
223                 ret = gesture_get_event(data, &event);
224                 ret_if(ret != GESTURE_ERROR_NONE);
225
226                 if (event == GESTURE_SHAKE_DETECTED) {
227                         BT_DBG("Scan devices");
228                         ret = power_wakeup(0);
229                         if (ret != 0)
230                                 BT_ERR("Power wakeup failed [%d]", ret);
231
232                         _bt_main_scan_device(ugd);
233                 }
234         }
235
236         FN_END;
237 }
238 #endif
239
240 static void __bt_main_vconf_change_cb(keynode_t *key, void *data)
241 {
242         retm_if(NULL == key, "key is NULL");
243         retm_if(NULL == data, "data is NULL");
244         bt_ug_data *ugd = (bt_ug_data *)data;
245
246         char *vconf_name = vconf_keynode_get_name(key);
247
248         if (!g_strcmp0(vconf_name, VCONFKEY_SETAPPL_DEVICE_NAME_STR)) {
249                 char *name_value = NULL;
250                 name_value = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
251                 retm_if(!name_value, "Get string is failed");
252                 BT_INFO("name : %s", name_value);
253
254                 if (ugd->device_name_item && g_strcmp0(ugd->phone_name, name_value)) {
255                         _bt_update_genlist_item(ugd->device_name_item);
256                         _bt_update_genlist_item(ugd->rename_entry_item);
257                 }
258                 g_free(name_value);
259         } else {
260                 BT_ERR("vconf_name is error");
261         }
262 }
263
264 static int __bt_initialize_view(bt_ug_data *ugd)
265 {
266         if (ugd->bt_launch_mode == BT_LAUNCH_VISIBILITY)
267                 _bt_main_draw_visibility_view(ugd);
268         else if (ugd->bt_launch_mode == BT_LAUNCH_ONOFF)
269                 _bt_main_draw_onoff_view(ugd);
270         else
271                 _bt_main_draw_list_view(ugd);
272
273         return 0;
274 }
275
276 #ifdef TIZEN_BT_A2DP_SINK_ENABLE
277 static void __bt_free_device_info(bt_dev_t *dev)
278 {
279         ret_if(!dev);
280
281         int i;
282
283         if (dev->uuids) {
284                 for (i = 0; i < dev->uuid_count ; i++) {
285                         if (dev->uuids[i]) {
286                                 g_free(dev->uuids[i]);
287                                 dev->uuids[i] = NULL;
288                         }
289                 }
290                 g_free(dev->uuids);
291                 dev->uuids = NULL;
292         }
293
294         free(dev);
295         dev = NULL;
296 }
297
298 static void __bt_free_paired_device(bt_ug_data *ugd)
299 {
300         FN_START;
301         retm_if(!ugd, "ad is NULL!");
302         retm_if(!ugd->paired_device, "paired_device is NULL!");
303
304         Eina_List *l = NULL;
305         Eina_List *l_next = NULL;
306         bt_dev_t *dev = NULL;
307
308         EINA_LIST_FOREACH_SAFE(ugd->paired_device, l, l_next, dev) {
309                 __bt_free_device_info(dev);
310         }
311
312         eina_list_free(ugd->paired_device);
313         ugd->paired_device = NULL;
314         FN_END;
315 }
316
317 static bool __bt_cb_adapter_create_paired_device_list
318                         (bt_device_info_s *device_info, void *user_data)
319 {
320         FN_START;
321         bt_dev_t *dev = NULL;
322         gboolean connected = FALSE;
323         bt_ug_data *ugd = NULL;
324         unsigned int service_class;
325
326
327         ugd = (bt_ug_data *)user_data;
328         retv_if(ugd == NULL, false);
329
330         dev = _bt_main_create_paired_device_item(device_info);
331         retv_if(!dev, false);
332
333         dev->ugd = (void *)ugd;
334
335         service_class = dev->service_class;
336
337         if (_bt_main_is_matched_profile(ugd->search_type,
338                                         dev->major_class,
339                                         service_class,
340                                         ugd->service,
341                                         dev->minor_class) == TRUE) {
342
343                 if (dev->service_list & BT_SC_HFP_SERVICE_MASK ||
344                     dev->service_list & BT_SC_HSP_SERVICE_MASK ||
345                     dev->service_list & BT_SC_A2DP_SERVICE_MASK) {
346                         connected = _bt_util_is_profile_connected(BT_HEADSET_CONNECTED,
347                                                         dev->bd_addr);
348                         dev->connected_mask |= connected ? BT_HEADSET_CONNECTED : 0x00;
349
350                         connected = _bt_util_is_profile_connected(BT_STEREO_HEADSET_CONNECTED,
351                                                         dev->bd_addr);
352                         dev->connected_mask |=
353                                 connected ? BT_STEREO_HEADSET_CONNECTED : 0x00;
354                 }
355
356                 if (dev->service_list & BT_SC_A2DP_SOURCE_SERVICE_MASK) {
357                         connected = _bt_util_is_profile_connected(BT_MUSIC_PLAYER_CONNECTED,
358                                                         dev->bd_addr);
359                         dev->connected_mask |=
360                                 connected ? BT_MUSIC_PLAYER_CONNECTED : 0x00;
361                 }
362
363                 dev->status = BT_IDLE;
364                 dev->ugd = (void *)ugd;
365                 dev->is_bonded = TRUE;
366                 ugd->paired_device =
367                         eina_list_append(ugd->paired_device, dev);
368                 _bt_update_device_list(ugd);
369         } else {
370                 BT_ERR("Device class and search type do not match");
371                 free(dev);
372         }
373
374         FN_END;
375         return true;
376 }
377
378 static void __bt_get_paired_devices(bt_ug_data *ugd)
379 {
380         FN_START;
381
382         ret_if(ugd == NULL);
383         __bt_free_paired_device(ugd);
384         if (bt_adapter_foreach_bonded_device(
385                         __bt_cb_adapter_create_paired_device_list,
386                         (void *)ugd) != BT_ERROR_NONE) {
387                 BT_ERR("bt_adapter_foreach_bonded_device() failed");
388         }
389
390         FN_END;
391         return;
392 }
393
394 static int __bt_get_paired_device_count(bt_ug_data *ugd)
395 {
396         FN_START;
397         retvm_if(!ugd, 0, "ugd is NULL!");
398         __bt_get_paired_devices(ugd);
399         retvm_if(!ugd->paired_device, 0, "paired_device is NULL!");
400         int count = eina_list_count(ugd->paired_device);
401         //BT_DBG("paired device count : %d", count);
402         return count;
403 }
404
405 static void __bt_create_autoconnect_popup(bt_dev_t *dev)
406 {
407         FN_START;
408
409         int ret = 0;
410         bundle *b = NULL;
411         b = bundle_create();
412         retm_if(!b, "Unable to create bundle");
413
414         bundle_add_str(b, "event-type", "music-auto-connect-request");
415
416         ret = syspopup_launch("bt-syspopup", b);
417         if (0 > ret)
418                 BT_ERR("Popup launch failed...retry %d", ret);
419         bundle_free(b);
420
421         FN_END;
422 }
423
424 static void __bt_auto_connect(bt_ug_data *ugd)
425 {
426         FN_START;
427         if (__bt_get_paired_device_count(ugd) == 1) {
428                 BT_DBG("Launch mode is %d", ugd->bt_launch_mode);
429
430                 bt_dev_t *dev = eina_list_nth(ugd->paired_device, 0);
431                 if (dev == NULL) {
432                         BT_ERR("dev is NULL");
433                         __on_destroy(ugd->ug, NULL, ugd);
434                         return;
435                 }
436
437                 /* Check whether the only paired device is Headset */
438                 if (dev->service_list & BT_SC_A2DP_SOURCE_SERVICE_MASK) {
439                         BT_DBG("Remote device support A2DP Source");
440                         _bt_main_connect_device(ugd, dev);
441                         BT_DBG("dev->status : %d", dev->status);
442                         if (dev->status == BT_CONNECTING) {
443                                 __bt_create_autoconnect_popup(dev);
444                                 return;
445                         }
446                 }
447         }
448
449         if (__bt_initialize_view(ugd) < 0) {
450                 app_control_h service = NULL;
451                 BT_ERR("__bt_initialize_view failed");
452                 service = __bt_main_get_connection_result(ugd, FALSE);
453                 _bt_ug_destroy(ugd, (void *)service);
454         }
455         FN_END;
456 }
457 #endif
458
459 static Eina_Bool __bt_launch_idler(void *data)
460 {
461         FN_START;
462         BT_DBG("UG_LAUNCH_PROFILING");
463
464         int ret;
465         int err;
466         bt_ug_data *ugd = NULL;
467
468         retv_if(data == NULL, ECORE_CALLBACK_CANCEL);
469
470         ugd = (bt_ug_data *)data;
471
472         /* Set event callbacks */
473 #ifndef __TIZEN_OPEN__
474         char *toggle_data = NULL;
475         bt_adapter_state_e value;
476 #endif
477
478 #ifdef TIZEN_BT_A2DP_SINK_ENABLE
479         if (ugd->bt_launch_mode == BT_LAUNCH_CONNECT_AUDIO_SOURCE) {
480                 if (bt_audio_initialize() != BT_ERROR_NONE)
481                         BT_ERR("bt_audio_initialize() failed");
482                 __bt_auto_connect(ugd);
483         } else
484 #endif
485                 __bt_initialize_view(ugd);
486
487         err = _bt_ipc_register_popup_event_signal(ugd);
488         if (err != BT_UG_ERROR_NONE)
489                 BT_ERR("_bt_ipc_register_popup_event_signal failed: %d", err);
490
491         if (bt_audio_initialize() != BT_ERROR_NONE)
492                 BT_ERR("bt_audio_initialize() failed");
493         if (_bt_create_net_connection(&ugd->connection) != BT_UG_ERROR_NONE)
494                 BT_ERR("_bt_create_net_connection fail");
495
496         ret =
497             bt_adapter_set_state_changed_cb(_bt_cb_state_changed, (void *)ugd);
498         if (ret != BT_ERROR_NONE)
499                 BT_ERR("bt_adapter_set_state_changed_cb failed");
500
501         if (ugd->bt_launch_mode == BT_LAUNCH_VISIBILITY ||
502              ugd->bt_launch_mode == BT_LAUNCH_ONOFF) {
503                 /* Don't need to register callback */
504                 return ECORE_CALLBACK_CANCEL;
505         }
506
507         ret =
508             bt_audio_set_connection_state_changed_cb(_bt_cb_audio_state_changed,
509                                                      (void *)ugd);
510         if (ret != BT_ERROR_NONE)
511                 BT_ERR("audio set connection state callback failed");
512
513         ret = bt_device_set_connection_state_changed_cb
514                         (_bt_cb_device_connection_state_changed, (void *)ugd);
515
516         if (ret != BT_ERROR_NONE)
517                 BT_ERR("device set connection state callback failed");
518
519         ret =
520             bt_adapter_set_device_discovery_state_changed_cb
521             (_bt_cb_discovery_state_changed, (void *)ugd);
522         if (ret != BT_ERROR_NONE)
523                 BT_ERR("adapter set device discovery state callback failed");
524
525         if (TIZEN_FEATURE_BT_HOG) {
526                 ret = bt_gatt_set_connection_state_changed_cb
527                         ((bt_gatt_connection_state_changed_cb)_bt_cb_gatt_connection_state_changed, (void *)ugd);
528                 if (ret != BT_ERROR_NONE)
529                         BT_ERR("gatt set connection state callback failed");
530         }
531
532         ret = bt_adapter_set_visibility_mode_changed_cb
533                 (_bt_cb_visibility_mode_changed, (void *)ugd);
534         if (ret != BT_ERROR_NONE)
535                 BT_ERR("adapter set device visibility mode callback failed");
536
537         ret = bt_device_set_bond_created_cb(_bt_cb_bonding_created,
538                                             (void *)ugd);
539         if (ret != BT_ERROR_NONE)
540                 BT_ERR("bt_device_set_bond_created_cb failed");
541
542         ret = bt_device_set_bond_destroyed_cb(_bt_cb_bonding_destroyed,
543                                               (void *)ugd);
544         if (ret != BT_ERROR_NONE)
545                 BT_ERR("bt_device_set_bond_destroyed_cb failed");
546
547         ret = bt_device_set_service_searched_cb(_bt_cb_service_searched,
548                                                 (void *)ugd);
549         if (ret != BT_ERROR_NONE)
550                 BT_ERR("bt_device_set_service_searched_cb failed");
551
552         ret = bt_adapter_set_name_changed_cb(_bt_cb_adapter_name_changed,
553                                              (void *)ugd);
554         if (ret != BT_ERROR_NONE)
555                 BT_ERR("bt_adapter_set_name_changed_cb failed");
556
557         ret = bt_hid_host_initialize(_bt_cb_hid_state_changed, (void *)ugd);
558         if (ret != BT_ERROR_NONE)
559                 BT_ERR("bt_hid_host_initialize failed");
560
561         ret = bt_nap_set_connection_state_changed_cb(_bt_cb_nap_state_changed,
562                                                         (void *)ugd);
563         if (ret != BT_ERROR_NONE)
564                 BT_ERR("bt_nap_set_connection_state_changed_cb failed");
565
566 #ifndef __TIZEN_OPEN__
567         if (app_control_get_extra_data(ugd->service, S_FINDER_TOGGLE_DATA,
568                                   &toggle_data) == APP_CONTROL_ERROR_NONE) {
569                 if (toggle_data) {
570                         if (bt_adapter_get_state(&value) == BT_ERROR_NONE) {
571                                 if (g_strcmp0(toggle_data, TOGGLE_OFF) == 0 &&
572                                         value == BT_ADAPTER_ENABLED) {
573                                         ret = _bt_main_disable_bt(ugd);
574                                         if (ret != BT_ERROR_NONE)
575                                                 BT_ERR("_bt_main_disable_bt fail!");
576                                 } else if (g_strcmp0(toggle_data, TOGGLE_ON) == 0 &&
577                                         value == BT_ADAPTER_DISABLED) {
578                                         ret = _bt_main_enable_bt(ugd);
579                                         if (ret != BT_ERROR_NONE)
580                                                 BT_ERR("_bt_main_enable_bt fail!");
581                                 }
582                         }
583                         free(toggle_data);
584                 }
585         }
586 #endif
587
588         /* In the NFC case, we don't need to display the paired device */
589         if (ugd->op_status == BT_ACTIVATED &&
590             ugd->bt_launch_mode != BT_LAUNCH_USE_NFC) {
591                 _bt_main_draw_paired_devices(ugd);
592         } else if (ugd->op_status == BT_DEACTIVATED &&
593                    ugd->bt_launch_mode != BT_LAUNCH_NORMAL) {
594                 _bt_main_enable_bt(ugd);
595         }
596
597         /* In the NFC case, we don't need to display the paired device */
598         if (ugd->op_status == BT_ACTIVATED &&
599                         ugd->bt_launch_mode != BT_LAUNCH_USE_NFC &&
600                         ugd->bt_launch_mode != BT_LAUNCH_ONOFF) {
601                 if (_bt_util_is_battery_low() == FALSE) {
602
603                         if (TIZEN_FEATURE_BT_HOG) {
604                                 ret = bt_adapter_le_start_scan((bt_adapter_le_scan_result_cb)_bt_cb_new_le_device_found, (void *)ugd);
605                                 if (!ret) {
606                                         ugd->is_le_discovery_started = TRUE;
607                                         BT_DBG("LE Discovery started");
608                                 } else {
609                                         BT_ERR("LE Discovery failed : Error Cause[%d]", ret);
610                                 }
611                         }
612                         ret = bt_adapter_start_device_discovery();
613                         if (!ret) {
614                                 ugd->op_status = BT_SEARCHING;
615                                 elm_object_text_set(ugd->scan_btn,
616                                                 BT_STR_STOP);
617
618                                 if (ugd->searched_title == NULL)
619                                         _bt_main_add_searched_title
620                                             (ugd);
621                         } else {
622                                 BT_ERR
623                                     ("Operation failed : Error Cause[%d]",
624                                      ret);
625                         }
626                 }
627         }
628         FN_END;
629         BT_DBG("UG_LAUNCH_PROFILING");
630         return ECORE_CALLBACK_CANCEL;
631 }
632
633 static void *__on_create(ui_gadget_h ug, enum ug_mode mode, app_control_h service,
634                          void *priv)
635 {
636         FN_START;
637
638         bt_ug_data *ugd = NULL;
639         BT_DBG("UG_LAUNCH_PROFILING");
640 #ifndef __TIZEN_OPEN__
641         gesture_h handle;
642 #endif
643
644         retv_if(!ug || !priv, NULL);
645
646         ugd = (bt_ug_data *)priv;
647         ugd->ug = ug;
648
649         bindtextdomain(PKGNAME, LOCALEDIR);
650
651         ugd->win_main = ug_get_window();
652
653         if (!ugd->win_main) {
654                 __on_destroy(ug, NULL, ugd);
655                 return NULL;
656         }
657
658         _bt_main_init_status(ugd, service);
659         BT_DBG("ugd->bt_launch_mode : %d", ugd->bt_launch_mode);
660
661 #ifndef __TIZEN_OPEN__
662         int ret;
663         bool supported = false;
664
665         ret = gesture_is_supported(GESTURE_SHAKE, &supported);
666         if (ret != GESTURE_ERROR_NONE) {
667                 BT_ERR("gesture_is_supported failed : %d", ret);
668         } else  {
669                 if (!supported) {
670                         BT_ERR("gesture is not supported");
671                 } else {
672                         ret =  gesture_create(&handle);
673                         if (ret != GESTURE_ERROR_NONE) {
674                                 BT_ERR("gesture_create failed : %d", ret);
675                         } else {
676                                 ugd->handle = handle;
677                                 ret = gesture_start_recognition(handle, GESTURE_SHAKE,
678                                         GESTURE_OPTION_DEFAULT, __bt_motion_shake_cb, ugd);
679                                 if (ret != GESTURE_ERROR_NONE) {
680                                         BT_ERR("gesture_start_recognition failed : %d", ret);
681                                         gesture_release(handle);
682                                         ugd->handle = NULL;
683                                 }
684                         }
685                 }
686         }
687 #endif
688         ugd->service = service;
689
690         ugd->base = _bt_main_base_layout_create(ugd->win_main, ugd);
691         if (!ugd->base) {
692                 __on_destroy(ug, NULL, ugd);
693                 return NULL;
694         }
695         /* Add layout for custom styles */
696         elm_theme_extension_add(NULL, BT_GENLIST_EDJ);
697
698 #if 0
699         if (ugd->bt_launch_mode == BT_LAUNCH_VISIBILITY)
700                 _bt_main_draw_visibility_view(ugd);
701         else if (ugd->bt_launch_mode == BT_LAUNCH_ONOFF)
702                 _bt_main_draw_onoff_view(ugd);
703         else
704                 _bt_main_draw_list_view(ugd);
705 #endif
706
707         ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
708                         __bt_main_vconf_change_cb, ugd);
709
710         if (ret < 0)
711                 BT_ERR("vconf_notify_key_changed failed");
712
713 //      ugd->visibility_changed_by_ug = FALSE;
714         ugd->ug_status = BT_UG_CREATE;
715
716         BT_DBG("UG_LAUNCH_PROFILING");
717         FN_END;
718         return ugd->base;
719 }
720
721 static void __on_start(ui_gadget_h ug, app_control_h service, void *priv)
722 {
723         FN_START;
724         BT_DBG("UG_LAUNCH_PROFILING");
725
726         bt_ug_data *ugd = NULL;
727
728         ugd = priv;
729
730 /* Tizen 2.4's setting supports auto rotate mode */
731 #if 0
732         elm_win_wm_rotation_preferred_rotation_set(ugd->win_main, 0);
733 #endif
734
735         if (!ecore_idler_add(__bt_launch_idler, ugd))
736                 BT_ERR("idler can not be added");
737         _bt_util_change_discoverable_mode(ugd, TRUE);
738         ugd->ug_status = BT_UG_START;
739         BT_DBG("UG_LAUNCH_PROFILING");
740
741         FN_END;
742 }
743
744 static void __on_pause(ui_gadget_h ug, app_control_h service, void *priv)
745 {
746         FN_START;
747         BT_DBG("UG_LAUNCH_PROFILING");
748         bt_ug_data *ugd = NULL;
749
750         retm_if(priv == NULL, "Invalid argument: priv is NULL");
751
752         BT_INFO("Pause UG");
753
754         ugd = (bt_ug_data *)priv;
755
756         if (ugd->op_status == BT_SEARCHING)
757                 bt_adapter_stop_device_discovery();
758
759         _bt_util_change_discoverable_mode(ugd, FALSE);
760
761         ugd->ug_status = BT_UG_PAUSE;
762
763         FN_END;
764 }
765
766 static void __on_resume(ui_gadget_h ug, app_control_h service, void *priv)
767 {
768         FN_START;
769         BT_DBG("UG_LAUNCH_PROFILING");
770
771         bt_ug_data *ugd = NULL;
772
773         retm_if(priv == NULL, "Invalid argument: priv is NULL");
774
775         BT_INFO("Resume UG");
776
777         ugd = (bt_ug_data *)priv;
778
779         if (ugd->timer) {
780                 ecore_timer_del(ugd->timer);
781                 ugd->timer = NULL;
782         }
783         ugd->timer = ecore_timer_add(2, _bt_timer_change_discoverable_mode, ugd);
784
785         ugd->ug_status = BT_UG_RESUME;
786
787         FN_END;
788 }
789
790 static void __on_destroy(ui_gadget_h ug, app_control_h service, void *priv)
791 {
792         FN_START;
793
794         bt_ug_data *ugd = NULL;
795         int err;
796
797         if (!ug || !priv)
798                 return;
799
800         ugd = priv;
801
802         ugd->ug_status = BT_UG_DESTORY;
803
804         if (ugd->timer) {
805                 ecore_timer_del(ugd->timer);
806                 ugd->timer = NULL;
807         }
808
809         if (ugd->request_timer) {
810                 ecore_timer_del(ugd->request_timer);
811                 ugd->request_timer = NULL;
812         }
813
814         if (ugd->timeout_id) {
815                 g_source_remove(ugd->timeout_id);
816                 ugd->timeout_id = 0;
817         }
818
819         if (ugd->network_timer) {
820                 ecore_timer_del(ugd->network_timer);
821                 ugd->network_timer = NULL;
822         }
823
824         /* This has been added before cancel discovery so as to
825                 prevent device addition till cancel discovery */
826         err = bt_adapter_unset_device_discovery_state_changed_cb();
827         if (err != BT_ERROR_NONE)
828                 BT_ERR("unset of device discovery state cb failed: %d", err);
829         if (ugd->op_status == BT_SEARCHING) {
830                 err = bt_adapter_stop_device_discovery();
831                 if (err != BT_ERROR_NONE)
832                         BT_ERR("Stop device discovery failed: %d", err);
833         }
834
835         err = bt_adapter_unset_state_changed_cb();
836         if (err != BT_ERROR_NONE)
837                 BT_ERR("unset of state change cb  failed: %d", err);
838
839         err = bt_device_unset_bond_created_cb();
840         if (err != BT_ERROR_NONE)
841                 BT_ERR("unset of bond creation cb failed: %d", err);
842
843         err = bt_device_unset_bond_destroyed_cb();
844         if (err != BT_ERROR_NONE)
845                 BT_ERR("unset of bond destroyed cb failed: %d", err);
846
847         err = bt_device_unset_service_searched_cb();
848         if (err != BT_ERROR_NONE)
849                 BT_ERR("unset of service search cb failed: %d", err);
850
851         err = bt_audio_unset_connection_state_changed_cb();
852         if (err != BT_ERROR_NONE)
853                 BT_ERR("unset audio connection state cb failed: %d", err);
854
855         err = bt_device_unset_connection_state_changed_cb();
856         if (err != BT_ERROR_NONE)
857                 BT_ERR("unset device connection state cb failed: %d", err);
858
859         err = bt_adapter_unset_name_changed_cb();
860         if (err != BT_ERROR_NONE)
861                 BT_ERR("unset name change cb failed: %d", err);
862
863         err = bt_hid_host_deinitialize();
864         if (err != BT_ERROR_NONE)
865                 BT_ERR("bt_hid_host_deinitialize failed: %d", err);
866
867         err = bt_audio_deinitialize();
868         if (err != BT_ERROR_NONE)
869                 BT_ERR("bt_audio_deinitialize failed: %d", err);
870
871         err = bt_nap_unset_connection_state_changed_cb();
872         if (err != BT_ERROR_NONE)
873                 BT_ERR("bt_nap_unset_connection_state_changed_cb err=%d", err);
874
875         _bt_util_change_discoverable_mode(ugd, FALSE);
876
877         err = bt_deinitialize();
878         if (err != BT_ERROR_NONE)
879                 BT_ERR("bt_deinitialize failed: %d", err);
880
881         if (ugd->connection) {
882                 err = _bt_destroy_net_connection(ugd->connection);
883                 if (err != BT_UG_ERROR_NONE)
884                         BT_ERR("_bt_destroy_net_connection failed: %d", err);
885
886                 ugd->connection = NULL;
887         }
888
889         err = vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
890                         (vconf_callback_fn) __bt_main_vconf_change_cb);
891
892         if (err < 0)
893                 BT_ERR("vconf_ignore_key_changed failed");
894
895         err = _bt_ipc_unregister_popup_event_signal(ugd);
896         if (err != BT_UG_ERROR_NONE)
897                 BT_ERR("_bt_ipc_unregister_popup_event_signal failed: %d", err);
898
899         _bt_util_destroy_dpm_context(ugd);
900
901         __bt_ug_release_memory(ugd);
902
903 #ifndef __TIZEN_OPEN__
904         if (ugd->handle) {
905                 gesture_release(ugd->handle);
906                 ugd->handle = NULL;
907         }
908 #endif
909         FN_END;
910 }
911
912 static void __on_message(ui_gadget_h ug, app_control_h msg, app_control_h service,
913                          void *priv)
914 {
915         FN_START;
916         FN_END;
917 }
918
919 static void __on_event(ui_gadget_h ug, enum ug_event event, app_control_h service,
920                        void *priv)
921 {
922         FN_START;
923
924         bt_ug_data *ugd = NULL;
925
926         ret_if(priv == NULL);
927
928         ugd = (bt_ug_data *)priv;
929
930         BT_INFO("Event : %d", event);
931
932         switch (event) {
933         case UG_EVENT_LOW_MEMORY:
934                 break;
935         case UG_EVENT_LOW_BATTERY:
936                 if (_bt_util_is_battery_low() == FALSE)
937                         return;
938
939                 if (ugd->op_status == BT_SEARCHING)
940                         bt_adapter_stop_device_discovery();
941
942                 _bt_main_create_information_popup(ugd, BT_STR_LOW_BATTERY);
943                 break;
944         case UG_EVENT_LANG_CHANGE:
945                 bt_ug_change_language(ugd);
946                 break;
947         default:
948                 break;
949         }
950         FN_END;
951 }
952
953 static void __on_key_event(ui_gadget_h ug, enum ug_key_event event,
954                            app_control_h service, void *priv)
955 {
956         FN_START;
957
958         bt_ug_data *ugd = NULL;
959
960         BT_INFO("Key event UG : %d", event);
961
962         ugd = (bt_ug_data *)priv;
963
964         switch (event) {
965         case UG_KEY_EVENT_END:
966                 _bt_ug_destroy(ugd, NULL);
967                 break;
968         default:
969                 break;
970         }
971         FN_END;
972 }
973
974 /**********************************************************************
975 *                                             Common Functions
976 ***********************************************************************/
977
978 UG_MODULE_API int UG_MODULE_INIT(struct ug_module_ops *ops)
979 {
980         FN_START;
981         BT_DBG("UG_LAUNCH_PROFILING");
982
983         bt_ug_data *ugd = NULL;
984
985         if (!ops)
986                 return BT_UG_FAIL;
987
988         ugd = calloc(1, sizeof(bt_ug_data));
989         if (!ugd)
990                 return BT_UG_FAIL;
991
992         ops->create = __on_create;
993         ops->start = __on_start;
994         ops->pause = __on_pause;
995         ops->resume = __on_resume;
996         ops->destroy = __on_destroy;
997         ops->message = __on_message;
998         ops->event = __on_event;
999         ops->key_event = __on_key_event;
1000         ops->priv = ugd;
1001         ops->opt = UG_OPT_INDICATOR_ENABLE;
1002
1003         FN_END;
1004         return BT_UG_ERROR_NONE;
1005 }
1006
1007 UG_MODULE_API void UG_MODULE_EXIT(struct ug_module_ops *ops)
1008 {
1009         FN_START;
1010
1011         bt_ug_data *ugd;
1012
1013         if (!ops)
1014                 return;
1015
1016         ugd = ops->priv;
1017         if (ugd)
1018                 free(ugd);
1019
1020         FN_END;
1021 }
1022
1023 UG_MODULE_API int setting_plugin_reset(app_control_h service, void *priv)
1024 {
1025         FN_START;
1026
1027         int ret;
1028         int result = 0;
1029
1030         ret = bt_initialize();
1031
1032         if (ret != BT_ERROR_NONE) {
1033                 BT_ERR("Fail to init BT %d", ret);
1034                 return ret;
1035         }
1036
1037         ret = bt_adapter_reset();
1038
1039         if (ret != BT_ERROR_NONE) {
1040                 BT_ERR("Fail to reset adapter: %d", ret);
1041                 result = ret;
1042         }
1043
1044         ret = bt_deinitialize();
1045
1046         if (ret != BT_ERROR_NONE)
1047                 BT_ERR("Fail to deinit BT: %d", ret);
1048
1049         FN_END;
1050         return result;
1051 }
1052
1053 void _bt_ug_destroy(void *data, void *result)
1054 {
1055         FN_START;
1056         int ret = 0;
1057         bt_ug_data *ugd = NULL;
1058
1059         BT_INFO("Destroy UG");
1060
1061         ret_if(data == NULL);
1062
1063         ugd = (bt_ug_data *)data;
1064
1065         if (result != NULL)
1066                 ug_send_result(ugd->ug, result);
1067
1068         if (ugd->ug)
1069                 ret = ug_destroy_me(ugd->ug);
1070
1071         if (ret < 0)
1072                 BT_ERR("Fail to destroy me");
1073
1074         FN_END;
1075 }
1076
1077 #ifndef __TIZEN_OPEN__
1078 #ifdef TIZEN_REDWOOD
1079
1080 static bt_setting_cfg_node_t s_cfg_node_array_bt[] = {
1081                 {"IDS_BT_BODY_BLUETOOTH", NULL,
1082                         "viewtype:frontpage;tab:first;keyword:IDS_BT_BODY_BLUETOOTH",
1083                         Cfg_Item_Pos_Level0, 0, 0, Cfg_Item_View_Node_Toggle, NULL},
1084                 {"IDS_BT_BODY_VISIBLE", NULL,
1085                         "viewtype:frontpage;tab:first;keyword:IDS_BT_BODY_VISIBLE",
1086                         Cfg_Item_Pos_Level0, 0, 0, BT_CFG_ITEM_VIEW_NODE, NULL},
1087 };
1088
1089 UG_MODULE_API int setting_plugin_search_init(app_control_h service,
1090                                         void *priv, char **applocale)
1091 {
1092         FN_START;
1093         BT_DBG("ug-bluetooth-efl DB search code: SETTING_SEARCH");
1094
1095         Eina_List **pplist = NULL;
1096         int i;
1097         int size = sizeof(s_cfg_node_array_bt)/sizeof(s_cfg_node_array_bt[0]);
1098
1099         *applocale = strdup(PKGNAME);
1100         pplist = (Eina_List **)priv;
1101
1102         for (i = 0; i < size; i++) {
1103                 bt_setting_cfg_node_t *node =
1104                                 (bt_setting_cfg_node_t *)setting_plugin_search_item_add(
1105                                         s_cfg_node_array_bt[i].key_name,
1106                                         s_cfg_node_array_bt[i].ug_args,
1107                                         NULL, s_cfg_node_array_bt[i].item_type,
1108                                         s_cfg_node_array_bt[i].data);
1109                 *pplist = eina_list_append(*pplist, node);
1110         }
1111         FN_END;
1112         return 0;
1113 }
1114
1115 /**************************************************************
1116  toggle state get/set function for "bluetooth status"
1117 ************************************************************/
1118 EXPORT_PUBLIC
1119 int get_display_ug_state(Cfg_Item_State *stat, void *data)
1120 {
1121         FN_START;
1122
1123         int value = -1;
1124         int ret = vconf_get_int(VCONFKEY_BT_STATUS, &value);
1125         retvm_if(ret != 0, ret, "fail to get vconf key!");
1126
1127         if (value == VCONFKEY_BT_STATUS_OFF)
1128                 *stat = Cfg_Item_Off;
1129         else
1130                 *stat = Cfg_Item_On;
1131
1132         FN_END;
1133         return ret;
1134 }
1135
1136 EXPORT_PUBLIC
1137 int set_display_ug_state(Cfg_Item_State stat, void *item, void *data)
1138 {
1139         FN_START;
1140         int value = -1;
1141         int ret = 0;
1142
1143         if (stat == Cfg_Item_On)
1144                 value = VCONFKEY_BT_STATUS_ON;
1145         else
1146                 value = VCONFKEY_BT_STATUS_OFF;
1147
1148         ret = vconf_set_int(VCONFKEY_BT_STATUS, value);
1149         retvm_if(ret != 0, ret, "fail to set vconf key!");
1150
1151         FN_END;
1152         return ret;
1153 }
1154
1155 EXPORT_PUBLIC
1156 int set_display_ug_update_ui(Cfg_Item_State stat, void* data)
1157 {
1158         FN_START;
1159
1160         FN_END;
1161         return 0;
1162 }
1163
1164 EXPORT_PUBLIC
1165 cfg_func_table opt_tab_bluetooth = {
1166         .get_item_state = get_display_ug_state,
1167         .set_item_state = set_display_ug_state,
1168         .set_item_update_ui = set_display_ug_update_ui,
1169 };
1170
1171 UG_MODULE_API int setting_plugin_search_query_ops(char *str_id,
1172                                                                 void **tfunc_obj)
1173 {
1174         FN_START;
1175         BT_DBG(">> get tfunc operation via plugin-model 1");
1176         if (str_id && !g_strcmp0(str_id, _("IDS_BT_BODY_BLUETOOTH")))
1177                 *tfunc_obj = (void*)&opt_tab_bluetooth;
1178
1179         FN_END;
1180         return 0;
1181 }
1182 #endif
1183 #endif
1184