[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:
@@ -162,7 +162,9 @@ public class BatteryInfo {
|
|||||||
.getBatteryUsageStats();
|
.getBatteryUsageStats();
|
||||||
} catch (RuntimeException e) {
|
} catch (RuntimeException e) {
|
||||||
Log.e(TAG, "getBatteryInfo() from getBatteryUsageStats()", 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);
|
return getBatteryInfo(context, stats, shortString);
|
||||||
|
@@ -48,7 +48,9 @@ public class BatteryUsageStatsLoader extends AsyncLoaderCompat<BatteryUsageStats
|
|||||||
return mBatteryStatsManager.getBatteryUsageStats(builder.build());
|
return mBatteryStatsManager.getBatteryUsageStats(builder.build());
|
||||||
} catch (RuntimeException e) {
|
} catch (RuntimeException e) {
|
||||||
Log.e(TAG, "loadInBackground() for getBatteryUsageStats()", e);
|
Log.e(TAG, "loadInBackground() for getBatteryUsageStats()", e);
|
||||||
return null;
|
// Use default BatteryUsageStats.
|
||||||
|
return new BatteryUsageStats.Builder(
|
||||||
|
new String[0], /* includePowerModels */ false).build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -373,8 +373,11 @@ public class BatteryUtils {
|
|||||||
batteryUsageStats = systemService.getBatteryUsageStats(
|
batteryUsageStats = systemService.getBatteryUsageStats(
|
||||||
new BatteryUsageStatsQuery.Builder().includeBatteryHistory().build());
|
new BatteryUsageStatsQuery.Builder().includeBatteryHistory().build());
|
||||||
} catch (RuntimeException e) {
|
} catch (RuntimeException e) {
|
||||||
Log.e(TAG, "getBatteryInfo() from getBatteryUsageStats()", e);
|
Log.e(TAG, "getBatteryInfo() error from getBatteryUsageStats()", e);
|
||||||
return null;
|
// Use default BatteryUsageStats.
|
||||||
|
batteryUsageStats =
|
||||||
|
new BatteryUsageStats.Builder(new String[0], /* includePowerModels */ false)
|
||||||
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
final long startTime = System.currentTimeMillis();
|
final long startTime = System.currentTimeMillis();
|
||||||
|
@@ -25,7 +25,6 @@ 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;
|
||||||
@@ -104,10 +103,6 @@ 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);
|
||||||
@@ -131,10 +126,6 @@ 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,7 +18,6 @@ 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;
|
||||||
|
|
||||||
@@ -68,16 +67,11 @@ 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());
|
||||||
Collections.sort(tips);
|
Collections.sort(tips);
|
||||||
return tips;
|
return tips;
|
||||||
|
Reference in New Issue
Block a user