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
This commit is contained in:
Salvador Martinez
2017-05-18 11:10:18 -07:00
parent 6f33a52d32
commit 44b9a4072c
2 changed files with 25 additions and 5 deletions

View File

@@ -106,7 +106,8 @@ public class PowerUsageSummary extends PowerUsageBase implements
@VisibleForTesting @VisibleForTesting
static final int ANOMALY_LOADER = 1; 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; private static final int MENU_STATS_TYPE = Menu.FIRST;
@VisibleForTesting @VisibleForTesting
static final int MENU_HIGH_POWER_APPS = Menu.FIRST + 3; static final int MENU_HIGH_POWER_APPS = Menu.FIRST + 3;
@@ -173,6 +174,7 @@ public class PowerUsageSummary extends PowerUsageBase implements
@Override @Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) { public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
final Uri queryUri = mPowerFeatureProvider.getEnhancedBatteryPredictionUri(); final Uri queryUri = mPowerFeatureProvider.getEnhancedBatteryPredictionUri();
return new CursorLoader(getContext(), queryUri, null, null, null, null); return new CursorLoader(getContext(), queryUri, null, null, null, null);
} }
@@ -221,10 +223,7 @@ public class PowerUsageSummary extends PowerUsageBase implements
mAnomalySparseArray = new SparseArray<>(); mAnomalySparseArray = new SparseArray<>();
initFeatureProvider(); initFeatureProvider();
if (mPowerFeatureProvider != null) { initializeBatteryEstimateLoader();
getLoaderManager().initLoader(BATTERY_ESTIMATE_LOADER, Bundle.EMPTY,
mBatteryPredictionLoaderCallbacks);
}
} }
@Override @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() { private static List<BatterySipper> getFakeStats() {
ArrayList<BatterySipper> stats = new ArrayList<>(); ArrayList<BatterySipper> stats = new ArrayList<>();
float use = 5; float use = 5;

View File

@@ -530,6 +530,18 @@ public class PowerUsageSummaryTest {
mFragment.mBatteryPredictionLoaderCallbacks.onLoadFinished(null, null); 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 @Test
public void testInitAnomalyDetectionIfPossible_detectionEnabled_init() { public void testInitAnomalyDetectionIfPossible_detectionEnabled_init() {
when(mFeatureFactory.powerUsageFeatureProvider.isAnomalyDetectionEnabled()).thenReturn( when(mFeatureFactory.powerUsageFeatureProvider.isAnomalyDetectionEnabled()).thenReturn(