Merge "[Robustness] protect get battery stats from DeadSystemException" into sc-v2-dev
This commit is contained in:
@@ -25,6 +25,7 @@ import android.os.BatteryStatsManager;
|
||||
import android.os.BatteryUsageStats;
|
||||
import android.os.SystemClock;
|
||||
import android.text.format.Formatter;
|
||||
import android.util.Log;
|
||||
import android.util.SparseIntArray;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
@@ -42,6 +43,7 @@ import com.android.settingslib.utils.PowerUtil;
|
||||
import com.android.settingslib.utils.StringUtil;
|
||||
|
||||
public class BatteryInfo {
|
||||
private static final String TAG = "BatteryInfo";
|
||||
|
||||
public CharSequence chargeLabel;
|
||||
public CharSequence remainingLabel;
|
||||
@@ -155,8 +157,13 @@ public class BatteryInfo {
|
||||
if (batteryUsageStats != null) {
|
||||
stats = batteryUsageStats;
|
||||
} else {
|
||||
try {
|
||||
stats = context.getSystemService(BatteryStatsManager.class)
|
||||
.getBatteryUsageStats();
|
||||
} catch (RuntimeException e) {
|
||||
Log.e(TAG, "getBatteryInfo() from getBatteryUsageStats()", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return getBatteryInfo(context, stats, shortString);
|
||||
}
|
||||
|
@@ -20,6 +20,7 @@ import android.content.Context;
|
||||
import android.os.BatteryStatsManager;
|
||||
import android.os.BatteryUsageStats;
|
||||
import android.os.BatteryUsageStatsQuery;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.settingslib.utils.AsyncLoaderCompat;
|
||||
|
||||
@@ -27,6 +28,7 @@ import com.android.settingslib.utils.AsyncLoaderCompat;
|
||||
* Loader to get new {@link BatteryUsageStats} in the background
|
||||
*/
|
||||
public class BatteryUsageStatsLoader extends AsyncLoaderCompat<BatteryUsageStats> {
|
||||
private static final String TAG = "BatteryUsageStatsLoader";
|
||||
private final BatteryStatsManager mBatteryStatsManager;
|
||||
private final boolean mIncludeBatteryHistory;
|
||||
|
||||
@@ -42,7 +44,12 @@ public class BatteryUsageStatsLoader extends AsyncLoaderCompat<BatteryUsageStats
|
||||
if (mIncludeBatteryHistory) {
|
||||
builder.includeBatteryHistory();
|
||||
}
|
||||
try {
|
||||
return mBatteryStatsManager.getBatteryUsageStats(builder.build());
|
||||
} catch (RuntimeException e) {
|
||||
Log.e(TAG, "loadInBackground() for getBatteryUsageStats()", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -368,8 +368,14 @@ public class BatteryUtils {
|
||||
public BatteryInfo getBatteryInfo(final String tag) {
|
||||
final BatteryStatsManager systemService = mContext.getSystemService(
|
||||
BatteryStatsManager.class);
|
||||
final BatteryUsageStats batteryUsageStats = systemService.getBatteryUsageStats(
|
||||
BatteryUsageStats batteryUsageStats;
|
||||
try {
|
||||
batteryUsageStats = systemService.getBatteryUsageStats(
|
||||
new BatteryUsageStatsQuery.Builder().includeBatteryHistory().build());
|
||||
} catch (RuntimeException e) {
|
||||
Log.e(TAG, "getBatteryInfo() from getBatteryUsageStats()", e);
|
||||
return null;
|
||||
}
|
||||
|
||||
final long startTime = System.currentTimeMillis();
|
||||
|
||||
|
@@ -25,6 +25,7 @@ import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.provider.Settings.Global;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.loader.app.LoaderManager;
|
||||
@@ -103,6 +104,10 @@ public class PowerUsageSummary extends PowerUsageBase implements
|
||||
|
||||
@Override
|
||||
public void onLoadFinished(Loader<BatteryInfo> loader, BatteryInfo batteryInfo) {
|
||||
if (batteryInfo == null) {
|
||||
Log.w(TAG, "mBatteryInfoLoaderCallbacks: batteryInfo = null");
|
||||
return;
|
||||
}
|
||||
mBatteryHeaderPreferenceController.updateHeaderPreference(batteryInfo);
|
||||
mBatteryHeaderPreferenceController.updateHeaderByBatteryTips(
|
||||
mBatteryTipPreferenceController.getCurrentBatteryTip(), batteryInfo);
|
||||
@@ -126,6 +131,10 @@ public class PowerUsageSummary extends PowerUsageBase implements
|
||||
@Override
|
||||
public void onLoadFinished(Loader<List<BatteryTip>> loader,
|
||||
List<BatteryTip> data) {
|
||||
if (mBatteryInfo == null) {
|
||||
Log.w(TAG, "mBatteryTipsCallbacks: batteryInfo = null");
|
||||
return;
|
||||
}
|
||||
mBatteryTipPreferenceController.updateBatteryTips(data);
|
||||
mBatteryHeaderPreferenceController.updateHeaderByBatteryTips(
|
||||
mBatteryTipPreferenceController.getCurrentBatteryTip(), mBatteryInfo);
|
||||
|
@@ -18,6 +18,7 @@ package com.android.settings.fuelgauge.batterytip;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.BatteryUsageStats;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
@@ -67,16 +68,17 @@ public class BatteryTipLoader extends AsyncLoaderCompat<List<BatteryTip>> {
|
||||
final BatteryInfo batteryInfo = mBatteryUtils.getBatteryInfo(TAG);
|
||||
final Context context = getContext();
|
||||
|
||||
tips.add(new EarlyWarningDetector(policy, context).detect());
|
||||
if (batteryInfo == null) {
|
||||
Log.w(TAG, "loadInBackground() batteryInfo = null");
|
||||
return tips;
|
||||
}
|
||||
|
||||
tips.add(new LowBatteryDetector(context, policy, batteryInfo).detect());
|
||||
tips.add(new HighUsageDetector(context, policy, mBatteryUsageStats, batteryInfo).detect());
|
||||
tips.add(new SmartBatteryDetector(
|
||||
context, policy, batteryInfo, context.getContentResolver()).detect());
|
||||
tips.add(new EarlyWarningDetector(policy, context).detect());
|
||||
tips.add(new BatteryDefenderDetector(batteryInfo).detect());
|
||||
// Disable this feature now since it introduces false positive cases. We will try to improve
|
||||
// it in the future.
|
||||
// tips.add(new RestrictAppDetector(context, policy).detect());
|
||||
|
||||
Collections.sort(tips);
|
||||
return tips;
|
||||
}
|
||||
|
Reference in New Issue
Block a user