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