Merge "Fix b/241872474 Battery usage page will crash when selecting the last hour chart bar, going to app detail page, and going back"

This commit is contained in:
TreeHugger Robot
2022-08-09 10:54:16 +00:00
committed by Android (Google) Code Review
4 changed files with 38 additions and 53 deletions

View File

@@ -284,7 +284,6 @@ public class BatteryChartPreferenceControllerV2 extends AbstractPreferenceContro
generateTimestampDayOfWeekTexts(
mContext, batteryLevelData.getDailyBatteryLevels().getTimestamps(),
/* isAbbreviation= */ true),
mDailyChartIndex,
BatteryChartViewModel.AxisLabelPosition.CENTER_OF_TRAPEZOIDS);
mHourlyViewModels = new ArrayList<>();
for (BatteryLevelData.PeriodBatteryLevelData hourlyBatteryLevelsPerDay :
@@ -293,7 +292,6 @@ public class BatteryChartPreferenceControllerV2 extends AbstractPreferenceContro
hourlyBatteryLevelsPerDay.getLevels(),
generateTimestampHourTexts(
mContext, hourlyBatteryLevelsPerDay.getTimestamps()),
mHourlyChartIndex,
BatteryChartViewModel.AxisLabelPosition.BETWEEN_TRAPEZOIDS));
}
refreshUi();

View File

@@ -41,22 +41,18 @@ class BatteryChartViewModel {
private final List<Integer> mLevels;
private final List<String> mTexts;
private final AxisLabelPosition mAxisLabelPosition;
private int mSelectedIndex;
private int mSelectedIndex = SELECTED_INDEX_ALL;
BatteryChartViewModel(
@NonNull List<Integer> levels, @NonNull List<String> texts, int selectedIndex,
@NonNull List<Integer> levels, @NonNull List<String> texts,
@NonNull AxisLabelPosition axisLabelPosition) {
Preconditions.checkArgument(
levels.size() == texts.size()
&& levels.size() >= MIN_LEVELS_DATA_SIZE
&& selectedIndex >= SELECTED_INDEX_ALL
&& selectedIndex < levels.size(),
String.format(Locale.ENGLISH, "Invalid BatteryChartViewModel"
+ " levels.size: %d\ntexts.size: %d\nselectedIndex: %d.",
levels.size(), texts.size(), selectedIndex));
levels.size() == texts.size() && levels.size() >= MIN_LEVELS_DATA_SIZE,
String.format(Locale.ENGLISH,
"Invalid BatteryChartViewModel levels.size: %d, texts.size: %d.",
levels.size(), texts.size()));
mLevels = levels;
mTexts = texts;
mSelectedIndex = selectedIndex;
mAxisLabelPosition = axisLabelPosition;
}
@@ -72,6 +68,10 @@ class BatteryChartViewModel {
return mTexts;
}
public AxisLabelPosition axisLabelPosition() {
return mAxisLabelPosition;
}
public int selectedIndex() {
return mSelectedIndex;
}
@@ -80,10 +80,6 @@ class BatteryChartViewModel {
mSelectedIndex = index;
}
public AxisLabelPosition axisLabelPosition() {
return mAxisLabelPosition;
}
@Override
public int hashCode() {
return Objects.hash(mLevels, mTexts, mSelectedIndex, mAxisLabelPosition);
@@ -99,15 +95,15 @@ class BatteryChartViewModel {
final BatteryChartViewModel batteryChartViewModel = (BatteryChartViewModel) other;
return Objects.equals(mLevels, batteryChartViewModel.mLevels)
&& Objects.equals(mTexts, batteryChartViewModel.mTexts)
&& mSelectedIndex == batteryChartViewModel.mSelectedIndex
&& mAxisLabelPosition == batteryChartViewModel.mAxisLabelPosition;
&& mAxisLabelPosition == batteryChartViewModel.mAxisLabelPosition
&& mSelectedIndex == batteryChartViewModel.mSelectedIndex;
}
@Override
public String toString() {
return String.format(Locale.ENGLISH,
"levels: %s\ntexts: %s\nselectedIndex: %d, axisLabelPosition: %s",
Objects.toString(mLevels), Objects.toString(mTexts), mSelectedIndex,
mAxisLabelPosition);
"levels: %s,\ntexts: %s,\naxisLabelPosition: %s, selectedIndex: %d",
Objects.toString(mLevels), Objects.toString(mTexts), mAxisLabelPosition,
mSelectedIndex);
}
}