Change UrlSpanFactory to a Function

Test: atest android.text.util.cts.LinkifyTest
Test: atest android.text.util.LinkifyTest

Bug: 28536972
Bug: 32613009
Bug: 29150779
Bug: 112303511
Change-Id: I73976e3e0bea6e61804d7735fb5901c91cb53758
This commit is contained in:
Siyamed Sinir
2019-01-11 12:03:20 -08:00
parent cf42497af1
commit 584d6d8cc5
2 changed files with 24 additions and 64 deletions

View File

@@ -46973,7 +46973,7 @@ 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.text.Spannable, int, java.util.function.Function<java.lang.String, android.text.style.URLSpan>);
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);
@@ -46981,7 +46981,7 @@ 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);
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, java.util.function.Function<java.lang.String, android.text.style.URLSpan>);
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
@@ -47000,11 +47000,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();

View File

@@ -37,7 +37,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;
@@ -49,6 +48,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Locale;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -69,7 +69,6 @@ import java.util.regex.Pattern;
*
* @see MatchFilter
* @see TransformFilter
* @see UrlSpanFactory
*/
public class Linkify {
@@ -227,44 +226,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.
@@ -277,7 +238,7 @@ public class Linkify {
*
* @return True if at least one link is found and applied.
*
* @see #addLinks(Spannable, int, UrlSpanFactory)
* @see #addLinks(Spannable, int, Function)
*/
public static final boolean addLinks(@NonNull Spannable text, @LinkifyMask int mask) {
return addLinks(text, mask, null, null);
@@ -292,11 +253,11 @@ public class Linkify {
*
* @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
* @param urlSpanFactory function used to create {@link URLSpan}s
* @return True if at least one link is found and applied.
*/
public static final boolean addLinks(@NonNull Spannable text, @LinkifyMask int mask,
@Nullable UrlSpanFactory urlSpanFactory) {
@Nullable Function<String, URLSpan> urlSpanFactory) {
return addLinks(text, mask, null, urlSpanFactory);
}
@@ -309,11 +270,11 @@ public class Linkify {
* @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
* @param urlSpanFactory function 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, @Nullable Function<String, URLSpan> urlSpanFactory) {
if (text != null && containsUnsupportedCharacters(text.toString())) {
android.util.EventLog.writeEvent(0x534e4554, "116321860", -1, "");
return false;
@@ -398,7 +359,7 @@ public class Linkify {
*
* @return True if at least one link is found and applied.
*
* @see #addLinks(Spannable, int, UrlSpanFactory)
* @see #addLinks(Spannable, int, Function)
*/
public static final boolean addLinks(@NonNull TextView text, @LinkifyMask int mask) {
if (mask == 0) {
@@ -512,8 +473,7 @@ 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)
* @see #addLinks(Spannable, Pattern, String, String[], MatchFilter, TransformFilter, Function)
*/
public static final boolean addLinks(@NonNull Spannable text, @NonNull Pattern pattern,
@Nullable String scheme) {
@@ -534,8 +494,7 @@ 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)
* @see #addLinks(Spannable, Pattern, String, String[], MatchFilter, TransformFilter, Function)
*/
public static final boolean addLinks(@NonNull Spannable spannable, @NonNull Pattern pattern,
@Nullable String scheme, @Nullable MatchFilter matchFilter,
@@ -560,8 +519,7 @@ public class Linkify {
*
* @return True if at least one link is found and applied.
*
* @see #addLinks(Spannable, Pattern, String, String[], MatchFilter, TransformFilter,
* UrlSpanFactory)
* @see #addLinks(Spannable, Pattern, String, String[], MatchFilter, TransformFilter, Function)
*/
public static final boolean addLinks(@NonNull Spannable spannable, @NonNull Pattern pattern,
@Nullable String defaultScheme, @Nullable String[] schemes,
@@ -584,14 +542,14 @@ public class Linkify {
* @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
* @param urlSpanFactory function 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) {
@Nullable Function<String, URLSpan> urlSpanFactory) {
if (spannable != null && containsUnsupportedCharacters(spannable.toString())) {
android.util.EventLog.writeEvent(0x534e4554, "116321860", -1, "");
return false;
@@ -634,11 +592,11 @@ public class Linkify {
}
private static void applyLink(String url, int start, int end, Spannable text,
@Nullable UrlSpanFactory urlSpanFactory) {
@Nullable Function<String, URLSpan> urlSpanFactory) {
if (urlSpanFactory == null) {
urlSpanFactory = UrlSpanFactory.getInstance();
urlSpanFactory = DEFAULT_SPAN_FACTORY;
}
final URLSpan span = urlSpanFactory.create(url);
final URLSpan span = urlSpanFactory.apply(url);
text.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
@@ -805,6 +763,13 @@ public class Linkify {
i++;
}
}
/**
* Default factory function to create {@link URLSpan}s. While adding spans to a
* {@link Spannable}, {@link Linkify} will call this function to create a {@link URLSpan}.
*/
private static final Function<String, URLSpan> DEFAULT_SPAN_FACTORY =
(String string) -> new URLSpan(string);
}
class LinkSpec {