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