Merge "Transition BatteryInfo and BatteryUtils to BatteryUsageStats API" into sc-dev

This commit is contained in:
TreeHugger Robot
2021-03-12 23:52:00 +00:00
committed by Android (Google) Code Review
18 changed files with 296 additions and 180 deletions

View File

@@ -230,7 +230,7 @@ public class AppBatteryPreferenceController extends BasePreferenceController
@Override
@NonNull
public Loader<BatteryUsageStats> onCreateLoader(int id, Bundle args) {
return new BatteryUsageStatsLoader(mContext);
return new BatteryUsageStatsLoader(mContext, /* includeBatteryHistory */ false);
}
@Override

View File

@@ -17,15 +17,16 @@
package com.android.settings.fuelgauge;
import android.content.Context;
import android.os.BatteryUsageStats;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;
import com.android.internal.os.BatteryStatsHelper;
import com.android.settings.R;
import com.android.settings.widget.UsageView;
@@ -50,11 +51,11 @@ public class BatteryHistoryPreference extends Preference {
setSelectable(false);
}
public void setStats(BatteryStatsHelper batteryStats) {
void setBatteryUsageStats(@NonNull BatteryUsageStats batteryUsageStats) {
BatteryInfo.getBatteryInfo(getContext(), info -> {
mBatteryInfo = info;
notifyChanged();
}, batteryStats, false);
}, batteryUsageStats, false);
}
public void setBottomSummary(CharSequence text) {

View File

@@ -20,16 +20,18 @@ import android.content.IntentFilter;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.BatteryManager;
import android.os.BatteryStats;
import android.os.BatteryStats.HistoryItem;
import android.os.Bundle;
import android.os.BatteryStatsManager;
import android.os.BatteryUsageStats;
import android.os.SystemClock;
import android.text.format.Formatter;
import android.util.SparseIntArray;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;
import com.android.internal.os.BatteryStatsHelper;
import com.android.internal.os.BatteryStatsHistoryIterator;
import com.android.settings.Utils;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.widget.UsageView;
@@ -52,7 +54,7 @@ public class BatteryInfo {
public String statusLabel;
public String suggestionLabel;
private boolean mCharging;
private BatteryStats mStats;
private BatteryUsageStats mBatteryUsageStats;
private static final String LOG_TAG = "BatteryInfo";
private long timePeriod;
@@ -126,7 +128,7 @@ public class BatteryInfo {
parserList[i] = parsers[i];
}
parserList[parsers.length] = parser;
parse(mStats, parserList);
parseBatteryHistory(parserList);
String timeString = context.getString(R.string.charge_length_format,
Formatter.formatShortElapsedTime(context, timePeriod));
String remaining = "";
@@ -137,22 +139,25 @@ public class BatteryInfo {
view.setBottomLabels(new CharSequence[]{timeString, remaining});
}
public static void getBatteryInfo(final Context context, final Callback callback) {
BatteryInfo.getBatteryInfo(context, callback, null /* statsHelper */,
false /* shortString */);
}
public static void getBatteryInfo(final Context context, final Callback callback,
boolean shortString) {
BatteryInfo.getBatteryInfo(context, callback, null /* statsHelper */, shortString);
BatteryInfo.getBatteryInfo(context, callback, /* batteryUsageStats */ null, shortString);
}
public static void getBatteryInfo(final Context context, final Callback callback,
final BatteryStatsHelper statsHelper, boolean shortString) {
@Nullable final BatteryUsageStats batteryUsageStats,
boolean shortString) {
new AsyncTask<Void, Void, BatteryInfo>() {
@Override
protected BatteryInfo doInBackground(Void... params) {
return getBatteryInfo(context, statsHelper, shortString);
BatteryUsageStats stats;
if (batteryUsageStats != null) {
stats = batteryUsageStats;
} else {
stats = context.getSystemService(BatteryStatsManager.class)
.getBatteryUsageStats();
}
return getBatteryInfo(context, stats, shortString);
}
@Override
@@ -164,18 +169,13 @@ public class BatteryInfo {
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
/**
* Creates a BatteryInfo based on BatteryUsageStats
*/
@WorkerThread
public static BatteryInfo getBatteryInfo(final Context context,
final BatteryStatsHelper statsHelper, boolean shortString) {
final BatteryStats stats;
@NonNull final BatteryUsageStats batteryUsageStats, boolean shortString) {
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();
@@ -197,38 +197,38 @@ public class BatteryInfo {
Estimate.storeCachedEstimate(context, estimate);
BatteryUtils
.logRuntime(LOG_TAG, "time for enhanced BatteryInfo", startTime);
return BatteryInfo.getBatteryInfo(context, batteryBroadcast, stats,
return BatteryInfo.getBatteryInfo(context, batteryBroadcast, batteryUsageStats,
estimate, elapsedRealtimeUs, shortString);
}
}
final long prediction = discharging
? stats.computeBatteryTimeRemaining(elapsedRealtimeUs) : 0;
final long prediction = discharging ? batteryUsageStats.getBatteryTimeRemainingMs() : 0;
final Estimate estimate = new Estimate(
PowerUtil.convertUsToMs(prediction),
false, /* isBasedOnUsage */
EstimateKt.AVERAGE_TIME_TO_DISCHARGE_UNKNOWN);
BatteryUtils.logRuntime(LOG_TAG, "time for regular BatteryInfo", startTime);
return BatteryInfo.getBatteryInfo(context, batteryBroadcast, stats,
return BatteryInfo.getBatteryInfo(context, batteryBroadcast, batteryUsageStats,
estimate, elapsedRealtimeUs, shortString);
}
@WorkerThread
public static BatteryInfo getBatteryInfoOld(Context context, Intent batteryBroadcast,
BatteryStats stats, long elapsedRealtimeUs, boolean shortString) {
BatteryUsageStats batteryUsageStats, long elapsedRealtimeUs, boolean shortString) {
Estimate estimate = new Estimate(
PowerUtil.convertUsToMs(stats.computeBatteryTimeRemaining(elapsedRealtimeUs)),
batteryUsageStats.getBatteryTimeRemainingMs(),
false,
EstimateKt.AVERAGE_TIME_TO_DISCHARGE_UNKNOWN);
return getBatteryInfo(context, batteryBroadcast, stats, estimate, elapsedRealtimeUs,
shortString);
return getBatteryInfo(context, batteryBroadcast, batteryUsageStats, estimate,
elapsedRealtimeUs, shortString);
}
@WorkerThread
public static BatteryInfo getBatteryInfo(Context context, Intent batteryBroadcast,
BatteryStats stats, Estimate estimate, long elapsedRealtimeUs, boolean shortString) {
@NonNull BatteryUsageStats batteryUsageStats, Estimate estimate,
long elapsedRealtimeUs, boolean shortString) {
final long startTime = System.currentTimeMillis();
BatteryInfo info = new BatteryInfo();
info.mStats = stats;
info.mBatteryUsageStats = batteryUsageStats;
info.batteryLevel = Utils.getBatteryLevel(batteryBroadcast);
info.batteryPercentString = Utils.formatPercentage(info.batteryLevel);
info.mCharging = batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0;
@@ -241,16 +241,17 @@ public class BatteryInfo {
if (!info.mCharging) {
updateBatteryInfoDischarging(context, shortString, estimate, info);
} else {
updateBatteryInfoCharging(context, batteryBroadcast, stats, elapsedRealtimeUs, info);
updateBatteryInfoCharging(context, batteryBroadcast, batteryUsageStats,
info);
}
BatteryUtils.logRuntime(LOG_TAG, "time for getBatteryInfo", startTime);
return info;
}
private static void updateBatteryInfoCharging(Context context, Intent batteryBroadcast,
BatteryStats stats, long elapsedRealtimeUs, BatteryInfo info) {
BatteryUsageStats stats, BatteryInfo info) {
final Resources resources = context.getResources();
final long chargeTime = stats.computeChargeTimeRemaining(elapsedRealtimeUs);
final long chargeTimeMs = stats.getChargeTimeRemainingMs();
final int status = batteryBroadcast.getIntExtra(BatteryManager.EXTRA_STATUS,
BatteryManager.BATTERY_STATUS_UNKNOWN);
info.discharging = false;
@@ -260,8 +261,8 @@ public class BatteryInfo {
int chargingLimitedResId = R.string.power_charging_limited;
info.chargeLabel =
context.getString(chargingLimitedResId, info.batteryPercentString);
} else if (chargeTime > 0 && status != BatteryManager.BATTERY_STATUS_FULL) {
info.remainingTimeUs = chargeTime;
} else if (chargeTimeMs > 0 && status != BatteryManager.BATTERY_STATUS_FULL) {
info.remainingTimeUs = PowerUtil.convertMsToUs(chargeTimeMs);
CharSequence timeString = StringUtil.formatElapsedTime(context,
PowerUtil.convertUsToMs(info.remainingTimeUs), false /* withSeconds */);
int resId = R.string.power_charging_duration;
@@ -313,7 +314,11 @@ public class BatteryInfo {
void onParsingDone();
}
public static void parse(BatteryStats stats, BatteryDataParser... parsers) {
/**
* Iterates over battery history included in the BatteryUsageStats that this object
* was initialized with.
*/
public void parseBatteryHistory(BatteryDataParser... parsers) {
long startWalltime = 0;
long endWalltime = 0;
long historyStart = 0;
@@ -324,41 +329,41 @@ public class BatteryInfo {
int lastInteresting = 0;
int pos = 0;
boolean first = true;
if (stats.startIteratingHistoryLocked()) {
final HistoryItem rec = new HistoryItem();
while (stats.getNextHistoryLocked(rec)) {
pos++;
if (first) {
first = false;
historyStart = rec.time;
final BatteryStatsHistoryIterator iterator1 =
mBatteryUsageStats.iterateBatteryStatsHistory();
final HistoryItem rec = new HistoryItem();
while (iterator1.next(rec)) {
pos++;
if (first) {
first = false;
historyStart = rec.time;
}
if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
|| rec.cmd == HistoryItem.CMD_RESET) {
// If there is a ridiculously large jump in time, then we won't be
// able to create a good chart with that data, so just ignore the
// times we got before and pretend like our data extends back from
// the time we have now.
// Also, if we are getting a time change and we are less than 5 minutes
// since the start of the history real time, then also use this new
// time to compute the base time, since whatever time we had before is
// pretty much just noise.
if (rec.currentTime > (lastWallTime + (180 * 24 * 60 * 60 * 1000L))
|| rec.time < (historyStart + (5 * 60 * 1000L))) {
startWalltime = 0;
}
if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
|| rec.cmd == HistoryItem.CMD_RESET) {
// If there is a ridiculously large jump in time, then we won't be
// able to create a good chart with that data, so just ignore the
// times we got before and pretend like our data extends back from
// the time we have now.
// Also, if we are getting a time change and we are less than 5 minutes
// since the start of the history real time, then also use this new
// time to compute the base time, since whatever time we had before is
// pretty much just noise.
if (rec.currentTime > (lastWallTime + (180 * 24 * 60 * 60 * 1000L))
|| rec.time < (historyStart + (5 * 60 * 1000L))) {
startWalltime = 0;
}
lastWallTime = rec.currentTime;
lastRealtime = rec.time;
if (startWalltime == 0) {
startWalltime = lastWallTime - (lastRealtime - historyStart);
}
}
if (rec.isDeltaData()) {
lastInteresting = pos;
historyEnd = rec.time;
lastWallTime = rec.currentTime;
lastRealtime = rec.time;
if (startWalltime == 0) {
startWalltime = lastWallTime - (lastRealtime - historyStart);
}
}
if (rec.isDeltaData()) {
lastInteresting = pos;
historyEnd = rec.time;
}
}
stats.finishIteratingHistoryLocked();
endWalltime = lastWallTime + historyEnd - lastRealtime;
int i = 0;
@@ -367,9 +372,11 @@ public class BatteryInfo {
for (int j = 0; j < parsers.length; j++) {
parsers[j].onParsingStarted(startWalltime, endWalltime);
}
if (endWalltime > startWalltime && stats.startIteratingHistoryLocked()) {
final HistoryItem rec = new HistoryItem();
while (stats.getNextHistoryLocked(rec) && i < N) {
if (endWalltime > startWalltime) {
final BatteryStatsHistoryIterator iterator2 =
mBatteryUsageStats.iterateBatteryStatsHistory();
while (iterator2.next(rec) && i < N) {
if (rec.isDeltaData()) {
curWalltime += rec.time - lastRealtime;
lastRealtime = rec.time;
@@ -404,8 +411,6 @@ public class BatteryInfo {
}
}
stats.finishIteratingHistoryLocked();
for (int j = 0; j < parsers.length; j++) {
parsers[j].onParsingDone();
}

View File

@@ -19,7 +19,6 @@ import android.content.Context;
import androidx.annotation.VisibleForTesting;
import com.android.internal.os.BatteryStatsHelper;
import com.android.settingslib.utils.AsyncLoaderCompat;
/**
@@ -28,17 +27,14 @@ import com.android.settingslib.utils.AsyncLoaderCompat;
* when not available.
*/
public class BatteryInfoLoader extends AsyncLoaderCompat<BatteryInfo>{
BatteryStatsHelper mStatsHelper;
private static final String LOG_TAG = "BatteryInfoLoader";
@VisibleForTesting
BatteryUtils batteryUtils;
BatteryUtils mBatteryUtils;
public BatteryInfoLoader(Context context, BatteryStatsHelper batteryStatsHelper) {
public BatteryInfoLoader(Context context) {
super(context);
mStatsHelper = batteryStatsHelper;
batteryUtils = BatteryUtils.getInstance(context);
mBatteryUtils = BatteryUtils.getInstance(context);
}
@Override
@@ -48,6 +44,6 @@ public class BatteryInfoLoader extends AsyncLoaderCompat<BatteryInfo>{
@Override
public BatteryInfo loadInBackground() {
return batteryUtils.getBatteryInfo(mStatsHelper, LOG_TAG);
return mBatteryUtils.getBatteryInfo(LOG_TAG);
}
}

View File

@@ -19,6 +19,7 @@ package com.android.settings.fuelgauge;
import android.content.Context;
import android.os.BatteryStatsManager;
import android.os.BatteryUsageStats;
import android.os.BatteryUsageStatsQuery;
import com.android.settingslib.utils.AsyncLoaderCompat;
@@ -27,15 +28,21 @@ import com.android.settingslib.utils.AsyncLoaderCompat;
*/
public class BatteryUsageStatsLoader extends AsyncLoaderCompat<BatteryUsageStats> {
private final BatteryStatsManager mBatteryStatsManager;
private final boolean mIncludeBatteryHistory;
public BatteryUsageStatsLoader(Context context) {
public BatteryUsageStatsLoader(Context context, boolean includeBatteryHistory) {
super(context);
mBatteryStatsManager = context.getSystemService(BatteryStatsManager.class);
mIncludeBatteryHistory = includeBatteryHistory;
}
@Override
public BatteryUsageStats loadInBackground() {
return mBatteryStatsManager.getBatteryUsageStats();
final BatteryUsageStatsQuery.Builder builder = new BatteryUsageStatsQuery.Builder();
if (mIncludeBatteryHistory) {
builder.includeBatteryHistory();
}
return mBatteryStatsManager.getBatteryUsageStats(builder.build());
}
@Override

View File

@@ -24,6 +24,9 @@ import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.BatteryStats;
import android.os.BatteryStatsManager;
import android.os.BatteryUsageStats;
import android.os.BatteryUsageStatsQuery;
import android.os.Build;
import android.os.Bundle;
import android.os.Process;
@@ -103,7 +106,7 @@ public class BatteryUtils {
}
@VisibleForTesting
BatteryUtils(Context context) {
public BatteryUtils(Context context) {
mContext = context;
mPackageManager = context.getPackageManager();
mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
@@ -472,29 +475,35 @@ public class BatteryUtils {
}
@WorkerThread
public BatteryInfo getBatteryInfo(final BatteryStatsHelper statsHelper, final String tag) {
public BatteryInfo getBatteryInfo(final String tag) {
final BatteryStatsManager systemService = mContext.getSystemService(
BatteryStatsManager.class);
final BatteryUsageStats batteryUsageStats = systemService.getBatteryUsageStats(
new BatteryUsageStatsQuery.Builder().includeBatteryHistory().build());
final long startTime = System.currentTimeMillis();
// Stuff we always need to get BatteryInfo
final Intent batteryBroadcast = mContext.registerReceiver(null,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
final long elapsedRealtimeUs = PowerUtil.convertMsToUs(
SystemClock.elapsedRealtime());
final BatteryStats stats = statsHelper.getStats();
BatteryInfo batteryInfo;
Estimate estimate = getEnhancedEstimate();
// couldn't get estimate from cache or provider, use fallback
if (estimate == null) {
estimate = new Estimate(
PowerUtil.convertUsToMs(stats.computeBatteryTimeRemaining(elapsedRealtimeUs)),
PowerUtil.convertUsToMs(batteryUsageStats.getBatteryTimeRemainingMs()),
false /* isBasedOnUsage */,
EstimateKt.AVERAGE_TIME_TO_DISCHARGE_UNKNOWN);
}
BatteryUtils.logRuntime(tag, "BatteryInfoLoader post query", startTime);
batteryInfo = BatteryInfo.getBatteryInfo(mContext, batteryBroadcast, stats,
estimate, elapsedRealtimeUs, false /* shortString */);
batteryInfo = BatteryInfo.getBatteryInfo(mContext, batteryBroadcast,
batteryUsageStats, estimate, elapsedRealtimeUs, false /* shortString */);
BatteryUtils.logRuntime(tag, "BatteryInfoLoader.loadInBackground", startTime);
return batteryInfo;

View File

@@ -19,6 +19,8 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryStats;
import android.os.BatteryStatsManager;
import android.os.BatteryUsageStats;
import android.os.SystemClock;
import com.android.internal.os.BatteryStatsHelper;
@@ -56,15 +58,17 @@ public class DebugEstimatesLoader extends AsyncLoaderCompat<List<BatteryInfo>> {
Intent batteryBroadcast = getContext().registerReceiver(null,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
BatteryStats stats = mStatsHelper.getStats();
BatteryUsageStats batteryUsageStats =
context.getSystemService(BatteryStatsManager.class).getBatteryUsageStats();
BatteryInfo oldinfo = BatteryInfo.getBatteryInfoOld(getContext(), batteryBroadcast,
stats, elapsedRealtimeUs, false);
batteryUsageStats, elapsedRealtimeUs, false);
Estimate estimate = powerUsageFeatureProvider.getEnhancedBatteryPrediction(context);
if (estimate == null) {
estimate = new Estimate(0, false, EstimateKt.AVERAGE_TIME_TO_DISCHARGE_UNKNOWN);
}
BatteryInfo newInfo = BatteryInfo.getBatteryInfo(getContext(), batteryBroadcast, stats,
BatteryInfo newInfo = BatteryInfo.getBatteryInfo(getContext(), batteryBroadcast,
batteryUsageStats,
estimate, elapsedRealtimeUs, false);
List<BatteryInfo> infos = new ArrayList<>();

View File

@@ -141,6 +141,11 @@ public class PowerUsageAdvanced extends PowerUsageBase {
return controllers;
}
@Override
protected boolean isBatteryHistoryNeeded() {
return true;
}
@Override
protected void refreshUi(@BatteryUpdateType int refreshType) {
final Context context = getContext();

View File

@@ -19,10 +19,12 @@ import static com.android.settings.fuelgauge.BatteryBroadcastReceiver.BatteryUpd
import android.app.Activity;
import android.content.Context;
import android.os.BatteryUsageStats;
import android.os.Bundle;
import android.os.UserManager;
import android.view.Menu;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.Loader;
@@ -40,12 +42,29 @@ public abstract class PowerUsageBase extends DashboardFragment {
static final int MENU_STATS_REFRESH = Menu.FIRST + 1;
private static final String TAG = "PowerUsageBase";
private static final String KEY_REFRESH_TYPE = "refresh_type";
private static final String KEY_INCLUDE_HISTORY = "include_history";
private static final int LOADER_BATTERY_STATS_HELPER = 0;
private static final int LOADER_BATTERY_USAGE_STATS = 1;
protected BatteryStatsHelper mStatsHelper;
@VisibleForTesting
BatteryUsageStats mBatteryUsageStats;
protected UserManager mUm;
private BatteryBroadcastReceiver mBatteryBroadcastReceiver;
protected boolean mIsBatteryPresent = true;
// TODO(b/180630447): switch to BatteryUsageStatsLoader and remove all references to
// BatteryStatsHelper and BatterySipper
@VisibleForTesting
final BatteryStatsHelperLoaderCallbacks mBatteryStatsHelperLoaderCallbacks =
new BatteryStatsHelperLoaderCallbacks();
@VisibleForTesting
final BatteryUsageStatsLoaderCallbacks mBatteryUsageStatsLoaderCallbacks =
new BatteryUsageStatsLoaderCallbacks();
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
@@ -83,42 +102,73 @@ public abstract class PowerUsageBase extends DashboardFragment {
protected void restartBatteryStatsLoader(int refreshType) {
final Bundle bundle = new Bundle();
bundle.putInt(KEY_REFRESH_TYPE, refreshType);
bundle.putBoolean(KEY_INCLUDE_HISTORY, isBatteryHistoryNeeded());
getLoaderManager().restartLoader(LOADER_BATTERY_STATS_HELPER, bundle,
mBatteryStatsHelperLoaderCallbacks);
getLoaderManager().restartLoader(LOADER_BATTERY_USAGE_STATS, bundle,
mBatteryUsageStatsLoaderCallbacks);
}
getLoaderManager().restartLoader(0, bundle, new PowerLoaderCallback());
private void onLoadFinished(@BatteryUpdateType int refreshType) {
// Wait for both loaders to finish before proceeding.
if (mStatsHelper == null || mBatteryUsageStats == null) {
return;
}
refreshUi(refreshType);
}
protected abstract void refreshUi(@BatteryUpdateType int refreshType);
protected abstract boolean isBatteryHistoryNeeded();
protected void updatePreference(BatteryHistoryPreference historyPref) {
final long startTime = System.currentTimeMillis();
historyPref.setStats(mStatsHelper);
historyPref.setBatteryUsageStats(mBatteryUsageStats);
BatteryUtils.logRuntime(TAG, "updatePreference", startTime);
}
/**
* {@link android.app.LoaderManager.LoaderCallbacks} for {@link PowerUsageBase} to load
* the {@link BatteryStatsHelper}
*/
public class PowerLoaderCallback implements LoaderManager.LoaderCallbacks<BatteryStatsHelper> {
private class BatteryStatsHelperLoaderCallbacks
implements LoaderManager.LoaderCallbacks<BatteryStatsHelper> {
private int mRefreshType;
@Override
public Loader<BatteryStatsHelper> onCreateLoader(int id,
Bundle args) {
public Loader<BatteryStatsHelper> onCreateLoader(int id, Bundle args) {
mRefreshType = args.getInt(KEY_REFRESH_TYPE);
return new BatteryStatsHelperLoader(getContext());
}
@Override
public void onLoadFinished(Loader<BatteryStatsHelper> loader,
BatteryStatsHelper statsHelper) {
mStatsHelper = statsHelper;
refreshUi(mRefreshType);
BatteryStatsHelper batteryHelper) {
mStatsHelper = batteryHelper;
PowerUsageBase.this.onLoadFinished(mRefreshType);
}
@Override
public void onLoaderReset(Loader<BatteryStatsHelper> loader) {
}
}
private class BatteryUsageStatsLoaderCallbacks
implements LoaderManager.LoaderCallbacks<BatteryUsageStats> {
private int mRefreshType;
@Override
@NonNull
public Loader<BatteryUsageStats> onCreateLoader(int id, Bundle args) {
mRefreshType = args.getInt(KEY_REFRESH_TYPE);
return new BatteryUsageStatsLoader(getContext(), args.getBoolean(KEY_INCLUDE_HISTORY));
}
@Override
public void onLoadFinished(Loader<BatteryUsageStats> loader,
BatteryUsageStats batteryUsageStats) {
mBatteryUsageStats = batteryUsageStats;
PowerUsageBase.this.onLoadFinished(mRefreshType);
}
@Override
public void onLoaderReset(Loader<BatteryUsageStats> loader) {
}
}
}

View File

@@ -88,7 +88,7 @@ public class PowerUsageSummary extends PowerUsageBase implements
@Override
public Loader<BatteryInfo> onCreateLoader(int i, Bundle bundle) {
return new BatteryInfoLoader(getContext(), mStatsHelper);
return new BatteryInfoLoader(getContext());
}
@Override
@@ -190,6 +190,11 @@ public class PowerUsageSummary extends PowerUsageBase implements
return R.string.help_url_battery;
}
@Override
protected boolean isBatteryHistoryNeeded() {
return false;
}
protected void refreshUi(@BatteryUpdateType int refreshType) {
final Context context = getContext();
if (context == null) {

View File

@@ -65,12 +65,11 @@ public class BatteryTipLoader extends AsyncLoaderCompat<List<BatteryTip>> {
}
final List<BatteryTip> tips = new ArrayList<>();
final BatteryTipPolicy policy = new BatteryTipPolicy(getContext());
final BatteryInfo batteryInfo = mBatteryUtils.getBatteryInfo(mBatteryStatsHelper, TAG);
final BatteryInfo batteryInfo = mBatteryUtils.getBatteryInfo(TAG);
final Context context = getContext();
tips.add(new LowBatteryDetector(context, policy, batteryInfo).detect());
tips.add(new HighUsageDetector(context, policy, mBatteryStatsHelper,
batteryInfo.discharging).detect());
tips.add(new HighUsageDetector(context, policy, mBatteryStatsHelper, batteryInfo).detect());
tips.add(new SmartBatteryDetector(policy, context.getContentResolver()).detect());
tips.add(new EarlyWarningDetector(policy, context).detect());
tips.add(new BatteryDefenderDetector(batteryInfo).detect());

View File

@@ -45,6 +45,7 @@ import java.util.concurrent.TimeUnit;
public class HighUsageDetector implements BatteryTipDetector {
private BatteryTipPolicy mPolicy;
private BatteryStatsHelper mBatteryStatsHelper;
private final BatteryInfo mBatteryInfo;
private List<AppInfo> mHighUsageAppList;
@VisibleForTesting
HighUsageDataParser mDataParser;
@@ -54,14 +55,15 @@ public class HighUsageDetector implements BatteryTipDetector {
boolean mDischarging;
public HighUsageDetector(Context context, BatteryTipPolicy policy,
BatteryStatsHelper batteryStatsHelper, boolean discharging) {
BatteryStatsHelper batteryStatsHelper, BatteryInfo batteryInfo) {
mPolicy = policy;
mBatteryStatsHelper = batteryStatsHelper;
mBatteryInfo = batteryInfo;
mHighUsageAppList = new ArrayList<>();
mBatteryUtils = BatteryUtils.getInstance(context);
mDataParser = new HighUsageDataParser(mPolicy.highUsagePeriodMs,
mPolicy.highUsageBatteryDraining);
mDischarging = discharging;
mDischarging = batteryInfo.discharging;
}
@Override
@@ -115,6 +117,6 @@ public class HighUsageDetector implements BatteryTipDetector {
@VisibleForTesting
void parseBatteryData() {
BatteryInfo.parse(mBatteryStatsHelper.getStats(), mDataParser);
mBatteryInfo.parseBatteryHistory(mDataParser);
}
}