Refactor the BatteryInfo

Move the battery stats logic to background because it is time
consuming.

Bug: 112262502
Test: robotest still pass
Change-Id: I794bc82a4f1b5ae5a883c89d447116601a86558f
This commit is contained in:
jackqdyulei
2018-08-09 17:02:13 -07:00
parent e39e8d7f93
commit 5c5e3bbd0c
2 changed files with 17 additions and 16 deletions

View File

@@ -54,7 +54,7 @@ public class BatteryHistoryPreference extends Preference {
BatteryInfo.getBatteryInfo(getContext(), info -> { BatteryInfo.getBatteryInfo(getContext(), info -> {
mBatteryInfo = info; mBatteryInfo = info;
notifyChanged(); notifyChanged();
}, batteryStats.getStats(), false); }, batteryStats, false);
} }
public void setBottomSummary(CharSequence text) { public void setBottomSummary(CharSequence text) {

View File

@@ -134,31 +134,32 @@ public class BatteryInfo {
} }
public static void getBatteryInfo(final Context context, final Callback callback) { public static void getBatteryInfo(final Context context, final Callback callback) {
BatteryInfo.getBatteryInfo(context, callback, false /* shortString */); BatteryInfo.getBatteryInfo(context, callback, null /* statsHelper */,
false /* shortString */);
} }
public static void getBatteryInfo(final Context context, final Callback callback, public static void getBatteryInfo(final Context context, final Callback callback,
boolean shortString) { boolean shortString) {
final long startTime = System.currentTimeMillis(); BatteryInfo.getBatteryInfo(context, callback, null /* statsHelper */, shortString);
BatteryStatsHelper statsHelper = new BatteryStatsHelper(context, true);
statsHelper.create((Bundle) null);
BatteryUtils.logRuntime(LOG_TAG, "time to make batteryStatsHelper", startTime);
BatteryInfo.getBatteryInfo(context, callback, statsHelper, shortString);
} }
public static void getBatteryInfo(final Context context, final Callback callback, public static void getBatteryInfo(final Context context, final Callback callback,
BatteryStatsHelper statsHelper, boolean shortString) { final BatteryStatsHelper statsHelper, boolean shortString) {
final long startTime = System.currentTimeMillis();
BatteryStats stats = statsHelper.getStats();
BatteryUtils.logRuntime(LOG_TAG, "time for getStats", startTime);
getBatteryInfo(context, callback, stats, shortString);
}
public static void getBatteryInfo(final Context context, final Callback callback,
BatteryStats stats, boolean shortString) {
new AsyncTask<Void, Void, BatteryInfo>() { new AsyncTask<Void, Void, BatteryInfo>() {
@Override @Override
protected BatteryInfo doInBackground(Void... params) { protected BatteryInfo doInBackground(Void... params) {
final BatteryStats stats;
final long batteryStatsTime = System.currentTimeMillis();
if (statsHelper == null) {
final BatteryStatsHelper localStatsHelper = new BatteryStatsHelper(context,
true);
localStatsHelper.create((Bundle) null);
stats = localStatsHelper.getStats();
} else {
stats = statsHelper.getStats();
}
BatteryUtils.logRuntime(LOG_TAG, "time for getStats", batteryStatsTime);
final long startTime = System.currentTimeMillis(); final long startTime = System.currentTimeMillis();
PowerUsageFeatureProvider provider = PowerUsageFeatureProvider provider =
FeatureFactory.getFactory(context).getPowerUsageFeatureProvider(context); FeatureFactory.getFactory(context).getPowerUsageFeatureProvider(context);