Fix incorrect battery history merging logic to cause data losing

Bug: 184807417
Test: make SettingsRoboTests
Test: make SettingsGoogleRoboTests
Change-Id: Ic26799f4d95b9f990e873ee26a42d6cc0c404659
This commit is contained in:
ykhung
2021-04-25 16:09:59 +08:00
committed by YUKAI HUNG
parent 41fd8a7494
commit 5e578ddd13
2 changed files with 7 additions and 5 deletions

View File

@@ -203,10 +203,11 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
Collections.sort(batteryHistoryKeyList); Collections.sort(batteryHistoryKeyList);
validateSlotTimestamp(batteryHistoryKeyList); validateSlotTimestamp(batteryHistoryKeyList);
mBatteryHistoryKeys = new long[CHART_KEY_ARRAY_SIZE]; mBatteryHistoryKeys = new long[CHART_KEY_ARRAY_SIZE];
final int elementSize = Math.min(batteryHistoryKeyList.size(), CHART_KEY_ARRAY_SIZE); final int listSize = batteryHistoryKeyList.size();
final int offset = CHART_KEY_ARRAY_SIZE - elementSize; final int elementSize = Math.min(listSize, CHART_KEY_ARRAY_SIZE);
for (int index = 0; index < elementSize; index++) { for (int index = 0; index < elementSize; index++) {
mBatteryHistoryKeys[index + offset] = batteryHistoryKeyList.get(index); mBatteryHistoryKeys[CHART_KEY_ARRAY_SIZE - index - 1] =
batteryHistoryKeyList.get(listSize - index - 1);
} }
// Generates the battery history levels. // Generates the battery history levels.
@@ -489,7 +490,7 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
} else { } else {
usageTimeSummary = buildUsageTimeInfo(totalUsageTimeInMs, false); usageTimeSummary = buildUsageTimeInfo(totalUsageTimeInMs, false);
// Shows background usage time if it is larger than a minute. // Shows background usage time if it is larger than a minute.
if (backgroundUsageTimeInMs >= DateUtils.MINUTE_IN_MILLIS) { if (backgroundUsageTimeInMs > 0) {
usageTimeSummary += usageTimeSummary +=
"\n" + buildUsageTimeInfo(backgroundUsageTimeInMs, true); "\n" + buildUsageTimeInfo(backgroundUsageTimeInMs, true);
} }

View File

@@ -370,7 +370,8 @@ public final class BatteryChartPreferenceControllerTest {
pref, createBatteryDiffEntry( pref, createBatteryDiffEntry(
/*foregroundUsageTimeInMs=*/ DateUtils.MINUTE_IN_MILLIS, /*foregroundUsageTimeInMs=*/ DateUtils.MINUTE_IN_MILLIS,
/*backgroundUsageTimeInMs=*/ 200)); /*backgroundUsageTimeInMs=*/ 200));
assertThat(pref.getSummary()).isEqualTo("Total: 1 min"); assertThat(pref.getSummary())
.isEqualTo("Total: 1 min\nBackground: less than a min");
} }
@Test @Test