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:
@@ -350,7 +350,7 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
|||||||
private void addAllPreferences() {
|
private void addAllPreferences() {
|
||||||
final List<BatteryDiffEntry> entries =
|
final List<BatteryDiffEntry> entries =
|
||||||
mBatteryIndexedMap.get(Integer.valueOf(mTrapezoidIndex));
|
mBatteryIndexedMap.get(Integer.valueOf(mTrapezoidIndex));
|
||||||
addFooterPreferenceIfNeeded(!entries.isEmpty());
|
addFooterPreferenceIfNeeded(entries != null && !entries.isEmpty());
|
||||||
if (entries == null) {
|
if (entries == null) {
|
||||||
Log.w(TAG, "cannot find BatteryDiffEntry for:" + mTrapezoidIndex);
|
Log.w(TAG, "cannot find BatteryDiffEntry for:" + mTrapezoidIndex);
|
||||||
return;
|
return;
|
||||||
|
@@ -79,12 +79,14 @@ public class BatteryChartView extends AppCompatImageView implements View.OnClick
|
|||||||
private boolean mIsSlotsClickabled;
|
private boolean mIsSlotsClickabled;
|
||||||
private String[] mPercentages = getPercentages();
|
private String[] mPercentages = getPercentages();
|
||||||
|
|
||||||
@VisibleForTesting int mSelectedIndex;
|
@VisibleForTesting int mHoveredIndex = SELECTED_INDEX_INVALID;
|
||||||
|
@VisibleForTesting int mSelectedIndex = SELECTED_INDEX_INVALID;
|
||||||
@VisibleForTesting String[] mTimestamps;
|
@VisibleForTesting String[] mTimestamps;
|
||||||
|
|
||||||
// Colors for drawing the trapezoid shape and dividers.
|
// Colors for drawing the trapezoid shape and dividers.
|
||||||
private int mTrapezoidColor;
|
private int mTrapezoidColor;
|
||||||
private int mTrapezoidSolidColor;
|
private int mTrapezoidSolidColor;
|
||||||
|
private int mTrapezoidHoverColor;
|
||||||
// For drawing the percentage information.
|
// For drawing the percentage information.
|
||||||
private int mTextPadding;
|
private int mTextPadding;
|
||||||
private final Rect mIndent = new Rect();
|
private final Rect mIndent = new Rect();
|
||||||
@@ -254,14 +256,42 @@ public class BatteryChartView extends AppCompatImageView implements View.OnClick
|
|||||||
public boolean onTouchEvent(MotionEvent event) {
|
public boolean onTouchEvent(MotionEvent event) {
|
||||||
// Caches the location to calculate selected trapezoid index.
|
// Caches the location to calculate selected trapezoid index.
|
||||||
final int action = event.getAction();
|
final int action = event.getAction();
|
||||||
if (action == MotionEvent.ACTION_UP) {
|
switch (action) {
|
||||||
mTouchUpEventX = event.getX();
|
case MotionEvent.ACTION_UP:
|
||||||
} else if (action == MotionEvent.ACTION_CANCEL) {
|
mTouchUpEventX = event.getX();
|
||||||
mTouchUpEventX = Float.MIN_VALUE; // reset
|
break;
|
||||||
|
case MotionEvent.ACTION_CANCEL:
|
||||||
|
mTouchUpEventX = Float.MIN_VALUE; // reset
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return super.onTouchEvent(event);
|
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
|
@Override
|
||||||
public void onClick(View view) {
|
public void onClick(View view) {
|
||||||
if (mTouchUpEventX == Float.MIN_VALUE) {
|
if (mTouchUpEventX == Float.MIN_VALUE) {
|
||||||
@@ -347,6 +377,8 @@ public class BatteryChartView extends AppCompatImageView implements View.OnClick
|
|||||||
setBackgroundColor(Color.TRANSPARENT);
|
setBackgroundColor(Color.TRANSPARENT);
|
||||||
mTrapezoidSolidColor = Utils.getColorAccentDefaultColor(context);
|
mTrapezoidSolidColor = Utils.getColorAccentDefaultColor(context);
|
||||||
mTrapezoidColor = Utils.getDisabled(context, mTrapezoidSolidColor);
|
mTrapezoidColor = Utils.getDisabled(context, mTrapezoidSolidColor);
|
||||||
|
mTrapezoidHoverColor = Utils.getColorAttrDefaultColor(context,
|
||||||
|
com.android.internal.R.attr.colorAccentSecondaryVariant);
|
||||||
// Initializes the divider line paint.
|
// Initializes the divider line paint.
|
||||||
final Resources resources = getContext().getResources();
|
final Resources resources = getContext().getResources();
|
||||||
mDividerWidth = resources.getDimensionPixelSize(R.dimen.chartview_divider_width);
|
mDividerWidth = resources.getDimensionPixelSize(R.dimen.chartview_divider_width);
|
||||||
@@ -494,7 +526,8 @@ public class BatteryChartView extends AppCompatImageView implements View.OnClick
|
|||||||
? mTrapezoidColor
|
? mTrapezoidColor
|
||||||
: mSelectedIndex == index || mSelectedIndex == SELECTED_INDEX_ALL
|
: mSelectedIndex == index || mSelectedIndex == SELECTED_INDEX_ALL
|
||||||
? mTrapezoidSolidColor : mTrapezoidColor;
|
? 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 leftTop = round(trapezoidBottom - mLevels[index] * unitHeight);
|
||||||
final float rightTop = round(trapezoidBottom - mLevels[index + 1] * unitHeight);
|
final float rightTop = round(trapezoidBottom - mLevels[index + 1] * unitHeight);
|
||||||
|
Reference in New Issue
Block a user