Allow set LinkSpan click listener on RichTextView

In addition to allowing setting a LinkSpan click listener in the
context, also allow setting it on RichTextView itself.

Test: ./gradlew connectedAndroidTest
Change-Id: I6b300068db0e68d351d635897220535a5aa92c93
This commit is contained in:
Maurice Lam 2017-04-18 16:01:26 -07:00
parent ba64d20153
commit 51f4609c81
4 changed files with 142 additions and 10 deletions

View file

@ -33,6 +33,7 @@ import android.util.Log;
import android.view.MotionEvent;
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;
@ -40,7 +41,7 @@ import com.android.setupwizardlib.util.LinkAccessibilityHelper;
* An extension of TextView that automatically replaces the annotation tags as specified in
* {@link SpanHelper#replaceSpan(android.text.Spannable, Object, Object)}
*/
public class RichTextView extends AppCompatTextView {
public class RichTextView extends AppCompatTextView implements OnLinkClickListener {
/* static section */
@ -89,6 +90,7 @@ public class RichTextView extends AppCompatTextView {
/* non-static section */
private LinkAccessibilityHelper mAccessibilityHelper;
private OnLinkClickListener mOnLinkClickListener;
public RichTextView(Context context) {
super(context);
@ -164,4 +166,20 @@ public class RichTextView extends AppCompatTextView {
}
}
}
public void setOnLinkClickListener(OnLinkClickListener listener) {
mOnLinkClickListener = listener;
}
public OnLinkClickListener getOnLinkClickListener() {
return mOnLinkClickListener;
}
@Override
public boolean onLinkClick(LinkSpan span) {
if (mOnLinkClickListener != null) {
return mOnLinkClickListener.onLinkClick(span);
}
return false;
}
}

View file

@ -28,8 +28,8 @@ import android.view.View;
/**
* A clickable span that will listen for click events and send it back to the context. To use this
* class, implement {@link com.android.setupwizardlib.span.LinkSpan.OnClickListener} in your
* context (typically your Activity).
* class, implement {@link OnLinkClickListener} in your TextView, or use
* {@link com.android.setupwizardlib.view.RichTextView#setOnClickListener(View.OnClickListener)}.
*
* <p />Note on accessibility: For TalkBack to be able to traverse and interact with the links, you
* should use {@code LinkAccessibilityHelper} in your {@code TextView} subclass. Optionally you can
@ -51,10 +51,29 @@ public class LinkSpan extends ClickableSpan {
private static final Typeface TYPEFACE_MEDIUM =
Typeface.create("sans-serif-medium", Typeface.NORMAL);
/**
* @deprecated Use {@link OnLinkClickListener}
*/
@Deprecated
public interface OnClickListener {
void onClick(LinkSpan span);
}
/**
* Listener that is invoked when a link span is clicked. If the containing view of this span
* implements this interface, this will be invoked when the link is clicked.
*/
public interface OnLinkClickListener {
/**
* Called when a link has been clicked.
*
* @param span The span that was clicked.
* @return True if the click was handled, stopping further propagation of the click event.
*/
boolean onLinkClick(LinkSpan span);
}
/* non-static section */
private final String mId;
@ -65,9 +84,7 @@ public class LinkSpan extends ClickableSpan {
@Override
public void onClick(View view) {
final OnClickListener listener = getListenerFromContext(view.getContext());
if (listener != null) {
listener.onClick(this);
if (dispatchClick(view)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
view.cancelPendingInputEvents();
}
@ -76,8 +93,27 @@ public class LinkSpan extends ClickableSpan {
}
}
private boolean dispatchClick(View view) {
boolean handled = false;
if (view instanceof OnLinkClickListener) {
handled = ((OnLinkClickListener) view).onLinkClick(this);
}
if (!handled) {
final OnClickListener listener = getLegacyListenerFromContext(view.getContext());
if (listener != null) {
listener.onClick(this);
handled = true;
}
}
return handled;
}
/**
* @deprecated Deprecated together with {@link OnClickListener}
*/
@Nullable
private OnClickListener getListenerFromContext(@Nullable Context context) {
@Deprecated
private OnClickListener getLegacyListenerFromContext(@Nullable Context context) {
while (true) {
if (context instanceof OnClickListener) {
return (OnClickListener) context;

View file

@ -28,6 +28,7 @@ import android.util.Log;
import android.widget.TextView;
import com.android.setupwizardlib.span.LinkSpan;
import com.android.setupwizardlib.span.LinkSpan.OnLinkClickListener;
import com.android.setupwizardlib.span.SpanHelper;
/**
@ -39,7 +40,7 @@ import com.android.setupwizardlib.span.SpanHelper;
* platform version, the links are exposed in the Local Context Menu of TalkBack instead of
* accessible directly through swiping.
*/
public class RichTextView extends TextView {
public class RichTextView extends TextView implements OnLinkClickListener {
/* static section */
@ -87,6 +88,8 @@ public class RichTextView extends TextView {
/* non-static section */
private OnLinkClickListener mOnLinkClickListener;
public RichTextView(Context context) {
super(context);
}
@ -128,4 +131,20 @@ public class RichTextView extends TextView {
}
return false;
}
public void setOnLinkClickListener(OnLinkClickListener listener) {
mOnLinkClickListener = listener;
}
public OnLinkClickListener getOnLinkClickListener() {
return mOnLinkClickListener;
}
@Override
public boolean onLinkClick(LinkSpan span) {
if (mOnLinkClickListener != null) {
return mOnLinkClickListener.onLinkClick(span);
}
return false;
}
}

View file

@ -18,9 +18,16 @@ package com.android.setupwizardlib.test;
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.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
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;
@ -30,6 +37,7 @@ import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import com.android.setupwizardlib.span.LinkSpan;
import com.android.setupwizardlib.span.LinkSpan.OnLinkClickListener;
import com.android.setupwizardlib.view.RichTextView;
import org.junit.Test;
@ -63,6 +71,45 @@ public class RichTextViewTest {
"foobar", ((LinkSpan) spans[0]).getId());
}
@Test
public void testOnLinkClickListener() {
Annotation link = new Annotation("link", "foobar");
SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world");
ssb.setSpan(link, 1, 2, 0 /* flags */);
RichTextView textView = new RichTextView(InstrumentationRegistry.getContext());
textView.setText(ssb);
OnLinkClickListener listener = mock(OnLinkClickListener.class);
textView.setOnLinkClickListener(listener);
assertSame(listener, textView.getOnLinkClickListener());
CharSequence text = textView.getText();
LinkSpan[] spans = ((Spanned) text).getSpans(0, text.length(), LinkSpan.class);
spans[0].onClick(textView);
verify(listener).onLinkClick(eq(spans[0]));
}
@Test
public void testLegacyContextOnClickListener() {
// Click listener implemented by context should still be invoked for compatibility.
Annotation link = new Annotation("link", "foobar");
SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world");
ssb.setSpan(link, 1, 2, 0 /* flags */);
TestContext context = spy(new TestContext(InstrumentationRegistry.getTargetContext()));
RichTextView textView = new RichTextView(context);
textView.setText(ssb);
CharSequence text = textView.getText();
LinkSpan[] spans = ((Spanned) text).getSpans(0, text.length(), LinkSpan.class);
spans[0].onClick(textView);
verify(context).onClick(eq(spans[0]));
}
@Test
public void testTextStyle() {
Annotation link = new Annotation("textAppearance", "foobar");
@ -85,7 +132,7 @@ public class RichTextViewTest {
}
@Test
public void testTextContaininingLinksAreFocusable() {
public void testTextContainingLinksAreFocusable() {
Annotation testLink = new Annotation("link", "value");
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder("Linked");
spannableStringBuilder.setSpan(testLink, 0, 3, 0);
@ -112,7 +159,7 @@ public class RichTextViewTest {
// should also be automatically changed.
@SuppressLint("SetTextI18n") // It's OK. This is just a test.
@Test
public void testRichTxtViewFocusChangesWithTextChange() {
public void testRichTextViewFocusChangesWithTextChange() {
RichTextView textView = new RichTextView(InstrumentationRegistry.getContext());
textView.setText("Thou shall not be focusable!");
@ -124,4 +171,16 @@ public class RichTextViewTest {
textView.setText(spannableStringBuilder);
assertTrue(textView.isFocusable());
}
public static class TestContext extends ContextWrapper implements LinkSpan.OnClickListener {
public TestContext(Context base) {
super(base);
}
@Override
public void onClick(LinkSpan span) {
// Ignore. Can be verified using Mockito
}
}
}