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