Improve X axis labels in battery chart (1)
Support showing labels under the trapezoids instead of between the trapezoids. For daily chart, the labels are under the trapezoids, for hourly chart, the labels are between the trapezoids. Test: manual Bug: 239491373 Bug: 236101166 Change-Id: I2efb5192d4baafc7745ce269224639511848293e
This commit is contained in:
@@ -279,15 +279,17 @@ public class BatteryChartPreferenceControllerV2 extends AbstractPreferenceContro
|
||||
batteryLevelData.getDailyBatteryLevels().getLevels(),
|
||||
generateTimestampDayOfWeekTexts(
|
||||
mContext, batteryLevelData.getDailyBatteryLevels().getTimestamps()),
|
||||
mDailyChartIndex);
|
||||
mDailyChartIndex,
|
||||
BatteryChartViewModel.AxisLabelPosition.CENTER_OF_TRAPEZOIDS);
|
||||
mHourlyViewModels = new ArrayList<>();
|
||||
for (BatteryLevelData.PeriodBatteryLevelData perDayData :
|
||||
for (BatteryLevelData.PeriodBatteryLevelData hourlyBatteryLevelsPerDay :
|
||||
batteryLevelData.getHourlyBatteryLevelsPerDay()) {
|
||||
mHourlyViewModels.add(new BatteryChartViewModel(
|
||||
perDayData.getLevels(),
|
||||
hourlyBatteryLevelsPerDay.getLevels(),
|
||||
generateTimestampHourTexts(
|
||||
mContext, perDayData.getTimestamps()),
|
||||
mHourlyChartIndex));
|
||||
mContext, hourlyBatteryLevelsPerDay.getTimestamps()),
|
||||
mHourlyChartIndex,
|
||||
BatteryChartViewModel.AxisLabelPosition.BETWEEN_TRAPEZOIDS));
|
||||
}
|
||||
refreshUi();
|
||||
// TODO: Loads item icon and label and build mBatteryIndexedMap.
|
||||
|
@@ -33,23 +33,31 @@ class BatteryChartViewModel {
|
||||
// We need at least 2 levels to draw a trapezoid.
|
||||
private static final int MIN_LEVELS_DATA_SIZE = 2;
|
||||
|
||||
enum AxisLabelPosition {
|
||||
BETWEEN_TRAPEZOIDS,
|
||||
CENTER_OF_TRAPEZOIDS,
|
||||
}
|
||||
|
||||
private final List<Integer> mLevels;
|
||||
private final List<String> mTexts;
|
||||
private final AxisLabelPosition mAxisLabelPosition;
|
||||
private int mSelectedIndex;
|
||||
|
||||
BatteryChartViewModel(
|
||||
@NonNull List<Integer> levels, @NonNull List<String> texts, int selectedIndex) {
|
||||
@NonNull List<Integer> levels, @NonNull List<String> texts, int selectedIndex,
|
||||
@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.getDefault(), "Invalid BatteryChartViewModel"
|
||||
String.format(Locale.ENGLISH, "Invalid BatteryChartViewModel"
|
||||
+ " levels.size: %d\ntexts.size: %d\nselectedIndex: %d.",
|
||||
levels.size(), texts.size(), selectedIndex));
|
||||
mLevels = levels;
|
||||
mTexts = texts;
|
||||
mSelectedIndex = selectedIndex;
|
||||
mAxisLabelPosition = axisLabelPosition;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
@@ -72,9 +80,13 @@ class BatteryChartViewModel {
|
||||
mSelectedIndex = index;
|
||||
}
|
||||
|
||||
public AxisLabelPosition axisLabelPosition() {
|
||||
return mAxisLabelPosition;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(mLevels, mTexts, mSelectedIndex);
|
||||
return Objects.hash(mLevels, mTexts, mSelectedIndex, mAxisLabelPosition);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -87,12 +99,15 @@ class BatteryChartViewModel {
|
||||
final BatteryChartViewModel batteryChartViewModel = (BatteryChartViewModel) other;
|
||||
return Objects.equals(mLevels, batteryChartViewModel.mLevels)
|
||||
&& Objects.equals(mTexts, batteryChartViewModel.mTexts)
|
||||
&& mSelectedIndex == batteryChartViewModel.mSelectedIndex;
|
||||
&& mSelectedIndex == batteryChartViewModel.mSelectedIndex
|
||||
&& mAxisLabelPosition == batteryChartViewModel.mAxisLabelPosition;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format(Locale.getDefault(), "levels: %s\ntexts: %s\nselectedIndex: %d",
|
||||
Objects.toString(mLevels), Objects.toString(mTexts), mSelectedIndex);
|
||||
return String.format(Locale.ENGLISH,
|
||||
"levels: %s\ntexts: %s\nselectedIndex: %d, axisLabelPosition: %s",
|
||||
Objects.toString(mLevels), Objects.toString(mTexts), mSelectedIndex,
|
||||
mAxisLabelPosition);
|
||||
}
|
||||
}
|
||||
|
@@ -441,11 +441,19 @@ public class BatteryChartViewV2 extends AppCompatImageView implements View.OnCli
|
||||
for (int index = 0; index < DEFAULT_AXIS_LABEL_COUNT; index++) {
|
||||
xOffsets[index] = baselineX + index * offsetX * slotBarOffset;
|
||||
}
|
||||
drawAxisLabel(canvas, xOffsets);
|
||||
switch (mViewModel.axisLabelPosition()) {
|
||||
case CENTER_OF_TRAPEZOIDS:
|
||||
drawAxisLabelsCenterOfTrapezoids(canvas, xOffsets, unitWidth);
|
||||
break;
|
||||
case BETWEEN_TRAPEZOIDS:
|
||||
default:
|
||||
drawAxisLabelsBetweenTrapezoids(canvas, xOffsets);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void drawAxisLabel(Canvas canvas, float[] xOffsets) {
|
||||
private void drawAxisLabelsBetweenTrapezoids(Canvas canvas, float[] xOffsets) {
|
||||
// Draws the 1st axis label info.
|
||||
canvas.drawText(
|
||||
mAxisLabels[0], xOffsets[0] - mAxisLabelsBounds[0].left, getAxisLabelY(0),
|
||||
@@ -471,6 +479,18 @@ public class BatteryChartViewV2 extends AppCompatImageView implements View.OnCli
|
||||
}
|
||||
}
|
||||
|
||||
private void drawAxisLabelsCenterOfTrapezoids(
|
||||
Canvas canvas, float[] xOffsets, float unitWidth) {
|
||||
for (int index = 0; index < DEFAULT_AXIS_LABEL_COUNT - 1; index++) {
|
||||
canvas.drawText(
|
||||
mAxisLabels[index],
|
||||
xOffsets[index] + (unitWidth - (mAxisLabelsBounds[index].width()
|
||||
- mAxisLabelsBounds[index].left)) * .5f,
|
||||
getAxisLabelY(index),
|
||||
mTextPaint);
|
||||
}
|
||||
}
|
||||
|
||||
private int getAxisLabelY(int index) {
|
||||
return getHeight()
|
||||
- mAxisLabelsBounds[index].height()
|
||||
|
@@ -178,7 +178,8 @@ public final class BatteryChartPreferenceControllerV2Test {
|
||||
verify(mHourlyChartView).setViewModel(new BatteryChartViewModel(
|
||||
List.of(100, 97, 95),
|
||||
List.of("8 am", "10 am", "12 pm"),
|
||||
BatteryChartViewModel.SELECTED_INDEX_ALL));
|
||||
BatteryChartViewModel.SELECTED_INDEX_ALL,
|
||||
BatteryChartViewModel.AxisLabelPosition.BETWEEN_TRAPEZOIDS));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -193,7 +194,8 @@ public final class BatteryChartPreferenceControllerV2Test {
|
||||
verify(mDailyChartView).setViewModel(new BatteryChartViewModel(
|
||||
List.of(100, 83, 59, 41),
|
||||
List.of("SAT", "SUN", "MON", "MON"),
|
||||
BatteryChartViewModel.SELECTED_INDEX_ALL));
|
||||
BatteryChartViewModel.SELECTED_INDEX_ALL,
|
||||
BatteryChartViewModel.AxisLabelPosition.CENTER_OF_TRAPEZOIDS));
|
||||
|
||||
reset(mDailyChartView);
|
||||
reset(mHourlyChartView);
|
||||
@@ -204,12 +206,14 @@ public final class BatteryChartPreferenceControllerV2Test {
|
||||
verify(mDailyChartView).setViewModel(new BatteryChartViewModel(
|
||||
List.of(100, 83, 59, 41),
|
||||
List.of("SAT", "SUN", "MON", "MON"),
|
||||
0));
|
||||
0,
|
||||
BatteryChartViewModel.AxisLabelPosition.CENTER_OF_TRAPEZOIDS));
|
||||
verify(mHourlyChartView).setViewModel(new BatteryChartViewModel(
|
||||
List.of(100, 97, 95, 93, 91, 89, 87, 85, 83),
|
||||
List.of("8 am", "10 am", "12 pm", "2 pm", "4 pm", "6 pm", "8 pm", "10 pm",
|
||||
"12 am"),
|
||||
BatteryChartViewModel.SELECTED_INDEX_ALL));
|
||||
BatteryChartViewModel.SELECTED_INDEX_ALL,
|
||||
BatteryChartViewModel.AxisLabelPosition.BETWEEN_TRAPEZOIDS));
|
||||
|
||||
reset(mDailyChartView);
|
||||
reset(mHourlyChartView);
|
||||
@@ -221,12 +225,14 @@ public final class BatteryChartPreferenceControllerV2Test {
|
||||
verify(mDailyChartView).setViewModel(new BatteryChartViewModel(
|
||||
List.of(100, 83, 59, 41),
|
||||
List.of("SAT", "SUN", "MON", "MON"),
|
||||
1));
|
||||
1,
|
||||
BatteryChartViewModel.AxisLabelPosition.CENTER_OF_TRAPEZOIDS));
|
||||
verify(mHourlyChartView).setViewModel(new BatteryChartViewModel(
|
||||
List.of(83, 81, 79, 77, 75, 73, 71, 69, 67, 65, 63, 61, 59),
|
||||
List.of("12 am", "2 am", "4 am", "6 am", "8 am", "10 am", "12 pm", "2 pm",
|
||||
"4 pm", "6 pm", "8 pm", "10 pm", "12 am"),
|
||||
6));
|
||||
6,
|
||||
BatteryChartViewModel.AxisLabelPosition.BETWEEN_TRAPEZOIDS));
|
||||
|
||||
reset(mDailyChartView);
|
||||
reset(mHourlyChartView);
|
||||
@@ -239,12 +245,14 @@ public final class BatteryChartPreferenceControllerV2Test {
|
||||
verify(mDailyChartView).setViewModel(new BatteryChartViewModel(
|
||||
List.of(100, 83, 59, 41),
|
||||
List.of("SAT", "SUN", "MON", "MON"),
|
||||
2));
|
||||
2,
|
||||
BatteryChartViewModel.AxisLabelPosition.CENTER_OF_TRAPEZOIDS));
|
||||
verify(mHourlyChartView).setViewModel(new BatteryChartViewModel(
|
||||
List.of(59, 57, 55, 53, 51, 49, 47, 45, 43, 41),
|
||||
List.of("12 am", "2 am", "4 am", "6 am", "8 am", "10 am", "12 pm", "2 pm",
|
||||
"4 pm", "6 pm"),
|
||||
BatteryChartViewModel.SELECTED_INDEX_ALL));
|
||||
BatteryChartViewModel.SELECTED_INDEX_ALL,
|
||||
BatteryChartViewModel.AxisLabelPosition.BETWEEN_TRAPEZOIDS));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@@ -104,7 +104,8 @@ public final class BatteryChartViewV2Test {
|
||||
final int originalSelectedIndex = 2;
|
||||
mBatteryChartView.setViewModel(
|
||||
new BatteryChartViewModel(List.of(90, 80, 70, 60), List.of("", "", "", ""),
|
||||
originalSelectedIndex));
|
||||
originalSelectedIndex,
|
||||
BatteryChartViewModel.AxisLabelPosition.BETWEEN_TRAPEZOIDS));
|
||||
for (int i = 0; i < mBatteryChartView.mTrapezoidSlots.length; i++) {
|
||||
mBatteryChartView.mTrapezoidSlots[i] = new BatteryChartViewV2.TrapezoidSlot();
|
||||
mBatteryChartView.mTrapezoidSlots[i].mLeft = i;
|
||||
@@ -136,6 +137,7 @@ public final class BatteryChartViewV2Test {
|
||||
.thenReturn(false);
|
||||
|
||||
mBatteryChartView.onAttachedToWindow();
|
||||
|
||||
assertThat(mBatteryChartView.isClickable()).isFalse();
|
||||
assertThat(mBatteryChartView.mTrapezoidCurvePaint).isNotNull();
|
||||
}
|
||||
@@ -148,6 +150,7 @@ public final class BatteryChartViewV2Test {
|
||||
doReturn(false).when(mMockAccessibilityManager).isEnabled();
|
||||
|
||||
mBatteryChartView.onAttachedToWindow();
|
||||
|
||||
assertThat(mBatteryChartView.isClickable()).isTrue();
|
||||
assertThat(mBatteryChartView.mTrapezoidCurvePaint).isNull();
|
||||
}
|
||||
@@ -163,6 +166,7 @@ public final class BatteryChartViewV2Test {
|
||||
.getEnabledAccessibilityServiceList(anyInt());
|
||||
|
||||
mBatteryChartView.onAttachedToWindow();
|
||||
|
||||
assertThat(mBatteryChartView.isClickable()).isTrue();
|
||||
assertThat(mBatteryChartView.mTrapezoidCurvePaint).isNull();
|
||||
}
|
||||
@@ -175,6 +179,7 @@ public final class BatteryChartViewV2Test {
|
||||
doReturn(true).when(mMockAccessibilityManager).isEnabled();
|
||||
|
||||
mBatteryChartView.onAttachedToWindow();
|
||||
|
||||
assertThat(mBatteryChartView.isClickable()).isFalse();
|
||||
assertThat(mBatteryChartView.mTrapezoidCurvePaint).isNotNull();
|
||||
}
|
||||
@@ -188,7 +193,8 @@ public final class BatteryChartViewV2Test {
|
||||
texts.add("");
|
||||
}
|
||||
mBatteryChartView.setViewModel(new BatteryChartViewModel(
|
||||
levels, texts, BatteryChartViewModel.SELECTED_INDEX_ALL));
|
||||
levels, texts, BatteryChartViewModel.SELECTED_INDEX_ALL,
|
||||
BatteryChartViewModel.AxisLabelPosition.BETWEEN_TRAPEZOIDS));
|
||||
mBatteryChartView.setClickableForce(true);
|
||||
when(mPowerUsageFeatureProvider.isChartGraphSlotsEnabled(mContext))
|
||||
.thenReturn(true);
|
||||
|
Reference in New Issue
Block a user