Initialize Tizen 2.3
[apps/home/b2-clocksetting.git] / src / setting-bluetooth.c
1 /*
2  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 /*
18  * setting-bluetooth.c
19  *
20  *  Created on: Oct 9, 2013
21  *      Author: min-hoyun
22  */
23
24 #include "setting-bluetooth.h"
25 #include "setting_control_bt.h"
26 #include "setting_data_vconf.h"
27 #include "util.h"
28
29
30 static struct _bt_menu_item bt_menu_its[] = {
31         { "IDS_QP_BUTTON_BLUETOOTH",            DISABLE, _blutooth_cb           },
32         { "IDS_VCALL_BODY_BT_HEADSET",          DISABLE, _BT_headset_cb         },
33         { "IDS_ST_HEADER_DEVICE_NAME_ABB",      DISABLE, _visibility_cb         },      // "IDS_OP_BODY_VISIBILITY"
34         { NULL, DISABLE, NULL }
35 };
36
37 static char * bluetooth_enable_str[] = {
38                 "IDS_ST_BODY_OFF_M_STATUS",
39                 "IDS_ST_BODY_ON_M_STATUS",
40                 "IDS_COM_POP_PROCESSING"
41 };
42
43 static char * visible_str[] = {
44                 "IDS_ST_BODY_DISABLED_M_STATUS",
45                 "IDS_ST_BODY_YOUR_GEAR_IS_VISIBLE_FOR_PS_ABB"
46 };
47
48
49 static Ecore_Timer *bt_timer = NULL;
50 static Ecore_Timer *vb_timer = NULL;
51
52 static Elm_Object_Item* bt_it = NULL;
53 static Elm_Object_Item* vb_it = NULL;
54 static Elm_Object_Item* hf_it = NULL;
55
56 static Evas_Object * bt_genlist = NULL;
57 static Evas_Object * g_bt_check = NULL;
58
59 static int is_bt_operating = BT_NON_OPERATING;
60 static int timeout_seconds = VISIBILITY_TIMEOUT;
61 static bt_adapter_visibility_mode_e visibility_mode = BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE;
62 static int origin_bt_enable;
63 static double bt_time = 0.0;
64 static int is_connected_hf = FALSE;
65 static char * g_device_name = NULL;
66
67
68 //static void bt_state_vconf_change_cb(keynode_t * key, void * data);
69 static void _init_bt_value();
70 static int is_handsfree_connected();
71 static void _bt_genlist_update();
72
73 static void sap_state_vconf_change_cb(keynode_t *key, void * data)
74 {
75         _update_visibility_item_view(is_handsfree_connected());
76 }
77
78 static void _bt_adapter_state_enabled_cb(int result, bt_adapter_state_e adapter_state, void *user_data)
79 {
80         DBG("Setting - _bt_adapter_state_enabled_cb() is called!");
81
82         if (adapter_state == BT_ADAPTER_ENABLED)
83         {
84                 DBG("Setting - BT adapter state : BT_ADAPTER_ENABLED");
85
86                 bt_menu_its[BT_MENU_TYPE_BT_ON_OFF].is_enable = 1;
87
88                 _bt_genlist_update();
89         }
90         else if (adapter_state == BT_ADAPTER_DISABLED)
91         {
92                 DBG("Setting - BT adapter state : BT_ADAPTER_ENABLED");
93 #if 0
94                 if( bt_it )
95                 {
96                         elm_genlist_item_fields_update( bt_it, "elm.text.2", ELM_GENLIST_ITEM_FIELD_TEXT);
97                         edje_object_signal_emit(elm_layout_edje_get(g_bt_check), "elm,state,enabled", "elm");
98                         elm_check_state_set(g_bt_check, EINA_FALSE);
99                 }
100 #endif
101                 bt_menu_its[BT_MENU_TYPE_BT_ON_OFF].is_enable = 0;
102                 is_connected_hf = FALSE;
103                 _bt_genlist_update();
104         }
105         _init_bt_value();
106
107 }
108
109 void initialize_bt()
110 {
111         DBG("Setting - initialize_bt()");
112
113         if( bt_initialize() != BT_ERROR_NONE )
114         {
115                 ERR("Setting - bt_initialize() is failed....");
116                 return;
117         }
118         DBG("Setting - bt_initialize() is success");
119
120         if(bt_adapter_set_state_changed_cb(_bt_adapter_state_enabled_cb, NULL) != BT_ERROR_NONE)
121         {
122                 ERR("[%s] bt_adapter_set_state_changed_cb() failed.", __FUNCTION__);
123                 return;
124         }
125         else
126         {
127                 DBG("bt_adapter_set_state_changed_cb() is success!");
128         }
129
130         bluetooth_hf_init(hf_event_handler, NULL);
131
132         register_vconf_changing("memory/private/sap/conn_status", sap_state_vconf_change_cb, NULL);
133 }
134
135 Eina_Bool _clear_bluetooth_cb(void *data, Elm_Object_Item *it)
136 {
137         clear_bt_resource();
138
139         return EINA_TRUE;
140 }
141
142 static void _disable_visibility_item_view()
143 {
144         bt_menu_its[BT_MENU_TYPE_VISIBLE_ON_OFF].is_enable = DISABLE;
145         timeout_seconds = VISIBILITY_TIMEOUT;
146
147         if( vb_it )
148         {
149                 elm_genlist_item_update(vb_it);
150         }
151 }
152
153 static void hf_event_handler(int event, void *data, void *user_data)
154 {
155         switch (event) {
156         case BLUETOOTH_EVENT_HF_CONNECTED:
157                 DBG("Setting - BLUETOOTH_EVENT_HF_CONNECTED");
158                 _update_visibility_item_view(TRUE);
159                 break;
160
161         case BLUETOOTH_EVENT_HF_DISCONNECTED:
162                 DBG("Setting - BLUETOOTH_EVENT_HF_DISCONNECTED");
163                 _update_visibility_item_view(is_handsfree_connected());
164                 break;
165         }
166 }
167
168 #if 0
169 static void bt_state_vconf_change_cb(keynode_t * key, void * data)
170 {
171         DBG("Setting - bt_state_vconf_change_cb() is called!");
172
173         bt_menu_its[BT_MENU_TYPE_BT_ON_OFF].is_enable = vconf_keynode_get_int(key);
174
175         if( bt_menu_its[BT_MENU_TYPE_BT_ON_OFF].is_enable == VCONFKEY_BT_STATUS_ON )
176         {
177                 DBG("Setting - bt is On");
178
179                 _bt_genlist_update();
180         }
181         else if( bt_menu_its[BT_MENU_TYPE_BT_ON_OFF].is_enable == VCONFKEY_BT_STATUS_OFF )
182         {
183                 DBG("Setting - bt is Off");
184
185                 is_connected_hf = FALSE;
186
187                 _disable_visibility_item_view();
188
189                 if( bt_it )
190                 {
191                         elm_genlist_item_fields_update( bt_it, "elm.text.2", ELM_GENLIST_ITEM_FIELD_TEXT);
192
193                         elm_check_state_set(g_bt_check, EINA_FALSE);
194                 }
195                 if( hf_it )
196                 {
197                         elm_genlist_item_update(hf_it);
198                 }
199         }
200
201         _init_bt_value();
202 }
203 #endif
204
205 void _update_visibility_item_view(int is_hf_connected)
206 {
207         DBG("Setting - _update_view() is called!!");
208
209         if(is_hf_connected)
210         {
211                 is_connected_hf = TRUE;
212
213                 if(vb_timer)
214                 {
215                         ecore_timer_del(vb_timer);
216                         vb_timer = NULL;
217                 }
218
219                 bt_menu_its[BT_MENU_TYPE_VISIBLE_ON_OFF].is_enable = DISABLE;
220                 timeout_seconds = VISIBILITY_TIMEOUT;
221
222                 if( vb_it )
223                 {
224                         elm_genlist_item_update(vb_it);
225                 }
226         }
227         else
228         {
229                 is_connected_hf = FALSE;
230
231                 if( vb_it )
232                 {
233                         elm_genlist_item_update(vb_it);
234                 }
235         }
236 }
237
238 void clear_bt_resource()
239 {
240         bt_menu_its[BT_MENU_TYPE_BT_ON_OFF].is_enable = DISABLE;
241         is_bt_operating = BT_NON_OPERATING;
242
243         if ( timeout_seconds == 0 && vb_timer )
244         {
245                 ecore_timer_del(vb_timer);
246                 vb_timer = NULL;
247
248                 bt_menu_its[BT_MENU_TYPE_VISIBLE_ON_OFF].is_enable = DISABLE;
249                 timeout_seconds = VISIBILITY_TIMEOUT;
250         }
251
252         bluetooth_hf_deinit();
253
254         int ret = bt_adapter_unset_state_changed_cb();
255         
256         if (ret != BT_ERROR_NONE)
257         {
258                 ERR("Setting - bt adapter unset state changed cb is fail");
259         }
260
261         if (bt_deinitialize() < 0)
262         {
263                 ERR("Setting - bt_deinitialize() failed.");
264                 return;
265         }
266         DBG("Setting - bt_deinitialize() is success");
267
268         bt_genlist = NULL;
269         vb_it = NULL;
270         bt_it = NULL;
271         g_bt_check = NULL;
272
273         /* Unregister SAP status vconf changeing callback */
274         unregister_vconf_changing("memory/private/sap/conn_status", sap_state_vconf_change_cb);
275 }
276
277 static void _init_bt_value()
278 {
279         bt_timer = NULL;
280         bt_time = 0.0;
281         timeout_seconds = VISIBILITY_TIMEOUT;
282         bt_menu_its[BT_MENU_TYPE_VISIBLE_ON_OFF].is_enable = DISABLE;
283         is_bt_operating = BT_NON_OPERATING;
284 }
285
286 static void _alternate_bt_mode( void * data )
287 {
288         int ret = 0;
289         bt_adapter_state_e value;
290
291         //appdata * ad = data;
292
293         int prev_bt_enable = bt_menu_its[BT_MENU_TYPE_BT_ON_OFF].is_enable;
294         bt_menu_its[BT_MENU_TYPE_BT_ON_OFF].is_enable = 2;
295         if( bt_it != NULL)
296         {
297                 elm_genlist_item_fields_update( bt_it, "elm.text.2", ELM_GENLIST_ITEM_FIELD_TEXT);
298                 elm_check_state_set(g_bt_check, !prev_bt_enable);
299                 edje_object_signal_emit(elm_layout_edje_get(g_bt_check), "elm,state,disabled", "elm");
300         }
301
302         if (bt_adapter_get_state(&value) != BT_ERROR_NONE)
303         {
304                 ERR("Setting - bt_adapter_get_state() is failed ");
305                 _init_bt_value();
306                 return;
307         }       
308
309         if( value == EINA_TRUE )
310         {
311                 DBG("Setting - Current bt is on....disable...");
312
313                 bt_menu_its[BT_MENU_TYPE_BT_ON_OFF].is_enable = 0;
314                 is_connected_hf = FALSE;
315                 _disable_visibility_item_view();
316
317                 if( hf_it )
318                 {
319                         elm_genlist_item_update(hf_it);
320                 }
321
322                 ret = bt_adapter_disable();
323
324                 timeout_seconds = 0;    // vb_timer stop!!
325         }
326         else
327         {
328                 DBG("Setting - Current bt is off....enable...");
329                 ret = bt_adapter_enable();
330         }
331
332         if ( ret != BT_ERROR_NONE )
333         {
334                 DBG("Setting - Enalbe or Disable failed.... : %d", ret);
335
336                 bt_menu_its[BT_MENU_TYPE_BT_ON_OFF].is_enable = origin_bt_enable;
337                 
338                 _bt_genlist_update();
339                 _init_bt_value();
340
341                 return;
342         }
343 }
344
345 static void _blutooth_cb(void *data, Evas_Object *obj, void *event_info)
346 {
347         DBG("Setting - %s", "Setting - _blutooth_cb is called");
348
349         Elm_Object_Item* it = (Elm_Object_Item *)event_info;
350         elm_genlist_item_selected_set(it, EINA_FALSE);
351
352         if (is_bt_operating == BT_OPERATING)
353         {
354                 DBG("Setting - _blutooth_cb() is same status ");
355
356                 return;
357         }
358         is_bt_operating = BT_OPERATING;
359
360         appdata *ad = data;
361         if( ad == NULL )
362         {
363                 DBG("%s", "Setting - _blutooth_cb - ad or check is null");
364                 return;
365         }
366
367         // previous state backup
368         origin_bt_enable = bt_menu_its[BT_MENU_TYPE_BT_ON_OFF].is_enable;
369
370         if( vb_timer != NULL && vb_it != NULL )
371         {
372                 DBG("Setting - vb_it is disabled");
373
374                 bt_menu_its[BT_MENU_TYPE_VISIBLE_ON_OFF].is_enable = DISABLE;
375                 timeout_seconds = 0;
376
377                 ecore_timer_del(vb_timer);
378                 vb_timer = NULL;
379
380                 elm_genlist_item_update(vb_it);
381         }
382
383         _alternate_bt_mode(data);
384 }
385
386 static int is_valid_timeout(int seconds)
387 {
388         return ( (seconds > 0) && (seconds <= VISIBILITY_TIMEOUT) );
389 }
390
391 static void _bt_genlist_update()
392 {
393         if( bt_it )
394         {
395                 elm_genlist_item_fields_update( bt_it, "elm.text.2", ELM_GENLIST_ITEM_FIELD_TEXT);
396
397                 int bt_enable = bt_menu_its[BT_MENU_TYPE_BT_ON_OFF].is_enable;
398                 edje_object_signal_emit(elm_layout_edje_get(g_bt_check), "elm,state,enabled", "elm");
399                 elm_check_state_set(g_bt_check, (bt_enable == 1) ? EINA_TRUE : EINA_FALSE);
400         }
401         if( hf_it )
402         {
403                 elm_genlist_item_update(hf_it);
404         }
405         if( vb_it )
406         {
407                 elm_genlist_item_update(vb_it);
408         }
409 }
410
411 static void _update_visibility_item_update(void * data)
412 {
413         appdata * ad = data;
414         if( ad == NULL )
415         {
416                 DBG("Setting - ad is null.");
417                 return;
418         }
419
420         if( ad->MENU_TYPE == SETTING_BLUETOOTH )
421         {
422                 DBG("Setting - update_visibility_item_update");
423
424                 _bt_genlist_update();
425         }
426 }
427
428 static void _init_vb_data(void * data)
429 {
430         bt_menu_its[BT_MENU_TYPE_VISIBLE_ON_OFF].is_enable = DISABLE;
431         timeout_seconds = VISIBILITY_TIMEOUT;
432         _update_visibility_item_update(data);
433         vb_timer = NULL;
434 }
435
436 static Eina_Bool __vb_timeout(void *data)
437 {
438         if( is_valid_timeout(timeout_seconds) )
439         {
440                 DBG("Setting - %d seconds", timeout_seconds);
441
442                 --timeout_seconds;
443
444                 _update_visibility_item_update(data);
445
446                 return ECORE_CALLBACK_RENEW;
447         }
448
449         bt_menu_its[BT_MENU_TYPE_VISIBLE_ON_OFF].is_enable = DISABLE;
450         _update_visibility_item_update(data);
451
452         DBG("Setting - visibility time is out.");
453
454         if( visibility_mode != BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE )
455         {
456                 if( bt_adapter_set_visibility(BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE, 0) != BT_ERROR_NONE )
457                 {
458                         DBG("[%s] Setting - bt_adapter_set_visibility(NON) failed.", __FUNCTION__);
459                         _init_vb_data(data);
460                         return ECORE_CALLBACK_CANCEL;
461                 }
462                 visibility_mode = BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE;
463                 DBG("[%s] Setting - bt_adapter_set_visibility(NON) success.", __FUNCTION__);
464         }
465
466         _init_vb_data(data);
467
468         return ECORE_CALLBACK_CANCEL;
469 }
470
471 static void _start_visibility_timer(void * data)
472 {
473         if ( vb_timer)  // Timer Start
474         {
475                 ecore_timer_del(vb_timer);
476                 vb_timer = NULL;
477         }
478         vb_timer = ecore_timer_add(1.0, (Ecore_Task_Cb) __vb_timeout, data);
479 }
480
481 static void _bt_visibility_mode(void * data)
482 {
483         bt_adapter_state_e bt_state;
484
485         if( bt_adapter_get_state(&bt_state) != BT_ERROR_NONE )
486         {
487                 DBG("[%s] Setting - bt_adapter_get_state() failed.", __FUNCTION__);
488                 return;
489         }
490
491         int state = DISABLE;
492         vconf_get_int(VCONFKEY_BT_STATUS, &state);
493
494         DBG("Setting - bt state : %d", state);
495
496         if( bt_state == BT_ADAPTER_ENABLED )
497         {
498                 if(bt_adapter_get_visibility(&visibility_mode, NULL) != BT_ERROR_NONE)
499                 {
500                         DBG("[%s] Setting - bt_adapter_get_visibility() failed.", __FUNCTION__);
501                         return;
502                 }
503
504                 DBG("Setting - visibility_mode : %d", visibility_mode);
505
506                 if( visibility_mode == BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE )
507                 {
508                         if( bt_adapter_set_visibility(BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE, 0) != BT_ERROR_NONE )
509                         {
510                                 DBG("[%s] Setting - bt_adapter_set_visibility(VISIBLE) failed.", __FUNCTION__);
511                         }
512                         else
513                         {
514                                 visibility_mode = BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE;
515                                 DBG("[%s] Setting - bt_adapter_set_visibility(VISIBLE) success.", __FUNCTION__);
516
517                                 bt_menu_its[BT_MENU_TYPE_VISIBLE_ON_OFF].is_enable = ENABLE;
518                                 timeout_seconds = VISIBILITY_TIMEOUT;
519
520                                 if( vb_it )
521                                 {
522                                         elm_genlist_item_update(vb_it);
523                                 }
524
525                                 _start_visibility_timer(data);  // Timer start
526                         }
527                 }
528                 else if( visibility_mode == BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE )
529                 {
530                         DBG("[%s] Setting - Visibility mode was already set as BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE.", __FUNCTION__);
531
532                         timeout_seconds = 0;    // Timer stop;
533
534                         if( bt_adapter_set_visibility(BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE, 0) != BT_ERROR_NONE )
535                         {
536                                 DBG("[%s] Setting - bt_adapter_set_visibility(NON) failed.", __FUNCTION__);
537                         }
538                         else
539                         {
540                                 visibility_mode = BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE;
541                                 DBG("[%s] Setting - bt_adapter_set_visibility(NON) success.", __FUNCTION__);
542                         }
543                 }
544         }
545 }
546
547 static int is_disable_visibility_item_view()
548 {
549         return ( !bt_menu_its[BT_MENU_TYPE_BT_ON_OFF].is_enable || is_connected_hf );
550 }
551
552 static void _visibility_cb(void *data, Evas_Object *obj, void *event_info)
553 {
554         DBG("%s", "Setting - _visibility_cb is called");
555
556         Elm_Object_Item* it = (Elm_Object_Item *)event_info;
557         elm_genlist_item_selected_set(it, EINA_FALSE);
558
559         if( is_disable_visibility_item_view() )
560         {
561                 DBG("Setting - disable visibility!!");
562                 return;
563         }
564
565         appdata *ad = data;
566         if( ad == NULL )
567         {
568                 DBG("%s", "Setting - _visibility_cb - ad or check is null");
569                 return;
570         }
571
572         _bt_visibility_mode(data);
573 }
574
575 static int _is_enable_BT_headset()
576 {
577         int enable = FALSE;
578
579         vconf_get_int(VCONFKEY_BT_STATUS, &enable);
580
581         return (enable != VCONFKEY_BT_STATUS_OFF) && bt_menu_its[BT_MENU_TYPE_BT_ON_OFF].is_enable;
582 }
583
584 static void _BT_headset_cb(void *data, Evas_Object *obj, void *event_info)
585 {
586         DBG("%s", "Setting - _BT_headset_cb is called");
587
588         Elm_Object_Item* it = (Elm_Object_Item *)event_info;
589         elm_genlist_item_selected_set(it, EINA_FALSE);
590
591         if( !_is_enable_BT_headset() )
592                 return;
593
594         service_h service;
595         service_create(&service);
596         service_set_app_id(service, "org.tizen.bluetooth");
597         service_add_extra_data(service, "launch-type", "setting");
598         service_send_launch_request(service, NULL, NULL);
599         service_destroy(service);
600 }
601
602 static char * _get_device_name()
603 {
604         char *bt_adapter_name = NULL;
605
606 #if 0
607         bt_adapter_state_e bt_state;
608         if( bt_adapter_get_state(&bt_state) != BT_ERROR_NONE )
609         {
610                 DBG("[%s] Setting - bt_adapter_get_state() failed.", __FUNCTION__);
611                 return NULL;
612         }
613         if (bt_adapter_get_name(&bt_adapter_name) != BT_ERROR_NONE)
614         {
615                 ERR("%s,%d: bt get name fail", __func__, __LINE__);
616                 return NULL;
617         }
618 #endif
619         bt_adapter_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
620
621         return strdup(bt_adapter_name);
622 }
623
624 static char * _gl_bt_title_get(void *data, Evas_Object *obj, const char *part)
625 {
626         char buf[1024];
627         Bt_Item_Data *id = data;
628         int index = id->index;
629         char expression[32];
630
631         if( !strcmp(part, "elm.text.1") || !strcmp(part, "elm.text") )
632         {
633                 if( index == BT_MENU_TYPE_BT_ON_OFF )
634                 {
635                         snprintf(buf, sizeof(buf)-1, "%s", _(bt_menu_its[BT_MENU_TYPE_BT_ON_OFF].name));
636                 }
637                 else if( index == BT_MENU_TYPE_VISIBLE_ON_OFF )
638                 {
639                         if( is_disable_visibility_item_view() )
640                         {
641                                 strcpy(expression, "<font color=#515151>%s</font>");
642                         }
643                         else
644                         {
645                                 strcpy(expression, "%s");
646                         }
647                         snprintf(buf, sizeof(buf)-1, expression, _(bt_menu_its[BT_MENU_TYPE_VISIBLE_ON_OFF].name));
648                 }
649                 else
650                 {
651                         if( !_is_enable_BT_headset() )
652                         {
653                                 strcpy(expression, "<font color=#515151>%s</font>");
654                         }
655                         else
656                         {
657                                 strcpy(expression, "%s");
658                         }
659                         snprintf(buf, sizeof(buf)-1, expression, _(bt_menu_its[BT_MENU_TYPE_BT_HEADSET].name));
660                 }
661         }
662         else if( !strcmp(part, "elm.text.2") )
663         {
664                 if( index == BT_MENU_TYPE_BT_ON_OFF )
665                 {
666                         DBG("BT item subtitle updated!!");
667                         int sub_title_type = bt_menu_its[BT_MENU_TYPE_BT_ON_OFF].is_enable;
668                         char * subtitle = subtitle = _(bluetooth_enable_str[sub_title_type]);
669                         snprintf(buf, sizeof(buf)-1, "%s", subtitle);
670                 }
671                 else if( index == BT_MENU_TYPE_VISIBLE_ON_OFF )
672                 {
673                         if( is_disable_visibility_item_view() )
674                         {
675                                 strcpy(expression, "<font color=#515151>%s</font>");
676
677                                 if( g_device_name == NULL )
678                                 {
679                                         g_device_name = _get_device_name();
680                                         if( g_device_name == NULL )
681                                         {
682                                                 g_device_name = _(visible_str[bt_menu_its[BT_MENU_TYPE_VISIBLE_ON_OFF].is_enable]);
683                                         }
684                                 }
685
686                                 snprintf(buf, sizeof(buf)-1, expression, g_device_name);
687                         }
688                         else
689                         {
690                                 if( bt_menu_its[BT_MENU_TYPE_VISIBLE_ON_OFF].is_enable )
691                                 {
692                                         int hour = timeout_seconds / 60;
693                                         int minutes = timeout_seconds % 60;
694
695                                         char time_buf[16] = {0,};
696                                         snprintf(time_buf, sizeof(time_buf)-1, "%02d:%02d", hour, minutes);
697
698                                         snprintf(buf, sizeof(buf)-1, _(visible_str[bt_menu_its[BT_MENU_TYPE_VISIBLE_ON_OFF].is_enable]), time_buf);
699                                 }
700                                 else
701                                 {
702                                         //snprintf(buf, sizeof(buf)-1, "%s", _(visible_str[bt_menu_its[BT_MENU_TYPE_VISIBLE_ON_OFF].is_enable]));
703
704                                         if( g_device_name == NULL )
705                                         {
706                                                 g_device_name = _get_device_name();
707                                                 if( g_device_name == NULL )
708                                                 {
709                                                         g_device_name = _(visible_str[bt_menu_its[BT_MENU_TYPE_VISIBLE_ON_OFF].is_enable]);
710                                                 }
711                                         }
712
713                                         snprintf(buf, sizeof(buf)-1, "%s", g_device_name);
714                                 }
715                         }
716                 }
717         }
718         return strdup(buf);
719 }
720
721 static Evas_Object * _get_emtpy_layout(Evas_Object * parent)
722 {
723         if( parent == NULL )
724                 return NULL;
725
726         Evas_Object * layout = elm_layout_add(parent);
727         elm_layout_file_set(layout, EDJE_PATH, "setting-empty/swallow");
728
729         return layout;
730 }
731
732 static Evas_Object * _gl_bt_check_get(void *data, Evas_Object *obj, const char *part)
733 {
734         Evas_Object *layout = NULL;
735         Evas_Object *check = NULL;
736         //appdata *ad = data;
737
738         Bt_Item_Data *id = data;
739         int index = id->index;
740
741         if( !strcmp(part, "elm.icon") )
742         {
743                 layout = _get_emtpy_layout(obj);
744
745                 check = elm_check_add(obj);
746                 elm_check_state_set(check, (bt_menu_its[index].is_enable == TRUE) ? EINA_TRUE : EINA_FALSE);
747                 evas_object_size_hint_align_set(check, EVAS_HINT_FILL, EVAS_HINT_FILL);
748                 evas_object_size_hint_weight_set(check, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
749
750                 if( index == BT_MENU_TYPE_VISIBLE_ON_OFF )
751                 {
752                         if( is_disable_visibility_item_view() )
753                         {
754                                 edje_object_signal_emit(elm_layout_edje_get(check), "elm,state,disabled", "elm");
755                         }
756                 }
757                 else
758                 {
759                         g_bt_check = check;
760                 }
761
762                 elm_object_part_content_set(layout, "empty.swallow", check);
763         }
764         return layout;
765 }
766
767 static void _bt_gl_del(void *data, Evas_Object *obj)
768 {
769         Bt_Item_Data *id = data;
770         if (id)
771                 free(id);
772 }
773
774 static void init_values()
775 {
776         if( !vb_timer )
777         {
778                 bt_menu_its[BT_MENU_TYPE_VISIBLE_ON_OFF].is_enable = DISABLE;
779         }
780 }
781
782 static int is_handsfree_connected()
783 {
784         int ret = FALSE;
785         int headset_connected = FALSE;
786         int sap_connected = FALSE;
787
788         vconf_get_int(VCONFKEY_BT_DEVICE, &headset_connected);
789         DBG("Heaadset connected : %x", headset_connected);
790
791         if ( headset_connected & VCONFKEY_BT_DEVICE_AG_CONNECTED )
792         {
793                 DBG("Setting - Heaadset connected");
794                 return TRUE;
795         }
796
797         vconf_get_int("memory/private/sap/conn_status", &sap_connected);
798         DBG("Sap connected : %d", sap_connected);
799
800         if (sap_connected) {
801                 DBG("Setting - Sap connected");
802                 return TRUE;
803         }
804
805         return ret;
806 }
807
808 static int is_add_BT_headset()
809 {
810         // temporary code!!
811         // this code will be changed!!
812         return TRUE;
813 }
814
815 static int is_BT_enable()
816 {
817         int enable = DISABLE;
818
819         if (bt_adapter_get_state(&enable) != BT_ERROR_NONE)
820         {
821                 ERR("Setting - bt_adapter_get_state() is failed ");
822                 _init_bt_value();
823                 enable = DISABLE;
824         }
825
826         return enable;
827 }
828
829 Evas_Object* _create_bt_list(void* data)
830 {
831         appdata *ad = data;
832         if( ad == NULL )
833         {
834                 DBG("%s", "_create_bluetooth_list - appdata is null");
835                 return NULL;
836         }
837
838         Evas_Object *genlist  = NULL;
839         struct _bt_menu_item *menu_its = NULL;
840         Elm_Genlist_Item_Class *temp_itc = NULL;
841         int idx = 0;
842
843         init_values();
844
845         // get BT enable Vconf
846         //vconf_get_int(VCONFKEY_BT_STATUS, &bt_menu_its[BT_MENU_TYPE_BT_ON_OFF].is_enable);
847
848         bt_menu_its[BT_MENU_TYPE_BT_ON_OFF].is_enable = is_BT_enable();
849
850         DBG("Setting - BT status is %d", bt_menu_its[BT_MENU_TYPE_BT_ON_OFF].is_enable);
851
852         if( bt_menu_its[BT_MENU_TYPE_BT_ON_OFF].is_enable > 1 )
853         {
854                 bt_menu_its[BT_MENU_TYPE_BT_ON_OFF].is_enable = ENABLE;
855         }
856
857         if( bt_menu_its[BT_MENU_TYPE_BT_ON_OFF].is_enable )
858         {
859                 is_connected_hf = is_handsfree_connected();
860         }
861         else
862         {
863                 is_connected_hf = FALSE;
864         }
865
866         Elm_Genlist_Item_Class *itc = elm_genlist_item_class_new();
867         itc->item_style = "2text.1icon.1";
868         itc->func.text_get = _gl_bt_title_get;
869         itc->func.content_get = _gl_bt_check_get;
870         itc->func.del = _bt_gl_del;
871
872         Elm_Genlist_Item_Class *itc2 = elm_genlist_item_class_new();
873         itc2->item_style = "1text";
874         itc2->func.text_get = _gl_bt_title_get;
875         itc2->func.del = _bt_gl_del;
876
877         Evas_Object * layout = elm_layout_add(ad->nf);
878         elm_layout_file_set(layout, EDJE_PATH, "setting/genlist/layout");
879         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
880
881         genlist = elm_genlist_add(layout);
882         elm_genlist_block_count_set(genlist, 14);
883         elm_genlist_mode_set(genlist, ELM_LIST_COMPRESS);
884
885         menu_its = bt_menu_its;
886
887         for (idx = 0; idx < BT_LIST_ITEM_COUNT; idx++)
888         {
889                 // if bt headset is disable, continue
890                 if( idx == BT_MENU_TYPE_BT_HEADSET && !is_add_BT_headset() )
891                         continue;
892
893                 temp_itc = (idx == BT_MENU_TYPE_BT_HEADSET) ? itc2 : itc;
894
895                 Bt_Item_Data *id = calloc(sizeof(Bt_Item_Data), 1);
896                 id->index = idx;
897                 id->item = elm_genlist_item_append(
898                                                         genlist,                                // genlist object
899                                                         temp_itc,                               // item class
900                                                         id,                             // data
901                                                         NULL,
902                                                         ELM_GENLIST_ITEM_NONE,
903                                                         menu_its[ idx ].func,   // call back
904                                                         ad);
905
906                 if(idx == BT_MENU_TYPE_BT_ON_OFF)
907                 {
908                         bt_it = id->item;
909                 }
910                 else if(idx == BT_MENU_TYPE_BT_HEADSET)
911                 {
912                         hf_it = id->item;
913                 }
914                 else
915                 {
916                         vb_it = id->item;
917                 }
918         }
919         elm_genlist_item_class_free(itc);
920         elm_genlist_item_class_free(itc2);
921
922         bt_genlist = genlist;
923
924         elm_object_part_content_set(layout, "elm.genlist", genlist);
925
926         return layout;
927 }