Merge "Update the conditions of power plugged determination." into main

This commit is contained in:
Treehugger Robot
2024-07-02 11:56:53 +00:00
committed by Android (Google) Code Review
2 changed files with 137 additions and 14 deletions

View File

@@ -307,12 +307,13 @@ public class BatteryInfo {
info.pluggedStatus = batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
info.mCharging = info.pluggedStatus != 0;
info.averageTimeToDischarge = estimate.getAverageDischargeTime();
info.isLongLife =
batteryBroadcast.getIntExtra(
BatteryManager.EXTRA_CHARGING_STATUS,
BatteryManager.CHARGING_POLICY_DEFAULT)
== BatteryManager.CHARGING_POLICY_ADAPTIVE_LONGLIFE;
final int chargingPolicy =
batteryBroadcast.getIntExtra(
BatteryManager.EXTRA_CHARGING_STATUS,
BatteryManager.CHARGING_POLICY_DEFAULT);
info.isLongLife = chargingPolicy == BatteryManager.CHARGING_POLICY_ADAPTIVE_LONGLIFE;
info.statusLabel = Utils.getBatteryStatus(context, batteryBroadcast, isCompactStatus);
info.batteryStatus =
batteryBroadcast.getIntExtra(
@@ -326,7 +327,15 @@ public class BatteryInfo {
.getPowerUsageFeatureProvider()
.isBatteryDefend(info);
}
if (!info.mCharging) {
Log.d(
TAG,
"chargingPolicy = "
+ chargingPolicy
+ ", pluggedStatus = "
+ info.pluggedStatus
+ ", batteryStatus = "
+ info.batteryStatus);
if (!isPluggedIn(context, info.mCharging, chargingPolicy)) {
updateBatteryInfoDischarging(context, shortString, estimate, info);
} else {
updateBatteryInfoCharging(
@@ -556,6 +565,14 @@ public class BatteryInfo {
}
}
private static boolean isPluggedIn(Context context, boolean isCharging, int chargingPolicy) {
return isCharging
|| (chargingPolicy == BatteryManager.CHARGING_POLICY_ADAPTIVE_LONGLIFE
&& FeatureFactory.getFeatureFactory()
.getBatterySettingsFeatureProvider()
.isChargingOptimizationMode(context));
}
public interface BatteryDataParser {
void onParsingStarted(long startTime, long endTime);

View File

@@ -48,6 +48,7 @@ import com.android.settings.testutils.BatteryTestUtils;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.widget.UsageView;
import com.android.settingslib.fuelgauge.Estimate;
import com.android.settingslib.utils.PowerUtil;
import org.junit.After;
import org.junit.Before;
@@ -93,7 +94,8 @@ public class BatteryInfoTest {
Map.of(
ChargingType.WIRED, BatteryManager.BATTERY_PLUGGED_AC,
ChargingType.WIRELESS, BatteryManager.BATTERY_PLUGGED_WIRELESS,
ChargingType.DOCKED, BatteryManager.BATTERY_PLUGGED_DOCK);
ChargingType.DOCKED, BatteryManager.BATTERY_PLUGGED_DOCK,
ChargingType.NONE, 0);
private static final Map<ChargingSpeed, Integer> CHARGING_SPEED_MAP =
Map.of(
ChargingSpeed.FAST, 1501000,
@@ -823,6 +825,92 @@ public class BatteryInfoTest {
assertThat(batteryInfo.isLongLife).isFalse();
}
@Test
public void getBatteryInfo_plugTypeNoneWithLonglifeAndChargeOptimization_chargingString() {
prepareTestGetBatteryInfoEnvironment(
/* remainingTimeMs= */ Duration.ofMinutes(130).toMillis(),
/* chargingStringV2Enabled= */ false);
Intent batteryIntent =
createIntentForGetBatteryInfoTest(
ChargingType.NONE,
ChargingSpeed.REGULAR,
/* batteryLevel= */ 85,
BatteryManager.BATTERY_STATUS_DISCHARGING,
/* isLonglife= */ true);
var expectedRemainingLabel = "Expected remaining label";
var expectedChargeLabel = "85% - " + expectedRemainingLabel;
when(mFeatureFactory.batterySettingsFeatureProvider.isChargingOptimizationMode(mContext))
.thenReturn(true);
when(mFeatureFactory.batterySettingsFeatureProvider.getChargingOptimizationRemainingLabel(
eq(mContext), anyInt(), anyInt(), anyLong(), anyLong()))
.thenReturn(expectedRemainingLabel);
when(mFeatureFactory.batterySettingsFeatureProvider.getChargingOptimizationChargeLabel(
eq(mContext), anyInt(), anyString(), anyLong(), anyLong()))
.thenReturn(expectedChargeLabel);
var expectedStatusLabel = "Not charging";
assertGetBatteryInfo(
batteryIntent,
/* currentTimeMillis= */ UNUSED_TIME_MS,
expectedStatusLabel,
expectedRemainingLabel,
expectedChargeLabel);
}
@Test
public void getBatteryInfo_plugTypeNoneNotChargeOptimizationLonglife_dischargingString() {
prepareTestGetBatteryInfoEnvironment(
/* remainingTimeMs= */ Duration.ofMinutes(130).toMillis(),
/* chargingStringV2Enabled= */ false);
Intent batteryIntent =
createIntentForGetBatteryInfoTest(
ChargingType.NONE,
ChargingSpeed.REGULAR,
/* batteryLevel= */ 85,
BatteryManager.BATTERY_STATUS_DISCHARGING,
/* isLonglife= */ true);
var expectedRemainingLabel =
PowerUtil.getBatteryRemainingShortStringFormatted(
mContext, PowerUtil.convertUsToMs(1000L));
when(mFeatureFactory.batterySettingsFeatureProvider.isChargingOptimizationMode(mContext))
.thenReturn(false);
var expectedStatusLabel = "Not charging";
assertGetBatteryInfo(
batteryIntent,
/* currentTimeMillis= */ UNUSED_TIME_MS,
expectedStatusLabel,
expectedRemainingLabel,
expectedRemainingLabel);
}
@Test
public void getBatteryInfo_plugTypeNoneChargeOptimizationNotLonglife_dischargingString() {
prepareTestGetBatteryInfoEnvironment(
/* remainingTimeMs= */ Duration.ofMinutes(130).toMillis(),
/* chargingStringV2Enabled= */ false);
Intent batteryIntent =
createIntentForGetBatteryInfoTest(
ChargingType.NONE,
ChargingSpeed.REGULAR,
/* batteryLevel= */ 85,
BatteryManager.BATTERY_STATUS_DISCHARGING,
/* isLonglife= */ false);
var expectedRemainingLabel =
PowerUtil.getBatteryRemainingShortStringFormatted(
mContext, PowerUtil.convertUsToMs(1000L));
when(mFeatureFactory.batterySettingsFeatureProvider.isChargingOptimizationMode(mContext))
.thenReturn(true);
var expectedStatusLabel = "Not charging";
assertGetBatteryInfo(
batteryIntent,
/* currentTimeMillis= */ UNUSED_TIME_MS,
expectedStatusLabel,
expectedRemainingLabel,
expectedRemainingLabel);
}
private enum ChargingSpeed {
FAST,
REGULAR,
@@ -832,10 +920,11 @@ public class BatteryInfoTest {
private enum ChargingType {
WIRED,
WIRELESS,
DOCKED
DOCKED,
NONE
}
private Intent createIntentForLongLifeTest(Boolean hasLongLife) {
private static Intent createIntentForLongLifeTest(Boolean hasLongLife) {
return new Intent(Intent.ACTION_BATTERY_CHANGED)
.putExtra(
BatteryManager.EXTRA_CHARGING_STATUS,
@@ -844,16 +933,33 @@ public class BatteryInfoTest {
: BatteryManager.CHARGING_POLICY_DEFAULT);
}
private Intent createIntentForGetBatteryInfoTest(
private static Intent createIntentForGetBatteryInfoTest(
ChargingType chargingType, ChargingSpeed chargingSpeed, int batteryLevel) {
return createIntentForGetBatteryInfoTest(
chargingType,
chargingSpeed,
batteryLevel,
BatteryManager.BATTERY_STATUS_CHARGING,
/* isLonglife= */ false);
}
private static Intent createIntentForGetBatteryInfoTest(
ChargingType chargingType,
ChargingSpeed chargingSpeed,
int batteryLevel,
int chargingStatus,
boolean isLonglife) {
return createBatteryIntent(
CHARGING_TYPE_MAP.get(chargingType),
batteryLevel,
BatteryManager.BATTERY_STATUS_CHARGING)
CHARGING_TYPE_MAP.get(chargingType), batteryLevel, chargingStatus)
.putExtra(
BatteryManager.EXTRA_MAX_CHARGING_CURRENT,
CHARGING_SPEED_MAP.get(chargingSpeed))
.putExtra(BatteryManager.EXTRA_MAX_CHARGING_VOLTAGE, 5000000);
.putExtra(BatteryManager.EXTRA_MAX_CHARGING_VOLTAGE, 5000000)
.putExtra(
BatteryManager.EXTRA_CHARGING_STATUS,
isLonglife
? BatteryManager.CHARGING_POLICY_ADAPTIVE_LONGLIFE
: BatteryManager.CHARGING_POLICY_DEFAULT);
}
private void prepareTestGetBatteryInfoEnvironment(