@Test public void testApplyDifferentText() { SpannableString text = new SpannableString("foo"); TextLinks links = new TextLinks.Builder("bar").build(); assertEquals(links.apply(text, TextLinks.APPLY_STRATEGY_REPLACE, null), TextLinks.STATUS_DIFFERENT_TEXT); }
@Status public int apply( @NonNull Spannable text, @ApplyStrategy int applyStrategy, @Nullable SpanFactory spanFactory) { Preconditions.checkNotNull(text); checkValidApplyStrategy(applyStrategy); if (!mFullText.equals(text.toString())) { return STATUS_DIFFERENT_TEXT; } if (mLinks.isEmpty()) { return STATUS_NO_LINKS_FOUND; } if (spanFactory == null) { spanFactory = DEFAULT_SPAN_FACTORY; } int applyCount = 0; for (TextLink link : mLinks) { final TextLinkSpan span = spanFactory.createSpan(link); if (span != null) { final ClickableSpan[] existingSpans = text.getSpans( link.getStart(), link.getEnd(), ClickableSpan.class); if (existingSpans.length > 0) { if (applyStrategy == APPLY_STRATEGY_REPLACE) { for (ClickableSpan existingSpan : existingSpans) { text.removeSpan(existingSpan); } text.setSpan(span, link.getStart(), link.getEnd(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); applyCount++; } } else { text.setSpan(span, link.getStart(), link.getEnd(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); applyCount++; } } } if (applyCount == 0) { return STATUS_NO_LINKS_APPLIED; } return STATUS_LINKS_APPLIED; }
TextLinks implements Parcelable { @Status public int apply( @NonNull Spannable text, @ApplyStrategy int applyStrategy, @Nullable SpanFactory spanFactory) { Preconditions.checkNotNull(text); checkValidApplyStrategy(applyStrategy); if (!mFullText.equals(text.toString())) { return STATUS_DIFFERENT_TEXT; } if (mLinks.isEmpty()) { return STATUS_NO_LINKS_FOUND; } if (spanFactory == null) { spanFactory = DEFAULT_SPAN_FACTORY; } int applyCount = 0; for (TextLink link : mLinks) { final TextLinkSpan span = spanFactory.createSpan(link); if (span != null) { final ClickableSpan[] existingSpans = text.getSpans( link.getStart(), link.getEnd(), ClickableSpan.class); if (existingSpans.length > 0) { if (applyStrategy == APPLY_STRATEGY_REPLACE) { for (ClickableSpan existingSpan : existingSpans) { text.removeSpan(existingSpan); } text.setSpan(span, link.getStart(), link.getEnd(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); applyCount++; } } else { text.setSpan(span, link.getStart(), link.getEnd(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); applyCount++; } } } if (applyCount == 0) { return STATUS_NO_LINKS_APPLIED; } return STATUS_LINKS_APPLIED; } }
TextLinks implements Parcelable { @Status public int apply( @NonNull Spannable text, @ApplyStrategy int applyStrategy, @Nullable SpanFactory spanFactory) { Preconditions.checkNotNull(text); checkValidApplyStrategy(applyStrategy); if (!mFullText.equals(text.toString())) { return STATUS_DIFFERENT_TEXT; } if (mLinks.isEmpty()) { return STATUS_NO_LINKS_FOUND; } if (spanFactory == null) { spanFactory = DEFAULT_SPAN_FACTORY; } int applyCount = 0; for (TextLink link : mLinks) { final TextLinkSpan span = spanFactory.createSpan(link); if (span != null) { final ClickableSpan[] existingSpans = text.getSpans( link.getStart(), link.getEnd(), ClickableSpan.class); if (existingSpans.length > 0) { if (applyStrategy == APPLY_STRATEGY_REPLACE) { for (ClickableSpan existingSpan : existingSpans) { text.removeSpan(existingSpan); } text.setSpan(span, link.getStart(), link.getEnd(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); applyCount++; } } else { text.setSpan(span, link.getStart(), link.getEnd(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); applyCount++; } } } if (applyCount == 0) { return STATUS_NO_LINKS_APPLIED; } return STATUS_LINKS_APPLIED; } private  TextLinks(String fullText, ArrayList<TextLink> links); private  TextLinks(Parcel in); }
TextLinks implements Parcelable { @Status public int apply( @NonNull Spannable text, @ApplyStrategy int applyStrategy, @Nullable SpanFactory spanFactory) { Preconditions.checkNotNull(text); checkValidApplyStrategy(applyStrategy); if (!mFullText.equals(text.toString())) { return STATUS_DIFFERENT_TEXT; } if (mLinks.isEmpty()) { return STATUS_NO_LINKS_FOUND; } if (spanFactory == null) { spanFactory = DEFAULT_SPAN_FACTORY; } int applyCount = 0; for (TextLink link : mLinks) { final TextLinkSpan span = spanFactory.createSpan(link); if (span != null) { final ClickableSpan[] existingSpans = text.getSpans( link.getStart(), link.getEnd(), ClickableSpan.class); if (existingSpans.length > 0) { if (applyStrategy == APPLY_STRATEGY_REPLACE) { for (ClickableSpan existingSpan : existingSpans) { text.removeSpan(existingSpan); } text.setSpan(span, link.getStart(), link.getEnd(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); applyCount++; } } else { text.setSpan(span, link.getStart(), link.getEnd(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); applyCount++; } } } if (applyCount == 0) { return STATUS_NO_LINKS_APPLIED; } return STATUS_LINKS_APPLIED; } private  TextLinks(String fullText, ArrayList<TextLink> links); private  TextLinks(Parcel in); Collection<TextLink> getLinks(); @Status int apply(
            @NonNull Spannable text,
            @ApplyStrategy int applyStrategy,
            @Nullable SpanFactory spanFactory); @Override int describeContents(); @Override void writeToParcel(Parcel dest, int flags); }
TextLinks implements Parcelable { @Status public int apply( @NonNull Spannable text, @ApplyStrategy int applyStrategy, @Nullable SpanFactory spanFactory) { Preconditions.checkNotNull(text); checkValidApplyStrategy(applyStrategy); if (!mFullText.equals(text.toString())) { return STATUS_DIFFERENT_TEXT; } if (mLinks.isEmpty()) { return STATUS_NO_LINKS_FOUND; } if (spanFactory == null) { spanFactory = DEFAULT_SPAN_FACTORY; } int applyCount = 0; for (TextLink link : mLinks) { final TextLinkSpan span = spanFactory.createSpan(link); if (span != null) { final ClickableSpan[] existingSpans = text.getSpans( link.getStart(), link.getEnd(), ClickableSpan.class); if (existingSpans.length > 0) { if (applyStrategy == APPLY_STRATEGY_REPLACE) { for (ClickableSpan existingSpan : existingSpans) { text.removeSpan(existingSpan); } text.setSpan(span, link.getStart(), link.getEnd(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); applyCount++; } } else { text.setSpan(span, link.getStart(), link.getEnd(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); applyCount++; } } } if (applyCount == 0) { return STATUS_NO_LINKS_APPLIED; } return STATUS_LINKS_APPLIED; } private  TextLinks(String fullText, ArrayList<TextLink> links); private  TextLinks(Parcel in); Collection<TextLink> getLinks(); @Status int apply(
            @NonNull Spannable text,
            @ApplyStrategy int applyStrategy,
            @Nullable SpanFactory spanFactory); @Override int describeContents(); @Override void writeToParcel(Parcel dest, int flags); static final int STATUS_LINKS_APPLIED; static final int STATUS_NO_LINKS_FOUND; static final int STATUS_NO_LINKS_APPLIED; static final int STATUS_DIFFERENT_TEXT; static final int APPLY_STRATEGY_IGNORE; static final int APPLY_STRATEGY_REPLACE; static final Parcelable.Creator<TextLinks> CREATOR; }