Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d9afa508 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Revert "Add UrlSpanFactory to Linkify""

parents 90671be7 0a35c20b
Loading
Loading
Loading
Loading
+0 −7
Original line number Diff line number Diff line
@@ -44749,7 +44749,6 @@ package android.text.util {
  public class Linkify {
    ctor public Linkify();
    method public static final boolean addLinks(android.text.Spannable, int);
    method public static final boolean addLinks(android.text.Spannable, int, android.text.util.Linkify.UrlSpanFactory);
    method public static final boolean addLinks(android.widget.TextView, int);
    method public static final void addLinks(android.widget.TextView, java.util.regex.Pattern, java.lang.String);
    method public static final void addLinks(android.widget.TextView, java.util.regex.Pattern, java.lang.String, android.text.util.Linkify.MatchFilter, android.text.util.Linkify.TransformFilter);
@@ -44757,7 +44756,6 @@ package android.text.util {
    method public static final boolean addLinks(android.text.Spannable, java.util.regex.Pattern, java.lang.String);
    method public static final boolean addLinks(android.text.Spannable, java.util.regex.Pattern, java.lang.String, android.text.util.Linkify.MatchFilter, android.text.util.Linkify.TransformFilter);
    method public static final boolean addLinks(android.text.Spannable, java.util.regex.Pattern, java.lang.String, java.lang.String[], android.text.util.Linkify.MatchFilter, android.text.util.Linkify.TransformFilter);
    method public static final boolean addLinks(android.text.Spannable, java.util.regex.Pattern, java.lang.String, java.lang.String[], android.text.util.Linkify.MatchFilter, android.text.util.Linkify.TransformFilter, android.text.util.Linkify.UrlSpanFactory);
    field public static final int ALL = 15; // 0xf
    field public static final int EMAIL_ADDRESSES = 2; // 0x2
    field public static final deprecated int MAP_ADDRESSES = 8; // 0x8
@@ -44776,11 +44774,6 @@ package android.text.util {
    method public abstract java.lang.String transformUrl(java.util.regex.Matcher, java.lang.String);
  }
  public static class Linkify.UrlSpanFactory {
    ctor public Linkify.UrlSpanFactory();
    method public android.text.style.URLSpan create(java.lang.String);
  }
  public class Rfc822Token {
    ctor public Rfc822Token(java.lang.String, java.lang.String, java.lang.String);
    method public java.lang.String getAddress();
+12 −126
Original line number Diff line number Diff line
@@ -35,7 +35,6 @@ import android.widget.TextView;
import com.android.i18n.phonenumbers.PhoneNumberMatch;
import com.android.i18n.phonenumbers.PhoneNumberUtil;
import com.android.i18n.phonenumbers.PhoneNumberUtil.Leniency;
import com.android.internal.annotations.GuardedBy;

import libcore.util.EmptyArray;

@@ -64,10 +63,6 @@ import java.util.regex.Pattern;
 *  does not have a URL scheme prefix, the supplied scheme will be prepended to
 *  create <code>http://example.com</code> when the clickable URL link is
 *  created.
 *
 * @see MatchFilter
 * @see TransformFilter
 * @see UrlSpanFactory
 */

public class Linkify {
@@ -222,44 +217,6 @@ public class Linkify {
        String transformUrl(final Matcher match, String url);
    }

    /**
     * Factory class to create {@link URLSpan}s. While adding spans to a {@link Spannable},
     * {@link Linkify} will call {@link UrlSpanFactory#create(String)} function to create a
     * {@link URLSpan}.
     *
     * @see #addLinks(Spannable, int, UrlSpanFactory)
     * @see #addLinks(Spannable, Pattern, String, String[], MatchFilter, TransformFilter,
     * UrlSpanFactory)
     */
    public static class UrlSpanFactory {
        private static final Object sInstanceLock = new Object();

        @GuardedBy("sInstanceLock")
        private static volatile UrlSpanFactory sInstance = null;

        private static synchronized UrlSpanFactory getInstance() {
            if (sInstance == null) {
                synchronized (sInstanceLock) {
                    if (sInstance == null) {
                        sInstance = new UrlSpanFactory();
                    }
                }
            }
            return sInstance;
        }

        /**
         * Factory function that will called by {@link Linkify} in order to create a
         * {@link URLSpan}.
         *
         * @param url URL found
         * @return a URLSpan instance
         */
        public URLSpan create(final String url) {
            return new URLSpan(url);
        }
    }

    /**
     *  Scans the text of the provided Spannable and turns all occurrences
     *  of the link types indicated in the mask into clickable links.
@@ -271,57 +228,24 @@ public class Linkify {
     *  @param mask Mask to define which kinds of links will be searched.
     *
     *  @return True if at least one link is found and applied.
     *
     * @see #addLinks(Spannable, int, UrlSpanFactory)
     */
    public static final boolean addLinks(@NonNull Spannable text, @LinkifyMask int mask) {
        return addLinks(text, mask, null, null);
        return addLinks(text, mask, null);
    }

    /**
     *  Scans the text of the provided Spannable and turns all occurrences
     *  of the link types indicated in the mask into clickable links.
     *  If the mask is nonzero, it also removes any existing URLSpans
     *  attached to the Spannable, to avoid problems if you call it
     *  repeatedly on the same text.
     *
     *  @param text Spannable whose text is to be marked-up with links
     *  @param mask mask to define which kinds of links will be searched
     *  @param urlSpanFactory factory class used to create {@link URLSpan}s
     *  @return True if at least one link is found and applied.
     *
     * @see #addLinks(Spannable, int, UrlSpanFactory)
     */
    public static final boolean addLinks(@NonNull Spannable text, @LinkifyMask int mask,
            @Nullable UrlSpanFactory urlSpanFactory) {
        return addLinks(text, mask, null, null);
    }

    /**
     *  Scans the text of the provided Spannable and turns all occurrences of the link types
     *  indicated in the mask into clickable links. If the mask is nonzero, it also removes any
     *  existing URLSpans attached to the Spannable, to avoid problems if you call it repeatedly
     *  on the same text.
     *
     * @param text Spannable whose text is to be marked-up with links
     * @param mask mask to define which kinds of links will be searched
     * @param context Context to be used while identifying phone numbers
     * @param urlSpanFactory factory class used to create {@link URLSpan}s
     * @return true if at least one link is found and applied.
     */
    private static boolean addLinks(@NonNull Spannable text, @LinkifyMask int mask,
            @Nullable Context context, @Nullable UrlSpanFactory urlSpanFactory) {
            @Nullable Context context) {
        if (mask == 0) {
            return false;
        }

        final URLSpan[] old = text.getSpans(0, text.length(), URLSpan.class);
        URLSpan[] old = text.getSpans(0, text.length(), URLSpan.class);

        for (int i = old.length - 1; i >= 0; i--) {
            text.removeSpan(old[i]);
        }

        final ArrayList<LinkSpec> links = new ArrayList<LinkSpec>();
        ArrayList<LinkSpec> links = new ArrayList<LinkSpec>();

        if ((mask & WEB_URLS) != 0) {
            gatherLinks(links, text, Patterns.AUTOLINK_WEB_URL,
@@ -350,7 +274,7 @@ public class Linkify {
        }

        for (LinkSpec link: links) {
            applyLink(link.url, link.start, link.end, text, urlSpanFactory);
            applyLink(link.url, link.start, link.end, text);
        }

        return true;
@@ -366,8 +290,6 @@ public class Linkify {
     *  @param mask Mask to define which kinds of links will be searched.
     *
     *  @return True if at least one link is found and applied.
     *
     *  @see #addLinks(Spannable, int, UrlSpanFactory)
     */
    public static final boolean addLinks(@NonNull TextView text, @LinkifyMask int mask) {
        if (mask == 0) {
@@ -377,7 +299,7 @@ public class Linkify {
        final Context context = text.getContext();
        final CharSequence t = text.getText();
        if (t instanceof Spannable) {
            if (addLinks((Spannable) t, mask, context, null)) {
            if (addLinks((Spannable) t, mask, context)) {
                addLinkMovementMethod(text);
                return true;
            }
@@ -386,7 +308,7 @@ public class Linkify {
        } else {
            SpannableString s = SpannableString.valueOf(t);

            if (addLinks(s, mask, context, null)) {
            if (addLinks(s, mask, context)) {
                addLinkMovementMethod(text);
                text.setText(s);

@@ -481,8 +403,6 @@ public class Linkify {
     *  @param pattern      Regex pattern to be used for finding links
     *  @param scheme       URL scheme string (eg <code>http://</code>) to be
     *                      prepended to the links that do not start with this scheme.
     * @see #addLinks(Spannable, Pattern, String, String[], MatchFilter, TransformFilter,
     * UrlSpanFactory)
     */
    public static final boolean addLinks(@NonNull Spannable text, @NonNull Pattern pattern,
            @Nullable String scheme) {
@@ -503,8 +423,6 @@ public class Linkify {
     * @param transformFilter Filter to allow the client code to update the link found.
     *
     * @return True if at least one link is found and applied.
     * @see #addLinks(Spannable, Pattern, String, String[], MatchFilter, TransformFilter,
     * UrlSpanFactory)
     */
    public static final boolean addLinks(@NonNull Spannable spannable, @NonNull Pattern pattern,
            @Nullable String scheme, @Nullable MatchFilter matchFilter,
@@ -528,39 +446,10 @@ public class Linkify {
     * @param transformFilter Filter to allow the client code to update the link found.
     *
     * @return True if at least one link is found and applied.
     *
     * @see #addLinks(Spannable, Pattern, String, String[], MatchFilter, TransformFilter,
     * UrlSpanFactory)
     */
    public static final boolean addLinks(@NonNull Spannable spannable, @NonNull Pattern pattern,
            @Nullable  String defaultScheme, @Nullable String[] schemes,
            @Nullable MatchFilter matchFilter, @Nullable TransformFilter transformFilter) {
        return addLinks(spannable, pattern, defaultScheme, schemes, matchFilter, transformFilter,
                null);
    }

    /**
     * Applies a regex to a Spannable turning the matches into links.
     *
     * @param spannable       spannable whose text is to be marked-up with links.
     * @param pattern         regex pattern to be used for finding links.
     * @param defaultScheme   the default scheme to be prepended to links if the link does not
     *                        start with one of the <code>schemes</code> given.
     * @param schemes         array of schemes (eg <code>http://</code>) to check if the link found
     *                        contains a scheme. Passing a null or empty value means prepend
     *                        defaultScheme
     *                        to all links.
     * @param matchFilter     the filter that is used to allow the client code additional control
     *                        over which pattern matches are to be converted into links.
     * @param transformFilter filter to allow the client code to update the link found.
     * @param urlSpanFactory  factory class used to create {@link URLSpan}s
     *
     * @return True if at least one link is found and applied.
     */
    public static final boolean addLinks(@NonNull Spannable spannable, @NonNull Pattern pattern,
            @Nullable String defaultScheme, @Nullable String[] schemes,
            @Nullable MatchFilter matchFilter, @Nullable TransformFilter transformFilter,
            @Nullable UrlSpanFactory urlSpanFactory) {
        final String[] schemesCopy;
        if (defaultScheme == null) defaultScheme = "";
        if (schemes == null || schemes.length < 1) {
@@ -589,7 +478,7 @@ public class Linkify {
            if (allowed) {
                String url = makeUrl(m.group(0), schemesCopy, m, transformFilter);

                applyLink(url, start, end, spannable, urlSpanFactory);
                applyLink(url, start, end, spannable);
                hasMatches = true;
            }
        }
@@ -597,12 +486,9 @@ public class Linkify {
        return hasMatches;
    }

    private static void applyLink(String url, int start, int end, Spannable text,
            @Nullable UrlSpanFactory urlSpanFactory) {
        if (urlSpanFactory == null) {
            urlSpanFactory = UrlSpanFactory.getInstance();
        }
        final URLSpan span = urlSpanFactory.create(url);
    private static final void applyLink(String url, int start, int end, Spannable text) {
        URLSpan span = new URLSpan(url);

        text.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }