Use empty BatteryDiffData instead of null to avoid NullPointerException

This is one action post P0 bug b/301362376.

Bug: 304067689
Bug: 303838369
Fix: 304067689
Fix: 303838369
Test: manual
Change-Id: I8bb6bfe9c3fca78ab19dfbfd2a184b0f92f4ef09
This commit is contained in:
Zaiyue Xue
2023-10-08 15:09:49 +08:00
parent 1735474987
commit 834e19b188
2 changed files with 17 additions and 21 deletions

View File

@@ -626,17 +626,20 @@ public final class DataProcessor {
final List<BatteryHistEntry> batteryHistEntryList,
final @NonNull Set<String> systemAppsPackageNames,
final @NonNull Set<Integer> systemAppsUids) {
final List<BatteryDiffEntry> appEntries = new ArrayList<>();
final List<BatteryDiffEntry> systemEntries = new ArrayList<>();
if (batteryHistEntryList == null || batteryHistEntryList.isEmpty()) {
Log.w(TAG, "batteryHistEntryList is null or empty in generateBatteryDiffData()");
return null;
return new BatteryDiffData(context, startTimestamp, getCurrentTimeMillis(),
/* startBatteryLevel =*/ 100, getCurrentLevel(context), /* screenOnTime= */ 0L,
appEntries, systemEntries, systemAppsPackageNames, systemAppsUids,
/* isAccumulated= */ false);
}
final int currentUserId = context.getUserId();
final UserHandle userHandle =
Utils.getManagedProfile(context.getSystemService(UserManager.class));
final int workProfileUserId =
userHandle != null ? userHandle.getIdentifier() : Integer.MIN_VALUE;
final List<BatteryDiffEntry> appEntries = new ArrayList<>();
final List<BatteryDiffEntry> systemEntries = new ArrayList<>();
for (BatteryHistEntry entry : batteryHistEntryList) {
final boolean isFromOtherUsers = isConsumedFromOtherUsers(
@@ -670,11 +673,6 @@ public final class DataProcessor {
}
}
}
// If there is no data, return null instead of empty item.
if (appEntries.isEmpty() && systemEntries.isEmpty()) {
return null;
}
return new BatteryDiffData(context, startTimestamp, getCurrentTimeMillis(),
/* startBatteryLevel =*/ 100, getCurrentLevel(context), /* screenOnTime= */ 0L,
appEntries, systemEntries, systemAppsPackageNames, systemAppsUids,
@@ -1318,7 +1316,9 @@ public final class DataProcessor {
// We should not get the empty list since we have at least one fake data to record
// the battery level and status in each time slot, the empty list is used to
// represent there is no enough data to apply interpolation arithmetic.
return null;
return new BatteryDiffData(context, startTimestamp, endTimestamp, startBatteryLevel,
endBatteryLevel, /* screenOnTime= */ 0L, appEntries, systemEntries,
systemAppsPackageNames, systemAppsUids, /* isAccumulated= */ false);
}
allBatteryHistEntryKeys.addAll(slotBatteryHistMap.keySet());
}
@@ -1458,12 +1458,6 @@ public final class DataProcessor {
appEntries.add(currentBatteryDiffEntry);
}
}
// If there is no data, return null instead of empty item.
if (appEntries.isEmpty() && systemEntries.isEmpty()) {
return null;
}
return new BatteryDiffData(context, startTimestamp, endTimestamp, startBatteryLevel,
endBatteryLevel, slotScreenOnTime, appEntries, systemEntries,
systemAppsPackageNames, systemAppsUids, /* isAccumulated= */ false);
@@ -1563,9 +1557,9 @@ public final class DataProcessor {
}
}
return diffEntryList.isEmpty() ? null : new BatteryDiffData(context, startTimestamp,
endTimestamp, startBatteryLevel, endBatteryLevel, totalScreenOnTime, appEntries,
systemEntries, /* systemAppsPackageNames= */ new ArraySet<>(),
return new BatteryDiffData(context, startTimestamp, endTimestamp, startBatteryLevel,
endBatteryLevel, totalScreenOnTime, appEntries, systemEntries,
/* systemAppsPackageNames= */ new ArraySet<>(),
/* systemAppsUids= */ new ArraySet<>(), /* isAccumulated= */ true);
}

View File

@@ -1397,12 +1397,14 @@ public final class DataProcessorTest {
}
@Test
public void generateBatteryDiffData_emptyBatteryEntryList_returnNull() {
assertThat(DataProcessor.generateBatteryDiffData(mContext,
public void generateBatteryDiffData_emptyBatteryEntryList_returnEmptyBatteryDiffData() {
final BatteryDiffData batteryDiffData = DataProcessor.generateBatteryDiffData(mContext,
System.currentTimeMillis(),
DataProcessor.convertToBatteryHistEntry(null, mBatteryUsageStats),
/* systemAppsPackageNames= */ Set.of(),
/* systemAppsUids= */ Set.of())).isNull();
/* systemAppsUids= */ Set.of());
assertThat(batteryDiffData.getAppDiffEntryList()).isEmpty();
assertThat(batteryDiffData.getSystemDiffEntryList()).isEmpty();
}
@Test