Make conformant expand EVAS_HINT_EXPAND instead of 0
[apps/native/ug-wifi-direct.git] / popup-wifidirect / src / wfd-app-main.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 main functions.
22  *
23  * @file    wfd-app-main.c
24  * @author  Sungsik Jang (sungsik.jang@samsung.com)
25  * @version 0.1
26  */
27
28 #include <libintl.h>
29 #include <Elementary.h>
30 #include <notification.h>
31 #include <ui-gadget-module.h>
32 #include <app_control_internal.h>
33 #include <feedback.h>
34 #include <wifi-direct.h>
35 #include <efl_util.h>
36 #include <linux/unistd.h>
37 #include <vconf.h>
38
39 #include "wfd-app.h"
40 #include "wfd-app-util.h"
41
42 wfd_appdata_t *g_wfd_ad;
43
44
45 wfd_appdata_t *wfd_get_appdata()
46 {
47         return g_wfd_ad;
48 }
49
50 static void _win_del(void *data, Evas_Object *obj, void *event)
51 {
52         elm_exit();
53 }
54
55 static Evas_Object *_create_win(Evas_Object *parent, const char *name)
56 {
57         Evas_Object *eo;
58
59         /* eo = elm_win_add(parent, name, ELM_WIN_BASIC); */
60         eo = elm_win_add(NULL, name, ELM_WIN_NOTIFICATION);
61         if (eo) {
62                 elm_win_title_set(eo, name);
63                 elm_win_borderless_set(eo, EINA_TRUE);
64                 elm_win_alpha_set(eo, EINA_TRUE);
65                 evas_object_smart_callback_add(eo, "delete,request", _win_del, NULL);
66                 efl_util_set_notification_window_level(eo, EFL_UTIL_NOTIFICATION_LEVEL_1);
67                 evas_object_raise(eo);
68         }
69
70         return eo;
71 }
72
73 static bool _app_create(void *data)
74 {
75         __WFD_APP_FUNC_ENTER__;
76         wfd_appdata_t *ad = wfd_get_appdata();
77         int ret = 0;
78
79         if (data == NULL) {
80                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Incorrect parameter\n");
81                 return FALSE;
82         }
83
84         bindtextdomain(LOCALE_FILE_NAME, LOCALEDIR);
85
86         ad->popup_data = (wfd_popup_t *) malloc(sizeof(wfd_popup_t));
87         if (!ad->popup_data) {
88                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "malloc failed\n");
89                 return FALSE;
90         }
91
92         memset(ad->popup_data, 0x0, sizeof(wfd_popup_t));
93         ad->win = _create_win(NULL, PACKAGE);
94
95         /* set rotation */
96         if (elm_win_wm_rotation_supported_get(ad->win)) {
97                 int rots[4] = {0, 90, 180, 270};
98                 elm_win_wm_rotation_available_rotations_set(ad->win, (const int *)(&rots), 4);
99         }
100
101         ad->conformant = elm_conformant_add(ad->win);
102         assertm_if(NULL == ad->conformant, "conformant is NULL!!");
103         elm_win_conformant_set(ad->win, EINA_TRUE);
104         elm_win_resize_object_add(ad->win, ad->conformant);
105         evas_object_size_hint_weight_set(ad->conformant, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
106         evas_object_size_hint_align_set(ad->conformant, EVAS_HINT_FILL, 0.0);
107         evas_object_show(ad->conformant);
108
109
110         ad->back_grnd = elm_bg_add(ad->conformant);
111         if (NULL == ad->back_grnd) {
112                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Create background failed\n");
113                 return FALSE;
114         }
115         elm_object_signal_emit(ad->conformant, "elm,state,indicator,nooverlap", "elm");
116         elm_object_style_set(ad->back_grnd, "indicator/headerbg");
117         elm_object_part_content_set(ad->conformant, "elm.swallow.indicator_bg", ad->back_grnd);
118         evas_object_size_hint_weight_set(ad->back_grnd,
119                 EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
120         evas_object_show(ad->back_grnd);
121
122         ad->layout = elm_layout_add(ad->conformant);
123         elm_object_content_set(ad->conformant, ad->layout);
124
125         ret = init_wfd_client(ad);
126         if (!ret) {
127                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "init_wfd_popup_client error\n");
128                 wfd_prepare_popup(WFD_POP_FAIL_INIT, NULL);
129                 __WFD_APP_FUNC_EXIT__;
130                 return FALSE;
131         }
132
133         ret = wfd_app_util_register_vconf_callbacks(ad);
134         if (ret < 0) {
135                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to register vconf notification");
136                 return FALSE;
137         }
138
139         /* Register Hard Key Press CB */
140         wfd_app_util_register_hard_key_down_cb(ad);
141
142         /* Initializes feedback API */
143         ret = feedback_initialize();
144         if (ret != FEEDBACK_ERROR_NONE) {
145                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "feedback_initialize error : %d\n", ret);
146                 return FALSE;
147         }
148         __WFD_APP_FUNC_EXIT__;
149         return TRUE;
150 }
151
152 static void _app_terminate(void *data)
153 {
154         __WFD_APP_FUNC_ENTER__;
155         wfd_appdata_t *ad = (wfd_appdata_t *) data;
156         int ret = 0;
157
158         if (data == NULL) {
159                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Incorrect parameter\n");
160                 return;
161         }
162
163         wfd_app_util_del_notification(ad);
164
165         ret = wfd_app_util_deregister_vconf_callbacks(ad);
166         if (ret < 0) {
167                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to register vconf notification");
168         }
169
170         /* Deregister Hardkey CB */
171         wfd_app_util_deregister_hard_key_down_cb(ad);
172
173         /* Deinitializes feedback API */
174         ret = feedback_deinitialize();
175         if (ret != FEEDBACK_ERROR_NONE) {
176                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "feedback_deinitialize error : %d\n", ret);
177         }
178         if (ad->transmit_timer) {
179                 ecore_timer_del(ad->transmit_timer);
180                 ad->transmit_timer = NULL;
181         }
182
183         wfd_destroy_popup();
184
185         ret = deinit_wfd_client(ad);
186         if (ret < 0) {
187                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "deinit_wfd_client error\n");
188         }
189
190         if (ad->back_grnd) {
191                 evas_object_del(ad->back_grnd);
192                 ad->back_grnd = NULL;
193         }
194
195         if (ad->win) {
196                 evas_object_del(ad->win);
197                 ad->win = NULL;
198         }
199
200         if (ad->popup_data) {
201                 free(ad->popup_data);
202                 ad->popup_data = NULL;
203         }
204
205         __WFD_APP_FUNC_EXIT__;
206         return;
207 }
208
209 static void _app_pause(void *data)
210 {
211         __WFD_APP_FUNC_ENTER__;
212         __WFD_APP_FUNC_EXIT__;
213         return;
214 }
215
216 static void _app_resume(void *data)
217 {
218         __WFD_APP_FUNC_ENTER__;
219         __WFD_APP_FUNC_EXIT__;
220         return;
221 }
222
223 static void _app_reset(app_control_h control, void *data)
224 {
225         __WFD_APP_FUNC_ENTER__;
226
227         int ret;
228         wfd_appdata_t *ad = (wfd_appdata_t *) data;
229         if (ad == NULL) {
230                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Incorrect parameter\n");
231                 return;
232         }
233         if (control == NULL) {
234                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Service is NULL");
235                 return;
236         }
237
238         // From Notification
239         char *noti_type = NULL;
240         app_control_get_extra_data(control, NOTIFICATION_BUNDLE_PARAM, &noti_type);
241
242         if (noti_type == NULL) {
243                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Notification type is wrong.");
244                 return;
245         }
246
247         WFD_APP_LOG(WFD_APP_LOG_LOW, "Notification type is [%s]", noti_type);
248         if (strncmp(noti_type, NOTIFICATION_BUNDLE_VALUE, strlen(NOTIFICATION_BUNDLE_PARAM)) == 0) {
249                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Launch wifidirect-ug");
250                 wifi_direct_get_state(&ad->wfd_status);
251                 WFD_APP_LOG(WFD_APP_LOG_LOW, "State: %d", ad->wfd_status);
252                 if (ad->wfd_status == WIFI_DIRECT_STATE_CONNECTED) {
253                         WFD_APP_LOG(WFD_APP_LOG_LOW, "Connected");
254                         if (ad->transmit_timer) {
255                                 ecore_timer_del(ad->transmit_timer);
256                                 ad->transmit_timer = NULL;
257                         }
258                         WFD_APP_LOG(WFD_APP_LOG_LOW, "start the transmit timer again\n");
259                         ad->last_wfd_transmit_time = time(NULL);
260                         ad->transmit_timer = ecore_timer_add(5.0,
261                                 (Ecore_Task_Cb)wfd_automatic_deactivated_for_connection_cb, ad);
262                 }
263                 app_control_h ug_control;
264                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Launching Settings EFL from notification\n");
265                 app_control_create(&ug_control);
266                 app_control_set_operation(ug_control, APP_CONTROL_OPERATION_DEFAULT);
267                 app_control_set_launch_mode(ug_control, APP_CONTROL_LAUNCH_MODE_GROUP);
268                 app_control_set_app_id(ug_control, "setting-wifidirect-efl");
269
270                 ret = app_control_send_launch_request(ug_control, NULL, NULL);
271                 if(ret == APP_CONTROL_ERROR_NONE) {
272                         WFD_APP_LOG(WFD_APP_LOG_LOW, "Launch Wi-Fi Direct successful");
273                 } else {
274                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to launch Wi-Fi Direct");
275                 }
276                 app_control_destroy(ug_control);
277
278         }
279         WFD_IF_FREE_MEM(noti_type);
280         __WFD_APP_FUNC_EXIT__;
281         return;
282 }
283
284 int main(int argc, char *argv[])
285 {
286         wfd_appdata_t ad;
287         ui_app_lifecycle_callback_s event_callback;
288         memset(&event_callback, 0x0, sizeof(ui_app_lifecycle_callback_s));
289
290         event_callback.create = _app_create;
291         event_callback.terminate = _app_terminate;
292         event_callback.pause = _app_pause;
293         event_callback.resume = _app_resume;
294         event_callback.app_control = _app_reset;
295
296         memset(&ad, 0x0, sizeof(wfd_appdata_t));
297         g_wfd_ad = &ad;
298
299         return ui_app_main(argc, argv, &event_callback, &ad);
300 }