tizen 2.3.1 release
[apps/home/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 <utilX.h>
34 #include <vconf.h>
35 #include <app_control.h>
36 #include <notification.h>
37 #include <notification_internal.h>
38
39 #include <wifi-direct.h>
40
41 #include "wfd-app.h"
42 #include "wfd-app-util.h"
43
44 char *wfd_app_trim_path(const char *filewithpath)
45 {
46 #if 0
47         char *filename = NULL;
48         if ((filename = strrchr(filewithpath, '/')) == NULL)
49             return (char *) filewithpath;
50         else
51             return (filename + 1);
52 #else
53         static char *filename[100];
54         char *strptr = NULL;
55         int start = 0;
56         const char *space = "                                        ";
57         int len = strlen(filewithpath);
58
59         if (len > 20) {
60                 strptr = (char *) filewithpath + (len - 20);
61                 start = 0;
62         } else if (len < 20) {
63                 strptr = (char *) filewithpath;
64                 start = 20 - len;
65         }
66
67         strncpy((char *) filename, space, strlen(space));
68         strncpy((char *) filename + start, strptr, 50);
69
70         return (char *) filename;
71 #endif
72 }
73
74
75 int wfd_app_gettid()
76 {
77 #ifdef __NR_gettid
78         return syscall(__NR_gettid);
79 #else
80         fprintf(stderr, "__NR_gettid is not defined, please include linux/unistd.h ");
81         return -1;
82 #endif
83 }
84
85 static void __launch_app_result_cb(app_control_h request, app_control_h reply, app_control_result_e result, void *data)
86 {
87         __WFD_APP_FUNC_ENTER__;
88
89         if(result == APP_CONTROL_RESULT_FAILED) {
90                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "User cancel to reconnect screen mirroring\n");
91 #ifdef WFD_SCREEN_MIRRORING_ENABLED
92                 ad->screen_mirroring_state = WFD_POP_SCREEN_MIRROR_NONE;
93 #endif
94         } else {
95                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "app_control launch result: [%d]\n", result);
96         }
97
98         __WFD_APP_FUNC_EXIT__;
99 }
100
101 static void _move_data_to_app_control(const char *key, const char *val, void *data)
102 {
103         __WFD_APP_FUNC_ENTER__;
104
105         WFD_RET_IF(data == NULL || key == NULL || val == NULL, , "Invialid parameter!");
106
107         app_control_h control = data;
108         app_control_add_extra_data(control, key, val);
109
110         __WFD_APP_FUNC_EXIT__;
111 }
112
113 static void _launch_app(char *app_id, void *data)
114 {
115         __WFD_APP_FUNC_ENTER__;
116         WFD_RET_IF(app_id == NULL || data == NULL, "Invialid parameter!");
117
118         int ret = APP_CONTROL_ERROR_NONE;
119         app_control_h control = NULL;
120         ret = app_control_create(&control);
121         if (ret != APP_CONTROL_ERROR_NONE) {
122                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "app_control_create() return error : %d", ret);
123                 return;
124         }
125         WFD_RET_IF(control == NULL, "Failed to create app_control handle!");
126
127         app_control_set_operation(control, APP_CONTROL_OPERATION_DEFAULT);
128         app_control_set_app_id(control, app_id);
129         bundle_iterate((bundle *)data, _move_data_to_app_control, control);
130
131         char *launch_type = (char*)bundle_get_val(data, "-t");
132         if (!strcmp(launch_type, "reconnect_by_connecting_wifi_ap")) {
133                 ret = app_control_send_launch_request(control, __launch_app_result_cb, NULL);
134         } else {
135                 ret = app_control_send_launch_request(control, NULL, NULL);
136         }
137
138         if (ret != APP_CONTROL_ERROR_NONE) {
139                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "app_control_send_launch_request() is failed : %d", ret);
140                 app_control_destroy(control);
141                 return;
142         }
143         app_control_destroy(control);
144
145         __WFD_APP_FUNC_EXIT__;
146         return;
147 }
148
149 #ifdef WFD_SCREEN_MIRRORING_ENABLED
150 void _add_screen_mirroring_activated_indicator(void *user_data)
151 {
152         __WFD_APP_FUNC_ENTER__;
153         wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
154         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
155         WFD_RET_IF(ad->noti_screen_mirroring_on, "Indicator noti_screen_mirroring_on already exists");
156
157         if (ad->noti_screen_mirroring_play) {
158                 notification_delete(ad->noti_screen_mirroring_play);
159                 notification_free(ad->noti_screen_mirroring_play);
160                 ad->noti_screen_mirroring_play = NULL;
161         }
162
163         if(ad->noti_screen_mirroring_on != NULL) {
164                 noti_err = notification_free(ad->noti_screen_mirroring_on);
165                 WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_free. [%d]", noti_err);
166         }
167
168         ad->noti_screen_mirroring_on = notification_new(NOTIFICATION_TYPE_ONGOING, NOTIFICATION_GROUP_ID_NONE, NOTIFICATION_PRIV_ID_NONE);
169         WFD_RET_IF(NULL == ad->noti_screen_mirroring_on, "NULL parameters.\n");
170
171         noti_err = notification_set_image(ad->noti_screen_mirroring_on, NOTIFICATION_IMAGE_TYPE_ICON_FOR_INDICATOR, SCREEN_MIRRIONG_INDICATOR_ICON_PATH);
172         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_image. [%d]", noti_err);
173
174         noti_err = notification_set_property(ad->noti_screen_mirroring_on, NOTIFICATION_PROP_DISABLE_TICKERNOTI);
175         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Unable to notification_set_property. [%d]", noti_err);
176
177         noti_err = notification_set_display_applist(ad->noti_screen_mirroring_on, NOTIFICATION_DISPLAY_APP_INDICATOR);
178         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Unable to notification_set_display_applist. [%d]", noti_err);
179
180         /* notify the quick panel */
181         noti_err = notification_insert(ad->noti_screen_mirroring_on, NULL);
182         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_insert. [%d]", noti_err);
183
184         __WFD_APP_FUNC_EXIT__;
185         return;
186 }
187 #endif
188
189
190 #ifdef WFD_SCREEN_MIRRORING_ENABLED
191 /**
192  *      This function let the app add the notification when it is connected
193  *      @return   void
194  *      @param[in] user_data the pointer to the main data structure
195  */
196 void _add_wfd_peers_connected_notification(void *user_data, char* package_name)
197 {
198         __WFD_APP_FUNC_ENTER__;
199
200         int res = NOTIFICATION_ERROR_NONE;
201         wfd_appdata_t *ad = (wfd_appdata_t *) user_data;
202         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
203
204         WFD_RET_IF(NULL == ad || NULL == package_name, "NULL parameters.\n");
205
206         if (ad->noti_screen_mirroring_on) {
207                 notification_delete(ad->noti_screen_mirroring_on);
208                 notification_free(ad->noti_screen_mirroring_on);
209                 ad->noti_screen_mirroring_on = NULL;
210         }
211
212         if(ad->noti_screen_mirroring_play != NULL) {
213                 noti_err = notification_free(ad->noti_screen_mirroring_play);
214                 WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_free. [%d]", noti_err);
215         }
216
217         ad->noti_screen_mirroring_play = notification_new(NOTIFICATION_TYPE_ONGOING, NOTIFICATION_GROUP_ID_NONE, NOTIFICATION_PRIV_ID_NONE);
218         WFD_RET_IF(NULL == ad->noti_screen_mirroring_play, "NULL parameters.\n");
219
220         char msg[WFD_MAX_SIZE] = {0};
221
222         bundle *b = NULL;
223         app_control_h control;
224         res = app_control_create(&control);
225         WFD_RET_IF(res != APP_CONTROL_ERROR_NONE, "app_control_create() return error : %d", res);
226
227         app_control_set_package(control, package_name);
228         app_control_add_extra_data(control, "-t", "notification");
229         res = app_control_to_bundle(control, &b);
230         if (res != APP_CONTROL_ERROR_NONE) {
231                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "app_control_to_bundle() return error : %d", res);
232                 app_control_destroy(control);
233                 return;
234         }
235
236         res = notification_set_execute_option(ad->noti_screen_mirroring_play, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, /*Button Text*/NULL, NULL, b);
237         if (res != NOTIFICATION_ERROR_NONE) {
238                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to notification_set_execute_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(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(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(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(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, KEY_SELECT)) {
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_by_type(NULL, NOTIFICATION_TYPE_ONGOING);
729         if (noti_err != NOTIFICATION_ERROR_NONE) {
730                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to notification_delete_all_by_type.(%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_new(NOTIFICATION_TYPE_ONGOING, NOTIFICATION_GROUP_ID_NONE, NOTIFICATION_PRIV_ID_NONE);
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_new(NOTIFICATION_TYPE_ONGOING, NOTIFICATION_GROUP_ID_NONE, NOTIFICATION_PRIV_ID_NONE);
873         WFD_RET_IF(NULL == ad->noti_wifi_direct_connected, "NULL parameters.\n");
874
875         bundle *b = NULL;
876         app_control_h control;
877         res = app_control_create(&control);
878         WFD_RET_IF(res != APP_CONTROL_ERROR_NONE, "app_control_create() return error : %d", res);
879
880         app_control_set_package(control, PACKAGE);
881         app_control_add_extra_data(control, NOTIFICATION_BUNDLE_PARAM, NOTIFICATION_BUNDLE_VALUE);
882         res = app_control_to_bundle(control, &b);
883         if (res != APP_CONTROL_ERROR_NONE) {
884                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "app_control_to_bundle() return error : %d", res);
885                 app_control_destroy(control);
886                 return;
887         }
888
889         noti_err = notification_set_execute_option(ad->noti_wifi_direct_connected, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, /*Button Text*/NULL, NULL, b);
890         if (noti_err != NOTIFICATION_ERROR_NONE) {
891                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to notification_set_execute_option. [%d]", noti_err);
892                 app_control_destroy(control);
893                 return;
894         }
895
896         app_control_destroy(control);
897
898         noti_err = notification_set_layout(ad->noti_wifi_direct_connected, NOTIFICATION_LY_ONGOING_EVENT);
899         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_layout. [%d]", noti_err);
900
901         /* set the icon */
902         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Icon Path: %s\n", WFD_NOTI_ICON_PATH);
903         noti_err = notification_set_image(ad->noti_wifi_direct_connected, NOTIFICATION_IMAGE_TYPE_ICON, WFD_NOTI_ICON_PATH);
904         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_image. [%d]", noti_err);
905
906         noti_err = notification_set_display_applist(ad->noti_wifi_direct_connected, NOTIFICATION_DISPLAY_APP_TICKER | NOTIFICATION_DISPLAY_APP_NOTIFICATION_TRAY);
907         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Unable to notification_set_display_applist. [%d]", noti_err);
908
909         noti_err = notification_set_text_domain(ad->noti_wifi_direct_connected, LOCALE_FILE_NAME, LOCALEDIR);
910         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_text_domain. [%d]", noti_err);
911
912         /* set the title and content */
913         noti_err = notification_set_text(ad->noti_wifi_direct_connected, NOTIFICATION_TEXT_TYPE_TITLE,
914                 _("IDS_WIFI_BODY_WI_FI_DIRECT_ABB"), "IDS_WIFI_BODY_WI_FI_DIRECT_ABB", NOTIFICATION_VARIABLE_TYPE_NONE);
915         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_text. [%d]", noti_err);
916
917         noti_err = notification_set_text(ad->noti_wifi_direct_connected, NOTIFICATION_TEXT_TYPE_CONTENT,
918                 _("IDS_WIFI_BODY_DISABLE_WI_FI_DIRECT_AFTER_USE_ABB"),
919                 "IDS_WIFI_BODY_DISABLE_WI_FI_DIRECT_AFTER_USE_ABB", NOTIFICATION_VARIABLE_TYPE_NONE);
920         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_set_text. [%d]", noti_err);
921
922         /* notify the quick panel */
923         noti_err = notification_insert(ad->noti_wifi_direct_connected, NULL);
924         WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_insert. [%d]", noti_err);
925
926         __WFD_APP_FUNC_EXIT__;
927         return;
928
929 }
930
931 void wfd_app_util_del_wfd_connected_notification(wfd_appdata_t *ad)
932 {
933         __WFD_APP_FUNC_ENTER__;
934         WFD_RET_IF(NULL == ad, "NULL parameters.\n");
935
936         /* delete the notification */
937         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
938
939         noti_err  = notification_delete(ad->noti_wifi_direct_connected);
940         if (noti_err != NOTIFICATION_ERROR_NONE) {
941                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to notification_delete.(%d)\n", noti_err);
942                 return;
943         }
944
945         if (ad->noti_wifi_direct_connected) {
946                 noti_err = notification_free(ad->noti_wifi_direct_connected);
947                 ad->noti_wifi_direct_connected = NULL;
948                 if (noti_err != NOTIFICATION_ERROR_NONE) {
949                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to notification_free.(%d)\n", noti_err);
950                 }
951         }
952
953         __WFD_APP_FUNC_EXIT__;
954         return;
955 }