Use enhanced estimate battery info when available
This CL updates the battery page to get the updated strings and other relevant battery info from directly from SettingsLib by providing it an overrided drain time instead of manually changing the value in the returned info. This will provide the added benefit of using the new strings when appropriate that tell the user that the estimate provided is based on their usage. Test: robotests Bug: 38399659 Change-Id: I0db572c2ea78910756314b6bf066d37e9f90a15c
This commit is contained in:
@@ -178,7 +178,7 @@ public class BatteryUtils {
|
||||
return timeUs / 1000;
|
||||
}
|
||||
|
||||
private long convertMsToUs(long timeMs) {
|
||||
public long convertMsToUs(long timeMs) {
|
||||
return timeMs * 1000;
|
||||
}
|
||||
|
||||
|
@@ -189,12 +189,11 @@ public class PowerUsageSummary extends PowerUsageBase implements
|
||||
} finally {
|
||||
cursor.close();
|
||||
}
|
||||
final long elapsedRealtimeUs = SystemClock.elapsedRealtime() * 1000;
|
||||
final long elapsedRealtimeUs =
|
||||
mBatteryUtils.convertMsToUs(SystemClock.elapsedRealtime());
|
||||
Intent batteryBroadcast = getContext().registerReceiver(null,
|
||||
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
||||
BatteryInfo batteryInfo = BatteryInfo.getBatteryInfo(getContext(),
|
||||
batteryBroadcast, mStatsHelper.getStats(), elapsedRealtimeUs, false);
|
||||
useEnhancedEstimateIfAvailable(getContext(), batteryInfo);
|
||||
BatteryInfo batteryInfo = getBatteryInfo(elapsedRealtimeUs, batteryBroadcast);
|
||||
updateHeaderPreference(batteryInfo);
|
||||
}
|
||||
|
||||
@@ -499,12 +498,10 @@ public class PowerUsageSummary extends PowerUsageBase implements
|
||||
|
||||
initAnomalyDetectionIfPossible();
|
||||
|
||||
final long elapsedRealtimeUs = SystemClock.elapsedRealtime() * 1000;
|
||||
final long elapsedRealtimeUs = mBatteryUtils.convertMsToUs(SystemClock.elapsedRealtime());
|
||||
Intent batteryBroadcast = context.registerReceiver(null,
|
||||
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
||||
BatteryInfo batteryInfo = BatteryInfo.getBatteryInfo(context, batteryBroadcast,
|
||||
mStatsHelper.getStats(), elapsedRealtimeUs, false);
|
||||
useEnhancedEstimateIfAvailable(context, batteryInfo);
|
||||
BatteryInfo batteryInfo = getBatteryInfo(elapsedRealtimeUs, batteryBroadcast);
|
||||
updateHeaderPreference(batteryInfo);
|
||||
|
||||
final long runningTime = calculateRunningTimeBasedOnStatsType();
|
||||
@@ -665,7 +662,7 @@ public class PowerUsageSummary extends PowerUsageBase implements
|
||||
|
||||
@VisibleForTesting
|
||||
long calculateRunningTimeBasedOnStatsType() {
|
||||
final long elapsedRealtimeUs = SystemClock.elapsedRealtime() * 1000;
|
||||
final long elapsedRealtimeUs = mBatteryUtils.convertMsToUs(SystemClock.elapsedRealtime());
|
||||
// Return the battery time (millisecond) on status mStatsType
|
||||
return mStatsHelper.getStats().computeBatteryRealtime(elapsedRealtimeUs,
|
||||
mStatsType /* STATS_SINCE_CHARGED */) / 1000;
|
||||
@@ -748,17 +745,20 @@ public class PowerUsageSummary extends PowerUsageBase implements
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void useEnhancedEstimateIfAvailable(Context context, BatteryInfo batteryInfo) {
|
||||
if (mEnhancedEstimate > 0
|
||||
&& mPowerFeatureProvider.isEnhancedBatteryPredictionEnabled(context)) {
|
||||
final Resources resources = context.getResources();
|
||||
batteryInfo.remainingTimeUs = mEnhancedEstimate;
|
||||
String timeString = Formatter.formatShortElapsedTime(context, mEnhancedEstimate);
|
||||
batteryInfo.remainingLabel = resources.getString(
|
||||
com.android.settingslib.R.string.power_remaining_duration_only,
|
||||
timeString);
|
||||
private BatteryInfo getBatteryInfo(long elapsedRealtimeUs, Intent batteryBroadcast) {
|
||||
BatteryInfo batteryInfo;
|
||||
if (mEnhancedEstimate > 0 &&
|
||||
mPowerFeatureProvider.isEnhancedBatteryPredictionEnabled(
|
||||
getContext())) {
|
||||
// Drain time is in micro-seconds so we have to multiply by 1000
|
||||
batteryInfo = BatteryInfo.getBatteryInfo(getContext(), batteryBroadcast,
|
||||
mStatsHelper.getStats(), elapsedRealtimeUs, false,
|
||||
mBatteryUtils.convertMsToUs(mEnhancedEstimate), true);
|
||||
} else {
|
||||
batteryInfo = BatteryInfo.getBatteryInfo(getContext(), batteryBroadcast,
|
||||
mStatsHelper.getStats(), elapsedRealtimeUs, false);
|
||||
}
|
||||
return batteryInfo;
|
||||
}
|
||||
|
||||
private static List<BatterySipper> getFakeStats() {
|
||||
|
@@ -489,41 +489,6 @@ public class PowerUsageSummaryTest {
|
||||
assertThat(mFragment.mAnomalySparseArray.get(UID_2)).containsExactly(anomaly3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUseEnhancedEstimateIfAvailable() {
|
||||
// mock out the provider
|
||||
final long time = 60 * 1000 * 1000;
|
||||
PowerUsageFeatureProvider provider = mFeatureFactory.getPowerUsageFeatureProvider(mContext);
|
||||
when(provider.isEnhancedBatteryPredictionEnabled(any())).thenReturn(true);
|
||||
mFragment.mPowerFeatureProvider = provider;
|
||||
mFragment.mEnhancedEstimate = time;
|
||||
|
||||
mFragment.useEnhancedEstimateIfAvailable(mRealContext, mBatteryInfo);
|
||||
|
||||
// The string that gets returned always has weird whitespacing to make it fit
|
||||
// so we're just going to check that it contains the correct value we care about.
|
||||
assertThat(mBatteryInfo.remainingTimeUs).isEqualTo(time);
|
||||
assertThat(mBatteryInfo.remainingLabel).contains("About 17 hrs");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUseEnhancedEstimateIfAvailable_noOpsOnDisabled() {
|
||||
// mock out the provider
|
||||
final long time = 60 * 1000 * 1000;
|
||||
PowerUsageFeatureProvider provider = mFeatureFactory.getPowerUsageFeatureProvider(mContext);
|
||||
when(provider.isEnhancedBatteryPredictionEnabled(any())).thenReturn(false);
|
||||
mFragment.mPowerFeatureProvider = provider;
|
||||
mFragment.mEnhancedEstimate = time;
|
||||
mBatteryInfo.remainingTimeUs = TIME_SINCE_LAST_FULL_CHARGE_US;
|
||||
mBatteryInfo.remainingLabel = TIME_LEFT;
|
||||
|
||||
mFragment.useEnhancedEstimateIfAvailable(mRealContext, mBatteryInfo);
|
||||
|
||||
// check to make sure the values did not change
|
||||
assertThat(mBatteryInfo.remainingTimeUs).isEqualTo(TIME_SINCE_LAST_FULL_CHARGE_US);
|
||||
assertThat(mBatteryInfo.remainingLabel).contains(TIME_LEFT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBatteryPredictionLoaderCallbacks_DoesNotCrashOnNull() {
|
||||
// Sanity test to check for crash
|
||||
|
Reference in New Issue
Block a user