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

Commit b47b99ed authored by Lei Yu's avatar Lei Yu
Browse files

Add null check when restart batteryinfo loader

Even though we unregister contentObserver in onPause and register
in onResume, callback still be called while fragment is detached.

Anyhow add a null check in settings to stop crash

Fixes: 131905853
Test: RunSettingsRoboTests
Change-Id: I8c0c2c04c3b8d942e0c97cf71a7d3e735a24b467
parent 65a258e2
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -378,6 +378,9 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList

    @VisibleForTesting
    void restartBatteryInfoLoader() {
        if (getContext() == null) {
            return;
        }
        getLoaderManager().restartLoader(BATTERY_INFO_LOADER, Bundle.EMPTY,
                mBatteryInfoLoaderCallbacks);
        if (mPowerFeatureProvider.isEstimateDebugEnabled()) {
+12 −0
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
 */
package com.android.settings.fuelgauge;

import static com.android.settings.fuelgauge.PowerUsageSummary.BATTERY_INFO_LOADER;
import static com.android.settings.fuelgauge.PowerUsageSummary.MENU_ADVANCED_BATTERY;

import static com.google.common.truth.Truth.assertThat;
@@ -373,6 +374,17 @@ public class PowerUsageSummaryTest {
                mFragment.mSettingsObserver);
    }

    @Test
    public void restartBatteryInfoLoader_contextNull_doNothing() {
        when(mFragment.getContext()).thenReturn(null);
        when(mFragment.getLoaderManager()).thenReturn(mLoaderManager);

        mFragment.restartBatteryInfoLoader();

        verify(mLoaderManager, never()).restartLoader(BATTERY_INFO_LOADER, Bundle.EMPTY,
                mFragment.mBatteryInfoLoaderCallbacks);
    }

    public static class TestFragment extends PowerUsageSummary {
        private Context mContext;