summaryrefslogtreecommitdiffstats
path: root/recovery.cpp
diff options
context:
space:
mode:
authorTao Bao <tbao@google.com>2019-10-03 18:14:34 +0200
committerandroid-build-merger <android-build-merger@google.com>2019-10-03 18:14:34 +0200
commitc2e4f011cedfadfae7d0c445a4b61f6522432c66 (patch)
treebcea6887c7633fac75f63ab9a9172a88bfccc158 /recovery.cpp
parentMerge "otautil: Factor out the utils that're private to recovery." am: c6fb9f9df5 (diff)
parentMerge "Refactor battery info querying functions into librecovery_utils." (diff)
downloadandroid_bootable_recovery-c2e4f011cedfadfae7d0c445a4b61f6522432c66.tar
android_bootable_recovery-c2e4f011cedfadfae7d0c445a4b61f6522432c66.tar.gz
android_bootable_recovery-c2e4f011cedfadfae7d0c445a4b61f6522432c66.tar.bz2
android_bootable_recovery-c2e4f011cedfadfae7d0c445a4b61f6522432c66.tar.lz
android_bootable_recovery-c2e4f011cedfadfae7d0c445a4b61f6522432c66.tar.xz
android_bootable_recovery-c2e4f011cedfadfae7d0c445a4b61f6522432c66.tar.zst
android_bootable_recovery-c2e4f011cedfadfae7d0c445a4b61f6522432c66.zip
Diffstat (limited to 'recovery.cpp')
-rw-r--r--recovery.cpp87
1 files changed, 14 insertions, 73 deletions
diff --git a/recovery.cpp b/recovery.cpp
index 682ddbc4c..f59a940fc 100644
--- a/recovery.cpp
+++ b/recovery.cpp
@@ -42,7 +42,6 @@
#include <android-base/strings.h>
#include <cutils/properties.h> /* for property_list */
#include <fs_mgr/roots.h>
-#include <healthhalutils/HealthHalUtils.h>
#include <ziparchive/zip_archive.h>
#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<IHealth> 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::vector<std::stri
// to log the update attempt since update_package is non-NULL.
save_current_log = true;
- int required_battery_level;
- if (retry_count == 0 && !is_battery_ok(&required_battery_level)) {
+ if (int required_battery_level; retry_count == 0 && !IsBatteryOk(&required_battery_level)) {
ui->Print("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()) {