Update trapezoid validation in battery chart view.
If the battery level data cannot be gotten for an hour, ag/19358207 changes the level indication from 0 to null. This cl updates the battery chart view trapezoid validation logic from checking the level not zero to not null. Test: manual Bug: 236101166 Change-Id: I240f2b02b94ba1a3d99022c7c72a7a5adb21f9a5
This commit is contained in:
@@ -18,6 +18,7 @@ package com.android.settings.fuelgauge.batteryusage;
|
|||||||
import static com.android.settings.Utils.formatPercentage;
|
import static com.android.settings.Utils.formatPercentage;
|
||||||
|
|
||||||
import static java.lang.Math.round;
|
import static java.lang.Math.round;
|
||||||
|
import static java.util.Objects.requireNonNull;
|
||||||
|
|
||||||
import android.accessibilityservice.AccessibilityServiceInfo;
|
import android.accessibilityservice.AccessibilityServiceInfo;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
@@ -139,7 +140,7 @@ public class BatteryChartViewV2 extends AppCompatImageView implements View.OnCli
|
|||||||
|
|
||||||
initializeTrapezoidSlots(viewModel.size() - 1);
|
initializeTrapezoidSlots(viewModel.size() - 1);
|
||||||
initializeAxisLabels(viewModel.texts());
|
initializeAxisLabels(viewModel.texts());
|
||||||
setClickable(hasNonZeroTrapezoid(viewModel.levels()));
|
setClickable(hasAnyValidTrapezoid(viewModel));
|
||||||
requestLayout();
|
requestLayout();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -253,7 +254,7 @@ public class BatteryChartViewV2 extends AppCompatImageView implements View.OnCli
|
|||||||
final int trapezoidIndex = getTrapezoidIndex(mTouchUpEventX);
|
final int trapezoidIndex = getTrapezoidIndex(mTouchUpEventX);
|
||||||
// Ignores the click event if the level is zero.
|
// Ignores the click event if the level is zero.
|
||||||
if (trapezoidIndex == BatteryChartViewModel.SELECTED_INDEX_INVALID
|
if (trapezoidIndex == BatteryChartViewModel.SELECTED_INDEX_INVALID
|
||||||
|| !isValidToDraw(trapezoidIndex)) {
|
|| !isValidToDraw(mViewModel, trapezoidIndex)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (mOnSelectListener != null) {
|
if (mOnSelectListener != null) {
|
||||||
@@ -492,7 +493,7 @@ public class BatteryChartViewV2 extends AppCompatImageView implements View.OnCli
|
|||||||
Path trapezoidCurvePath = null;
|
Path trapezoidCurvePath = null;
|
||||||
for (int index = 0; index < mTrapezoidSlots.length; index++) {
|
for (int index = 0; index < mTrapezoidSlots.length; index++) {
|
||||||
// Not draws the trapezoid for corner or not initialization cases.
|
// Not draws the trapezoid for corner or not initialization cases.
|
||||||
if (!isValidToDraw(index)) {
|
if (!isValidToDraw(mViewModel, index)) {
|
||||||
if (mTrapezoidCurvePaint != null && trapezoidCurvePath != null) {
|
if (mTrapezoidCurvePaint != null && trapezoidCurvePath != null) {
|
||||||
canvas.drawPath(trapezoidCurvePath, mTrapezoidCurvePaint);
|
canvas.drawPath(trapezoidCurvePath, mTrapezoidCurvePaint);
|
||||||
trapezoidCurvePath = null;
|
trapezoidCurvePath = null;
|
||||||
@@ -504,13 +505,14 @@ public class BatteryChartViewV2 extends AppCompatImageView implements View.OnCli
|
|||||||
|| mViewModel.selectedIndex() == BatteryChartViewModel.SELECTED_INDEX_ALL)
|
|| mViewModel.selectedIndex() == BatteryChartViewModel.SELECTED_INDEX_ALL)
|
||||||
? mTrapezoidSolidColor : mTrapezoidColor;
|
? mTrapezoidSolidColor : mTrapezoidColor;
|
||||||
final boolean isHoverState =
|
final boolean isHoverState =
|
||||||
mIsSlotsClickabled && mHoveredIndex == index && isValidToDraw(mHoveredIndex);
|
mIsSlotsClickabled && mHoveredIndex == index
|
||||||
|
&& isValidToDraw(mViewModel, mHoveredIndex);
|
||||||
mTrapezoidPaint.setColor(isHoverState ? mTrapezoidHoverColor : trapezoidColor);
|
mTrapezoidPaint.setColor(isHoverState ? mTrapezoidHoverColor : trapezoidColor);
|
||||||
|
|
||||||
final float leftTop = round(
|
final float leftTop = round(
|
||||||
trapezoidBottom - mViewModel.levels().get(index) * unitHeight);
|
trapezoidBottom - requireNonNull(mViewModel.levels().get(index)) * unitHeight);
|
||||||
final float rightTop = round(
|
final float rightTop = round(trapezoidBottom
|
||||||
trapezoidBottom - mViewModel.levels().get(index + 1) * unitHeight);
|
- requireNonNull(mViewModel.levels().get(index + 1)) * unitHeight);
|
||||||
trapezoidPath.reset();
|
trapezoidPath.reset();
|
||||||
trapezoidPath.moveTo(mTrapezoidSlots[index].mLeft, trapezoidBottom);
|
trapezoidPath.moveTo(mTrapezoidSlots[index].mLeft, trapezoidBottom);
|
||||||
trapezoidPath.lineTo(mTrapezoidSlots[index].mLeft, leftTop);
|
trapezoidPath.lineTo(mTrapezoidSlots[index].mLeft, leftTop);
|
||||||
@@ -552,18 +554,23 @@ public class BatteryChartViewV2 extends AppCompatImageView implements View.OnCli
|
|||||||
return BatteryChartViewModel.SELECTED_INDEX_INVALID;
|
return BatteryChartViewModel.SELECTED_INDEX_INVALID;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isValidToDraw(int trapezoidIndex) {
|
private static boolean isTrapezoidValid(
|
||||||
return mViewModel != null
|
@NonNull BatteryChartViewModel viewModel, int trapezoidIndex) {
|
||||||
&& trapezoidIndex >= 0
|
return viewModel.levels().get(trapezoidIndex) != null
|
||||||
&& trapezoidIndex < mViewModel.size() - 1
|
&& viewModel.levels().get(trapezoidIndex + 1) != null;
|
||||||
&& mViewModel.levels().get(trapezoidIndex) != 0
|
|
||||||
&& mViewModel.levels().get(trapezoidIndex + 1) != 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean hasNonZeroTrapezoid(List<Integer> levels) {
|
private static boolean isValidToDraw(BatteryChartViewModel viewModel, int trapezoidIndex) {
|
||||||
|
return viewModel != null
|
||||||
|
&& trapezoidIndex >= 0
|
||||||
|
&& trapezoidIndex < viewModel.size() - 1
|
||||||
|
&& isTrapezoidValid(viewModel, trapezoidIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean hasAnyValidTrapezoid(@NonNull BatteryChartViewModel viewModel) {
|
||||||
// Sets the chart is clickable if there is at least one valid item in it.
|
// Sets the chart is clickable if there is at least one valid item in it.
|
||||||
for (int index = 0; index < levels.size() - 1; index++) {
|
for (int trapezoidIndex = 0; trapezoidIndex < viewModel.size() - 1; trapezoidIndex++) {
|
||||||
if (levels.get(index) != 0 && levels.get(index + 1) != 0) {
|
if (isTrapezoidValid(viewModel, trapezoidIndex)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user