2105d42a87a795f6fe879b739358b7b5b55a9301
[apps/core/preloaded/lockscreen.git] / src / battery.c
1 /*
2  * Copyright (c) 2009-2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 #include <device/battery.h>
18 #include <device/callback.h>
19
20 #include "lockscreen.h"
21 #include "log.h"
22 #include "battery.h"
23 #include "property.h"
24 #include "default_lock.h"
25
26 static struct _s_info {
27         bool is_connected;
28         bool is_charging;
29 } s_info = {
30         .is_connected = false,
31         .is_charging = false,
32 };
33
34 bool lock_battery_is_charging_get(void)
35 {
36         return s_info.is_charging;
37 }
38
39 bool lock_battery_is_connected_get(void)
40 {
41         return s_info.is_connected;
42 }
43
44 static char *_replaceString(char *strInput, const char *strTarget, const char *strChange)
45 {
46         char* strResult;
47         char* strTemp;
48         int i = 0;
49         int nCount = 0;
50         int nTargetLength = strlen(strTarget);
51
52         if (nTargetLength < 1) {
53                 _E("there is no target to chnage");
54                 return NULL;
55         }
56
57         int nChangeLength = strlen(strChange);
58
59         if (nChangeLength != nTargetLength) {
60                 for (i = 0; strInput[i] != '\0';) {
61                         if (memcmp(&strInput[i], strTarget, nTargetLength) == 0) {
62                                 nCount++;               //consider same string exist
63                                 i += nTargetLength;
64                         } else {
65                                 i++;
66                         }
67                 }
68         } else {
69                 i = strlen(strInput);
70         }
71
72         strResult = (char *) malloc(i + 1 + nCount * (nChangeLength - nTargetLength));
73
74         if (!strResult) {
75                 _E("fail malloc!!");
76                 return NULL;
77         }
78
79
80         strTemp = strResult;
81         while (*strInput) {
82                 if (memcmp(strInput, strTarget, nTargetLength) == 0) {
83                         memcpy(strTemp, strChange, nChangeLength);
84                         strTemp += nChangeLength;       //move changed length
85                         strInput  += nTargetLength;     // move target length
86                 } else {
87                         *strTemp++ = *strInput++;               // original str cpy
88                 }
89         }
90
91         *strTemp = '\0';
92
93         return strResult;
94 }
95
96 lock_error_e lock_battery_update(void)
97 {
98         Evas_Object *swipe_layout = NULL;
99
100         bool status = false;
101         int capacity = 0;
102         device_battery_level_e battery_level = 0;
103         bool charger = false;
104         int ret = 0;
105
106         swipe_layout = lock_default_swipe_layout_get();
107         retv_if(!swipe_layout, LOCK_ERROR_FAIL);
108
109         ret = lock_property_get_bool(PROPERTY_TYPE_RUNTIME_INFO, (void *)RUNTIME_INFO_KEY_BATTERY_IS_CHARGING, &status);
110         if (ret != LOCK_ERROR_OK) {
111                 _E("Failed to get runtime info : RUNTIME_INFO_KEY_BATTERY_IS_CHARGING");
112                 elm_object_part_text_set(swipe_layout, "txt.battery", "");
113                 return LOCK_ERROR_FAIL;
114         } else {
115                 elm_object_signal_emit(swipe_layout, "show,txt,battery", "txt.battery");
116
117                 if (status == true) {
118                         ret = device_battery_get_percent(&capacity);
119                         if (ret != DEVICE_ERROR_NONE) {
120                                 _E("Failed to get battery percent(%d)", ret);
121                         }
122
123                         ret = device_battery_get_level_status(&battery_level);
124                         if (ret != DEVICE_ERROR_NONE) {
125                                 _E("Failed to get battery level status(%d)", ret);
126                         }
127
128                         if (capacity == 100) {
129                                 _D("Fully charged");
130                                 elm_object_part_text_set(swipe_layout, "txt.battery", _("IDS_SM_POP_FULLY_CHARGED"));
131                         } else {
132                                 char buff[64];
133                                 char *newString = NULL;
134                                 newString = _replaceString(_("IDS_LCKSCN_BODY_CHARGING_C_PDP"), "%d%", "%d%%");
135
136                                 if (newString != NULL) {
137                                         snprintf(buff, sizeof(buff), newString , capacity);
138                                         free(newString) ;
139                                 } else {
140                                         snprintf(buff, sizeof(buff), _("IDS_LCKSCN_BODY_CHARGING_C_PDP") , capacity);
141                                 }
142
143                                 elm_object_part_text_set(swipe_layout, "txt.battery", buff);
144                         }
145                 } else {
146                         elm_object_part_text_set(swipe_layout, "txt.battery", "");
147
148                         ret = lock_property_get_bool(PROPERTY_TYPE_RUNTIME_INFO, (void *)RUNTIME_INFO_KEY_CHARGER_CONNECTED , &charger);
149                         if (ret != LOCK_ERROR_OK) {
150                                 _E("Failed to get runtime info : RUNTIME_INFO_KEY_CHARGER_CONNECTED");
151                         } else {
152                                 ret = device_battery_get_percent(&capacity);
153                                 if (ret != DEVICE_ERROR_NONE) {
154                                         _E("Failed to get battery percent(%d)", ret);
155                                 }
156
157                                 ret = device_battery_get_level_status(&battery_level);
158                                 if (ret != DEVICE_ERROR_NONE) {
159                                         _E("Failed to get battery level status(%d)", ret);
160                                 }
161
162                                 if (capacity == 100 && charger == true) {
163                                         elm_object_part_text_set(swipe_layout, "txt.battery", _("IDS_SM_POP_FULLY_CHARGED"));
164                                 } else {
165                                         elm_object_part_text_set(swipe_layout, "txt.battery", "");
166                                 }
167                         }
168                 }
169         }
170
171         return LOCK_ERROR_OK;
172 }
173
174 static void _battery_changed_cb(device_callback_e type, void *value, void *user_data)
175 {
176         _D("%s", __func__);
177
178         if (LOCK_ERROR_OK != lock_battery_update()) {
179                 _E("Failed to update battery information");
180         }
181 }
182
183 lock_error_e lock_battery_show(void)
184 {
185         Evas_Object *swipe_layout = NULL;
186
187         int ret = 0;
188
189         swipe_layout = lock_default_swipe_layout_get();
190         retv_if(!swipe_layout, LOCK_ERROR_FAIL);
191
192         elm_object_signal_emit(swipe_layout, "show,txt,battery", "txt.battery");
193
194         ret = device_add_callback(DEVICE_CALLBACK_BATTERY_CAPACITY, _battery_changed_cb, NULL);
195         if (ret != DEVICE_ERROR_NONE) {
196                 _E("Failed to add device callback : DEVICE_CALLBACK_BATTERY_CAPACITY(%d)", ret);
197         }
198
199         ret = device_add_callback(DEVICE_CALLBACK_BATTERY_LEVEL, _battery_changed_cb, NULL);
200         if (ret != DEVICE_ERROR_NONE) {
201                 _E("Failed to add device callback : DEVICE_CALLBACK_BATTERY_LEVEL(%d)", ret);
202         }
203
204         return LOCK_ERROR_OK;
205 }
206
207 lock_error_e lock_battery_hide(void)
208 {
209         Evas_Object *swipe_layout = NULL;
210
211         int ret = 0;
212
213         swipe_layout = lock_default_swipe_layout_get();
214         retv_if(!swipe_layout, LOCK_ERROR_FAIL);
215
216         elm_object_signal_emit(swipe_layout, "hide,txt,battery", "txt.battery");
217
218         ret = device_remove_callback(DEVICE_CALLBACK_BATTERY_CAPACITY, _battery_changed_cb);
219         if (ret != DEVICE_ERROR_NONE) {
220                 _E("Failed to remove device callback : DEVICE_CALLBCK_BATTERY_CAPACITY(%d)", ret);
221         }
222
223         ret = device_remove_callback(DEVICE_CALLBACK_BATTERY_LEVEL, _battery_changed_cb);
224         if (ret != DEVICE_ERROR_NONE) {
225                 _E("Failed to remove device callback : DEVICE_CALLBCK_BATTERY_LEVEL(%d)", ret);
226         }
227
228         return LOCK_ERROR_OK;
229 }
230
231 static void _battery_charger_changed_cb(runtime_info_key_e key, void *data)
232 {
233         _D("%s", __func__);
234
235         int ret = 0;
236         bool is_connected = 0;
237
238         ret = lock_property_get_bool(PROPERTY_TYPE_RUNTIME_INFO, (void *)RUNTIME_INFO_KEY_CHARGER_CONNECTED , &is_connected);
239         if (ret != LOCK_ERROR_OK) {
240                 _E("Failed to get runtime info : RUNTIME_INFO_KEY_CHARGER_CONNECTED");
241         }
242
243         _D("charger connected : %d", is_connected);
244         s_info.is_connected = is_connected;
245
246         if (is_connected) {
247                 _D("show battery information");
248                 if (LOCK_ERROR_OK != lock_battery_show()) {
249                         _E("Failed to show battery infomation");
250                 }
251
252                 if (LOCK_ERROR_OK != lock_battery_update()) {
253                         _E("Failed to update battery information");
254                 }
255         } else {
256                 _D("hide battery inforamtion");
257                 if (LOCK_ERROR_OK != lock_battery_hide()) {
258                         _E("Failed to hide battery information");
259                 }
260         }
261 }
262
263 lock_error_e lock_battery_init(void)
264 {
265         int ret = 0;
266
267         ret = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_CHARGER_CONNECTED, _battery_charger_changed_cb, NULL);
268         if (ret != RUNTIME_INFO_ERROR_NONE) {
269                 _E("Failed to set changed cb : RUNTIME_INFO_KEY_CHANGER_CONNECTED(%d)", ret);
270         }
271
272         ret = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_BATTERY_IS_CHARGING, _battery_charger_changed_cb, NULL);
273         if (ret != RUNTIME_INFO_ERROR_NONE) {
274                 _E("Failed to set changed cb : RUNTIME_INFO_KEY_BATTERY_IS_CHARGING(%d)", ret);
275         }
276
277         _battery_charger_changed_cb(RUNTIME_INFO_KEY_CHARGER_CONNECTED, NULL);
278
279         return LOCK_ERROR_OK;
280 }
281
282 void lock_battery_fini(void)
283 {
284         int ret = 0;
285
286         ret = runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_CHARGER_CONNECTED);
287         if (ret != RUNTIME_INFO_ERROR_NONE) {
288                 _E("Failed to set changed cb : RUNTIME_INFO_KEY_CHANGER_CONNECTED(%d)", ret);
289         }
290
291         ret = runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_BATTERY_IS_CHARGING);
292         if (ret != RUNTIME_INFO_ERROR_NONE) {
293                 _E("Failed to set changed cb : RUNTIME_INFO_KEY_BATTERY_IS_CHARGING(%d)", ret);
294         }
295 }