TextView/LinkMovementMethod/ClickableSpan - touch up revert

Reverts the change which adds a gesture detector in
TextView for detecting clicks on ClickableSpans. A click
is considered MotionEvent.ACTION_UP. Revert needed because
the singleTapUp refactoring has a potential to break
existing apps.

Bug: 23692690
Test: attached in the same topic
Change-Id: Ife47fd0608480130123091bc60a7e9dd1efe8785
This commit is contained in:
Andrei Stingaceanu
2017-02-10 12:55:01 +00:00
parent 849a31075c
commit d834c58252
2 changed files with 17 additions and 47 deletions

View File

@@ -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) {
Selection.setSelection(buffer,
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);

View File

@@ -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.