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

Commit c5cf04c5 authored by Gilles Debunne's avatar Gilles Debunne Committed by Android (Google) Code Review
Browse files

Merge "Text selection can be started with a double tap in TextView"

parents 3d773def 5347c588
Loading
Loading
Loading
Loading
+38 −16
Original line number Diff line number Diff line
@@ -6903,7 +6903,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        final int action = event.getActionMasked();
        if (action == MotionEvent.ACTION_DOWN) {
        if (mInsertionPointCursorController != null) {
            mInsertionPointCursorController.onTouchEvent(event);
        }
@@ -6911,6 +6910,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
            mSelectionModifierCursorController.onTouchEvent(event);
        }

        if (action == MotionEvent.ACTION_DOWN) {
            // Reset this state; it will be re-set if super.onTouchEvent
            // causes focus to move to the view.
            mTouchFocusSelected = false;
@@ -6931,13 +6931,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener

        if ((mMovement != null || onCheckIsTextEditor()) && isEnabled()
                && mText instanceof Spannable && mLayout != null) {
            if (mInsertionPointCursorController != null) {
                mInsertionPointCursorController.onTouchEvent(event);
            }
            if (mSelectionModifierCursorController != null) {
                mSelectionModifierCursorController.onTouchEvent(event);
            }

            boolean handled = false;

            // Save previous selection, in case this event is used to show the IME.
@@ -7981,7 +7974,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
                }
                mDrawable = mSelectHandleLeft;
                handleWidth = mDrawable.getIntrinsicWidth();
                mHotspotX = handleWidth / 4 * 3;
                mHotspotX = (handleWidth * 3) / 4;
                break;
            }

@@ -8264,6 +8257,11 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
        // Whether selection anchors are active
        private boolean mIsShowing;

        // Double tap detection
        private long mPreviousTapUpTime = 0;
        private int mPreviousTapPositionX;
        private int mPreviousTapPositionY;

        private static final int DELAY_BEFORE_FADE_OUT = 4100;

        private final Runnable mHider = new Runnable() {
@@ -8369,6 +8367,26 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
                        // Remember finger down position, to be able to start selection from there
                        mMinTouchOffset = mMaxTouchOffset = getOffset(x, y);

                        // Double tap detection
                        long duration = SystemClock.uptimeMillis() - mPreviousTapUpTime;
                        if (duration <= ViewConfiguration.getDoubleTapTimeout()) {
                            final int deltaX = x - mPreviousTapPositionX;
                            final int deltaY = y - mPreviousTapPositionY;
                            final int distanceSquared = deltaX * deltaX + deltaY * deltaY;
                            final int doubleTapSlop =
                                ViewConfiguration.get(getContext()).getScaledDoubleTapSlop();
                            final int slopSquared = doubleTapSlop * doubleTapSlop;
                            if (distanceSquared < slopSquared) {
                                startSelectionActionMode();
                                // Hacky: onTapUpEvent will open a context menu with cut/copy
                                // Prevent this by hiding handles which will be revived instead.
                                hide();
                            }
                        }

                        mPreviousTapPositionX = x;
                        mPreviousTapPositionY = y;

                        break;

                    case MotionEvent.ACTION_POINTER_DOWN:
@@ -8380,6 +8398,10 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
                            updateMinAndMaxOffsets(event);
                        }
                        break;

                    case MotionEvent.ACTION_UP:
                        mPreviousTapUpTime = SystemClock.uptimeMillis();
                        break;
                }
            }
            return false;