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