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

Commit 44b9a407 authored by Salvador Martinez's avatar Salvador Martinez
Browse files

Skip cursor loader if not needed

The cursor loader was being started when the battery prediction
feature was not enabled. This could lead to null pointers because
it was not possible to provide it with a valid URI which would
make the CursorLoader unhappy. This CL makes it so we just skip
the CursorLoader initialization entirely when we know the feature
is disabled so we don't have this issue. It also includes a test
to make sure this does not regress.

Test: Robotests
Bug: 38371686
Change-Id: I4f6f6278bbc16668bca0b51fcc7e30f27a9e216f
parent 6f33a52d
Loading
Loading
Loading
Loading
+13 −5
Original line number Diff line number Diff line
@@ -106,7 +106,8 @@ public class PowerUsageSummary extends PowerUsageBase implements

    @VisibleForTesting
    static final int ANOMALY_LOADER = 1;
    private static final int BATTERY_ESTIMATE_LOADER = 2;
    @VisibleForTesting
    static final int BATTERY_ESTIMATE_LOADER = 2;
    private static final int MENU_STATS_TYPE = Menu.FIRST;
    @VisibleForTesting
    static final int MENU_HIGH_POWER_APPS = Menu.FIRST + 3;
@@ -173,6 +174,7 @@ public class PowerUsageSummary extends PowerUsageBase implements
                @Override
                public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
                    final Uri queryUri = mPowerFeatureProvider.getEnhancedBatteryPredictionUri();

                    return new CursorLoader(getContext(), queryUri, null, null, null, null);
                }

@@ -221,10 +223,7 @@ public class PowerUsageSummary extends PowerUsageBase implements
        mAnomalySparseArray = new SparseArray<>();

        initFeatureProvider();
        if (mPowerFeatureProvider != null) {
            getLoaderManager().initLoader(BATTERY_ESTIMATE_LOADER, Bundle.EMPTY,
                    mBatteryPredictionLoaderCallbacks);
        }
        initializeBatteryEstimateLoader();
    }

    @Override
@@ -761,6 +760,15 @@ public class PowerUsageSummary extends PowerUsageBase implements
        }
    }

    @VisibleForTesting
    void initializeBatteryEstimateLoader() {
        if (mPowerFeatureProvider != null
                && mPowerFeatureProvider.isEnhancedBatteryPredictionEnabled(getContext())) {
            getLoaderManager().initLoader(BATTERY_ESTIMATE_LOADER, Bundle.EMPTY,
                    mBatteryPredictionLoaderCallbacks);
        }
    }

    private static List<BatterySipper> getFakeStats() {
        ArrayList<BatterySipper> stats = new ArrayList<>();
        float use = 5;
+12 −0
Original line number Diff line number Diff line
@@ -530,6 +530,18 @@ public class PowerUsageSummaryTest {
        mFragment.mBatteryPredictionLoaderCallbacks.onLoadFinished(null, null);
    }

    @Test
    public void testOnCreate_BatteryPredictionSkippedWhenDisabled() {
        PowerUsageFeatureProvider provider = mFeatureFactory.getPowerUsageFeatureProvider(mContext);
        when(provider.isEnhancedBatteryPredictionEnabled(any())).thenReturn(false);
        mFragment.mPowerFeatureProvider = provider;
        doReturn(mLoaderManager).when(mFragment).getLoaderManager();
        mFragment.initializeBatteryEstimateLoader();

        verify(mLoaderManager, never()).initLoader(eq(PowerUsageSummary.BATTERY_ESTIMATE_LOADER),
                eq(Bundle.EMPTY), any());
    }

    @Test
    public void testInitAnomalyDetectionIfPossible_detectionEnabled_init() {
        when(mFeatureFactory.powerUsageFeatureProvider.isAnomalyDetectionEnabled()).thenReturn(