Closes app now when you hit the back button. This is a temp fix to keep media workin...
[profile/ivi/ug-camera-efl.git] / src / cam_lbs.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *        http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #include <time.h>
19 #include <locations.h>
20 #include "cam_lbs.h"
21 #include "cam_debug.h"
22
23 typedef struct {
24         location_manager_h location_handle;
25         CamLBSState m_lbs_state;
26         void (*lbs_update_cb) (void *data, int lbs_state);
27         void *cb_data;
28         double latitude;
29         double longitude;
30         double altitude;
31         time_t time_stamp;
32 } CamLBSInfo;
33
34 static CamLBSInfo *cam_lbs_info = NULL; /* lbs_info */
35
36 void cam_lbs_set_state_from_accuracy_level()
37 {
38         g_return_if_fail(cam_lbs_info);
39         g_return_if_fail(cam_lbs_info->location_handle);
40
41         location_accuracy_level_e accuracy_level;
42         double horizontal;
43         double vertical;
44
45         int ret = LOCATIONS_ERROR_NONE;
46
47         ret = location_manager_get_accuracy(cam_lbs_info->location_handle,
48                                                                                 &accuracy_level, &horizontal, &vertical);
49         if (ret != LOCATIONS_ERROR_NONE) {
50                 cam_debug(LOG_SYS, "location_manager_get_accuracy failed!! error = %d", ret);
51                 return;
52         }
53
54         cam_lbs_info->m_lbs_state = CAM_LBS_STATE_DISABLE;
55
56         switch (accuracy_level) {
57         case LOCATIONS_ACCURACY_NONE:
58                 cam_lbs_info->m_lbs_state = CAM_LBS_STATE_SERVICE_START;
59                 break;
60         case LOCATIONS_ACCURACY_COUNTRY:
61         case LOCATIONS_ACCURACY_REGION :
62                 cam_lbs_info->m_lbs_state = CAM_LBS_STATE_SERVICE_ENABLE;
63                 break;
64         case LOCATIONS_ACCURACY_LOCALITY :
65         case LOCATIONS_ACCURACY_POSTALCODE:
66                 cam_lbs_info->m_lbs_state = CAM_LBS_STATE_ACCURANCY_ROUGH;
67                 break;
68         case LOCATIONS_ACCURACY_STREET :
69         case LOCATIONS_ACCURACY_DETAILED :
70                 cam_lbs_info->m_lbs_state = CAM_LBS_STATE_ACCURANCY_DETAILED;
71                 break;
72         default:
73                 cam_lbs_info->m_lbs_state = CAM_LBS_STATE_DISABLE;
74                 break;
75         }
76 }
77
78 static void cam_lbs_position_updated_cb(double latitude, double longitude,
79                                                                                                 double altitude, time_t timestamp, void *user_data)
80 {
81         cam_debug(LOG_SYS, "cam_lbs_position_updated_cb : lat(%f), long(%f), alt(%f), time(%f)",
82                                 latitude, longitude, altitude, timestamp);
83
84         g_return_if_fail(cam_lbs_info);
85
86         cam_lbs_set_state_from_accuracy_level();
87
88         cam_lbs_info->latitude = latitude;
89         cam_lbs_info->longitude = longitude;
90         cam_lbs_info->altitude = altitude;
91         cam_lbs_info->time_stamp = timestamp;
92
93         /* call callback function */
94         if (cam_lbs_info->lbs_update_cb)
95                 cam_lbs_info->lbs_update_cb(cam_lbs_info->cb_data, cam_lbs_get_state());
96 }
97
98 static void cam_lbs_service_state_changed_cb(location_service_state_e state, void *user_data)
99 {
100         cam_debug(LOG_SYS, "cam_lbs_service_state_changed_cb : state(%d)", state);
101
102         g_return_if_fail(cam_lbs_info);
103         g_return_if_fail(cam_lbs_info->location_handle);
104
105         switch (state){
106         case LOCATIONS_SERVICE_ENABLED : {
107                 cam_lbs_info->m_lbs_state = CAM_LBS_STATE_SERVICE_ENABLE;
108
109                 double altitude = -1.0;
110                 double latitude = -1.0;
111                 double longitude = -1.0;
112                 time_t timestamp = -1.0;
113
114                 int ret = LOCATIONS_ERROR_NONE;
115
116                 ret = location_manager_get_position(cam_lbs_info->location_handle,
117                                                                                         &altitude, &latitude, &longitude, &timestamp);
118                 if(ret != LOCATIONS_ERROR_NONE) {
119                         cam_debug(LOG_SYS, "location_manager_get_position failed!! error = %d", ret);
120                         return;
121                 }
122
123                 cam_debug(LOG_SYS, "cam_lbs_service_state_changed_cb : alt(%f), lat(%f), long(%f), time(%f)",
124                                         altitude, latitude, longitude, timestamp);
125
126                 cam_lbs_info->altitude = altitude;
127                 cam_lbs_info->latitude = latitude;
128                 cam_lbs_info->longitude = longitude;
129                 cam_lbs_info->time_stamp = timestamp;
130
131                 cam_lbs_set_state_from_accuracy_level();
132
133                 /* call callback function */
134                 if (cam_lbs_info->lbs_update_cb)
135                         cam_lbs_info->lbs_update_cb(cam_lbs_info->cb_data, cam_lbs_get_state());
136                 }
137
138         case LOCATIONS_SERVICE_DISABLED : {
139                 cam_lbs_info->m_lbs_state = CAM_LBS_STATE_DISABLE;
140
141                 /* call callback function */
142                 if (cam_lbs_info->lbs_update_cb)
143                         cam_lbs_info->lbs_update_cb(cam_lbs_info->cb_data, cam_lbs_get_state());
144                 }
145
146         default :
147                 break;
148         }
149 }
150
151 gboolean cam_lbs_init(void)
152 {
153         g_return_val_if_fail(!cam_lbs_info, FALSE);
154
155         debug_fenter(LOG_UI);
156
157         cam_lbs_info = g_new0(CamLBSInfo, 1);
158         cam_lbs_info->location_handle = NULL;
159         cam_lbs_info->m_lbs_state = CAM_LBS_STATE_DISABLE;
160         cam_lbs_info->lbs_update_cb = NULL;
161         cam_lbs_info->cb_data = NULL;
162         cam_lbs_info->altitude = -1.0;
163         cam_lbs_info->latitude = -1.0;
164         cam_lbs_info->longitude = -1.0;
165         cam_lbs_info->time_stamp = -1.0;
166
167         int ret = LOCATIONS_ERROR_NONE;
168
169         ret = location_manager_create(LOCATIONS_METHOD_HYBRID, &cam_lbs_info->location_handle);
170         if (ret != LOCATIONS_ERROR_NONE) {
171                 cam_debug(LOG_SYS, "location_manager_create failed!! error = %d", ret);
172                 goto ERROR;
173         }
174
175         ret = location_manager_set_service_state_changed_cb(cam_lbs_info->location_handle,
176                                                                                                                 cam_lbs_service_state_changed_cb, (void*)NULL);
177         if (ret != LOCATIONS_ERROR_NONE) {
178                 cam_debug(LOG_SYS, "location_manager_set_service_state_changed_cb failed!! error = %d", ret);
179                 goto ERROR;
180         }
181
182         ret = location_manager_set_position_updated_cb(cam_lbs_info->location_handle,
183                                                                                                         cam_lbs_position_updated_cb, 30, (void*)NULL);
184         if (ret != LOCATIONS_ERROR_NONE) {
185                 cam_debug(LOG_SYS, "location_manager_set_position_updated_cb failed!! error = %d", ret);
186                 goto ERROR;
187         }
188
189         return TRUE;
190
191         ERROR:
192
193         if (cam_lbs_info)
194                 g_free(cam_lbs_info);
195         cam_lbs_info = NULL;
196
197         return FALSE;
198 }
199
200 gboolean cam_lbs_finialize(void)
201 {
202         g_return_val_if_fail(cam_lbs_info, FALSE);
203         g_return_val_if_fail(cam_lbs_info->location_handle, FALSE);
204
205         debug_fenter(LOG_UI);
206
207         int ret = LOCATIONS_ERROR_NONE;
208
209         ret = location_manager_destroy(cam_lbs_info->location_handle);
210
211         if (cam_lbs_info)
212                 g_free(cam_lbs_info);
213         cam_lbs_info = NULL;
214
215         if (ret != LOCATIONS_ERROR_NONE) {
216                 cam_debug(LOG_SYS, "location_manager_destroy failed!! error = %d", ret);
217                 return FALSE;
218         }
219
220         return TRUE;
221 }
222
223 gboolean cam_lbs_start(void (*lbs_update_cb) (void *data, int lbs_state), void *data)
224 {
225         g_return_val_if_fail(cam_lbs_info, FALSE);
226         g_return_val_if_fail(cam_lbs_info->location_handle, FALSE);
227
228         cam_lbs_info->lbs_update_cb = lbs_update_cb;
229         cam_lbs_info->cb_data = data;
230
231         int ret = LOCATIONS_ERROR_NONE;
232
233         ret = location_manager_start(cam_lbs_info->location_handle);
234         if (ret != LOCATIONS_ERROR_NONE) {
235                 cam_debug(LOG_SYS, "location_manager_start failed!! error = %d", ret);
236                 cam_lbs_info->m_lbs_state = CAM_LBS_STATE_DISABLE;
237                 return FALSE;
238         }
239
240         cam_lbs_info->m_lbs_state = CAM_LBS_STATE_SERVICE_START;
241
242         return TRUE;
243 }
244
245 gboolean cam_lbs_stop(void)
246 {
247         g_return_val_if_fail(cam_lbs_info, FALSE);
248         g_return_val_if_fail(cam_lbs_info->location_handle, FALSE);
249
250         int ret = LOCATIONS_ERROR_NONE;
251
252         ret = location_manager_stop(cam_lbs_info->location_handle);
253         if (ret != LOCATIONS_ERROR_NONE) {
254                 cam_debug(LOG_SYS, "location_manager_stop failed!! error = %d", ret);
255                 return FALSE;
256         }
257
258         cam_lbs_info->m_lbs_state = CAM_LBS_STATE_DISABLE;
259
260         return TRUE;
261 }
262
263 gboolean cam_lbs_is_valid(void)
264 {
265         g_return_val_if_fail(cam_lbs_info, FALSE);
266
267         if (cam_lbs_info->m_lbs_state > CAM_LBS_STATE_SERVICE_START)
268                 return TRUE;
269         else
270                 return FALSE;
271 }
272
273 gboolean cam_lbs_get_current_position(double *longitude, double *latitude,
274                                                                                         double *altitude, time_t *time_stamp)
275 {
276         g_return_val_if_fail(cam_lbs_info, FALSE);
277         g_return_val_if_fail(cam_lbs_info->m_lbs_state >= CAM_LBS_STATE_SERVICE_ENABLE, FALSE);
278
279         cam_debug(LOG_SYS, "SYNC>> Current position:");
280         cam_debug(LOG_SYS, "\ttime: %f, lat: %f, long: %f, alt: %f",
281                   cam_lbs_info->time_stamp, cam_lbs_info->latitude,
282                   cam_lbs_info->longitude, cam_lbs_info->altitude);
283
284         *altitude = cam_lbs_info->altitude;
285         *latitude = cam_lbs_info->latitude;
286         *longitude = cam_lbs_info->longitude;
287         *time_stamp = cam_lbs_info->time_stamp;
288
289         return TRUE;
290 }
291
292 gboolean cam_lbs_get_address(char *address, int max_length)
293 {
294         g_return_val_if_fail(cam_lbs_info, FALSE);
295
296         return TRUE;
297 }
298
299 int cam_lbs_get_state(void)
300 {
301         g_return_val_if_fail(cam_lbs_info, CAM_LBS_STATE_DISABLE);
302
303         return cam_lbs_info->m_lbs_state;
304 }