summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaurice Lam <yukl@google.com>2018-04-06 21:25:55 +0000
committerMaurice Lam <yukl@google.com>2018-04-06 14:32:56 -0700
commita6d66b41b08b2d612da1a3e27c651053ceb7a871 (patch)
tree38bb10565cd00959f7cdc5649a30cf541407e4da
parente4475dcf52c1235a7d9756b454fa81fb4b54726c (diff)
downloadsetupwizard-a6d66b41b08b2d612da1a3e27c651053ceb7a871.tar.gz
Revert "Add workaround for touch event propagation"
This reverts commit e4475dcf52c1235a7d9756b454fa81fb4b54726c. Reason for revert: Breaks ub-setupwizard-master build (lint error) Bug: 77338508 Test: ./gradlew lint Change-Id: I251ff930b7d8391db3ea55597fa42f491cdff963
-rw-r--r--library/gingerbread/src/com/android/setupwizardlib/view/RichTextView.java23
-rw-r--r--library/main/src/com/android/setupwizardlib/view/TouchableMovementMethod.java83
-rw-r--r--library/platform/src/com/android/setupwizardlib/view/RichTextView.java24
-rw-r--r--library/test/robotest/src/com/android/setupwizardlib/view/RichTextViewTest.java45
4 files changed, 4 insertions, 171 deletions
diff --git a/library/gingerbread/src/com/android/setupwizardlib/view/RichTextView.java b/library/gingerbread/src/com/android/setupwizardlib/view/RichTextView.java
index ff2b1a7..6694cb2 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.MovementMethod;
+import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.TextAppearanceSpan;
import android.util.AttributeSet;
@@ -36,7 +36,6 @@ 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
@@ -122,7 +121,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(TouchableLinkMovementMethod.getInstance());
+ setMovementMethod(LinkMovementMethod.getInstance());
} else {
setMovementMethod(null);
}
@@ -154,24 +153,6 @@ public class RichTextView extends AppCompatTextView implements OnLinkClickListen
}
@Override
- 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
deleted file mode 100644
index 10e91f4..0000000
--- a/library/main/src/com/android/setupwizardlib/view/TouchableMovementMethod.java
+++ /dev/null
@@ -1,83 +0,0 @@
-/*
- * 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 4a53304..aab3238 100644
--- a/library/platform/src/com/android/setupwizardlib/view/RichTextView.java
+++ b/library/platform/src/com/android/setupwizardlib/view/RichTextView.java
@@ -20,18 +20,16 @@ import android.content.Context;
import android.text.Annotation;
import android.text.SpannableString;
import android.text.Spanned;
-import android.text.method.MovementMethod;
+import android.text.method.LinkMovementMethod;
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
@@ -114,7 +112,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(TouchableLinkMovementMethod.getInstance());
+ setMovementMethod(LinkMovementMethod.getInstance());
} else {
setMovementMethod(null);
}
@@ -139,24 +137,6 @@ public class RichTextView extends TextView implements OnLinkClickListener {
return false;
}
- @Override
- 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 f77de68..2e28b48 100644
--- a/library/test/robotest/src/com/android/setupwizardlib/view/RichTextViewTest.java
+++ b/library/test/robotest/src/com/android/setupwizardlib/view/RichTextViewTest.java
@@ -16,14 +16,11 @@
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;
@@ -38,12 +35,10 @@ 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;
@@ -67,8 +62,6 @@ 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);
@@ -119,44 +112,6 @@ 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");