Merge "Customize the remaining time label during wireless charging." into main

This commit is contained in:
Yiling Chuang
2024-04-23 02:40:06 +00:00
committed by Android (Google) Code Review
5 changed files with 115 additions and 3 deletions

View File

@@ -396,7 +396,11 @@ public class BatteryInfo {
chargeTimeMs <= 0 chargeTimeMs <= 0
? null ? null
: getPowerRemainingChargingLabel( : getPowerRemainingChargingLabel(
context, chargeTimeMs, info.isFastCharging, currentTimeMs); context,
chargeTimeMs,
info.isFastCharging,
info.pluggedStatus,
currentTimeMs);
info.chargeLabel = info.chargeLabel =
chargeTimeMs <= 0 chargeTimeMs <= 0
@@ -428,7 +432,21 @@ public class BatteryInfo {
} }
private static CharSequence getPowerRemainingChargingLabel( private static CharSequence getPowerRemainingChargingLabel(
Context context, long remainingTimeMs, boolean isFastCharging, long currentTimeMs) { Context context,
long remainingTimeMs,
boolean isFastCharging,
int pluggedStatus,
long currentTimeMs) {
if (pluggedStatus == BatteryManager.BATTERY_PLUGGED_WIRELESS) {
BatterySettingsFeatureProvider provider =
FeatureFactory.getFeatureFactory().getBatterySettingsFeatureProvider();
final CharSequence wirelessChargingRemainingLabel =
provider.getWirelessChargingRemainingLabel(
context, remainingTimeMs, currentTimeMs);
if (wirelessChargingRemainingLabel != null) {
return wirelessChargingRemainingLabel;
}
}
if (com.android.settingslib.fuelgauge.BatteryUtils.isChargingStringV2Enabled()) { if (com.android.settingslib.fuelgauge.BatteryUtils.isChargingStringV2Enabled()) {
int chargeLabelResId = int chargeLabelResId =
isFastCharging isFastCharging

View File

@@ -48,4 +48,9 @@ public interface BatterySettingsFeatureProvider {
/** Return a label for the bottom summary during wireless charging. */ /** Return a label for the bottom summary during wireless charging. */
@Nullable @Nullable
CharSequence getWirelessChargingLabel(@NonNull Context context, @NonNull BatteryInfo info); CharSequence getWirelessChargingLabel(@NonNull Context context, @NonNull BatteryInfo info);
/** Return a remaining time label for wireless charging. */
@Nullable
CharSequence getWirelessChargingRemainingLabel(
@NonNull Context context, long remainingTimeMs, long currentTimeMs);
} }

View File

@@ -60,4 +60,11 @@ public class BatterySettingsFeatureProviderImpl implements BatterySettingsFeatur
@NonNull Context context, @NonNull BatteryInfo info) { @NonNull Context context, @NonNull BatteryInfo info) {
return null; return null;
} }
@Nullable
@Override
public CharSequence getWirelessChargingRemainingLabel(
@NonNull Context context, long remainingTimeMs, long currentTimeMs) {
return null;
}
} }

View File

@@ -21,6 +21,7 @@ import static com.google.common.truth.Truth.assertWithMessage;
import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.any; import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
@@ -634,6 +635,81 @@ public class BatteryInfoTest {
expectedChargeLabel); expectedChargeLabel);
} }
@Test
public void
getBatteryInfo_customizedWirelessChargingLabel_updateRemainingLabelAndStatusLabel() {
prepareTestGetBatteryInfoEnvironment(
/* remainingTimeMs= */ Duration.ofHours(1).toMillis(),
/* chargingStringV2Enabled= */ true);
Intent batteryIntent =
createIntentForGetBatteryInfoTest(
ChargingType.WIRELESS, ChargingSpeed.REGULAR, /* batteryLevel= */ 45);
CharSequence expectedLabel = "Full by 8:00 AM";
when(mFeatureFactory.batterySettingsFeatureProvider.getWirelessChargingRemainingLabel(
eq(mContext), anyLong(), anyLong()))
.thenReturn(expectedLabel);
var currentTimeMillis = Instant.parse("2021-02-09T13:00:00.00Z").toEpochMilli();
var info =
BatteryInfo.getBatteryInfo(
mContext,
batteryIntent,
mBatteryUsageStats,
MOCK_ESTIMATE,
/* elapsedRealtimeUs= */ UNUSED_TIME_MS,
/* shortString= */ false,
/* currentTimeMillis= */ currentTimeMillis);
assertThat(info.remainingLabel).isEqualTo(expectedLabel);
}
@Test
public void
getBatteryInfo_noCustomizedWirelessChargingLabel_updateRemainingLabelAndStatusLabel() {
prepareTestGetBatteryInfoEnvironment(
/* remainingTimeMs= */ Duration.ofHours(1).toMillis(),
/* chargingStringV2Enabled= */ true);
Intent batteryIntent =
createIntentForGetBatteryInfoTest(
ChargingType.WIRELESS, ChargingSpeed.REGULAR, /* batteryLevel= */ 45);
when(mFeatureFactory.batterySettingsFeatureProvider.getWirelessChargingRemainingLabel(
eq(mContext), anyLong(), anyLong()))
.thenReturn(null);
var expectedStatusLabel = "Charging";
var expectedRemainingLabel = "Fully charged by";
var expectedChargeLabel = "45% - " + expectedRemainingLabel;
var currentTimeMillis = Instant.parse("2024-04-01T15:00:00Z").toEpochMilli();
assertGetBatteryInfo(
batteryIntent,
currentTimeMillis,
expectedStatusLabel,
expectedRemainingLabel,
expectedChargeLabel);
}
@Test
public void getBatteryInfo_noCustomWirelessChargingLabelWithV1_updateRemainingAndStatusLabel() {
prepareTestGetBatteryInfoEnvironment(
/* remainingTimeMs= */ Duration.ofMinutes(130).toMillis(),
/* chargingStringV2Enabled= */ false);
Intent batteryIntent =
createIntentForGetBatteryInfoTest(
ChargingType.WIRELESS, ChargingSpeed.REGULAR, /* batteryLevel= */ 10);
when(mFeatureFactory.batterySettingsFeatureProvider.getWirelessChargingRemainingLabel(
eq(mContext), anyLong(), anyLong()))
.thenReturn(null);
var expectedStatusLabel = "Charging wirelessly";
var expectedRemainingLabel = "2 hr, 10 min left until full";
var expectedChargeLabel = "10% - " + expectedRemainingLabel;
assertGetBatteryInfo(
batteryIntent,
/* currentTimeMillis= */ UNUSED_TIME_MS,
expectedStatusLabel,
expectedRemainingLabel,
expectedChargeLabel);
}
private enum ChargingSpeed { private enum ChargingSpeed {
FAST, FAST,
REGULAR, REGULAR,

View File

@@ -70,7 +70,13 @@ public class BatterySettingsFeatureProviderImplTest {
assertThat(expectedResult).isTrue(); assertThat(expectedResult).isTrue();
} }
@Test void getWirelessChargingLabel_returnNull() { @Test
public void getWirelessChargingLabel_returnNull() {
assertThat(mImpl.getWirelessChargingLabel(mContext, new BatteryInfo())).isNull(); assertThat(mImpl.getWirelessChargingLabel(mContext, new BatteryInfo())).isNull();
} }
@Test
public void getWirelessChargingRemainingLabel_returnNull() {
assertThat(mImpl.getWirelessChargingRemainingLabel(mContext, 1000L, 1000L)).isNull();
}
} }