Add NullPtrCheck when smearing in settings

This only happens in new device with broken power_profile.xml, in
which it doesn't have screen power model.

This CL catches the null pointer crash and log it.

Change-Id: Iac23e3a8fc51385bdb22979c4b59000319f6fd49
Fixes: 74617269
Test: RunSettingsRoboTests
This commit is contained in:
Lei Yu
2018-04-06 14:50:05 -07:00
parent 78e2cad8b1
commit c5f5b18b22
2 changed files with 17 additions and 0 deletions

View File

@@ -214,6 +214,11 @@ public class BatteryUtils {
} }
if (totalActivityTimeMs >= 10 * DateUtils.MINUTE_IN_MILLIS) { if (totalActivityTimeMs >= 10 * DateUtils.MINUTE_IN_MILLIS) {
if (screenSipper == null) {
Log.e(TAG, "screen sipper is null even when app screen time is not zero");
return;
}
final double screenPowerMah = screenSipper.totalPowerMah; final double screenPowerMah = screenSipper.totalPowerMah;
for (int i = 0, size = sippers.size(); i < size; i++) { for (int i = 0, size = sippers.size(); i < size; i++) {
final BatterySipper sipper = sippers.get(i); final BatterySipper sipper = sippers.get(i);

View File

@@ -376,6 +376,18 @@ public class BatteryUtilsTest {
BATTERY_APP_USAGE + BATTERY_SCREEN_USAGE / 2); BATTERY_APP_USAGE + BATTERY_SCREEN_USAGE / 2);
} }
@Test
public void testSmearScreenBatterySipper_screenSipperNull_shouldNotCrash() {
final BatterySipper sipperFg = createTestSmearBatterySipper(TIME_FOREGROUND,
BATTERY_APP_USAGE, 2 /* uid */, false /* isUidNull */);
final List<BatterySipper> sippers = new ArrayList<>();
sippers.add(sipperFg);
// Shouldn't crash
mBatteryUtils.smearScreenBatterySipper(sippers, null /* screenSipper */);
}
@Test @Test
public void testCalculateRunningTimeBasedOnStatsType() { public void testCalculateRunningTimeBasedOnStatsType() {
assertThat(mBatteryUtils.calculateRunningTimeBasedOnStatsType(mBatteryStatsHelper, assertThat(mBatteryUtils.calculateRunningTimeBasedOnStatsType(mBatteryStatsHelper,