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