summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaurice Lam <yukl@google.com>2018-04-06 14:27:41 -0700
committerMaurice Lam <yukl@google.com>2018-04-06 14:33:22 -0700
commite58194099d9133ce7b2e19fe7c717679b62fddb2 (patch)
treed2d7c1d8bf6b97d6ff16f5504a472f6249cfa52c
parenta6d66b41b08b2d612da1a3e27c651053ceb7a871 (diff)
downloadsetupwizard-e58194099d9133ce7b2e19fe7c717679b62fddb2.tar.gz
Revert "Revert "Add workaround for touch event propagation""
This reverts commit b268a8c365a4c9dcb7b1382a7ce3ec5607b22de0. Rolling forward with suppress for the lint errors Test: ./gradlew lint Bug: 77338508 Change-Id: I9582bfbb4c73ae60a15e8285facf20da458b9690
-rw-r--r--library/gingerbread/src/com/android/setupwizardlib/view/RichTextView.java24
-rw-r--r--library/main/src/com/android/setupwizardlib/view/TouchableMovementMethod.java83
-rw-r--r--library/platform/src/com/android/setupwizardlib/view/RichTextView.java25
-rw-r--r--library/test/robotest/src/com/android/setupwizardlib/view/RichTextViewTest.java45
4 files changed, 173 insertions, 4 deletions
diff --git a/library/gingerbread/src/com/android/setupwizardlib/view/RichTextView.java b/library/gingerbread/src/com/android/setupwizardlib/view/RichTextView.java
index 6694cb2..1b1f82e 100644
--- a/library/gingerbread/src/com/android/setupwizardlib/view/RichTextView.java
+++ b/library/gingerbread/src/com/android/setupwizardlib/view/RichTextView.java
@@ -25,7 +25,7 @@ import android.support.v7.widget.AppCompatTextView;
import android.text.Annotation;
import android.text.SpannableString;
import android.text.Spanned;
-import android.text.method.LinkMovementMethod;
+import android.text.method.MovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.TextAppearanceSpan;
import android.util.AttributeSet;
@@ -36,6 +36,7 @@ import com.android.setupwizardlib.span.LinkSpan;
import com.android.setupwizardlib.span.LinkSpan.OnLinkClickListener;
import com.android.setupwizardlib.span.SpanHelper;
import com.android.setupwizardlib.util.LinkAccessibilityHelper;
+import com.android.setupwizardlib.view.TouchableMovementMethod.TouchableLinkMovementMethod;
/**
* An extension of TextView that automatically replaces the annotation tags as specified in
@@ -121,7 +122,7 @@ public class RichTextView extends AppCompatTextView implements OnLinkClickListen
// nullifying any return values of MovementMethod.onTouchEvent.
// To still allow propagating touch events to the parent when this view doesn't have
// links, we only set the movement method here if the text contains links.
- setMovementMethod(LinkMovementMethod.getInstance());
+ setMovementMethod(TouchableLinkMovementMethod.getInstance());
} else {
setMovementMethod(null);
}
@@ -153,6 +154,25 @@ public class RichTextView extends AppCompatTextView implements OnLinkClickListen
}
@Override
+ @SuppressWarnings("ClickableViewAccessibility") // super.onTouchEvent is called
+ public boolean onTouchEvent(MotionEvent event) {
+ // Since View#onTouchEvent always return true if the view is clickable (which is the case
+ // when a TextView has a movement method), override the implementation to allow the movement
+ // method, if it implements TouchableMovementMethod, to say that the touch is not handled,
+ // allowing the event to bubble up to the parent view.
+ boolean superResult = super.onTouchEvent(event);
+ MovementMethod movementMethod = getMovementMethod();
+ if (movementMethod instanceof TouchableMovementMethod) {
+ TouchableMovementMethod touchableMovementMethod =
+ (TouchableMovementMethod) movementMethod;
+ if (touchableMovementMethod.getLastTouchEvent() == event) {
+ return touchableMovementMethod.isLastTouchEventHandled();
+ }
+ }
+ return superResult;
+ }
+
+ @Override
protected boolean dispatchHoverEvent(MotionEvent event) {
if (mAccessibilityHelper != null && mAccessibilityHelper.dispatchHoverEvent(event)) {
return true;
diff --git a/library/main/src/com/android/setupwizardlib/view/TouchableMovementMethod.java b/library/main/src/com/android/setupwizardlib/view/TouchableMovementMethod.java
new file mode 100644
index 0000000..10e91f4
--- /dev/null
+++ b/library/main/src/com/android/setupwizardlib/view/TouchableMovementMethod.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.setupwizardlib.view;
+
+import android.text.Selection;
+import android.text.Spannable;
+import android.text.method.LinkMovementMethod;
+import android.text.method.MovementMethod;
+import android.view.MotionEvent;
+import android.widget.TextView;
+
+/**
+ * A movement method that tracks the last result of whether touch events are handled. This is
+ * used to patch the return value of {@link TextView#onTouchEvent} so that it consumes the touch
+ * events only when the movement method says the event is consumed.
+ */
+public interface TouchableMovementMethod {
+
+ /**
+ * @return The last touch event received in {@link MovementMethod#onTouchEvent}
+ */
+ MotionEvent getLastTouchEvent();
+
+ /**
+ * @return The return value of the last {@link MovementMethod#onTouchEvent}, or whether the
+ * last touch event should be considered handled by the text view
+ */
+ boolean isLastTouchEventHandled();
+
+ /**
+ * An extension of LinkMovementMethod that tracks whether the event is handled when it is
+ * touched.
+ */
+ class TouchableLinkMovementMethod extends LinkMovementMethod
+ implements TouchableMovementMethod {
+
+ public static TouchableLinkMovementMethod getInstance() {
+ return new TouchableLinkMovementMethod();
+ }
+
+ boolean mLastEventResult = false;
+ MotionEvent mLastEvent;
+
+ @Override
+ public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
+ mLastEvent = event;
+ boolean result = super.onTouchEvent(widget, buffer, event);
+ if (event.getAction() == MotionEvent.ACTION_DOWN) {
+ // Unfortunately, LinkMovementMethod extends ScrollMovementMethod, and it always
+ // consume the down event. So here we use the selection instead as a hint of whether
+ // the down event landed on a link.
+ mLastEventResult = Selection.getSelectionStart(buffer) != -1;
+ } else {
+ mLastEventResult = result;
+ }
+ return result;
+ }
+
+ @Override
+ public MotionEvent getLastTouchEvent() {
+ return mLastEvent;
+ }
+
+ @Override
+ public boolean isLastTouchEventHandled() {
+ return mLastEventResult;
+ }
+ }
+}
diff --git a/library/platform/src/com/android/setupwizardlib/view/RichTextView.java b/library/platform/src/com/android/setupwizardlib/view/RichTextView.java
index aab3238..fa68a68 100644
--- a/library/platform/src/com/android/setupwizardlib/view/RichTextView.java
+++ b/library/platform/src/com/android/setupwizardlib/view/RichTextView.java
@@ -20,16 +20,18 @@ import android.content.Context;
import android.text.Annotation;
import android.text.SpannableString;
import android.text.Spanned;
-import android.text.method.LinkMovementMethod;
+import android.text.method.MovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.TextAppearanceSpan;
import android.util.AttributeSet;
import android.util.Log;
+import android.view.MotionEvent;
import android.widget.TextView;
import com.android.setupwizardlib.span.LinkSpan;
import com.android.setupwizardlib.span.LinkSpan.OnLinkClickListener;
import com.android.setupwizardlib.span.SpanHelper;
+import com.android.setupwizardlib.view.TouchableMovementMethod.TouchableLinkMovementMethod;
/**
* An extension of TextView that automatically replaces the annotation tags as specified in
@@ -112,7 +114,7 @@ public class RichTextView extends TextView implements OnLinkClickListener {
// nullifying any return values of MovementMethod.onTouchEvent.
// To still allow propagating touch events to the parent when this view doesn't have
// links, we only set the movement method here if the text contains links.
- setMovementMethod(LinkMovementMethod.getInstance());
+ setMovementMethod(TouchableLinkMovementMethod.getInstance());
} else {
setMovementMethod(null);
}
@@ -137,6 +139,25 @@ public class RichTextView extends TextView implements OnLinkClickListener {
return false;
}
+ @Override
+ @SuppressWarnings("ClickableViewAccessibility") // super.onTouchEvent is called
+ public boolean onTouchEvent(MotionEvent event) {
+ // Since View#onTouchEvent always return true if the view is clickable (which is the case
+ // when a TextView has a movement method), override the implementation to allow the movement
+ // method, if it implements TouchableMovementMethod, to say that the touch is not handled,
+ // allowing the event to bubble up to the parent view.
+ boolean superResult = super.onTouchEvent(event);
+ MovementMethod movementMethod = getMovementMethod();
+ if (movementMethod instanceof TouchableMovementMethod) {
+ TouchableMovementMethod touchableMovementMethod =
+ (TouchableMovementMethod) movementMethod;
+ if (touchableMovementMethod.getLastTouchEvent() == event) {
+ return touchableMovementMethod.isLastTouchEventHandled();
+ }
+ }
+ return superResult;
+ }
+
public void setOnLinkClickListener(OnLinkClickListener listener) {
mOnLinkClickListener = listener;
}
diff --git a/library/test/robotest/src/com/android/setupwizardlib/view/RichTextViewTest.java b/library/test/robotest/src/com/android/setupwizardlib/view/RichTextViewTest.java
index 2e28b48..f77de68 100644
--- a/library/test/robotest/src/com/android/setupwizardlib/view/RichTextViewTest.java
+++ b/library/test/robotest/src/com/android/setupwizardlib/view/RichTextViewTest.java
@@ -16,11 +16,14 @@
package com.android.setupwizardlib.view;
+import static com.google.common.truth.Truth.assertThat;
+
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
@@ -35,10 +38,12 @@ import android.text.Annotation;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
+import android.view.MotionEvent;
import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
import com.android.setupwizardlib.span.LinkSpan;
import com.android.setupwizardlib.span.LinkSpan.OnLinkClickListener;
+import com.android.setupwizardlib.view.TouchableMovementMethod.TouchableLinkMovementMethod;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -62,6 +67,8 @@ public class RichTextViewTest {
final CharSequence text = textView.getText();
assertTrue("Text should be spanned", text instanceof Spanned);
+ assertThat(textView.getMovementMethod()).isInstanceOf(TouchableLinkMovementMethod.class);
+
Object[] spans = ((Spanned) text).getSpans(0, text.length(), Annotation.class);
assertEquals("Annotation should be removed " + Arrays.toString(spans), 0, spans.length);
@@ -112,6 +119,44 @@ public class RichTextViewTest {
}
@Test
+ public void onTouchEvent_clickOnLinks_shouldReturnTrue() {
+ Annotation link = new Annotation("link", "foobar");
+ SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world");
+ ssb.setSpan(link, 0, 2, 0 /* flags */);
+
+ RichTextView textView = new RichTextView(application);
+ textView.setText(ssb);
+
+ TouchableLinkMovementMethod mockMovementMethod = mock(TouchableLinkMovementMethod.class);
+ textView.setMovementMethod(mockMovementMethod);
+
+ MotionEvent motionEvent =
+ MotionEvent.obtain(123, 22, MotionEvent.ACTION_DOWN, 0, 0, 0);
+ doReturn(motionEvent).when(mockMovementMethod).getLastTouchEvent();
+ doReturn(true).when(mockMovementMethod).isLastTouchEventHandled();
+ assertThat(textView.onTouchEvent(motionEvent)).isTrue();
+ }
+
+ @Test
+ public void onTouchEvent_clickOutsideLinks_shouldReturnFalse() {
+ Annotation link = new Annotation("link", "foobar");
+ SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world");
+ ssb.setSpan(link, 0, 2, 0 /* flags */);
+
+ RichTextView textView = new RichTextView(application);
+ textView.setText(ssb);
+
+ TouchableLinkMovementMethod mockMovementMethod = mock(TouchableLinkMovementMethod.class);
+ textView.setMovementMethod(mockMovementMethod);
+
+ MotionEvent motionEvent =
+ MotionEvent.obtain(123, 22, MotionEvent.ACTION_DOWN, 0, 0, 0);
+ doReturn(motionEvent).when(mockMovementMethod).getLastTouchEvent();
+ doReturn(false).when(mockMovementMethod).isLastTouchEventHandled();
+ assertThat(textView.onTouchEvent(motionEvent)).isFalse();
+ }
+
+ @Test
public void testTextStyle() {
Annotation link = new Annotation("textAppearance", "foobar");
SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world");