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
This commit is contained in:
Maurice Lam 2018-04-06 14:27:41 -07:00
parent a6d66b41b0
commit e58194099d
4 changed files with 173 additions and 4 deletions

View file

@ -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);
}
@ -152,6 +153,25 @@ public class RichTextView extends AppCompatTextView implements OnLinkClickListen
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;
}
@Override
protected boolean dispatchHoverEvent(MotionEvent event) {
if (mAccessibilityHelper != null && mAccessibilityHelper.dispatchHoverEvent(event)) {

View file

@ -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;
}
}
}

View file

@ -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;
}

View file

@ -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);
@ -111,6 +118,44 @@ public class RichTextViewTest {
verify(context).onClick(eq(spans[0]));
}
@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");