Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit aea4d365 authored by Zaiyue Xue's avatar Zaiyue Xue
Browse files

Fix b/279120121: Unplug 100% Pixel phone won't reset the battery usage data...

Fix b/279120121: Unplug 100% Pixel phone won't reset the battery usage data when the phone is just started.

Bug: 279120121
Fix: 279120121
Test: manual
Change-Id: Ie793266f8453a2902d7e16e5c58443a3c7bf55ef
parent 85e51741
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -106,9 +106,13 @@ public final class BatteryUsageBroadcastReceiver extends BroadcastReceiver {
            return;
        }

        final boolean delayHourlyJobWhenBooting =
                FeatureFactory.getFactory(context)
                        .getPowerUsageFeatureProvider(context)
                        .delayHourlyJobWhenBooting();
        final long broadcastDelay = sBroadcastDelayFromBoot - SystemClock.elapsedRealtime();
        // 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 "
                    + broadcastDelay + "ms.");
            return;
+25 −9
Original line number Diff line number Diff line
@@ -68,9 +68,11 @@ public final class BatteryUsageBroadcastReceiverTest {
    }

    @Test
    public void onReceive_actionBatteryLevelChanged_notFetchUsageData_notFullCharged() {
    public void onReceive_aospNotFullCharged_notFetchUsageData() {
        when(mFakeFeatureFactory.powerUsageFeatureProvider.getFullChargeIntentAction())
                .thenReturn(Intent.ACTION_BATTERY_LEVEL_CHANGED);
        when(mFakeFeatureFactory.powerUsageFeatureProvider.delayHourlyJobWhenBooting())
                .thenReturn(true);
        doReturn(getBatteryIntent(/*level=*/ 20, BatteryManager.BATTERY_STATUS_UNKNOWN))
                .when(mContext).registerReceiver(any(), any());

@@ -82,9 +84,11 @@ public final class BatteryUsageBroadcastReceiverTest {
    }

    @Test
    public void onReceive_actionBatteryLevelChanged_notFetchUsageData_nearBooting() {
    public void onReceive_aospNearBooting_notFetchUsageData() {
        when(mFakeFeatureFactory.powerUsageFeatureProvider.getFullChargeIntentAction())
                .thenReturn(Intent.ACTION_BATTERY_LEVEL_CHANGED);
        when(mFakeFeatureFactory.powerUsageFeatureProvider.delayHourlyJobWhenBooting())
                .thenReturn(true);
        // Make sure isCharged returns true.
        doReturn(getBatteryIntent(/*level=*/ 100, BatteryManager.BATTERY_STATUS_FULL))
                .when(mContext).registerReceiver(any(), any());
@@ -100,9 +104,11 @@ public final class BatteryUsageBroadcastReceiverTest {
    }

    @Test
    public void onReceive_actionBatteryLevelChanged_notFetchUsageData_wrongAction() {
    public void onReceive_aospWrongAction_notFetchUsageData() {
        when(mFakeFeatureFactory.powerUsageFeatureProvider.getFullChargeIntentAction())
                .thenReturn(Intent.ACTION_POWER_DISCONNECTED);
        when(mFakeFeatureFactory.powerUsageFeatureProvider.delayHourlyJobWhenBooting())
                .thenReturn(true);
        // Make sure isCharged returns true.
        doReturn(getBatteryIntent(/*level=*/ 100, BatteryManager.BATTERY_STATUS_UNKNOWN))
                .when(mContext).registerReceiver(any(), any());
@@ -117,9 +123,11 @@ public final class BatteryUsageBroadcastReceiverTest {
    }

    @Test
    public void onReceive_actionBatteryLevelChanged_fetchUsageData() {
    public void onReceive_aospNormal_fetchUsageData() {
        when(mFakeFeatureFactory.powerUsageFeatureProvider.getFullChargeIntentAction())
                .thenReturn(Intent.ACTION_BATTERY_LEVEL_CHANGED);
        when(mFakeFeatureFactory.powerUsageFeatureProvider.delayHourlyJobWhenBooting())
                .thenReturn(true);
        // Make sure isCharged returns true.
        doReturn(getBatteryIntent(/*level=*/ 100, BatteryManager.BATTERY_STATUS_UNKNOWN))
                .when(mContext).registerReceiver(any(), any());
@@ -134,9 +142,11 @@ public final class BatteryUsageBroadcastReceiverTest {
    }

    @Test
    public void onReceive_actionBatteryUnplugging_notFetchUsageData_notFullCharged() {
    public void onReceive_pixelNotFullCharged_notFetchUsageData() {
        when(mFakeFeatureFactory.powerUsageFeatureProvider.getFullChargeIntentAction())
                .thenReturn(Intent.ACTION_POWER_DISCONNECTED);
        when(mFakeFeatureFactory.powerUsageFeatureProvider.delayHourlyJobWhenBooting())
                .thenReturn(false);
        doReturn(getBatteryIntent(/*level=*/ 20, BatteryManager.BATTERY_STATUS_UNKNOWN))
                .when(mContext).registerReceiver(any(), any());

@@ -148,9 +158,11 @@ public final class BatteryUsageBroadcastReceiverTest {
    }

    @Test
    public void onReceive_actionBatteryUnplugging_notFetchUsageData_nearBooting() {
    public void onReceive_pixelNearBooting_fetchUsageData() {
        when(mFakeFeatureFactory.powerUsageFeatureProvider.getFullChargeIntentAction())
                .thenReturn(Intent.ACTION_POWER_DISCONNECTED);
        when(mFakeFeatureFactory.powerUsageFeatureProvider.delayHourlyJobWhenBooting())
                .thenReturn(false);
        // Make sure isCharged returns true.
        doReturn(getBatteryIntent(/*level=*/ 100, BatteryManager.BATTERY_STATUS_FULL))
                .when(mContext).registerReceiver(any(), any());
@@ -161,14 +173,16 @@ public final class BatteryUsageBroadcastReceiverTest {
        mBatteryUsageBroadcastReceiver.onReceive(mContext,
                new Intent(BatteryUsageBroadcastReceiver.ACTION_BATTERY_UNPLUGGING));

        assertThat(mBatteryUsageBroadcastReceiver.mFetchBatteryUsageData).isFalse();
        assertThat(mBatteryUsageBroadcastReceiver.mFetchBatteryUsageData).isTrue();
        assertSharedPreferences(BatteryUsageBroadcastReceiver.ACTION_BATTERY_UNPLUGGING);
    }

    @Test
    public void onReceive_actionBatteryUnplugging_notFetchUsageData_wrongAction() {
    public void onReceive_pixelWrongAction_notFetchUsageData() {
        when(mFakeFeatureFactory.powerUsageFeatureProvider.getFullChargeIntentAction())
                .thenReturn(Intent.ACTION_BATTERY_LEVEL_CHANGED);
        when(mFakeFeatureFactory.powerUsageFeatureProvider.delayHourlyJobWhenBooting())
                .thenReturn(false);
        // Make sure isCharged returns true.
        doReturn(getBatteryIntent(/*level=*/ 100, BatteryManager.BATTERY_STATUS_UNKNOWN))
                .when(mContext).registerReceiver(any(), any());
@@ -183,9 +197,11 @@ public final class BatteryUsageBroadcastReceiverTest {
    }

    @Test
    public void onReceive_actionBatteryUnplugging_fetchUsageData() {
    public void onReceive_pixelNormal_fetchUsageData() {
        when(mFakeFeatureFactory.powerUsageFeatureProvider.getFullChargeIntentAction())
                .thenReturn(Intent.ACTION_POWER_DISCONNECTED);
        when(mFakeFeatureFactory.powerUsageFeatureProvider.delayHourlyJobWhenBooting())
                .thenReturn(false);
        // Make sure isCharged returns true.
        doReturn(getBatteryIntent(/*level=*/ 100, BatteryManager.BATTERY_STATUS_UNKNOWN))
                .when(mContext).registerReceiver(any(), any());