tizen_2.0_build
[profile/ivi/indicator-win.git] / modules / setting / gps.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://www.tizenopensource.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 <stdio.h>
19 #include <stdlib.h>
20 #include <vconf.h>
21 #include "common.h"
22 #include "indicator.h"
23 #include "indicator_icon_util.h"
24 #include "modules.h"
25 #include "indicator_ui.h"
26
27 #define ICON_PRIORITY   INDICATOR_PRIORITY_NON_FIXED_5
28 #define MODULE_NAME             "gps"
29 #define TIMER_INTERVAL  0.3
30
31 static int register_gps_module(void *data);
32 static int unregister_gps_module(void);
33 static int hib_enter_gps_module(void);
34 static int hib_leave_gps_module(void *data);
35
36 Indicator_Icon_Object gps = {
37         .type = INDICATOR_IMG_ICON,
38         .name = MODULE_NAME,
39         .priority = ICON_PRIORITY,
40         .always_top = EINA_TRUE,
41         .exist_in_view = EINA_FALSE,
42         .txt_obj = {0,},
43         .img_obj = {0,},
44         .obj_exist = EINA_FALSE,
45         .fixed = EINA_FALSE,
46         .init = register_gps_module,
47         .fini = unregister_gps_module,
48         .hib_enter = hib_enter_gps_module,
49         .hib_leave = hib_leave_gps_module
50 };
51
52 enum {
53         LEVEL_MIN = 0,
54         LEVEL_GPS_ON = LEVEL_MIN,
55         LEVEL_GPS_SEARCHING,
56         LEVEL_MAX
57 };
58
59 static const char *icon_path[LEVEL_MAX] = {
60         [LEVEL_GPS_ON] = "Bluetooth_NFC_GPS/B03_GPS_On.png",
61         [LEVEL_GPS_SEARCHING] = "Bluetooth_NFC_GPS/B03_GPS_Searching.png",
62 };
63
64 static void show_image_icon(void *data, int i)
65 {
66         if (i < LEVEL_MIN)
67                 i = LEVEL_MIN;
68         else if (i >= LEVEL_MAX)
69                 i = LEVEL_GPS_SEARCHING;
70
71         gps.img_obj.data = icon_path[i];
72         indicator_util_icon_show(&gps);
73 }
74
75 static void hide_image_icon(void)
76 {
77         indicator_util_icon_hide(&gps);
78 }
79
80 static int indicator_gps_state_get(void)
81 {
82         int ret = 0;
83         int status = VCONFKEY_LOCATION_POSITION_OFF;
84
85         ret = vconf_get_int(VCONFKEY_LOCATION_POSITION_STATE, &status);
86         if (ret < 0)
87                 ERR("fail to get [%s]", VCONFKEY_LOCATION_POSITION_STATE);
88
89         INFO("GPS STATUS: %d", status);
90
91         return status;
92 }
93
94 static void indicator_gps_state_icon_set(int status, void *data)
95 {
96         INFO("GPS STATUS: %d", status);
97
98         switch (status) {
99         case VCONFKEY_LOCATION_POSITION_OFF:
100                 hide_image_icon();
101                 break;
102         case VCONFKEY_LOCATION_POSITION_CONNECTED:
103                 show_image_icon(data, LEVEL_GPS_ON);
104                 indicator_util_icon_animation_set(&gps, ICON_ANI_NONE);
105                 break;
106         case VCONFKEY_LOCATION_POSITION_SEARCHING:
107                 show_image_icon(data, LEVEL_GPS_SEARCHING);
108                 indicator_util_icon_animation_set(&gps, ICON_ANI_BLINK);
109                 break;
110         default:
111                 hide_image_icon();
112                 ERR("Invalid value!");
113                 break;
114         }
115
116         return;
117 }
118
119 static void indicator_gps_change_cb(keynode_t *node, void *data)
120 {
121         int status = VCONFKEY_LOCATION_POSITION_OFF;
122
123         retif(data == NULL, , "Invalid parameter!");
124         retif(node == NULL, , "node is NULL");
125
126         status = vconf_keynode_get_int(node);
127         if (status < 0) {
128                 ERR("fail to get value from node");
129                 status = VCONFKEY_LOCATION_POSITION_OFF;
130         }
131
132         indicator_gps_state_icon_set(status, data);
133
134         return;
135 }
136
137 static int register_gps_module(void *data)
138 {
139         int ret;
140
141         retif(data == NULL, FAIL, "Invalid parameter!");
142
143         ret = vconf_notify_key_changed(VCONFKEY_LOCATION_POSITION_STATE,
144                                        indicator_gps_change_cb, data);
145         if (ret != OK)
146                 ERR("Failed to register callback!");
147
148         indicator_gps_state_icon_set(indicator_gps_state_get(), data);
149
150         return ret;
151 }
152
153 static int unregister_gps_module(void)
154 {
155         int ret;
156
157         ret = vconf_ignore_key_changed(VCONFKEY_LOCATION_POSITION_STATE,
158                                        indicator_gps_change_cb);
159         if (ret != OK)
160                 ERR("Failed to unregister callback!");
161
162         hide_image_icon();
163
164         return OK;
165 }
166
167 static int hib_enter_gps_module(void)
168 {
169         int ret;
170
171         ret = vconf_ignore_key_changed(VCONFKEY_LOCATION_POSITION_STATE,
172                                        indicator_gps_change_cb);
173         if (ret != OK)
174                 ERR("Failed to unregister callback!");
175
176         return OK;
177 }
178
179 static int hib_leave_gps_module(void *data)
180 {
181         int ret;
182
183         retif(data == NULL, FAIL, "Invalid parameter!");
184
185         ret = vconf_notify_key_changed(VCONFKEY_LOCATION_POSITION_STATE,
186                                        indicator_gps_change_cb, data);
187         retif(ret != OK, FAIL, "Failed to register callback!");
188
189         indicator_gps_state_icon_set(indicator_gps_state_get(), data);
190
191         return OK;
192 }
193