From 44b9a4072cf67c51a3fe968302074b3e83be086d Mon Sep 17 00:00:00 2001 From: Salvador Martinez Date: Thu, 18 May 2017 11:10:18 -0700 Subject: [PATCH] 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 --- .../settings/fuelgauge/PowerUsageSummary.java | 18 +++++++++++++----- .../fuelgauge/PowerUsageSummaryTest.java | 12 ++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/com/android/settings/fuelgauge/PowerUsageSummary.java b/src/com/android/settings/fuelgauge/PowerUsageSummary.java index b39fcfc0d9b..06566cf781c 100644 --- a/src/com/android/settings/fuelgauge/PowerUsageSummary.java +++ b/src/com/android/settings/fuelgauge/PowerUsageSummary.java @@ -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 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 getFakeStats() { ArrayList stats = new ArrayList<>(); float use = 5; diff --git a/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java b/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java index 95e33bbd494..b69f910b38d 100644 --- a/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java +++ b/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java @@ -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(