diff options
author | android-build-team Robot <android-build-team-robot@google.com> | 2018-04-08 07:26:55 +0000 |
---|---|---|
committer | android-build-team Robot <android-build-team-robot@google.com> | 2018-04-08 07:26:55 +0000 |
commit | 8aebd6208f773d52b91d4c9e04c918f2d8f85235 (patch) | |
tree | d2d7c1d8bf6b97d6ff16f5504a472f6249cfa52c | |
parent | 5fc80f04a2cbdf1a3497eadaf853e3f40473acb5 (diff) | |
parent | e58194099d9133ce7b2e19fe7c717679b62fddb2 (diff) | |
download | setupwizard-8aebd6208f773d52b91d4c9e04c918f2d8f85235.tar.gz |
Snap for 4706961 from e58194099d9133ce7b2e19fe7c717679b62fddb2 to pi-release
Change-Id: Ided4e1f17b512d7b38a1138c0f16267393eb7476
-rw-r--r-- | library/gingerbread/src/com/android/setupwizardlib/view/RichTextView.java | 35 | ||||
-rw-r--r-- | library/main/src/com/android/setupwizardlib/span/LinkSpan.java | 11 | ||||
-rw-r--r-- | library/main/src/com/android/setupwizardlib/view/TouchableMovementMethod.java | 83 | ||||
-rw-r--r-- | library/platform/src/com/android/setupwizardlib/view/RichTextView.java | 25 | ||||
-rw-r--r-- | library/test/robotest/src/com/android/setupwizardlib/span/LinkSpanTest.java | 32 | ||||
-rw-r--r-- | library/test/robotest/src/com/android/setupwizardlib/view/RichTextViewTest.java (renamed from library/test/instrumentation/src/com/android/setupwizardlib/test/RichTextViewTest.java) | 81 |
6 files changed, 246 insertions, 21 deletions
diff --git a/library/gingerbread/src/com/android/setupwizardlib/view/RichTextView.java b/library/gingerbread/src/com/android/setupwizardlib/view/RichTextView.java index e6bc9da..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); } @@ -130,6 +131,17 @@ public class RichTextView extends AppCompatTextView implements OnLinkClickListen // as individual TextViews consume touch events and thereby reducing the focus window // shown by Talkback. Disable focus if there are no links setFocusable(hasLinks); + // Do not "reveal" (i.e. scroll to) this view when this view is focused. Since this view is + // focusable in touch mode, we may be focused when the screen is first shown, and starting + // a screen halfway scrolled down is confusing to the user. + if (VERSION.SDK_INT >= VERSION_CODES.N_MR1) { + setRevealOnFocusHint(false); + // setRevealOnFocusHint is a new API added in SDK 25. For lower SDK versions, do not + // call setFocusableInTouchMode. We won't get touch effect on those earlier versions, + // but the link will still work, and will prevent the scroll view from starting halfway + // down the page. + setFocusableInTouchMode(hasLinks); + } } private boolean hasLinks(CharSequence text) { @@ -142,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/span/LinkSpan.java b/library/main/src/com/android/setupwizardlib/span/LinkSpan.java index a5f0424..26a3d16 100644 --- a/library/main/src/com/android/setupwizardlib/span/LinkSpan.java +++ b/library/main/src/com/android/setupwizardlib/span/LinkSpan.java @@ -21,10 +21,13 @@ import android.content.ContextWrapper; import android.graphics.Typeface; import android.os.Build; import android.support.annotation.Nullable; +import android.text.Selection; +import android.text.Spannable; import android.text.TextPaint; import android.text.style.ClickableSpan; import android.util.Log; import android.view.View; +import android.widget.TextView; /** * A clickable span that will listen for click events and send it back to the context. To use this @@ -86,11 +89,19 @@ public class LinkSpan extends ClickableSpan { public void onClick(View view) { if (dispatchClick(view)) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + // Prevent the touch event from bubbling up to the parent views. view.cancelPendingInputEvents(); } } else { Log.w(TAG, "Dropping click event. No listener attached."); } + if (view instanceof TextView) { + // Remove the highlight effect when the click happens by clearing the selection + CharSequence text = ((TextView) view).getText(); + if (text instanceof Spannable) { + Selection.setSelection((Spannable) text, 0); + } + } } private boolean dispatchClick(View view) { 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/span/LinkSpanTest.java b/library/test/robotest/src/com/android/setupwizardlib/span/LinkSpanTest.java index fe72e03..3aafa7d 100644 --- a/library/test/robotest/src/com/android/setupwizardlib/span/LinkSpanTest.java +++ b/library/test/robotest/src/com/android/setupwizardlib/span/LinkSpanTest.java @@ -16,11 +16,16 @@ package com.android.setupwizardlib.span; +import static com.google.common.truth.Truth.assertThat; + import static org.junit.Assert.assertSame; import static org.robolectric.RuntimeEnvironment.application; import android.content.Context; import android.content.ContextWrapper; +import android.text.Selection; +import android.text.SpannableStringBuilder; +import android.text.method.LinkMovementMethod; import android.widget.TextView; import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner; @@ -32,7 +37,7 @@ import org.junit.runner.RunWith; public class LinkSpanTest { @Test - public void testOnClick() { + public void onClick_shouldCallListenerOnContext() { final TestContext context = new TestContext(application); final TextView textView = new TextView(context); final LinkSpan linkSpan = new LinkSpan("test_id"); @@ -43,7 +48,7 @@ public class LinkSpanTest { } @Test - public void testNonImplementingContext() { + public void onClick_contextDoesNotImplementOnClickListener_shouldBeNoOp() { final TextView textView = new TextView(application); final LinkSpan linkSpan = new LinkSpan("test_id"); @@ -54,7 +59,7 @@ public class LinkSpanTest { } @Test - public void testWrappedListener() { + public void onClick_contextWrapsOnClickListener_shouldCallWrappedListener() { final TestContext context = new TestContext(application); final Context wrapperContext = new ContextWrapper(context); final TextView textView = new TextView(wrapperContext); @@ -65,6 +70,27 @@ public class LinkSpanTest { assertSame("Clicked LinkSpan should be passed to setup", linkSpan, context.clickedSpan); } + @Test + public void onClick_shouldClearSelection() { + final TestContext context = new TestContext(application); + final TextView textView = new TextView(context); + textView.setMovementMethod(LinkMovementMethod.getInstance()); + textView.setFocusable(true); + textView.setFocusableInTouchMode(true); + final LinkSpan linkSpan = new LinkSpan("test_id"); + + SpannableStringBuilder text = new SpannableStringBuilder("Lorem ipsum dolor sit"); + textView.setText(text); + text.setSpan(linkSpan, /* start= */ 0, /* end= */ 5, /* flags= */ 0); + // Simulate the touch effect set by TextView when touched. + Selection.setSelection(text, /* start= */ 0, /* end= */ 5); + + linkSpan.onClick(textView); + + assertThat(Selection.getSelectionStart(textView.getText())).isEqualTo(0); + assertThat(Selection.getSelectionEnd(textView.getText())).isEqualTo(0); + } + @SuppressWarnings("deprecation") private static class TestContext extends ContextWrapper implements LinkSpan.OnClickListener { diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/test/RichTextViewTest.java b/library/test/robotest/src/com/android/setupwizardlib/view/RichTextViewTest.java index 5f3eb9f..f77de68 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/test/RichTextViewTest.java +++ b/library/test/robotest/src/com/android/setupwizardlib/view/RichTextViewTest.java @@ -14,39 +14,45 @@ * limitations under the License. */ -package com.android.setupwizardlib.test; +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; +import static org.robolectric.RuntimeEnvironment.application; import android.annotation.SuppressLint; import android.content.Context; import android.content.ContextWrapper; -import android.support.test.InstrumentationRegistry; -import android.support.test.filters.SmallTest; -import android.support.test.runner.AndroidJUnit4; +import android.os.Build.VERSION; +import android.os.Build.VERSION_CODES; 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.RichTextView; +import com.android.setupwizardlib.view.TouchableMovementMethod.TouchableLinkMovementMethod; import org.junit.Test; import org.junit.runner.RunWith; +import org.robolectric.annotation.Config; import java.util.Arrays; -@RunWith(AndroidJUnit4.class) -@SmallTest +@RunWith(SuwLibRobolectricTestRunner.class) +@Config(sdk = { Config.OLDEST_SDK, Config.NEWEST_SDK }) public class RichTextViewTest { @Test @@ -55,12 +61,14 @@ public class RichTextViewTest { SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world"); ssb.setSpan(link, 1, 2, 0 /* flags */); - RichTextView textView = new RichTextView(InstrumentationRegistry.getContext()); + RichTextView textView = new RichTextView(application); textView.setText(ssb); 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); @@ -77,7 +85,7 @@ public class RichTextViewTest { SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world"); ssb.setSpan(link, 1, 2, 0 /* flags */); - RichTextView textView = new RichTextView(InstrumentationRegistry.getContext()); + RichTextView textView = new RichTextView(application); textView.setText(ssb); OnLinkClickListener listener = mock(OnLinkClickListener.class); @@ -99,7 +107,7 @@ public class RichTextViewTest { SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world"); ssb.setSpan(link, 1, 2, 0 /* flags */); - TestContext context = spy(new TestContext(InstrumentationRegistry.getTargetContext())); + TestContext context = spy(new TestContext(application)); RichTextView textView = new RichTextView(context); textView.setText(ssb); @@ -111,12 +119,50 @@ 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"); ssb.setSpan(link, 1, 2, 0 /* flags */); - RichTextView textView = new RichTextView(InstrumentationRegistry.getContext()); + RichTextView textView = new RichTextView(application); textView.setText(ssb); final CharSequence text = textView.getText(); @@ -137,7 +183,7 @@ public class RichTextViewTest { SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder("Linked"); spannableStringBuilder.setSpan(testLink, 0, 3, 0); - RichTextView view = new RichTextView(InstrumentationRegistry.getContext()); + RichTextView view = new RichTextView(application); view.setText(spannableStringBuilder); assertTrue("TextView should be focusable since it contains spans", view.isFocusable()); @@ -147,7 +193,7 @@ public class RichTextViewTest { @SuppressLint("SetTextI18n") // It's OK. This is just a test. @Test public void testTextContainingNoLinksAreNotFocusable() { - RichTextView textView = new RichTextView(InstrumentationRegistry.getContext()); + RichTextView textView = new RichTextView(application); textView.setText("Thou shall not be focusable!"); assertFalse("TextView should not be focusable since it does not contain any span", @@ -160,16 +206,23 @@ public class RichTextViewTest { @SuppressLint("SetTextI18n") // It's OK. This is just a test. @Test public void testRichTextViewFocusChangesWithTextChange() { - RichTextView textView = new RichTextView(InstrumentationRegistry.getContext()); + RichTextView textView = new RichTextView(application); textView.setText("Thou shall not be focusable!"); assertFalse(textView.isFocusable()); + assertFalse(textView.isFocusableInTouchMode()); SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder("I am focusable"); spannableStringBuilder.setSpan(new Annotation("link", "focus:on_me"), 0, 1, 0); textView.setText(spannableStringBuilder); assertTrue(textView.isFocusable()); + if (VERSION.SDK_INT >= VERSION_CODES.N_MR1) { + assertTrue(textView.isFocusableInTouchMode()); + assertFalse(textView.getRevealOnFocusHint()); + } else { + assertFalse(textView.isFocusableInTouchMode()); + } } public static class TestContext extends ContextWrapper implements LinkSpan.OnClickListener { |