textclassifier/src/main/java/androidx/textclassifier/TextLinks.java
@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;
    }
/**
     * Annotates the given text with the generated links. It will fail if the provided text doesn't
     * match the original text used to crete the TextLinks.
     *
     * @param text the text to apply the links to. Must match the original text.
     * @param spanFactory a factory to generate spans from TextLinks. Will use a default if null.
     *
     * @return one of {@link #STATUS_LINKS_APPLIED}, {@link #STATUS_NO_LINKS_FOUND},
     *      {@link #STATUS_NO_LINKS_APPLIED}, {@link #STATUS_DIFFERENT_TEXT}
     */
Annotates the given text with the generated links. It will fail if the provided text doesn't
match the original text used to crete the TextLinks.

@param text the text to apply the links to. Must match the original text.
@param spanFactory a factory to generate spans from TextLinks. Will use a default if null.


Annotates the given text with the generated links . It will fail if the provided text doesn ' t match the original text used to crete the TextLinks . @param text the text to apply the links to . Must match the original text . @param spanFactory a factory to generate spans from TextLinks . Will use a default if null .
@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;
    }
@ 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 ; }
Annotates the given text with the generated links.
Annotates the given text with the generated links .