From c0acc17924a121704e799a5924d7e41036d18afe Mon Sep 17 00:00:00 2001 From: Kuan Wang Date: Fri, 24 Jun 2022 15:08:10 +0800 Subject: [PATCH] Add static funciton isCharged in BatteryStatus to check charge status without creating BatteryStatus instance. Bug: 236101531 Test: presubmit Change-Id: I7967693b16275f94eb12edcd695a5199c50cb841 Merged-In: I7967693b16275f94eb12edcd695a5199c50cb841 --- .../settingslib/fuelgauge/BatteryStatus.java | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/SettingsLib/src/com/android/settingslib/fuelgauge/BatteryStatus.java b/packages/SettingsLib/src/com/android/settingslib/fuelgauge/BatteryStatus.java index 4939e04dad6a5..132a631e25cc2 100644 --- a/packages/SettingsLib/src/com/android/settingslib/fuelgauge/BatteryStatus.java +++ b/packages/SettingsLib/src/com/android/settingslib/fuelgauge/BatteryStatus.java @@ -135,7 +135,7 @@ public class BatteryStatus { * @return true if the device is charged */ public boolean isCharged() { - return status == BATTERY_STATUS_FULL || level >= 100; + return isCharged(status, level); } /** @@ -177,4 +177,31 @@ public class BatteryStatus { return "BatteryStatus{status=" + status + ",level=" + level + ",plugged=" + plugged + ",health=" + health + ",maxChargingWattage=" + maxChargingWattage + "}"; } + + /** + * Whether or not the device is charged. Note that some devices never return 100% for + * battery level, so this allows either battery level or status to determine if the + * battery is charged. + * + * @param batteryChangedIntent ACTION_BATTERY_CHANGED intent + * @return true if the device is charged + */ + public static boolean isCharged(Intent batteryChangedIntent) { + int status = batteryChangedIntent.getIntExtra(EXTRA_STATUS, BATTERY_STATUS_UNKNOWN); + int level = batteryChangedIntent.getIntExtra(EXTRA_LEVEL, 0); + return isCharged(status, level); + } + + /** + * Whether or not the device is charged. Note that some devices never return 100% for + * battery level, so this allows either battery level or status to determine if the + * battery is charged. + * + * @param status values for "status" field in the ACTION_BATTERY_CHANGED Intent + * @param level values from 0 to 100 + * @return true if the device is charged + */ + public static boolean isCharged(int status, int level) { + return status == BATTERY_STATUS_FULL || level >= 100; + } }