summaryrefslogtreecommitdiff
path: root/packages/SettingsLib/src
diff options
context:
space:
mode:
author ykhung <ykhung@google.com> 2023-02-08 13:29:37 +0800
committer ykhung <ykhung@google.com> 2023-02-09 10:57:25 +0800
commit1852542d3e216468654a809fb5bccdbef865760b (patch)
tree25ad421d96ceaee1bfbb55539c50995fc506eebb /packages/SettingsLib/src
parent72d66da389fcc5182ec7c0a8a4d9d53ba8ac3015 (diff)
Add a new create() for BatteryStatus without battery intent
Add a new create() method for SystemUI incompatible charging event to update the AOD string and move the similar implementation in the Settings to the centralized BatteryUtils Bug: N/A Test: presubmit Change-Id: Ief31fad50648de73e5e0a3c5138e13e0acc565c7
Diffstat (limited to 'packages/SettingsLib/src')
-rw-r--r--packages/SettingsLib/src/com/android/settingslib/fuelgauge/BatteryStatus.java49
-rw-r--r--packages/SettingsLib/src/com/android/settingslib/fuelgauge/BatteryUtils.java30
2 files changed, 43 insertions, 36 deletions
diff --git a/packages/SettingsLib/src/com/android/settingslib/fuelgauge/BatteryStatus.java b/packages/SettingsLib/src/com/android/settingslib/fuelgauge/BatteryStatus.java
index 8b68a09bbf65..e0588ee9cfeb 100644
--- a/packages/SettingsLib/src/com/android/settingslib/fuelgauge/BatteryStatus.java
+++ b/packages/SettingsLib/src/com/android/settingslib/fuelgauge/BatteryStatus.java
@@ -53,6 +53,12 @@ public class BatteryStatus {
public final int maxChargingWattage;
public final boolean present;
+ public static BatteryStatus create(Context context) {
+ final Intent batteryChangedIntent = BatteryUtils.getBatteryIntent(context);
+ return batteryChangedIntent == null
+ ? null : new BatteryStatus(batteryChangedIntent);
+ }
+
public BatteryStatus(int status, int level, int plugged, int health,
int maxChargingWattage, boolean present) {
this.status = status;
@@ -87,11 +93,7 @@ public class BatteryStatus {
}
}
- /**
- * Determine whether the device is plugged in (USB, power, wireless or dock).
- *
- * @return true if the device is plugged in.
- */
+ /** Determine whether the device is plugged. */
public boolean isPluggedIn() {
return plugged == BatteryManager.BATTERY_PLUGGED_AC
|| plugged == BatteryManager.BATTERY_PLUGGED_USB
@@ -99,30 +101,19 @@ public class BatteryStatus {
|| plugged == BatteryManager.BATTERY_PLUGGED_DOCK;
}
- /**
- * Determine whether the device is plugged in (USB, power).
- *
- * @return true if the device is plugged in wired (as opposed to wireless)
- */
+ /** Determine whether the device is plugged in (USB, power). */
public boolean isPluggedInWired() {
return plugged == BatteryManager.BATTERY_PLUGGED_AC
|| plugged == BatteryManager.BATTERY_PLUGGED_USB;
}
/**
- * Determine whether the device is plugged in wireless.
- *
- * @return true if the device is plugged in wireless
- */
+ * Determine whether the device is plugged in wireless. */
public boolean isPluggedInWireless() {
return plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
}
- /**
- * Determine whether the device is plugged in dock.
- *
- * @return true if the device is plugged in dock
- */
+ /** Determine whether the device is plugged in dock. */
public boolean isPluggedInDock() {
return plugged == BatteryManager.BATTERY_PLUGGED_DOCK;
}
@@ -131,36 +122,22 @@ public class BatteryStatus {
* 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.
- *
- * @return true if the device is charged
*/
public boolean isCharged() {
return isCharged(status, level);
}
- /**
- * Whether battery is low and needs to be charged.
- *
- * @return true if battery is low
- */
+ /** Whether battery is low and needs to be charged. */
public boolean isBatteryLow() {
return level < LOW_BATTERY_THRESHOLD;
}
- /**
- * Whether battery is overheated.
- *
- * @return true if battery is overheated
- */
+ /** Whether battery is overheated. */
public boolean isOverheated() {
return health == BATTERY_HEALTH_OVERHEAT;
}
- /**
- * Return current chargin speed is fast, slow or normal.
- *
- * @return the charing speed
- */
+ /** Return current chargin speed is fast, slow or normal. */
public final int getChargingSpeed(Context context) {
final int slowThreshold = context.getResources().getInteger(
R.integer.config_chargingSlowlyThreshold);
diff --git a/packages/SettingsLib/src/com/android/settingslib/fuelgauge/BatteryUtils.java b/packages/SettingsLib/src/com/android/settingslib/fuelgauge/BatteryUtils.java
new file mode 100644
index 000000000000..ad9886e1b0a1
--- /dev/null
+++ b/packages/SettingsLib/src/com/android/settingslib/fuelgauge/BatteryUtils.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settingslib.fuelgauge;
+
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+
+public final class BatteryUtils {
+
+ /** Gets the latest sticky battery intent from the Android system. */
+ public static Intent getBatteryIntent(Context context) {
+ return context.registerReceiver(
+ /*receiver=*/ null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
+ }
+}