Add NPE check for screen and cellular update.

When battery is full of charge, usageList in BatteryStatsHelper
will be empty. As a result, we cannot find BatterySipper with
type screen and cell, which creates the null pointer error.

In this cl, I add NPE check for sipper. When sipper is null, all
data will be set to default value. I also add check for totalPower
so we won't have divided by zero error.

Bug: 35757789
Test: RunSettingsRoboTests
Change-Id: I80f3c0c542e0a50868e7c314a8d9b3c17999d8c6
This commit is contained in:
jackqdyulei
2017-02-24 13:03:35 -08:00
parent 7e0df1e48c
commit 906055e383
2 changed files with 24 additions and 8 deletions

View File

@@ -422,7 +422,7 @@ public class PowerUsageSummary extends PowerUsageBase {
// With deduction in totalPower, percentOfTotal is higher because it adds the part
// used in screen, system, etc
final double percentOfTotal =
final double percentOfTotal = totalPower == 0 ? 0 :
((sipper.totalPowerMah / totalPower) * dischargeAmount);
if (((int) (percentOfTotal + .5)) < 1) {
@@ -513,10 +513,12 @@ public class PowerUsageSummary extends PowerUsageBase {
final BatterySipper sipper = findBatterySipperByType(
mStatsHelper.getUsageList(), DrainType.SCREEN);
final Context context = getContext();
final double percentOfTotal = calculatePercentage(sipper.totalPowerMah, dischargeAmount);
final double totalPowerMah = sipper != null ? sipper.totalPowerMah : 0;
final long usageTimeMs = sipper != null ? sipper.usageTimeMs : 0;
final double percentOfTotal = calculatePercentage(totalPowerMah, dischargeAmount);
mScreenUsagePref.setSummary(getString(R.string.battery_used_for,
Utils.formatElapsedTime(context, sipper.usageTimeMs, false)));
Utils.formatElapsedTime(context, usageTimeMs, false)));
mScreenConsumptionPref.setSummary(getString(R.string.battery_overall_usage,
Utils.formatPercentage(percentOfTotal, true)));
}
@@ -525,7 +527,8 @@ public class PowerUsageSummary extends PowerUsageBase {
void updateCellularPreference(final int dischargeAmount) {
final BatterySipper sipper = findBatterySipperByType(
mStatsHelper.getUsageList(), DrainType.CELL);
final double percentOfTotal = calculatePercentage(sipper.totalPowerMah, dischargeAmount);
final double totalPowerMah = sipper != null ? sipper.totalPowerMah : 0;
final double percentOfTotal = calculatePercentage(totalPowerMah, dischargeAmount);
mCellularNetworkPref.setSummary(getString(R.string.battery_overall_usage,
Utils.formatPercentage(percentOfTotal, true)));
}
@@ -556,7 +559,9 @@ public class PowerUsageSummary extends PowerUsageBase {
@VisibleForTesting
double calculatePercentage(double powerUsage, double dischargeAmount) {
return ((powerUsage / mStatsHelper.getTotalPower()) * dischargeAmount);
final double totalPower = mStatsHelper.getTotalPower();
return totalPower == 0 ? 0 :
((powerUsage / totalPower) * dischargeAmount);
}
@VisibleForTesting