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