blob: f689bb46ca31932400eae7902935ee8e9be61347 [file] [log] [blame]
Paul Lawrence87999172014-02-20 12:21:31 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
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#define LOG_TAG "VoldCheckBattery"
18#include <cutils/log.h>
19
20#include <binder/IServiceManager.h>
21#include <batteryservice/IBatteryPropertiesRegistrar.h>
22
23using namespace android;
24
25namespace
26{
27 // How often to check battery in seconds
28 const int CHECK_PERIOD = 30;
Paul Lawrence73d7a022014-06-09 14:10:09 -070029
30 // How charged should the battery be (percent) to start encrypting
31 const int START_THRESHOLD = 10;
32
33 // How charged should the battery be (percent) to continue encrypting
34 const int CONTINUE_THRESHOLD = 5;
35
Paul Lawrence87999172014-02-20 12:21:31 -080036 const String16 serviceName("batteryproperties");
37
38 sp<IBinder> bs;
39 sp<IBatteryPropertiesRegistrar> interface;
40
41 bool singletonInitialized = false;
42 time_t last_checked = {0};
Paul Lawrence73d7a022014-06-09 14:10:09 -070043 int last_result = 100;
44
45 int is_battery_ok(int threshold)
46 {
47 time_t now = time(NULL);
48 if (now == -1 || difftime(now, last_checked) < 5) {
49 goto finish;
50 }
51 last_checked = now;
52
53 if (!singletonInitialized) {
54 bs = defaultServiceManager()->checkService(serviceName);
55 if (bs == NULL) {
56 SLOGE("No batteryproperties service!");
57 goto finish;
58 }
59
60 interface = interface_cast<IBatteryPropertiesRegistrar>(bs);
61 if (interface == NULL) {
62 SLOGE("No IBatteryPropertiesRegistrar interface");
63 goto finish;
64 }
65
66 singletonInitialized = true;
67 }
68
69 {
70 BatteryProperty val;
71 status_t status = interface
72 ->getProperty(android::BATTERY_PROP_CAPACITY, &val);
73 if (status == NO_ERROR) {
74 SLOGD("Capacity is %d", (int)val.valueInt64);
75 last_result = val.valueInt64;
76 } else {
77 SLOGE("Failed to get battery charge");
78 last_result = 100;
79 }
80 }
81
82 finish:
83 return last_result >= threshold;
84 }
Paul Lawrence87999172014-02-20 12:21:31 -080085}
86
Paul Lawrence73d7a022014-06-09 14:10:09 -070087extern "C"
Paul Lawrence87999172014-02-20 12:21:31 -080088{
Paul Lawrence73d7a022014-06-09 14:10:09 -070089 int is_battery_ok_to_start()
90 {
91 return is_battery_ok(START_THRESHOLD);
Paul Lawrence87999172014-02-20 12:21:31 -080092 }
93
Paul Lawrence73d7a022014-06-09 14:10:09 -070094 int is_battery_ok_to_continue()
95 {
96 return is_battery_ok(CONTINUE_THRESHOLD);
Paul Lawrence87999172014-02-20 12:21:31 -080097 }
Paul Lawrence87999172014-02-20 12:21:31 -080098}