From 832c9cd24fcff55da0460b8587a1c5e6145e8ab5 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Fri, 27 Sep 2019 23:32:00 -0700 Subject: Refactor battery info querying functions into librecovery_utils. Bug: 134560109 Test: Run recovery_unit_test. Change-Id: Ibbcdcfd507fa23657ee7ff677208b0003ec382ba --- recovery.cpp | 87 ++++++++++-------------------------------------------------- 1 file changed, 14 insertions(+), 73 deletions(-) (limited to 'recovery.cpp') diff --git a/recovery.cpp b/recovery.cpp index 682ddbc4c..f59a940fc 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -42,7 +42,6 @@ #include #include /* for property_list */ #include -#include #include #include "bootloader_message/bootloader_message.h" @@ -59,6 +58,7 @@ #include "otautil/sysutil.h" #include "recovery_ui/screen_ui.h" #include "recovery_ui/ui.h" +#include "recovery_utils/battery_utils.h" #include "recovery_utils/logging.h" #include "recovery_utils/roots.h" @@ -453,74 +453,17 @@ static void print_property(const char* key, const char* name, void* /* cookie */ printf("%s=%s\n", key, name); } -static bool is_battery_ok(int* required_battery_level) { - using android::hardware::health::V1_0::BatteryStatus; - using android::hardware::health::V2_0::get_health_service; - using android::hardware::health::V2_0::IHealth; - using android::hardware::health::V2_0::Result; - using android::hardware::health::V2_0::toString; - - android::sp health = get_health_service(); - - static constexpr int BATTERY_READ_TIMEOUT_IN_SEC = 10; - int wait_second = 0; - while (true) { - auto charge_status = BatteryStatus::UNKNOWN; - - if (health == nullptr) { - LOG(WARNING) << "no health implementation is found, assuming defaults"; - } else { - health - ->getChargeStatus([&charge_status](auto res, auto out_status) { - if (res == Result::SUCCESS) { - charge_status = out_status; - } - }) - .isOk(); // should not have transport error - } - - // Treat unknown status as charged. - bool charged = (charge_status != BatteryStatus::DISCHARGING && - charge_status != BatteryStatus::NOT_CHARGING); - - Result res = Result::UNKNOWN; - int32_t capacity = INT32_MIN; - if (health != nullptr) { - health - ->getCapacity([&res, &capacity](auto out_res, auto out_capacity) { - res = out_res; - capacity = out_capacity; - }) - .isOk(); // should not have transport error - } - - LOG(INFO) << "charge_status " << toString(charge_status) << ", charged " << charged - << ", status " << toString(res) << ", capacity " << capacity; - // At startup, the battery drivers in devices like N5X/N6P take some time to load - // the battery profile. Before the load finishes, it reports value 50 as a fake - // capacity. BATTERY_READ_TIMEOUT_IN_SEC is set that the battery drivers are expected - // to finish loading the battery profile earlier than 10 seconds after kernel startup. - if (res == Result::SUCCESS && capacity == 50) { - if (wait_second < BATTERY_READ_TIMEOUT_IN_SEC) { - sleep(1); - wait_second++; - continue; - } - } - // If we can't read battery percentage, it may be a device without battery. In this - // situation, use 100 as a fake battery percentage. - if (res != Result::SUCCESS) { - capacity = 100; - } - - // GmsCore enters recovery mode to install package when having enough battery percentage. - // Normally, the threshold is 40% without charger and 20% with charger. So we should check - // battery with a slightly lower limitation. - static constexpr int BATTERY_OK_PERCENTAGE = 20; - static constexpr int BATTERY_WITH_CHARGER_OK_PERCENTAGE = 15; - *required_battery_level = charged ? BATTERY_WITH_CHARGER_OK_PERCENTAGE : BATTERY_OK_PERCENTAGE; - return capacity >= *required_battery_level; - } +static bool IsBatteryOk(int* required_battery_level) { + // GmsCore enters recovery mode to install package when having enough battery percentage. + // Normally, the threshold is 40% without charger and 20% with charger. So we check the battery + // level against a slightly lower limit. + constexpr int BATTERY_OK_PERCENTAGE = 20; + constexpr int BATTERY_WITH_CHARGER_OK_PERCENTAGE = 15; + + auto battery_info = GetBatteryInfo(); + *required_battery_level = + battery_info.charging ? BATTERY_WITH_CHARGER_OK_PERCENTAGE : BATTERY_OK_PERCENTAGE; + return battery_info.capacity >= *required_battery_level; } // Set the retry count to |retry_count| in BCB. @@ -713,12 +656,10 @@ Device::BuiltinAction start_recovery(Device* device, const std::vectorPrint("battery capacity is not enough for installing package: %d%% needed\n", required_battery_level); - // Log the error code to last_install when installation skips due to - // low battery. + // Log the error code to last_install when installation skips due to low battery. log_failure_code(kLowBattery, update_package); status = INSTALL_SKIPPED; } else if (retry_count == 0 && bootreason_in_blacklist()) { -- cgit v1.2.3