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

Commit 8b0cb128 authored by Tadashi G. Takaoka's avatar Tadashi G. Takaoka
Browse files

Refactor onModifiedTouchEvent of LatinKeyboardBaseView

This change simplifies onModifierTouchEvent to focus on handling
motion event related to a key event.

Other refactoring will follow in order to support multi touch.

Bug: 2910379

Change-Id: I036be64168d951a535600a7910b36bc109f88490
parent e07d6a77
Loading
Loading
Loading
Loading
+36 −34
Original line number Diff line number Diff line
@@ -205,7 +205,7 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener
    private boolean mAbortKey;
    private Key mInvalidatedKey;
    private Rect mClipRegion = new Rect(0, 0, 0, 0);
    private boolean mPossiblePoly;
    private boolean mCancelGestureDetector;
    private SwipeTracker mSwipeTracker = new SwipeTracker();
    private int mSwipeThreshold;
    private boolean mDisambiguateSwipe;
@@ -545,7 +545,7 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener
            @Override
            public boolean onFling(MotionEvent me1, MotionEvent me2,
                    float velocityX, float velocityY) {
                if (mPossiblePoly) return false;
                if (mCancelGestureDetector) return false;
                final float absX = Math.abs(velocityX);
                final float absY = Math.abs(velocityY);
                float deltaX = me2.getX() - me1.getX();
@@ -1279,68 +1279,70 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener
        boolean result = false;
        final long now = me.getEventTime();

        if (pointerCount > 1 && mOldPointerCount > 1) {
            // Don't do anything when 2 or more pointers are down and moving.
            return true;
        }

        // Track the last few movements to look for spurious swipes.
        if (action == MotionEvent.ACTION_DOWN) mSwipeTracker.clear();
        mSwipeTracker.addMovement(me);

        // Ignore all motion events until a DOWN.
        if (mAbortKey
                && action != MotionEvent.ACTION_DOWN && action != MotionEvent.ACTION_CANCEL) {
            return true;
        }

        mCancelGestureDetector = (pointerCount > 1);
        if (mGestureDetector.onTouchEvent(me)) {
            showPreview(NOT_A_KEY);
            mHandler.cancelKeyTimers();
            return true;
        }

        // Needs to be called after the gesture detector gets a turn, as it may have
        // displayed the mini keyboard
        if (mMiniKeyboardOnScreen && action != MotionEvent.ACTION_CANCEL) {
            return true;
        }

        if (pointerCount != mOldPointerCount) {
            if (pointerCount == 1) {
                // Send a down event for the latest pointer
                MotionEvent down = MotionEvent.obtain(now, now, MotionEvent.ACTION_DOWN,
                        me.getX(), me.getY(), me.getMetaState());
                result = onModifiedTouchEvent(down, false);
                result = onModifiedTouchEvent(down);
                down.recycle();
                // If it's an up action, then deliver the up as well.
                if (action == MotionEvent.ACTION_UP) {
                    result = onModifiedTouchEvent(me, true);
                    result = onModifiedTouchEvent(me);
                }
            } else {
                // Send an up event for the last pointer
                MotionEvent up = MotionEvent.obtain(now, now, MotionEvent.ACTION_UP,
                        mOldPointerX, mOldPointerY, me.getMetaState());
                result = onModifiedTouchEvent(up, true);
                result = onModifiedTouchEvent(up);
                up.recycle();
            }
            mOldPointerCount = pointerCount;
        } else {
            if (pointerCount == 1) {
                result = onModifiedTouchEvent(me, false);
                result = onModifiedTouchEvent(me);
                mOldPointerX = me.getX();
                mOldPointerY = me.getY();
            } else {
                // Don't do anything when 2 pointers are down and moving.
                result = true;
            }
        }
        mOldPointerCount = pointerCount;

        return result;
    }

    private boolean onModifiedTouchEvent(MotionEvent me, boolean possiblePoly) {
    private boolean onModifiedTouchEvent(MotionEvent me) {
        int touchX = (int) me.getX() - getPaddingLeft();
        int touchY = (int) me.getY() + mVerticalCorrection - getPaddingTop();
        final int action = me.getAction();
        final long eventTime = me.getEventTime();
        int keyIndex = getKeyIndexAndNearbyCodes(touchX, touchY, null);
        mPossiblePoly = possiblePoly;

        // Track the last few movements to look for spurious swipes.
        if (action == MotionEvent.ACTION_DOWN) mSwipeTracker.clear();
        mSwipeTracker.addMovement(me);

        // Ignore all motion events until a DOWN.
        if (mAbortKey
                && action != MotionEvent.ACTION_DOWN && action != MotionEvent.ACTION_CANCEL) {
            return true;
        }

        if (mGestureDetector.onTouchEvent(me)) {
            showPreview(NOT_A_KEY);
            mHandler.cancelKeyTimers();
            return true;
        }

        // Needs to be called after the gesture detector gets a turn, as it may have
        // displayed the mini keyboard
        if (mMiniKeyboardOnScreen && action != MotionEvent.ACTION_CANCEL) {
            return true;
        }

        switch (action) {
            case MotionEvent.ACTION_DOWN: