[Robustness] protect get battery stats from DeadSystemException

In some corner cases, we will receive the DeadSystemException from
BatteryStatService when invoking getBatteryUsageStats() method. Before
this issue is resolved by BatteryStatService team, we will add some
protections to avoid Settings app is crashed.

Bug: 195306545
Bug: 195467687
Test: make SettingsRoboTests
Change-Id: I75fcf63f4f69d86d6dce0e12bd4d738b1219ae47
This commit is contained in:
ykhung
2021-08-09 15:14:07 +08:00
committed by YUKAI HUNG
parent 7863a996a7
commit ad346e12d5
5 changed files with 41 additions and 10 deletions

View File

@@ -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(
new BatteryUsageStatsQuery.Builder().includeBatteryHistory().build());
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();