1 /* 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 23 using namespace android; 24 25 namespace 26 { 27 // How often to check battery in seconds 28 const int CHECK_PERIOD = 30; 29 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 36 const String16 serviceName("batteryproperties"); 37 38 sp<IBinder> bs; 39 sp<IBatteryPropertiesRegistrar> interface; 40 41 bool singletonInitialized = false; 42 time_t last_checked = {0}; 43 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 } 85 } 86 87 extern "C" 88 { 89 int is_battery_ok_to_start() 90 { 91 // Bug 16868177 exists to purge this code completely 92 return true; //is_battery_ok(START_THRESHOLD); 93 } 94 95 int is_battery_ok_to_continue() 96 { 97 // Bug 16868177 exists to purge this code completely 98 return true; //is_battery_ok(CONTINUE_THRESHOLD); 99 } 100 } 101