Make BatterySettings Asynchronous and use enhanced estimate
this CL changes BatteryInfo methods to all use the async style callback approach it had for one of the methods. Non-async methods are now annotated to only be used in worker threads. BatteryInfo can now be obtained via callback by calling one of the async methods. Alternatively if there is a worker thread available the synchronous methods similar to the old ones can be used. The callback methods have all been changed so that they cascade to a async method that takes all the required info as paremeters. This will minimize the amount of churn in files that currently use BatteryInfo. A new loader was created that can be used to get BatteryInfo in places that wish to get it. This loader is used in PowerUsageSummary to get the BatteryInfo. Test: Robotests Bug: 38399275 Bug: 38398949 Bug: 38399654 Change-Id: Ic5a82d8ca4c85fad1b883226327ec083badf861d
This commit is contained in:
@@ -19,21 +19,15 @@ package com.android.settings.fuelgauge;
|
||||
import android.app.Activity;
|
||||
import android.app.LoaderManager;
|
||||
import android.content.Context;
|
||||
import android.content.CursorLoader;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.Loader;
|
||||
import android.content.res.TypedArray;
|
||||
import android.database.Cursor;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.net.Uri;
|
||||
import android.os.BatteryStats;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.os.Process;
|
||||
import android.os.SystemClock;
|
||||
import android.os.UserHandle;
|
||||
import android.provider.SearchIndexableResource;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
@@ -101,7 +95,7 @@ public class PowerUsageSummary extends PowerUsageBase implements
|
||||
@VisibleForTesting
|
||||
static final int ANOMALY_LOADER = 1;
|
||||
@VisibleForTesting
|
||||
static final int BATTERY_ESTIMATE_LOADER = 2;
|
||||
static final int BATTERY_INFO_LOADER = 2;
|
||||
private static final int MENU_STATS_TYPE = Menu.FIRST;
|
||||
@VisibleForTesting
|
||||
static final int MENU_HIGH_POWER_APPS = Menu.FIRST + 3;
|
||||
@@ -124,8 +118,6 @@ public class PowerUsageSummary extends PowerUsageBase implements
|
||||
PowerUsageFeatureProvider mPowerFeatureProvider;
|
||||
@VisibleForTesting
|
||||
BatteryUtils mBatteryUtils;
|
||||
@VisibleForTesting
|
||||
long mEnhancedEstimate = -1;
|
||||
|
||||
/**
|
||||
* SparseArray that maps uid to {@link Anomaly}, so we could find {@link Anomaly} by uid
|
||||
@@ -163,35 +155,21 @@ public class PowerUsageSummary extends PowerUsageBase implements
|
||||
};
|
||||
|
||||
@VisibleForTesting
|
||||
LoaderManager.LoaderCallbacks<Cursor> mBatteryPredictionLoaderCallbacks =
|
||||
new LoaderManager.LoaderCallbacks<Cursor>() {
|
||||
LoaderManager.LoaderCallbacks<BatteryInfo> BatteryInfoLoaderCallbacks =
|
||||
new LoaderManager.LoaderCallbacks<BatteryInfo>() {
|
||||
|
||||
@Override
|
||||
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
|
||||
final Uri queryUri = mPowerFeatureProvider.getEnhancedBatteryPredictionUri();
|
||||
|
||||
return new CursorLoader(getContext(), queryUri, null, null, null, null);
|
||||
public Loader<BatteryInfo> onCreateLoader(int i, Bundle bundle) {
|
||||
return new BatteryInfoLoader(getContext(), mStatsHelper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
|
||||
if (cursor == null) {
|
||||
return;
|
||||
}
|
||||
if (cursor.moveToFirst()) {
|
||||
mEnhancedEstimate =
|
||||
mPowerFeatureProvider.getTimeRemainingEstimate(cursor);
|
||||
}
|
||||
final long elapsedRealtimeUs =
|
||||
mBatteryUtils.convertMsToUs(SystemClock.elapsedRealtime());
|
||||
Intent batteryBroadcast = getContext().registerReceiver(null,
|
||||
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
||||
BatteryInfo batteryInfo = getBatteryInfo(elapsedRealtimeUs, batteryBroadcast);
|
||||
public void onLoadFinished(Loader<BatteryInfo> loader, BatteryInfo batteryInfo) {
|
||||
mBatteryHeaderPreferenceController.updateHeaderPreference(batteryInfo);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoaderReset(Loader<Cursor> loader) {
|
||||
public void onLoaderReset(Loader<BatteryInfo> loader) {
|
||||
// do nothing
|
||||
}
|
||||
};
|
||||
@@ -213,7 +191,7 @@ public class PowerUsageSummary extends PowerUsageBase implements
|
||||
mAnomalySparseArray = new SparseArray<>();
|
||||
|
||||
initFeatureProvider();
|
||||
initializeBatteryEstimateLoader();
|
||||
restartBatteryInfoLoader();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -480,12 +458,8 @@ public class PowerUsageSummary extends PowerUsageBase implements
|
||||
|
||||
initAnomalyDetectionIfPossible();
|
||||
|
||||
final long elapsedRealtimeUs = mBatteryUtils.convertMsToUs(SystemClock.elapsedRealtime());
|
||||
Intent batteryBroadcast = context.registerReceiver(null,
|
||||
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
||||
BatteryInfo batteryInfo = getBatteryInfo(elapsedRealtimeUs, batteryBroadcast);
|
||||
mBatteryHeaderPreferenceController.updateHeaderPreference(batteryInfo);
|
||||
|
||||
// reload BatteryInfo and updateUI
|
||||
restartBatteryInfoLoader();
|
||||
final long lastFullChargeTime = mBatteryUtils.calculateLastFullChargeTime(mStatsHelper,
|
||||
System.currentTimeMillis());
|
||||
updateScreenPreference();
|
||||
@@ -701,28 +675,12 @@ public class PowerUsageSummary extends PowerUsageBase implements
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void initializeBatteryEstimateLoader() {
|
||||
void restartBatteryInfoLoader() {
|
||||
if (mPowerFeatureProvider != null
|
||||
&& mPowerFeatureProvider.isEnhancedBatteryPredictionEnabled(getContext())) {
|
||||
getLoaderManager().initLoader(BATTERY_ESTIMATE_LOADER, Bundle.EMPTY,
|
||||
mBatteryPredictionLoaderCallbacks);
|
||||
getLoaderManager().restartLoader(BATTERY_INFO_LOADER, Bundle.EMPTY,
|
||||
BatteryInfoLoaderCallbacks);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user