Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8eff5cc1 authored by ykhung's avatar ykhung Committed by YUKAI HUNG
Browse files

Support mouse hover event for BatteryChartView to highlight slot

https://drive.google.com/file/d/19Ms4JOPVfQ6rfXr71vTeXJLSvJqJtRfw/

Bug: 201501553
Test: make SettingsRoboTests
Change-Id: I2c03585163ddb7809a09944aec326a41f6bd4758
parent d1dd4cdf
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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;
+39 −6
Original line number Diff line number Diff line
@@ -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);