Merge "Fix b/279120121: Unplug 100% Pixel phone won't reset the battery usage data when the phone is just started." into udc-dev am: ea3a970826

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Settings/+/22779519

Change-Id: Ia19da83e61ec2bfbfbbb61110c605c401dd0295e
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Treehugger Robot
2023-04-25 04:12:11 +00:00
committed by Automerger Merge Worker
2 changed files with 30 additions and 10 deletions

View File

@@ -106,9 +106,13 @@ public final class BatteryUsageBroadcastReceiver extends BroadcastReceiver {
return; return;
} }
final boolean delayHourlyJobWhenBooting =
FeatureFactory.getFactory(context)
.getPowerUsageFeatureProvider(context)
.delayHourlyJobWhenBooting();
final long broadcastDelay = sBroadcastDelayFromBoot - SystemClock.elapsedRealtime(); final long broadcastDelay = sBroadcastDelayFromBoot - SystemClock.elapsedRealtime();
// If current boot time is smaller than expected delay, cancel sending the broadcast. // If current boot time is smaller than expected delay, cancel sending the broadcast.
if (broadcastDelay > 0) { if (delayHourlyJobWhenBooting && broadcastDelay > 0) {
Log.d(TAG, "cancel sendBroadcastToFetchUsageData when broadcastDelay is " Log.d(TAG, "cancel sendBroadcastToFetchUsageData when broadcastDelay is "
+ broadcastDelay + "ms."); + broadcastDelay + "ms.");
return; return;

View File

@@ -68,9 +68,11 @@ public final class BatteryUsageBroadcastReceiverTest {
} }
@Test @Test
public void onReceive_actionBatteryLevelChanged_notFetchUsageData_notFullCharged() { public void onReceive_aospNotFullCharged_notFetchUsageData() {
when(mFakeFeatureFactory.powerUsageFeatureProvider.getFullChargeIntentAction()) when(mFakeFeatureFactory.powerUsageFeatureProvider.getFullChargeIntentAction())
.thenReturn(Intent.ACTION_BATTERY_LEVEL_CHANGED); .thenReturn(Intent.ACTION_BATTERY_LEVEL_CHANGED);
when(mFakeFeatureFactory.powerUsageFeatureProvider.delayHourlyJobWhenBooting())
.thenReturn(true);
doReturn(getBatteryIntent(/*level=*/ 20, BatteryManager.BATTERY_STATUS_UNKNOWN)) doReturn(getBatteryIntent(/*level=*/ 20, BatteryManager.BATTERY_STATUS_UNKNOWN))
.when(mContext).registerReceiver(any(), any()); .when(mContext).registerReceiver(any(), any());
@@ -82,9 +84,11 @@ public final class BatteryUsageBroadcastReceiverTest {
} }
@Test @Test
public void onReceive_actionBatteryLevelChanged_notFetchUsageData_nearBooting() { public void onReceive_aospNearBooting_notFetchUsageData() {
when(mFakeFeatureFactory.powerUsageFeatureProvider.getFullChargeIntentAction()) when(mFakeFeatureFactory.powerUsageFeatureProvider.getFullChargeIntentAction())
.thenReturn(Intent.ACTION_BATTERY_LEVEL_CHANGED); .thenReturn(Intent.ACTION_BATTERY_LEVEL_CHANGED);
when(mFakeFeatureFactory.powerUsageFeatureProvider.delayHourlyJobWhenBooting())
.thenReturn(true);
// Make sure isCharged returns true. // Make sure isCharged returns true.
doReturn(getBatteryIntent(/*level=*/ 100, BatteryManager.BATTERY_STATUS_FULL)) doReturn(getBatteryIntent(/*level=*/ 100, BatteryManager.BATTERY_STATUS_FULL))
.when(mContext).registerReceiver(any(), any()); .when(mContext).registerReceiver(any(), any());
@@ -100,9 +104,11 @@ public final class BatteryUsageBroadcastReceiverTest {
} }
@Test @Test
public void onReceive_actionBatteryLevelChanged_notFetchUsageData_wrongAction() { public void onReceive_aospWrongAction_notFetchUsageData() {
when(mFakeFeatureFactory.powerUsageFeatureProvider.getFullChargeIntentAction()) when(mFakeFeatureFactory.powerUsageFeatureProvider.getFullChargeIntentAction())
.thenReturn(Intent.ACTION_POWER_DISCONNECTED); .thenReturn(Intent.ACTION_POWER_DISCONNECTED);
when(mFakeFeatureFactory.powerUsageFeatureProvider.delayHourlyJobWhenBooting())
.thenReturn(true);
// Make sure isCharged returns true. // Make sure isCharged returns true.
doReturn(getBatteryIntent(/*level=*/ 100, BatteryManager.BATTERY_STATUS_UNKNOWN)) doReturn(getBatteryIntent(/*level=*/ 100, BatteryManager.BATTERY_STATUS_UNKNOWN))
.when(mContext).registerReceiver(any(), any()); .when(mContext).registerReceiver(any(), any());
@@ -117,9 +123,11 @@ public final class BatteryUsageBroadcastReceiverTest {
} }
@Test @Test
public void onReceive_actionBatteryLevelChanged_fetchUsageData() { public void onReceive_aospNormal_fetchUsageData() {
when(mFakeFeatureFactory.powerUsageFeatureProvider.getFullChargeIntentAction()) when(mFakeFeatureFactory.powerUsageFeatureProvider.getFullChargeIntentAction())
.thenReturn(Intent.ACTION_BATTERY_LEVEL_CHANGED); .thenReturn(Intent.ACTION_BATTERY_LEVEL_CHANGED);
when(mFakeFeatureFactory.powerUsageFeatureProvider.delayHourlyJobWhenBooting())
.thenReturn(true);
// Make sure isCharged returns true. // Make sure isCharged returns true.
doReturn(getBatteryIntent(/*level=*/ 100, BatteryManager.BATTERY_STATUS_UNKNOWN)) doReturn(getBatteryIntent(/*level=*/ 100, BatteryManager.BATTERY_STATUS_UNKNOWN))
.when(mContext).registerReceiver(any(), any()); .when(mContext).registerReceiver(any(), any());
@@ -134,9 +142,11 @@ public final class BatteryUsageBroadcastReceiverTest {
} }
@Test @Test
public void onReceive_actionBatteryUnplugging_notFetchUsageData_notFullCharged() { public void onReceive_pixelNotFullCharged_notFetchUsageData() {
when(mFakeFeatureFactory.powerUsageFeatureProvider.getFullChargeIntentAction()) when(mFakeFeatureFactory.powerUsageFeatureProvider.getFullChargeIntentAction())
.thenReturn(Intent.ACTION_POWER_DISCONNECTED); .thenReturn(Intent.ACTION_POWER_DISCONNECTED);
when(mFakeFeatureFactory.powerUsageFeatureProvider.delayHourlyJobWhenBooting())
.thenReturn(false);
doReturn(getBatteryIntent(/*level=*/ 20, BatteryManager.BATTERY_STATUS_UNKNOWN)) doReturn(getBatteryIntent(/*level=*/ 20, BatteryManager.BATTERY_STATUS_UNKNOWN))
.when(mContext).registerReceiver(any(), any()); .when(mContext).registerReceiver(any(), any());
@@ -148,9 +158,11 @@ public final class BatteryUsageBroadcastReceiverTest {
} }
@Test @Test
public void onReceive_actionBatteryUnplugging_notFetchUsageData_nearBooting() { public void onReceive_pixelNearBooting_fetchUsageData() {
when(mFakeFeatureFactory.powerUsageFeatureProvider.getFullChargeIntentAction()) when(mFakeFeatureFactory.powerUsageFeatureProvider.getFullChargeIntentAction())
.thenReturn(Intent.ACTION_POWER_DISCONNECTED); .thenReturn(Intent.ACTION_POWER_DISCONNECTED);
when(mFakeFeatureFactory.powerUsageFeatureProvider.delayHourlyJobWhenBooting())
.thenReturn(false);
// Make sure isCharged returns true. // Make sure isCharged returns true.
doReturn(getBatteryIntent(/*level=*/ 100, BatteryManager.BATTERY_STATUS_FULL)) doReturn(getBatteryIntent(/*level=*/ 100, BatteryManager.BATTERY_STATUS_FULL))
.when(mContext).registerReceiver(any(), any()); .when(mContext).registerReceiver(any(), any());
@@ -161,14 +173,16 @@ public final class BatteryUsageBroadcastReceiverTest {
mBatteryUsageBroadcastReceiver.onReceive(mContext, mBatteryUsageBroadcastReceiver.onReceive(mContext,
new Intent(BatteryUsageBroadcastReceiver.ACTION_BATTERY_UNPLUGGING)); new Intent(BatteryUsageBroadcastReceiver.ACTION_BATTERY_UNPLUGGING));
assertThat(mBatteryUsageBroadcastReceiver.mFetchBatteryUsageData).isFalse(); assertThat(mBatteryUsageBroadcastReceiver.mFetchBatteryUsageData).isTrue();
assertSharedPreferences(BatteryUsageBroadcastReceiver.ACTION_BATTERY_UNPLUGGING); assertSharedPreferences(BatteryUsageBroadcastReceiver.ACTION_BATTERY_UNPLUGGING);
} }
@Test @Test
public void onReceive_actionBatteryUnplugging_notFetchUsageData_wrongAction() { public void onReceive_pixelWrongAction_notFetchUsageData() {
when(mFakeFeatureFactory.powerUsageFeatureProvider.getFullChargeIntentAction()) when(mFakeFeatureFactory.powerUsageFeatureProvider.getFullChargeIntentAction())
.thenReturn(Intent.ACTION_BATTERY_LEVEL_CHANGED); .thenReturn(Intent.ACTION_BATTERY_LEVEL_CHANGED);
when(mFakeFeatureFactory.powerUsageFeatureProvider.delayHourlyJobWhenBooting())
.thenReturn(false);
// Make sure isCharged returns true. // Make sure isCharged returns true.
doReturn(getBatteryIntent(/*level=*/ 100, BatteryManager.BATTERY_STATUS_UNKNOWN)) doReturn(getBatteryIntent(/*level=*/ 100, BatteryManager.BATTERY_STATUS_UNKNOWN))
.when(mContext).registerReceiver(any(), any()); .when(mContext).registerReceiver(any(), any());
@@ -183,9 +197,11 @@ public final class BatteryUsageBroadcastReceiverTest {
} }
@Test @Test
public void onReceive_actionBatteryUnplugging_fetchUsageData() { public void onReceive_pixelNormal_fetchUsageData() {
when(mFakeFeatureFactory.powerUsageFeatureProvider.getFullChargeIntentAction()) when(mFakeFeatureFactory.powerUsageFeatureProvider.getFullChargeIntentAction())
.thenReturn(Intent.ACTION_POWER_DISCONNECTED); .thenReturn(Intent.ACTION_POWER_DISCONNECTED);
when(mFakeFeatureFactory.powerUsageFeatureProvider.delayHourlyJobWhenBooting())
.thenReturn(false);
// Make sure isCharged returns true. // Make sure isCharged returns true.
doReturn(getBatteryIntent(/*level=*/ 100, BatteryManager.BATTERY_STATUS_UNKNOWN)) doReturn(getBatteryIntent(/*level=*/ 100, BatteryManager.BATTERY_STATUS_UNKNOWN))
.when(mContext).registerReceiver(any(), any()); .when(mContext).registerReceiver(any(), any());