2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <device/battery.h>
21 #include <device/callback.h>
22 #include <runtime_info.h>
26 static bool is_connected, is_charging;
27 static int level, init_count;
28 int LOCKSCREEN_EVENT_BATTERY_CHANGED;
30 int _battery_status_update()
32 bool model_changed = false;
35 bool charging, connected;
37 ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_CHARGER_CONNECTED, &connected);
38 if (ret != RUNTIME_INFO_ERROR_NONE) {
39 ERR("runtime_info_get_value_bool failed: %s", get_error_message(ret));
42 if (is_connected != connected) {
43 is_connected = connected;
47 ret = device_battery_is_charging(&charging);
48 if (ret != DEVICE_ERROR_NONE) {
49 ERR("device_battery_is_charging failed: %s", get_error_message(ret));
52 if (is_charging != charging) {
53 is_charging = charging;
57 ret = device_battery_get_percent(&percent);
58 if (ret != DEVICE_ERROR_NONE) {
59 ERR("device_battery_get_percent failed: %s", get_error_message(ret));
62 if (level != percent) {
68 ecore_event_add(LOCKSCREEN_EVENT_BATTERY_CHANGED, NULL, NULL, NULL);
73 static void _battery_changed_cb(device_callback_e type, void *value, void *user_data)
75 if (type == DEVICE_CALLBACK_BATTERY_LEVEL || type == DEVICE_CALLBACK_BATTERY_CAPACITY)
76 _battery_status_update();
79 static void _battery_charger_changed_cb(runtime_info_key_e key, void *data)
81 if (key == RUNTIME_INFO_KEY_CHARGER_CONNECTED)
82 _battery_status_update();
85 int lockscreen_battery_init()
88 LOCKSCREEN_EVENT_BATTERY_CHANGED = ecore_event_type_new();
89 int ret = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_CHARGER_CONNECTED, _battery_charger_changed_cb, NULL);
90 if (ret != RUNTIME_INFO_ERROR_NONE) {
91 ERR("runtime_info_set_changed_cb failed: %s", get_error_message(ret));
95 ret = device_add_callback(DEVICE_CALLBACK_BATTERY_CAPACITY, _battery_changed_cb, NULL);
96 if (ret != DEVICE_ERROR_NONE) {
97 ERR("device_add_callback failed: %s", get_error_message(ret));
98 runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_CHARGER_CONNECTED);
102 ret = device_add_callback(DEVICE_CALLBACK_BATTERY_CHARGING, _battery_changed_cb, NULL);
103 if (ret != DEVICE_ERROR_NONE) {
104 ERR("device_add_callback failed: %s", get_error_message(ret));
105 device_remove_callback(DEVICE_CALLBACK_BATTERY_CAPACITY, _battery_changed_cb);
106 runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_CHARGER_CONNECTED);
110 _battery_status_update();
116 void lockscreen_battery_shutdown(void)
121 runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_CHARGER_CONNECTED);
125 bool lockscreen_battery_is_charging(void)
130 bool lockscreen_battery_is_connected(void)
135 int lockscreen_battery_level_get(void)