[Robustness] protect get battery stats from DeadSystemException

Refine the original workaround patch in the ag/15527095 from NPE to
return default BatteryUsageStats instance as we merged in the
ag/15919139 (align the same solution).

Bug: 195306545
Test: make SettingsRoboTests
Change-Id: Ic03e8296d16ecb8629155f75727e9cde48c303eb
This commit is contained in:
ykhung
2021-09-28 17:30:04 +08:00
committed by YUKAI HUNG
parent d9db89af86
commit e893a890ca
5 changed files with 12 additions and 20 deletions

View File

@@ -162,7 +162,9 @@ public class BatteryInfo {
.getBatteryUsageStats();
} catch (RuntimeException e) {
Log.e(TAG, "getBatteryInfo() from getBatteryUsageStats()", e);
return null;
// Use default BatteryUsageStats.
stats = new BatteryUsageStats.Builder(
new String[0], /* includePowerModels */ false).build();
}
}
return getBatteryInfo(context, stats, shortString);

View File

@@ -48,7 +48,9 @@ public class BatteryUsageStatsLoader extends AsyncLoaderCompat<BatteryUsageStats
return mBatteryStatsManager.getBatteryUsageStats(builder.build());
} catch (RuntimeException e) {
Log.e(TAG, "loadInBackground() for getBatteryUsageStats()", e);
return null;
// Use default BatteryUsageStats.
return new BatteryUsageStats.Builder(
new String[0], /* includePowerModels */ false).build();
}
}

View File

@@ -373,8 +373,11 @@ public class BatteryUtils {
batteryUsageStats = systemService.getBatteryUsageStats(
new BatteryUsageStatsQuery.Builder().includeBatteryHistory().build());
} catch (RuntimeException e) {
Log.e(TAG, "getBatteryInfo() from getBatteryUsageStats()", e);
return null;
Log.e(TAG, "getBatteryInfo() error from getBatteryUsageStats()", e);
// Use default BatteryUsageStats.
batteryUsageStats =
new BatteryUsageStats.Builder(new String[0], /* includePowerModels */ false)
.build();
}
final long startTime = System.currentTimeMillis();

View File

@@ -25,7 +25,6 @@ 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;
@@ -104,10 +103,6 @@ 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);
@@ -131,10 +126,6 @@ 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);

View File

@@ -18,7 +18,6 @@ package com.android.settings.fuelgauge.batterytip;
import android.content.Context;
import android.os.BatteryUsageStats;
import android.util.Log;
import androidx.annotation.VisibleForTesting;
@@ -68,16 +67,11 @@ 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());
Collections.sort(tips);
return tips;