Tizen directory path migration
[apps/native/ug-wifi-direct.git] / popup-wifidirect / src / wfd-app-util.c
1 /*
2 *  WiFi-Direct UG
3 *
4 * Copyright 2012  Samsung Electronics Co., Ltd
5
6 * Licensed under the Flora License, Version 1.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9
10 * http://www.tizenopensource.org/license
11
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20 /**
21  * This file implements wifi direct application utils functions.
22  *
23  * @file    wfd-app-util.c
24  * @author  Sungsik Jang (sungsik.jang@samsung.com)
25  * @version 0.1
26  */
27
28 #include <linux/unistd.h>
29 #include <stdio.h>
30 #include <string.h>
31
32 #include <Elementary.h>
33 #include <vconf.h>
34 #include <app_control_internal.h>
35 #include <notification.h>
36 #include <notification_internal.h>
37 #include <notification_text_domain.h>
38 #include <bundle_internal.h>
39 #include <app_control.h>
40 #include <app.h>
41
42 #include <wifi-direct.h>
43
44 #include "wfd-app.h"
45 #include "wfd-app-util.h"
46
47 #define KEY_SELECT "XF86Home"
48
49 char *wfd_app_trim_path(const char *filewithpath)
50 {
51 #if 0
52         char *filename = NULL;
53         if ((filename = strrchr(filewithpath, '/')) == NULL)
54             return (char *) filewithpath;
55         else
56             return (filename + 1);
57 #else
58         static char *filename[100];
59         char *strptr = NULL;
60         int start = 0;
61         const char *space = "                                        ";
62         int len = strlen(filewithpath);
63
64         if (len > 20) {
65                 strptr = (char *) filewithpath + (len - 20);
66                 start = 0;
67         } else if (len < 20) {
68                 strptr = (char *) filewithpath;
69                 start = 20 - len;
70         }
71
72         strncpy((char *) filename, space, strlen(space));
73         strncpy((char *) filename + start, strptr, 50);
74
75         return (char *) filename;
76 #endif
77 }
78
79
80 int wfd_app_gettid()
81 {
82 #ifdef __NR_gettid
83         return syscall(__NR_gettid);
84 #else
85         fprintf(stderr, "__NR_gettid is not defined, please include linux/unistd.h ");
86         return -1;
87 #endif
88 }
89
90 static void __launch_app_result_cb(app_control_h request, app_control_h reply, app_control_result_e result, void *data)
91 {
92         __WFD_APP_FUNC_ENTER__;
93
94         if(result == APP_CONTROL_RESULT_FAILED) {
95                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "User cancel to reconnect screen mirroring\n");
96 #ifdef WFD_SCREEN_MIRRORING_ENABLED
97                 ad->screen_mirroring_state = WFD_POP_SCREEN_MIRROR_NONE;
98 #endif
99         } else {
100                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "app_control launch result: [%d]\n", result);
101         }
102
103         __WFD_APP_FUNC_EXIT__;
104 }
105
106 static void _move_data_to_app_control(const char *key, const int type,
107                 const bundle_keyval_t *kv, void *data)
108 {
109         __WFD_APP_FUNC_ENTER__;
110         void *ptr = NULL;
111         char *buff = NULL;
112         unsigned int size = 0;
113
114
115         WFD_RET_IF(data == NULL || key == NULL || type == 0, , "Invialid parameter!");
116
117         app_control_h control = data;
118
119         if (type == BUNDLE_TYPE_STR)
120         {
121                 bundle_keyval_get_basic_val((bundle_keyval_t *) kv, &ptr, &size);
122                 buff = malloc(sizeof(char)* size + 1);
123                 snprintf(buff, size + 1, "%s", ((char*) ptr));
124                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Found STR -KEY: %s, VAL: %s, SIZE: %d", key, buff, size);
125
126                 app_control_add_extra_data(control, key, buff);
127                 free(buff);
128         } else if (type == BUNDLE_TYPE_BYTE) {
129                 bundle_keyval_get_basic_val((bundle_keyval_t *) kv, &ptr, &size);
130                 buff = malloc(sizeof(char)* size + 1);
131                 snprintf(buff, size + 1, "%s", ((char*) ptr));
132                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Found STR -KEY: %s, VAL: %s, SIZE: %d", key, buff, size);
133
134                 app_control_add_extra_data(control, key, buff);
135                 free(buff);
136         } else if (type == BUNDLE_TYPE_STR_ARRAY) {
137                 int i = 0;
138                 void ** array;
139                 unsigned int len = 0;
140                 size_t *element_size = NULL;
141                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Found STR_ARRAY -KEY: %s", key);
142                 bundle_keyval_get_array_val((bundle_keyval_t *) kv, &array, &len, &element_size);
143                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "-Array len: %d", len);
144                 for (i = 0; i < len; i++) {
145                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "-%s", (char*)array[i]);
146
147                         app_control_add_extra_data(control, key, (char*)array[i]);
148                 }
149         }
150
151         __WFD_APP_FUNC_EXIT__;
152 }
153
154 static void _launch_app(char *app_id, void *data)
155 {
156         __WFD_APP_FUNC_ENTER__;
157         WFD_RET_IF(app_id == NULL || data == NULL, "Invialid parameter!");
158
159         int ret = APP_CONTROL_ERROR_NONE;
160         app_control_h control = NULL;
161         ret = app_control_create(&control);
162         if (ret != APP_CONTROL_ERROR_NONE) {
163                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "app_control_create() return error : %d", ret);
164                 return;
165         }
166         WFD_RET_IF(control == NULL, "Failed to create app_control handle!");
167
168         app_control_set_operation(control, APP_CONTROL_OPERATION_DEFAULT);
169         app_control_set_app_id(control, app_id);
170         bundle_foreach((bundle *)data, _move_data_to_app_control, control);
171
172         char *launch_type = NULL;
173         bundle_get_str(data, "-t", &launch_type);
174         if (!strcmp(launch_type, "reconnect_by_connecting_wifi_ap")) {
175                 ret = app_control_send_launch_request(control, __launch_app_result_cb, NULL);
176         } else {
177                 ret = app_control_send_launch_request(control, NULL, NULL);
178         }
179
180         if (ret != APP_CONTROL_ERROR_NONE) {
181                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "app_control_send_launch_request() is failed : %d", ret);
182                 app_control_destroy(control);
183                 return;
184         }
185         app_control_destroy(control);
186
187         __WFD_APP_FUNC_EXIT__;
188         return;
189 }
190
191 #ifdef WFD_SCREEN_MIRRORING_ENABLED
192 void _add_screen_mirroring_activated_indicator(void *user_data)
193 {
194         __WFD_APP_FUNC_ENTER__;
195         wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
196         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
197         WFD_RET_IF(ad->noti_screen_mirroring_on, "Indicator noti_screen_mirroring_on already exists");
198
199         if (ad->noti_screen_mirroring_play) {
200                 notification_delete(ad->noti_screen_mirroring_play);
201                 notification_free(ad->noti_screen_mirroring_play);
202                 ad->noti_screen_mirroring_play = NULL;
203         }
204
205         if(ad->noti_screen_mirroring_on != NULL) {
206                 noti_err = notification_free(ad->noti_screen_mirroring_on);
207                 WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_free. [%d]", noti_err);
208         }
209
210         ad->noti_screen_mirroring_on = notification_create(NOTIFICATION_TYPE_ONGOING);
211         WFD_RET_IF(NULL == ad->noti_screen_mirroring_on, "NULL parameters.\n");
212
213         noti_err = notification_set_image(ad->noti_screen_mirroring_on, NOTIFICATION_IMAGE_TYPE_ICON_FOR_INDICATOR, SCREEN_MIRRIONG_INDICATOR_ICON_PATH);
214         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_image. [%d]", noti_err);
215
216         noti_err = notification_set_property(ad->noti_screen_mirroring_on, NOTIFICATION_PROP_DISABLE_TICKERNOTI);
217         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Unable to notification_set_property. [%d]", noti_err);
218
219         noti_err = notification_set_display_applist(ad->noti_screen_mirroring_on, NOTIFICATION_DISPLAY_APP_INDICATOR);
220         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Unable to notification_set_display_applist. [%d]", noti_err);
221
222         /* notify the quick panel */
223         noti_err = notification_insert(ad->noti_screen_mirroring_on, NULL);
224         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_insert. [%d]", noti_err);
225
226         __WFD_APP_FUNC_EXIT__;
227         return;
228 }
229 #endif
230
231
232 #ifdef WFD_SCREEN_MIRRORING_ENABLED
233 /**
234  *      This function let the app add the notification when it is connected
235  *      @return   void
236  *      @param[in] user_data the pointer to the main data structure
237  */
238 void _add_wfd_peers_connected_notification(void *user_data, char* package_name)
239 {
240         __WFD_APP_FUNC_ENTER__;
241
242         int res = NOTIFICATION_ERROR_NONE;
243         wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
244         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
245
246         WFD_RET_IF(NULL == ad || NULL == package_name, "NULL parameters.\n");
247
248         if (ad->noti_screen_mirroring_on) {
249                 notification_delete(ad->noti_screen_mirroring_on);
250                 notification_free(ad->noti_screen_mirroring_on);
251                 ad->noti_screen_mirroring_on = NULL;
252         }
253
254         if(ad->noti_screen_mirroring_play != NULL) {
255                 noti_err = notification_free(ad->noti_screen_mirroring_play);
256                 WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_free. [%d]", noti_err);
257         }
258
259         ad->noti_screen_mirroring_play = notification_create(NOTIFICATION_TYPE_ONGOING);
260         WFD_RET_IF(NULL == ad->noti_screen_mirroring_play, "NULL parameters.\n");
261
262         char msg[WFD_MAX_SIZE] = {0};
263
264         bundle *b = NULL;
265         app_control_h control;
266         res = app_control_create(&control);
267         WFD_RET_IF(res != APP_CONTROL_ERROR_NONE, "app_control_create() return error : %d", res);
268
269         app_control_set_package(control, package_name);
270         app_control_add_extra_data(control, "-t", "notification");
271
272         res = notification_set_launch_option(ad->noti_screen_mirroring_play, NOTIFICATION_LAUNCH_OPTION_APP_CONTROL, control);
273         if (res != NOTIFICATION_ERROR_NONE) {
274                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to otification_set_launch_option. [%d]", res);
275                 app_control_destroy(control);
276                 return;
277         }
278
279         app_control_destroy(control);
280
281         noti_err = notification_set_layout(ad->noti_screen_mirroring_play, NOTIFICATION_LY_ONGOING_EVENT);
282         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_layout. [%d]", noti_err);
283
284         /* set the icon */
285         noti_err = notification_set_image(ad->noti_screen_mirroring_play, NOTIFICATION_IMAGE_TYPE_ICON, SCREEN_MIRRIONG_NOTI_ICON_PATH);
286         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_image. [%d]", noti_err);
287
288         noti_err = notification_set_image(ad->noti_screen_mirroring_play, NOTIFICATION_IMAGE_TYPE_ICON_FOR_INDICATOR, SCREEN_MIRRIONG_INDICATOR_PLAY_ICON_PATH);
289         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_image. [%d]", noti_err);
290
291         /* set the title and content */
292         wfd_app_get_connected_peers(ad);
293         noti_err = notification_set_text(ad->noti_screen_mirroring_play, NOTIFICATION_TEXT_TYPE_TITLE, D_("IDS_SMR_BODY_SCREEN_MIRRORING_IS_ENABLED"),
294                         NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
295         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_text. [%d]", noti_err);
296
297         snprintf(msg, WFD_MAX_SIZE, D_("IDS_WIFI_BODY_CONNECTED_TO_PS"), ad->raw_connected_peers[0].ssid);
298         noti_err = notification_set_text(ad->noti_screen_mirroring_play, NOTIFICATION_TEXT_TYPE_CONTENT,
299                         msg, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
300         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_text. [%d]", noti_err);
301
302
303         notification_set_property(ad->noti_screen_mirroring_play, NOTIFICATION_PROP_DISABLE_TICKERNOTI);
304
305         /* notify the quick panel */
306         noti_err = notification_insert(ad->noti_screen_mirroring_play, NULL);
307         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_insert. [%d]", noti_err);
308
309         __WFD_APP_FUNC_EXIT__;
310         return;
311 }
312 #endif
313
314 /**
315  *      This function let the app make a change callback for flight mode
316  *      @return   void
317  *      @param[in] key the pointer to the key
318  *      @param[in] user_data the pointer to the main data structure
319  */
320 static void _wfd_flight_mode_changed(keynode_t *node, void *user_data)
321 {
322         __WFD_APP_FUNC_ENTER__;
323         int res = -1;
324         int flight_mode = 0;
325 #ifdef WFD_SCREEN_MIRRORING_ENABLED
326         int screen_mirroring_status = 0;
327 #endif
328         wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
329         WFD_RET_IF(NULL == ad, "NULL parameters.\n");
330
331         res = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &flight_mode);
332         if (res != 0) {
333                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get flight state from vconf. [%d]\n", res);
334                 return;
335         }
336
337         if (flight_mode == FALSE) {
338                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Flight mode is off\n");
339                 return;
340         }
341
342         /* If flight mode is on, turn off WFD */
343         wifi_direct_get_state(&ad->wfd_status);
344         if (WIFI_DIRECT_STATE_DEACTIVATED == ad->wfd_status) {
345                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Wi-Fi Direct is deactivated.\n");
346                 return;
347         }
348
349         /* If connected, disconnect all devices*/
350         if (WIFI_DIRECT_STATE_CONNECTED == ad->wfd_status) {
351                 res = wifi_direct_disconnect_all();
352                 if (res != WIFI_DIRECT_ERROR_NONE) {
353                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to send disconnection request to all. [%d]\n", res);
354                         return;
355                 }
356         }
357
358         WFD_APP_LOG(WFD_APP_LOG_HIGH, "Deactivating WiFi DIrect..."
359                 "Due to Flight Mode is Enabled\n");
360         res = wifi_direct_deactivate();
361         if (res != WIFI_DIRECT_ERROR_NONE) {
362                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to deactivate Wi-Fi Direct. error code = [%d]\n", res);
363                 return;
364         }
365
366 #ifdef WFD_SCREEN_MIRRORING_ENABLED
367         /* checking Screen Mirroring */
368         if (vconf_get_int(VCONFKEY_SCREEN_MIRRORING_STATE, &screen_mirroring_status) < 0)
369         {
370                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_SCREEN_MIRRORING_STATE\n");
371         }
372         WFD_APP_LOG(WFD_APP_LOG_LOW, "screen_mirroring_status: %d\n", screen_mirroring_status);
373
374         if(screen_mirroring_status > VCONFKEY_SCREEN_MIRRORING_DEACTIVATED) {
375                 res = vconf_set_int(VCONFKEY_SCREEN_MIRRORING_STATE, VCONFKEY_SCREEN_MIRRORING_DEACTIVATED);
376                 if (res < 0) {
377                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to set vconf VCONFKEY_SCREEN_MIRRORING_STATE\n");
378                 }
379         }
380 #endif
381
382         __WFD_APP_FUNC_EXIT__;
383 }
384
385 static void _wfd_cpu_limit_mode_changed(keynode_t *node, void *user_data)
386 {
387         __WFD_APP_FUNC_ENTER__;
388         wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
389         WFD_RET_IF(NULL == ad, "NULL parameters.\n");
390         int power_mode = 0;
391         int screen_mirroring_status = 0;
392         int cup_limit_mode = 0;
393
394         if (vconf_get_int(VCONFKEY_SCREEN_MIRRORING_STATE, &screen_mirroring_status) < 0) {
395                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_SCREEN_MIRRORING_STATE\n");
396                 return;
397         }
398
399         if (screen_mirroring_status != VCONFKEY_SCREEN_MIRRORING_CONNECTED) {
400                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Allshare cast is not connected\n");
401                 return;
402         }
403
404         if(vconf_get_bool(VCONFKEY_SETAPPL_PWRSV_CUSTMODE_CPU, &cup_limit_mode) < 0) {
405                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_SETAPPL_PWRSV_CUSTMODE_CPU\n");
406                 return;
407         }
408
409         if (vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &power_mode) < 0)
410         {
411                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_SETAPPL_PSMODE\n");
412                 return;
413         }
414
415         if ((power_mode == SETTING_PSMODE_POWERFUL || power_mode == SETTING_PSMODE_ADVISOR) &&
416                                 cup_limit_mode){
417                 bundle *b = NULL;
418                 b = bundle_create();
419                 bundle_add_str(b, "-t", "notification_power_saving_on");
420                 _launch_app(PACKAGE_ALLSHARE_CAST, b);
421                 bundle_free(b);
422         }
423
424         __WFD_APP_FUNC_EXIT__;
425         return;
426 }
427
428 static void _wfd_power_saving_mode_changed(keynode_t *node, void *user_data)
429 {
430         __WFD_APP_FUNC_ENTER__;
431         wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
432         WFD_RET_IF(NULL == ad, "NULL parameters.\n");
433         int power_mode = 0;
434         int screen_mirroring_status = 0;
435         int cup_limit_mode = 0;
436
437         if (vconf_get_int(VCONFKEY_SCREEN_MIRRORING_STATE, &screen_mirroring_status) < 0) {
438                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_SCREEN_MIRRORING_STATE\n");
439                 return;
440         }
441
442         if (screen_mirroring_status != VCONFKEY_SCREEN_MIRRORING_CONNECTED) {
443                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Allshare cast is not connected\n");
444                 return;
445         }
446
447         if (vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &power_mode) < 0)
448         {
449                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_SETAPPL_PSMODE\n");
450                 return;
451         }
452
453         if (power_mode == SETTING_PSMODE_SURVIVAL ||
454                         power_mode == SETTING_PSMODE_EMERGENCY) {
455                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Ultra power saving mode on\n");
456                 bundle *b = NULL;
457                 b = bundle_create();
458                 bundle_add_str(b, "-t", "quit_by_ultra_power_saving_on");
459
460                 _launch_app(PACKAGE_ALLSHARE_CAST, b);
461                 bundle_free(b);
462         } else if (power_mode == SETTING_PSMODE_POWERFUL ||
463                         power_mode == SETTING_PSMODE_ADVISOR){
464                         if(vconf_get_bool(VCONFKEY_SETAPPL_PWRSV_CUSTMODE_CPU, &cup_limit_mode) < 0) {
465                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_SETAPPL_PWRSV_CUSTMODE_CPU\n");
466                                 return;
467                         }
468
469                         if (cup_limit_mode) {
470                                 bundle *b = NULL;
471                                 b = bundle_create();
472                                 bundle_add_str(b, "-t", "notification_power_saving_on");
473                                 _launch_app(PACKAGE_ALLSHARE_CAST, b);
474                                 bundle_free(b);
475                         }
476         }
477
478         __WFD_APP_FUNC_EXIT__;
479         return;
480 }
481
482 static void _wfd_wifi_status_changed(keynode_t *node, void *user_data)
483 {
484         __WFD_APP_FUNC_ENTER__;
485         wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
486         WFD_RET_IF(NULL == ad, "NULL parameters.\n");
487         int wifi_status = 0;
488 #ifdef WFD_SCREEN_MIRRORING_ENABLED
489         int screen_mirroring_status = 0;
490
491         if (vconf_get_int(VCONFKEY_SCREEN_MIRRORING_STATE, &screen_mirroring_status) < 0) {
492                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_SCREEN_MIRRORING_STATE\n");
493                 return;
494         }
495
496         if (screen_mirroring_status != VCONFKEY_SCREEN_MIRRORING_CONNECTED) {
497                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Allshare cast is not connected\n");
498                 return;
499         }
500 #endif
501
502         if (vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_status) < 0)
503         {
504                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_WIFI_STATE\n");
505                 return;
506         }
507
508         WFD_APP_LOG(WFD_APP_LOG_LOW, "Wi-Fi state is [%d]\n", wifi_status);
509         if (wifi_status == VCONFKEY_WIFI_CONNECTED) {
510                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Wi-Fi is connected\n");
511 #ifdef WFD_SCREEN_MIRRORING_ENABLED
512                 ad->screen_mirroring_state = WFD_POP_SCREEN_MIRROR_DISCONNECT_BY_RECONNECT_WIFI_AP;
513                 bundle *b = NULL;
514                 b = bundle_create();
515                 bundle_add_str(b, "-t", "reconnect_by_connecting_wifi_ap");
516
517                 _launch_app(PACKAGE_ALLSHARE_CAST, b);
518                 bundle_free(b);
519 #endif
520         } else if (VCONFKEY_WIFI_OFF == wifi_status) {
521                 /* Deactivate WiFi Direct */
522                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Deactivate WiFi Direct...");
523                 wfd_destroy_popup();
524                 /*
525                  * Currently, WiFi Direct OFF is handled at net-config.
526                  * Also, this patch is added to support ON-DEMAND launch destroy popup.
527                  * This patch will handle 5sec deadlock of popup destory from
528                  * wfd-manager.
529                  */
530                 /* wfd_app_client_switch_off(ad); */
531         } else {
532                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Wi-Fi state is [%d]\n", wifi_status);
533         }
534
535         __WFD_APP_FUNC_EXIT__;
536         return;
537 }
538
539 #ifdef WFD_SCREEN_MIRRORING_ENABLED
540 /**
541  *      This function let the app make a change callback for allshare cast
542  *      @return   void
543  *      @param[in] key the pointer to the key
544  *      @param[in] user_data the pointer to the main data structure
545  */
546 static void _wfd_allshare_cast_status_changed(keynode_t *node, void *user_data)
547 {
548         __WFD_APP_FUNC_ENTER__;
549
550         int screen_mirroring_status = 0;
551         wfd_appdata_t *ad = (wfd_appdata_t *)user_data;
552         WFD_RET_IF(NULL == ad, "NULL parameters.\n");
553
554         if (vconf_get_int(VCONFKEY_SCREEN_MIRRORING_STATE, &screen_mirroring_status) < 0)
555         {
556                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_SCREEN_MIRRORING_STATE\n");
557         }
558
559         WFD_APP_LOG(WFD_APP_LOG_ERROR, "VCONFKEY_SCREEN_MIRRORING_STATE:%d\n", screen_mirroring_status);
560
561         if (screen_mirroring_status == VCONFKEY_SCREEN_MIRRORING_CONNECTED) {
562                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Allshare cast is connected\n");
563                 if (ad->transmit_timer) {
564                         ecore_timer_del(ad->transmit_timer);
565                         ad->transmit_timer = NULL;
566                 }
567                 /* add connected notification */
568                 _add_wfd_peers_connected_notification(ad, PACKAGE_ALLSHARE_CAST);
569         } else if (screen_mirroring_status == VCONFKEY_SCREEN_MIRRORING_ACTIVATED) {
570                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Allshare cast is ACTIVATED\n");
571                 _add_screen_mirroring_activated_indicator(ad);
572         } else {
573                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Allshare cast is not connected\n");
574                 if (ad->noti_screen_mirroring_on) {
575                         notification_delete(ad->noti_screen_mirroring_on);
576                         notification_free(ad->noti_screen_mirroring_on);
577                         ad->noti_screen_mirroring_on = NULL;
578                 }
579
580                 if (ad->noti_screen_mirroring_play) {
581                         notification_delete(ad->noti_screen_mirroring_play);
582                         notification_free(ad->noti_screen_mirroring_play);
583                         ad->noti_screen_mirroring_play = NULL;
584                 }
585         }
586
587         return;
588
589         __WFD_APP_FUNC_EXIT__;
590 }
591 #endif
592
593 static Eina_Bool _wfd_hard_key_down_cb(void *data, int type, void *event)
594 {
595         wfd_appdata_t *ad = (wfd_appdata_t *)data;
596         Ecore_Event_Key *ev = (Ecore_Event_Key *)event;
597         int res = 0;
598
599         WFD_APP_LOG(WFD_APP_LOG_HIGH, "Hard Key Pressed CB...");
600         if (NULL == ad || NULL == ev) {
601                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Invalid AppData parameter");
602                 return EINA_FALSE;
603         }
604
605         if (!strcmp(ev->keyname, "XF86Home")) {
606                 WFD_APP_LOG(WFD_APP_LOG_HIGH, "[KEY]KEY_SELECT pressed");
607                 WFD_APP_LOG(WFD_APP_LOG_HIGH, "Mac : %s", ad->mac_addr_connecting);
608
609                 if (strnlen(ad->mac_addr_connecting, MACSTR_LENGTH) > 0) {
610                         res = wifi_direct_reject_connection(ad->mac_addr_connecting);
611                         if (res != WIFI_DIRECT_ERROR_NONE) {
612                                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to reject connection(%d)", res);
613                         }
614                 }
615                 memset(ad->mac_addr_connecting, 0x00, MACSTR_LENGTH);
616                 wfd_destroy_popup();
617         } else {
618                 WFD_APP_LOG(WFD_APP_LOG_HIGH, "[KEY][%s] pressed not Handled",
619                         ev->keyname);
620         }
621
622         return EINA_FALSE;
623 }
624
625 int wfd_app_util_register_hard_key_down_cb(void *data)
626 {
627         wfd_appdata_t *ad = (wfd_appdata_t *)data;
628
629         if (NULL == ad) {
630                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Invalid AppData parameter");
631                 return -1;
632         }
633
634         WFD_APP_LOG(WFD_APP_LOG_HIGH, "Register hard key down press CB !!!");
635         if (NULL == ad->downkey_handler)
636                 ad->downkey_handler = ecore_event_handler_add(ECORE_EVENT_KEY_DOWN,
637                 _wfd_hard_key_down_cb, ad);
638
639         return 0;
640 }
641
642 int wfd_app_util_deregister_hard_key_down_cb(void *data)
643 {
644         wfd_appdata_t *ad = (wfd_appdata_t *)data;
645
646         if (NULL == ad) {
647                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Invalid AppData parameter");
648                 return -1;
649         }
650         WFD_APP_LOG(WFD_APP_LOG_HIGH, "Deregister hard key down press CB !!!");
651         if (NULL != ad->downkey_handler) {
652                 ecore_event_handler_del(ad->downkey_handler);
653                 ad->downkey_handler = NULL;
654         }
655         return 0;
656 }
657
658 int wfd_app_util_register_vconf_callbacks(void *data)
659 {
660         wfd_appdata_t *ad = NULL;
661         int ret = 0;
662
663         if (!data) {
664                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Invalid parameter");
665                 return -1;
666         }
667
668         ad = data;
669
670         /* register flight mode */
671         ret = vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
672                                                         _wfd_flight_mode_changed, ad);
673         if (ret < 0) {
674                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to register vconf callback for flight mode\n");
675                 return -1;
676         }
677
678 #ifdef WFD_SCREEN_MIRRORING_ENABLED
679         /* allshare cast */
680         /* TODO: Make proper changes for vconfkey */
681         ret = vconf_notify_key_changed(VCONFKEY_SCREEN_MIRRORING_STATE,
682                                                         _wfd_allshare_cast_status_changed, ad);
683         if (ret < 0) {
684                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to register vconf callback for allshare cast\n");
685                 return -1;
686         }
687 #endif
688
689         /* wifi */
690         ret = vconf_notify_key_changed(VCONFKEY_WIFI_STATE, _wfd_wifi_status_changed, ad);
691         if (ret < 0) {
692                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to register vconf callback for wifi\n");
693                 return -1;
694         }
695
696         /* power mode */
697         ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE, _wfd_power_saving_mode_changed, ad);
698         if (ret < 0) {
699                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to register vconf callback for power mode\n");
700                 return -1;
701         }
702
703         /* cpu limit mode */
704         ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_PWRSV_CUSTMODE_CPU, _wfd_cpu_limit_mode_changed, ad);
705         if (ret < 0) {
706                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to register vconf callback for cpu limit mode\n");
707                 return -1;
708         }
709
710         return 0;
711 }
712
713 int wfd_app_util_deregister_vconf_callbacks(void *data)
714 {
715         int ret = 0;
716
717         /* remove callback for flight mode */
718         ret = vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE, _wfd_flight_mode_changed);
719         if (ret == -1) {
720                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to ignore vconf key callback for flight mode\n");
721         }
722
723 #ifdef WFD_SCREEN_MIRRORING_ENABLED
724         /* remove callback for allshare cast */
725         ret = vconf_ignore_key_changed(VCONFKEY_SCREEN_MIRRORING_STATE, _wfd_allshare_cast_status_changed);
726         if (ret == -1) {
727                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to ignore vconf key callback for allshare cast\n");
728         }
729 #endif
730
731         /* remove callback for wifi */
732         ret = vconf_ignore_key_changed(VCONFKEY_WIFI_STATE, _wfd_wifi_status_changed);
733         if (ret == -1) {
734                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to ignore vconf key callback for wifi\n");
735         }
736
737         /* remove callback for power mode */
738         ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE, _wfd_power_saving_mode_changed);
739         if (ret == -1) {
740                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to ignore vconf key callback for power mode\n");
741         }
742
743         /* remove callback for cpu limit mode */
744         ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_PWRSV_CUSTMODE_CPU, _wfd_cpu_limit_mode_changed);
745         if (ret == -1) {
746                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to ignore vconf key callback for cpu limit mode\n");
747         }
748
749         return 0;
750 }
751
752 /**
753  *      This function let the app delete the notification
754  *      @return   void
755  */
756 void wfd_app_util_del_notification(wfd_appdata_t *ad)
757 {
758         __WFD_APP_FUNC_ENTER__;
759         WFD_RET_IF(NULL == ad, "NULL parameters.\n");
760
761         /* delete the notification */
762         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
763
764         noti_err  = notification_delete_all(NOTIFICATION_TYPE_ONGOING);
765         if (noti_err != NOTIFICATION_ERROR_NONE) {
766                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to notification_delete_all.(%d)\n", noti_err);
767                 return;
768         }
769
770 #ifdef WFD_SCREEN_MIRRORING_ENABLED
771         if (ad->noti_screen_mirroring_on) {
772                 noti_err = notification_free(ad->noti_screen_mirroring_on);
773                 ad->noti_screen_mirroring_on = NULL;
774                 if (noti_err != NOTIFICATION_ERROR_NONE) {
775                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to notification_free.(%d)\n", noti_err);
776                 }
777         }
778
779         if (ad->noti_screen_mirroring_play) {
780                 noti_err = notification_free(ad->noti_screen_mirroring_play);
781                 ad->noti_screen_mirroring_play = NULL;
782                 if (noti_err != NOTIFICATION_ERROR_NONE) {
783                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to notification_free.(%d)\n", noti_err);
784                 }
785         }
786 #endif
787
788 #ifdef NOT_CONNECTED_INDICATOR_ICON
789         if (ad->noti_wifi_direct_on) {
790                 noti_err = notification_free(ad->noti_wifi_direct_on);
791                 ad->noti_wifi_direct_on = NULL;
792                 if (noti_err != NOTIFICATION_ERROR_NONE) {
793                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to notification_free.(%d)\n", noti_err);
794                 }
795         }
796 #endif
797
798         if (ad->noti_wifi_direct_connected) {
799                 noti_err = notification_free(ad->noti_wifi_direct_connected);
800                 ad->noti_wifi_direct_connected = NULL;
801                 if (noti_err != NOTIFICATION_ERROR_NONE) {
802                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to notification_free.(%d)\n", noti_err);
803                 }
804         }
805
806         __WFD_APP_FUNC_EXIT__;
807         return;
808 }
809
810 #ifdef NOT_CONNECTED_INDICATOR_ICON
811 /**
812  *      This function let the app add the indicator icon when wfd is activated
813  *      @return   void
814  *      @param[in] user_data the pointer to the main data structure
815  */
816 void wfd_app_util_add_indicator_icon(void *user_data)
817 {
818         __WFD_APP_FUNC_ENTER__;
819         wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
820         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
821         WFD_RET_IF(ad->noti_wifi_direct_on, "Indicator already exists");
822
823         if(ad->noti_wifi_direct_on != NULL) {
824                 noti_err = notification_free(ad->noti_wifi_direct_on);
825                 WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_free. [%d]", noti_err);
826         }
827
828         ad->noti_wifi_direct_on = notification_create(NOTIFICATION_TYPE_ONGOING);
829         WFD_RET_IF(NULL == ad->noti_wifi_direct_on, "NULL parameters.\n");
830
831         noti_err = notification_set_image(ad->noti_wifi_direct_on, NOTIFICATION_IMAGE_TYPE_ICON_FOR_INDICATOR, WFD_ACTIVATED_NOTI_ICON_PATH);
832         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_image. [%d]", noti_err);
833
834         noti_err = notification_set_property(ad->noti_wifi_direct_on, NOTIFICATION_PROP_DISABLE_TICKERNOTI);
835         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Unable to notification_set_property. [%d]", noti_err);
836
837         noti_err = notification_set_display_applist(ad->noti_wifi_direct_on, NOTIFICATION_DISPLAY_APP_INDICATOR);
838         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Unable to notification_set_display_applist. [%d]", noti_err);
839
840         /* notify the quick panel */
841         noti_err = notification_insert(ad->noti_wifi_direct_on, NULL);
842         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_insert. [%d]", noti_err);
843
844         __WFD_APP_FUNC_EXIT__;
845         return;
846 }
847 #endif
848
849 #ifdef WFD_SCREEN_MIRRORING_ENABLED
850 /**
851  *      This function let the app set VCONFKEY_SCREEN_MIRRORING_STATE to be DEACTIVATED
852 */
853 void wfd_app_util_set_screen_mirroring_deactivated(wfd_appdata_t *ad)
854 {
855         __WFD_APP_FUNC_ENTER__;
856         WFD_RET_IF(NULL == ad, "NULL == ad!\n");
857         int screen_mirroring_status = -1;
858         int result = -1;
859
860         /* Reconnect by ap connected, no need to set vconf to DEACTIVATED, allshare cast itself will set ACTIVATED*/
861         if (ad->screen_mirroring_state == WFD_POP_SCREEN_MIRROR_DISCONNECT_BY_RECONNECT_WIFI_AP) {
862                 ad->screen_mirroring_state = WFD_POP_SCREEN_MIRROR_NONE;
863                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Reconnect screen mirroring by app connected.\n");
864                 return;
865         }
866
867         if (vconf_get_int(VCONFKEY_SCREEN_MIRRORING_STATE, &screen_mirroring_status) < 0) {
868                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_SCREEN_MIRRORING_STATE\n");
869         }
870
871         WFD_APP_LOG(WFD_APP_LOG_LOW, "screen_mirroring_status: %d\n", screen_mirroring_status);
872         /* Set the vconf value to DEACTIVATED only when the previous vconf value is CONNECTED.
873         If the previous vconf value is ACTIVATED, it means that the Screen Mirroring UG changed that key already. So no need to change it. */
874         if(screen_mirroring_status == VCONFKEY_SCREEN_MIRRORING_CONNECTED) {
875                 result = vconf_set_int(VCONFKEY_SCREEN_MIRRORING_STATE, VCONFKEY_SCREEN_MIRRORING_DEACTIVATED);
876                 if (result < 0) {
877                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to set vconf VCONFKEY_SCREEN_MIRRORING_STATE\n");
878                 }
879                 notification_status_message_post(D_("IDS_SMR_POP_SCREEN_MIRRORING_HAS_BEEN_DISABLED"));
880         }
881
882         __WFD_APP_FUNC_EXIT__;
883 }
884 #endif
885 /**
886  *      This function let the app add the notification when it shoule be turned off
887  *      @return   void
888  *      @param[in] user_data the pointer to the main data structure
889  */
890 void wfd_app_util_add_wfd_turn_off_notification(void *user_data)
891 {
892         __WFD_APP_FUNC_ENTER__;
893
894         int res = NOTIFICATION_ERROR_NONE;
895         wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
896         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
897
898         WFD_RET_IF(NULL == ad, "NULL parameters.\n");
899
900         /* delete all notifications */
901         wfd_app_util_del_notification(ad);
902
903         if(ad->noti_wifi_direct_connected!= NULL) {
904                 noti_err = notification_free(ad->noti_wifi_direct_connected);
905                 WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_free. [%d]", noti_err);
906         }
907
908         ad->noti_wifi_direct_connected = (notification_h) notification_create(NOTIFICATION_TYPE_ONGOING);
909         WFD_RET_IF(NULL == ad->noti_wifi_direct_connected, "NULL parameters.\n");
910
911         app_control_h control;
912         res = app_control_create(&control);
913         WFD_RET_IF(res != APP_CONTROL_ERROR_NONE, "app_control_create() return error : %d", res);
914
915         app_control_set_package(control, PACKAGE);
916         app_control_add_extra_data(control, NOTIFICATION_BUNDLE_PARAM, NOTIFICATION_BUNDLE_VALUE);
917
918         noti_err = notification_set_launch_option(ad->noti_wifi_direct_connected, NOTIFICATION_LAUNCH_OPTION_APP_CONTROL, control);
919         if (noti_err != NOTIFICATION_ERROR_NONE) {
920                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to notification_set_launch_option. [%d]", noti_err);
921                 app_control_destroy(control);
922                 return;
923         }
924
925         app_control_destroy(control);
926
927         noti_err = notification_set_layout(ad->noti_wifi_direct_connected, NOTIFICATION_LY_ONGOING_EVENT);
928         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_layout. [%d]", noti_err);
929
930         /* set the icon */
931         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Icon Path: %s\n", WFD_NOTI_ICON_PATH);
932         noti_err = notification_set_image(ad->noti_wifi_direct_connected, NOTIFICATION_IMAGE_TYPE_ICON, WFD_NOTI_ICON_PATH);
933         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_image. [%d]", noti_err);
934
935         noti_err = notification_set_display_applist(ad->noti_wifi_direct_connected, NOTIFICATION_DISPLAY_APP_TICKER | NOTIFICATION_DISPLAY_APP_NOTIFICATION_TRAY);
936         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Unable to notification_set_display_applist. [%d]", noti_err);
937
938         noti_err = notification_set_text_domain(ad->noti_wifi_direct_connected, LOCALE_FILE_NAME, LOCALEDIR);
939         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_text_domain. [%d]", noti_err);
940
941         /* set the title and content */
942         noti_err = notification_set_text(ad->noti_wifi_direct_connected, NOTIFICATION_TEXT_TYPE_TITLE,
943                 D_("IDS_WIFI_BODY_WI_FI_DIRECT_ABB"), "IDS_WIFI_BODY_WI_FI_DIRECT_ABB", NOTIFICATION_VARIABLE_TYPE_NONE);
944         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_text. [%d]", noti_err);
945
946         noti_err = notification_set_text(ad->noti_wifi_direct_connected, NOTIFICATION_TEXT_TYPE_CONTENT,
947                 D_("IDS_WIFI_BODY_DISABLE_WI_FI_DIRECT_AFTER_USE_ABB"),
948                 "IDS_WIFI_BODY_DISABLE_WI_FI_DIRECT_AFTER_USE_ABB", NOTIFICATION_VARIABLE_TYPE_NONE);
949         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_text. [%d]", noti_err);
950
951         /* notify the quick panel */
952         noti_err = notification_insert(ad->noti_wifi_direct_connected, NULL);
953         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_insert. [%d]", noti_err);
954
955         __WFD_APP_FUNC_EXIT__;
956         return;
957
958 }
959
960 void wfd_app_util_del_wfd_connected_notification(wfd_appdata_t *ad)
961 {
962         __WFD_APP_FUNC_ENTER__;
963         WFD_RET_IF(NULL == ad, "NULL parameters.\n");
964
965         /* delete the notification */
966         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
967
968         noti_err  = notification_delete(ad->noti_wifi_direct_connected);
969         if (noti_err != NOTIFICATION_ERROR_NONE) {
970                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to notification_delete.(%d)\n", noti_err);
971                 return;
972         }
973
974         if (ad->noti_wifi_direct_connected) {
975                 noti_err = notification_free(ad->noti_wifi_direct_connected);
976                 ad->noti_wifi_direct_connected = NULL;
977                 if (noti_err != NOTIFICATION_ERROR_NONE) {
978                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to notification_free.(%d)\n", noti_err);
979                 }
980         }
981
982         __WFD_APP_FUNC_EXIT__;
983         return;
984 }