Merge "Skip cursor loader if not needed"

This commit is contained in:
TreeHugger Robot
2017-05-18 20:28:50 +00:00
committed by Android (Google) Code Review
2 changed files with 25 additions and 5 deletions

View File

@@ -113,7 +113,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;
@@ -182,6 +183,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);
}
@@ -228,10 +230,7 @@ public class PowerUsageSummary extends PowerUsageBase implements
mAnomalySparseArray = new SparseArray<>();
initFeatureProvider();
if (mPowerFeatureProvider != null) {
getLoaderManager().initLoader(BATTERY_ESTIMATE_LOADER, Bundle.EMPTY,
mBatteryPredictionLoaderCallbacks);
}
initializeBatteryEstimateLoader();
}
@Override
@@ -815,6 +814,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;

View File

@@ -576,6 +576,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(