Merge "Support mouse hover event for BatteryChartView to highlight slot" into sc-v2-dev am: 14139de0cf am: e1d45963a6

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Settings/+/15988049

Change-Id: I634f692a0706cf710571c58c6c7d4970effa7f5a
This commit is contained in:
YUKAI HUNG
2021-10-06 10:14:07 +00:00
committed by Automerger Merge Worker
2 changed files with 40 additions and 7 deletions

View File

@@ -350,7 +350,7 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
private void addAllPreferences() {
final List<BatteryDiffEntry> entries =
mBatteryIndexedMap.get(Integer.valueOf(mTrapezoidIndex));
addFooterPreferenceIfNeeded(!entries.isEmpty());
addFooterPreferenceIfNeeded(entries != null && !entries.isEmpty());
if (entries == null) {
Log.w(TAG, "cannot find BatteryDiffEntry for:" + mTrapezoidIndex);
return;

View File

@@ -79,12 +79,14 @@ public class BatteryChartView extends AppCompatImageView implements View.OnClick
private boolean mIsSlotsClickabled;
private String[] mPercentages = getPercentages();
@VisibleForTesting int mSelectedIndex;
@VisibleForTesting int mHoveredIndex = SELECTED_INDEX_INVALID;
@VisibleForTesting int mSelectedIndex = SELECTED_INDEX_INVALID;
@VisibleForTesting String[] mTimestamps;
// Colors for drawing the trapezoid shape and dividers.
private int mTrapezoidColor;
private int mTrapezoidSolidColor;
private int mTrapezoidHoverColor;
// For drawing the percentage information.
private int mTextPadding;
private final Rect mIndent = new Rect();
@@ -254,14 +256,42 @@ public class BatteryChartView extends AppCompatImageView implements View.OnClick
public boolean onTouchEvent(MotionEvent event) {
// Caches the location to calculate selected trapezoid index.
final int action = event.getAction();
if (action == MotionEvent.ACTION_UP) {
switch (action) {
case MotionEvent.ACTION_UP:
mTouchUpEventX = event.getX();
} else if (action == MotionEvent.ACTION_CANCEL) {
break;
case MotionEvent.ACTION_CANCEL:
mTouchUpEventX = Float.MIN_VALUE; // reset
break;
}
return super.onTouchEvent(event);
}
@Override
public boolean onHoverEvent(MotionEvent event) {
final int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_HOVER_ENTER:
case MotionEvent.ACTION_HOVER_MOVE:
final int trapezoidIndex = getTrapezoidIndex(event.getX());
if (mHoveredIndex != trapezoidIndex) {
mHoveredIndex = trapezoidIndex;
invalidate();
}
break;
}
return super.onHoverEvent(event);
}
@Override
public void onHoverChanged(boolean hovered) {
super.onHoverChanged(hovered);
if (!hovered) {
mHoveredIndex = SELECTED_INDEX_INVALID; // reset
invalidate();
}
}
@Override
public void onClick(View view) {
if (mTouchUpEventX == Float.MIN_VALUE) {
@@ -347,6 +377,8 @@ public class BatteryChartView extends AppCompatImageView implements View.OnClick
setBackgroundColor(Color.TRANSPARENT);
mTrapezoidSolidColor = Utils.getColorAccentDefaultColor(context);
mTrapezoidColor = Utils.getDisabled(context, mTrapezoidSolidColor);
mTrapezoidHoverColor = Utils.getColorAttrDefaultColor(context,
com.android.internal.R.attr.colorAccentSecondaryVariant);
// Initializes the divider line paint.
final Resources resources = getContext().getResources();
mDividerWidth = resources.getDimensionPixelSize(R.dimen.chartview_divider_width);
@@ -494,7 +526,8 @@ public class BatteryChartView extends AppCompatImageView implements View.OnClick
? mTrapezoidColor
: mSelectedIndex == index || mSelectedIndex == SELECTED_INDEX_ALL
? mTrapezoidSolidColor : mTrapezoidColor;
mTrapezoidPaint.setColor(trapezoidColor);
final boolean isHover = mHoveredIndex == index && isValidToDraw(mHoveredIndex);
mTrapezoidPaint.setColor(isHover ? mTrapezoidHoverColor : trapezoidColor);
final float leftTop = round(trapezoidBottom - mLevels[index] * unitHeight);
final float rightTop = round(trapezoidBottom - mLevels[index + 1] * unitHeight);