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

Commit 302fe909 authored by Andrei Stingaceanu's avatar Andrei Stingaceanu Committed by Android (Google) Code Review
Browse files

Merge "TextView/LinkMovementMethod/ClickableSpan - touch up revert"

parents c3dfa819 d834c582
Loading
Loading
Loading
Loading
+6 −5
Original line number Diff line number Diff line
@@ -29,9 +29,6 @@ import android.widget.TextView;
/**
 * A movement method that traverses links in the text buffer and scrolls if necessary.
 * Supports clicking on links with DPad Center or Enter.
 *
 * <p>Note: Starting from Android 8.0 (API level 25) this class no longer handles the touch
 * clicks.
 */
public class LinkMovementMethod extends ScrollingMovementMethod {
    private static final int CLICK = 1;
@@ -198,7 +195,7 @@ public class LinkMovementMethod extends ScrollingMovementMethod {
                                MotionEvent event) {
        int action = event.getAction();

        if (action == MotionEvent.ACTION_DOWN) {
        if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
            int x = (int) event.getX();
            int y = (int) event.getY();

@@ -215,9 +212,13 @@ public class LinkMovementMethod extends ScrollingMovementMethod {
            ClickableSpan[] links = buffer.getSpans(off, off, ClickableSpan.class);

            if (links.length != 0) {
                if (action == MotionEvent.ACTION_UP) {
                    links[0].onClick(widget);
                } else if (action == MotionEvent.ACTION_DOWN) {
                    Selection.setSelection(buffer,
                        buffer.getSpanStart(links[0]),
                        buffer.getSpanEnd(links[0]));
                }
                return true;
            } else {
                Selection.removeSelection(buffer);
+11 −42
Original line number Diff line number Diff line
@@ -121,7 +121,6 @@ import android.view.ActionMode;
import android.view.Choreographer;
import android.view.ContextMenu;
import android.view.DragEvent;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.HapticFeedbackConstants;
import android.view.KeyCharacterMap;
@@ -682,8 +681,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
     */
    private Editor mEditor;

    private GestureDetector mClickableSpanOnClickGestureDetector;

    private static final int DEVICE_PROVISIONED_UNKNOWN = 0;
    private static final int DEVICE_PROVISIONED_NO = 1;
    private static final int DEVICE_PROVISIONED_YES = 2;
@@ -9319,24 +9316,21 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
                handled |= mMovement.onTouchEvent(this, (Spannable) mText, event);
            }

            // Lazily create the clickable span gesture detector only if it looks like it
            // might be useful.
            if (action == MotionEvent.ACTION_DOWN && mClickableSpanOnClickGestureDetector == null
                    && shouldUseClickableSpanOnClickGestureDetector()) {
                ClickableSpan[] links = ((Spannable) mText).getSpans(
                        getSelectionStart(), getSelectionEnd(),
                        ClickableSpan.class);
            final boolean textIsSelectable = isTextSelectable();
            if (touchIsFinished && mLinksClickable && mAutoLinkMask != 0 && textIsSelectable) {
                // The LinkMovementMethod which should handle taps on links has not been installed
                // on non editable text that support text selection.
                // We reproduce its behavior here to open links for these.
                ClickableSpan[] links = ((Spannable) mText).getSpans(getSelectionStart(),
                    getSelectionEnd(), ClickableSpan.class);

                if (links.length > 0) {
                    mClickableSpanOnClickGestureDetector =
                            createClickableSpanOnClickGestureDetector();
                }
                    links[0].onClick(this);
                    handled = true;
                }

            if (mClickableSpanOnClickGestureDetector != null) {
                handled |= mClickableSpanOnClickGestureDetector.onTouchEvent(event);
            }

            if (touchIsFinished && (isTextEditable() || isTextSelectable())) {
            if (touchIsFinished && (isTextEditable() || textIsSelectable)) {
                // Show the IME, except when selecting in read-only text.
                final InputMethodManager imm = InputMethodManager.peekInstance();
                viewClicked(imm);
@@ -9754,31 +9748,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
        mEditor.onLocaleChanged();
    }

    private GestureDetector createClickableSpanOnClickGestureDetector() {
        return new GestureDetector(mContext,
                new GestureDetector.SimpleOnGestureListener() {
                    @Override
                    public boolean onSingleTapUp(MotionEvent e) {
                        if (shouldUseClickableSpanOnClickGestureDetector()) {
                            ClickableSpan[] links = ((Spannable) mText).getSpans(
                                    getSelectionStart(), getSelectionEnd(),
                                    ClickableSpan.class);
                            if (links.length > 0) {
                                links[0].onClick(TextView.this);
                                return true;
                            }
                        }
                        return false;
                    }
                });
    }

    private boolean shouldUseClickableSpanOnClickGestureDetector() {
        return mLinksClickable && (mMovement != null) &&
                (mMovement instanceof LinkMovementMethod
                        || (mAutoLinkMask != 0 && isTextSelectable()));
    }

    /**
     * This method is used by the ArrowKeyMovementMethod to jump from one word to the other.
     * Made available to achieve a consistent behavior.