Merge "Use enhanced estimate battery info when available"
This commit is contained in:
committed by
Android (Google) Code Review
commit
6976ccecee
@@ -217,7 +217,7 @@ public class BatteryUtils {
|
|||||||
return timeUs / 1000;
|
return timeUs / 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
private long convertMsToUs(long timeMs) {
|
public long convertMsToUs(long timeMs) {
|
||||||
return timeMs * 1000;
|
return timeMs * 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -187,12 +187,11 @@ public class PowerUsageSummary extends PowerUsageBase implements
|
|||||||
mEnhancedEstimate =
|
mEnhancedEstimate =
|
||||||
mPowerFeatureProvider.getTimeRemainingEstimate(cursor);
|
mPowerFeatureProvider.getTimeRemainingEstimate(cursor);
|
||||||
}
|
}
|
||||||
final long elapsedRealtimeUs = SystemClock.elapsedRealtime() * 1000;
|
final long elapsedRealtimeUs =
|
||||||
|
mBatteryUtils.convertMsToUs(SystemClock.elapsedRealtime());
|
||||||
Intent batteryBroadcast = getContext().registerReceiver(null,
|
Intent batteryBroadcast = getContext().registerReceiver(null,
|
||||||
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
||||||
BatteryInfo batteryInfo = BatteryInfo.getBatteryInfo(getContext(),
|
BatteryInfo batteryInfo = getBatteryInfo(elapsedRealtimeUs, batteryBroadcast);
|
||||||
batteryBroadcast, mStatsHelper.getStats(), elapsedRealtimeUs, false);
|
|
||||||
useEnhancedEstimateIfAvailable(getContext(), batteryInfo);
|
|
||||||
updateHeaderPreference(batteryInfo);
|
updateHeaderPreference(batteryInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -494,12 +493,10 @@ public class PowerUsageSummary extends PowerUsageBase implements
|
|||||||
|
|
||||||
initAnomalyDetectionIfPossible();
|
initAnomalyDetectionIfPossible();
|
||||||
|
|
||||||
final long elapsedRealtimeUs = SystemClock.elapsedRealtime() * 1000;
|
final long elapsedRealtimeUs = mBatteryUtils.convertMsToUs(SystemClock.elapsedRealtime());
|
||||||
Intent batteryBroadcast = context.registerReceiver(null,
|
Intent batteryBroadcast = context.registerReceiver(null,
|
||||||
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
||||||
BatteryInfo batteryInfo = BatteryInfo.getBatteryInfo(context, batteryBroadcast,
|
BatteryInfo batteryInfo = getBatteryInfo(elapsedRealtimeUs, batteryBroadcast);
|
||||||
mStatsHelper.getStats(), elapsedRealtimeUs, false);
|
|
||||||
useEnhancedEstimateIfAvailable(context, batteryInfo);
|
|
||||||
updateHeaderPreference(batteryInfo);
|
updateHeaderPreference(batteryInfo);
|
||||||
|
|
||||||
final long runningTime = calculateRunningTimeBasedOnStatsType();
|
final long runningTime = calculateRunningTimeBasedOnStatsType();
|
||||||
@@ -660,7 +657,7 @@ public class PowerUsageSummary extends PowerUsageBase implements
|
|||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
long calculateRunningTimeBasedOnStatsType() {
|
long calculateRunningTimeBasedOnStatsType() {
|
||||||
final long elapsedRealtimeUs = SystemClock.elapsedRealtime() * 1000;
|
final long elapsedRealtimeUs = mBatteryUtils.convertMsToUs(SystemClock.elapsedRealtime());
|
||||||
// Return the battery time (millisecond) on status mStatsType
|
// Return the battery time (millisecond) on status mStatsType
|
||||||
return mStatsHelper.getStats().computeBatteryRealtime(elapsedRealtimeUs,
|
return mStatsHelper.getStats().computeBatteryRealtime(elapsedRealtimeUs,
|
||||||
mStatsType /* STATS_SINCE_CHARGED */) / 1000;
|
mStatsType /* STATS_SINCE_CHARGED */) / 1000;
|
||||||
@@ -743,17 +740,20 @@ public class PowerUsageSummary extends PowerUsageBase implements
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
private BatteryInfo getBatteryInfo(long elapsedRealtimeUs, Intent batteryBroadcast) {
|
||||||
void useEnhancedEstimateIfAvailable(Context context, BatteryInfo batteryInfo) {
|
BatteryInfo batteryInfo;
|
||||||
if (mEnhancedEstimate > 0
|
if (mEnhancedEstimate > 0 &&
|
||||||
&& mPowerFeatureProvider.isEnhancedBatteryPredictionEnabled(context)) {
|
mPowerFeatureProvider.isEnhancedBatteryPredictionEnabled(
|
||||||
final Resources resources = context.getResources();
|
getContext())) {
|
||||||
batteryInfo.remainingTimeUs = mEnhancedEstimate;
|
// Drain time is in micro-seconds so we have to multiply by 1000
|
||||||
String timeString = Formatter.formatShortElapsedTime(context, mEnhancedEstimate);
|
batteryInfo = BatteryInfo.getBatteryInfo(getContext(), batteryBroadcast,
|
||||||
batteryInfo.remainingLabel = resources.getString(
|
mStatsHelper.getStats(), elapsedRealtimeUs, false,
|
||||||
com.android.settingslib.R.string.power_remaining_duration_only,
|
mBatteryUtils.convertMsToUs(mEnhancedEstimate), true);
|
||||||
timeString);
|
} else {
|
||||||
|
batteryInfo = BatteryInfo.getBatteryInfo(getContext(), batteryBroadcast,
|
||||||
|
mStatsHelper.getStats(), elapsedRealtimeUs, false);
|
||||||
}
|
}
|
||||||
|
return batteryInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
|
@@ -491,41 +491,6 @@ public class PowerUsageSummaryTest {
|
|||||||
assertThat(mFragment.mAnomalySparseArray.get(UID_2)).containsExactly(anomaly3);
|
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
|
@Test
|
||||||
public void testBatteryPredictionLoaderCallbacks_DoesNotCrashOnNull() {
|
public void testBatteryPredictionLoaderCallbacks_DoesNotCrashOnNull() {
|
||||||
// Sanity test to check for crash
|
// Sanity test to check for crash
|
||||||
|
Reference in New Issue
Block a user