diff options
-rw-r--r-- | library/gingerbread/src/com/android/setupwizardlib/view/RichTextView.java | 20 | ||||
-rw-r--r-- | library/main/src/com/android/setupwizardlib/span/LinkSpan.java | 48 | ||||
-rw-r--r-- | library/platform/src/com/android/setupwizardlib/view/RichTextView.java | 21 | ||||
-rw-r--r-- | library/test/instrumentation/src/com/android/setupwizardlib/test/RichTextViewTest.java (renamed from library/gingerbread/test/instrumentation/src/com/android/setupwizardlib/test/RichTextViewTest.java) | 63 |
4 files changed, 142 insertions, 10 deletions
diff --git a/library/gingerbread/src/com/android/setupwizardlib/view/RichTextView.java b/library/gingerbread/src/com/android/setupwizardlib/view/RichTextView.java index 1cf76e1..e6bc9da 100644 --- a/library/gingerbread/src/com/android/setupwizardlib/view/RichTextView.java +++ b/library/gingerbread/src/com/android/setupwizardlib/view/RichTextView.java @@ -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; + } } diff --git a/library/main/src/com/android/setupwizardlib/span/LinkSpan.java b/library/main/src/com/android/setupwizardlib/span/LinkSpan.java index 89ffff7..a5f0424 100644 --- a/library/main/src/com/android/setupwizardlib/span/LinkSpan.java +++ b/library/main/src/com/android/setupwizardlib/span/LinkSpan.java @@ -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; diff --git a/library/platform/src/com/android/setupwizardlib/view/RichTextView.java b/library/platform/src/com/android/setupwizardlib/view/RichTextView.java index 3acb918..5a78561 100644 --- a/library/platform/src/com/android/setupwizardlib/view/RichTextView.java +++ b/library/platform/src/com/android/setupwizardlib/view/RichTextView.java @@ -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; + } } diff --git a/library/gingerbread/test/instrumentation/src/com/android/setupwizardlib/test/RichTextViewTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/test/RichTextViewTest.java index 2e2b01e..5f3eb9f 100644 --- a/library/gingerbread/test/instrumentation/src/com/android/setupwizardlib/test/RichTextViewTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/test/RichTextViewTest.java @@ -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; @@ -64,6 +72,45 @@ public class RichTextViewTest { } @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"); SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world"); @@ -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 + } + } } |