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
This commit is contained in:
ykhung
2023-02-08 13:29:37 +08:00
parent 72d66da389
commit 1852542d3e
2 changed files with 43 additions and 36 deletions

View File

@@ -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);

View File

@@ -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));
}
}