[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:
@@ -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 {
|
||||
stats = context.getSystemService(BatteryStatsManager.class)
|
||||
.getBatteryUsageStats();
|
||||
try {
|
||||
stats = context.getSystemService(BatteryStatsManager.class)
|
||||
.getBatteryUsageStats();
|
||||
} catch (RuntimeException e) {
|
||||
Log.e(TAG, "getBatteryInfo() from getBatteryUsageStats()", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return getBatteryInfo(context, stats, shortString);
|
||||
}
|
||||
|
Reference in New Issue
Block a user