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