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