4 * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Youngae Kang <youngae.kang@samsung.com>, Yunhan Kim <yhan.kim@samsung.com>,
7 * Genie Kim <daejins.kim@samsung.com>, Minjune Kim <sena06.kim@samsung.com>
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
27 #include <sys/types.h>
29 #include <location-appman.h>
32 #include "location-common-util.h"
33 #include "location-setting.h"
34 #include "location-log.h"
37 int location_application_enabled (void)
40 location_appman_s *appman;
45 if (TRUE == location_appman_check_developer_mode()) {
46 LOCATION_LOGE("Location is Enabled");
50 if (location_appman_get_package_by_pid(pid, &appman) != LOCATION_APPMAN_ERROR_NONE) {
51 LOCATION_LOGE("Fail to location_appman_get_package_by_pid");
55 if (location_appman_find_package(appman->package, &found) != LOCATION_APPMAN_ERROR_NONE) {
56 LOCATION_LOGE("Cannot find package [%s]", appman->package);
60 if (found == LOCATION_APPMAN_PACKAGE_NOTFOUND) {
61 LOCATION_LOGD("First time to use location [%s]", appman->package);
62 if (location_appman_register_package(appman) != LOCATION_APPMAN_ERROR_NONE) {
63 LOCATION_LOGE("Fail to register [%s]", appman->package);
67 LOCATION_LOGD("[%s] is already registered. Update recently used time", appman->package);
69 if (location_appman_set_recently_used(appman->package, timestamp) != LOCATION_APPMAN_ERROR_NONE) {
70 LOCATION_LOGD("Cannot update recently used time");
74 if (location_appman_is_enabled(appman->package, &enabled) != LOCATION_APPMAN_ERROR_NONE) {
75 LOCATION_LOGE("Fail to location_appman_is_enabled");
81 static gint compare_position (gconstpointer a, gconstpointer b)
83 g_return_val_if_fail(a, 1);
84 g_return_val_if_fail(b, -1);
86 if(location_position_equal((LocationPosition*) a, (LocationPosition *)b) == TRUE) {
94 boundary_compare (gconstpointer comp1, gconstpointer comp2)
96 g_return_val_if_fail(comp1, 1);
97 g_return_val_if_fail(comp2, -1);
101 LocationBoundary *boundary1 = (LocationBoundary *)comp1;
102 LocationBoundary *boundary2 = (LocationBoundary *)comp2;
104 if (boundary1->type == boundary2->type) {
105 switch (boundary1->type) {
106 case LOCATION_BOUNDARY_CIRCLE: {
107 if (location_position_equal(boundary1->circle.center, boundary2->circle.center)
108 && boundary1->circle.radius == boundary2->circle.radius) {
113 case LOCATION_BOUNDARY_RECT: {
114 if (location_position_equal(boundary1->rect.left_top, boundary2->rect.left_top)
115 && location_position_equal(boundary1->rect.right_bottom, boundary2->rect.right_bottom)) {
120 case LOCATION_BOUNDARY_POLYGON: {
122 GList *boundary1_next = NULL;
123 GList *boundary2_start = NULL, *boundary2_prev = NULL, *boundary2_next = NULL;
124 if (g_list_length(boundary1->polygon.position_list) != g_list_length(boundary2->polygon.position_list)) {
128 // Find a matching index of Boundary2 with Boundary1's 1st postion.
129 boundary2_start = g_list_find_custom(boundary2->polygon.position_list, g_list_nth_data(boundary1->polygon.position_list, 0), (GCompareFunc) compare_position);
130 if (boundary2_start == NULL) return -1;
132 boundary2_prev = g_list_previous(boundary2_start);
133 boundary2_next = g_list_next(boundary2_start);
134 if (boundary2_prev == NULL) boundary2_prev = g_list_last(boundary2->polygon.position_list);
135 if (boundary2_next == NULL) boundary2_next = g_list_first(boundary2->polygon.position_list);
137 boundary1_next = g_list_next(boundary1->polygon.position_list);
138 if (location_position_equal((LocationPosition*)boundary1_next->data, (LocationPosition*)boundary2_prev->data) == TRUE){
139 boundary1_next = g_list_next(boundary1_next);
140 while (boundary1_next) {
141 boundary2_prev = g_list_previous(boundary2_prev);
142 if (boundary2_prev == NULL) boundary2_prev = g_list_last(boundary2->polygon.position_list);
143 if (location_position_equal((LocationPosition*)boundary1_next->data, (LocationPosition*) boundary2_prev->data) == FALSE){
146 boundary1_next = g_list_next(boundary1_next);
150 else if (location_position_equal((LocationPosition*)boundary1_next->data, (LocationPosition*)boundary2_next->data) == TRUE) {
151 boundary1_next = g_list_next(boundary1_next);
152 while(boundary1_next) {
153 boundary2_next = g_list_next(boundary2_next);
154 if (boundary2_next == NULL) boundary2_next = g_list_first(boundary2->polygon.position_list);
155 if (location_position_equal((LocationPosition*)boundary1_next->data, (LocationPosition*) boundary2_next->data) == FALSE){
158 boundary1_next = g_list_next(boundary1_next);
178 int set_prop_boundary(GList **prev_boundary_list, GList *new_boundary_list)
180 g_return_val_if_fail(new_boundary_list, LOCATION_ERROR_PARAMETER);
183 GList *check_list = NULL;
185 LocationBoundary *new_boundary = NULL;
186 LocationBoundary *copy_boundary = NULL;
189 while((new_boundary = (LocationBoundary*) g_list_nth_data(new_boundary_list, index)) != NULL) {
191 check_list = g_list_find_custom(*prev_boundary_list, new_boundary, (GCompareFunc)boundary_compare);
192 if (check_list == NULL) {
193 LOCATION_LOGD("Set Prop >> boundary type: [%d]", new_boundary->type);
194 copy_boundary = location_boundary_copy(new_boundary);
195 *prev_boundary_list = g_list_append(*prev_boundary_list, copy_boundary);
199 *prev_boundary_list = g_list_first(*prev_boundary_list);
201 return LOCATION_ERROR_NONE;
206 int set_prop_removal_boundary(GList **prev_boundary_list, LocationBoundary* boundary)
208 g_return_val_if_fail(*prev_boundary_list, LOCATION_ERROR_PARAMETER);
210 GList *check_list = NULL;
212 check_list = g_list_find_custom (*prev_boundary_list, boundary, (GCompareFunc) boundary_compare);
214 LOCATION_LOGD("Found");
215 *prev_boundary_list = g_list_delete_link(*prev_boundary_list, check_list);
218 if (g_list_length(*prev_boundary_list) == 0 ) {
219 LOCATION_LOGD("Boundary List is empty");
220 g_list_free(*prev_boundary_list);
221 *prev_boundary_list = NULL;
224 return LOCATION_ERROR_NONE;
227 void free_boundary_list (gpointer data)
229 LocationBoundary *boundary = (LocationBoundary *)data;
231 location_boundary_free(boundary);