下面列出了android.text.util.Linkify#WEB_URLS 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private SpannableString linkifyMessageBody(SpannableString messageBody, boolean shouldLinkifyAllLinks) {
int linkPattern = Linkify.WEB_URLS | Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS;
boolean hasLinks = Linkify.addLinks(messageBody, shouldLinkifyAllLinks ? linkPattern : 0);
if (hasLinks) {
Stream.of(messageBody.getSpans(0, messageBody.length(), URLSpan.class))
.filterNot(url -> LinkPreviewUtil.isLegalUrl(url.getURL()))
.forEach(messageBody::removeSpan);
URLSpan[] urlSpans = messageBody.getSpans(0, messageBody.length(), URLSpan.class);
for (URLSpan urlSpan : urlSpans) {
int start = messageBody.getSpanStart(urlSpan);
int end = messageBody.getSpanEnd(urlSpan);
messageBody.setSpan(new LongClickCopySpan(urlSpan.getURL()), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
return messageBody;
}
/**
* Returns a new TextLinksParams object based on the specified link mask.
*
* @param mask the link mask
* e.g. {@link LinkifyMask#PHONE_NUMBERS} | {@link LinkifyMask#EMAIL_ADDRESSES}
* @hide
*/
@NonNull
public static TextLinksParams fromLinkMask(@LinkifyMask int mask) {
final List<String> entitiesToFind = new ArrayList<>();
if ((mask & Linkify.WEB_URLS) != 0) {
entitiesToFind.add(TextClassifier.TYPE_URL);
}
if ((mask & Linkify.EMAIL_ADDRESSES) != 0) {
entitiesToFind.add(TextClassifier.TYPE_EMAIL);
}
if ((mask & Linkify.PHONE_NUMBERS) != 0) {
entitiesToFind.add(TextClassifier.TYPE_PHONE);
}
if ((mask & Linkify.MAP_ADDRESSES) != 0) {
entitiesToFind.add(TextClassifier.TYPE_ADDRESS);
}
return new TextLinksParams.Builder().setEntityConfig(
TextClassifier.EntityConfig.createWithExplicitEntityList(entitiesToFind))
.build();
}
/** Returns a new options object based on the specified link mask. */
public static Options fromLinkMask(@LinkifyMask int mask) {
final List<String> entitiesToFind = new ArrayList<>();
if ((mask & Linkify.WEB_URLS) != 0) {
entitiesToFind.add(TextClassifier.TYPE_URL);
}
if ((mask & Linkify.EMAIL_ADDRESSES) != 0) {
entitiesToFind.add(TextClassifier.TYPE_EMAIL);
}
if ((mask & Linkify.PHONE_NUMBERS) != 0) {
entitiesToFind.add(TextClassifier.TYPE_PHONE);
}
if ((mask & Linkify.MAP_ADDRESSES) != 0) {
entitiesToFind.add(TextClassifier.TYPE_ADDRESS);
}
return new Options().setEntityConfig(
TextClassifier.EntityConfig.createWithEntityList(entitiesToFind));
}
private SpannableString linkifyMessageBody(SpannableString messageBody) {
int linkPattern = Linkify.WEB_URLS | Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS;
boolean hasLinks = Linkify.addLinks(messageBody, linkPattern);
if (hasLinks) {
Stream.of(messageBody.getSpans(0, messageBody.length(), URLSpan.class))
.filterNot(url -> LinkPreviewUtil.isLegalUrl(url.getURL()))
.forEach(messageBody::removeSpan);
}
return messageBody;
}
@LinkifyMask
private static int linkMask(@EntityType String entityType) {
switch (entityType) {
case TextClassifier.TYPE_URL:
return Linkify.WEB_URLS;
case TextClassifier.TYPE_PHONE:
return Linkify.PHONE_NUMBERS;
case TextClassifier.TYPE_EMAIL:
return Linkify.EMAIL_ADDRESSES;
default:
// NOTE: Do not support MAP_ADDRESSES. Legacy version does not work well.
return 0;
}
}
public static boolean addLinks(Spannable text, int mask) {
if (text != null && containsUnsupportedCharacters(text.toString()) || mask == 0) {
return false;
}
final 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<>();
if ((mask & Linkify.PHONE_NUMBERS) != 0) {
Linkify.addLinks(text, Linkify.PHONE_NUMBERS);
}
if ((mask & Linkify.WEB_URLS) != 0) {
gatherLinks(links, text, LinkifyPort.WEB_URL, new String[]{"http://", "https://", "ton://", "tg://"}, sUrlMatchFilter);
}
pruneOverlaps(links);
if (links.size() == 0) {
return false;
}
for (int a = 0, N = links.size(); a < N; a++) {
LinkSpec link = links.get(a);
URLSpan[] oldSpans = text.getSpans(link.start, link.end, URLSpan.class);
if (oldSpans != null && oldSpans.length > 0) {
for (int b = 0; b < oldSpans.length; b++) {
text.removeSpan(oldSpans[b]);
}
}
text.setSpan(new URLSpan(link.url), link.start, link.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return true;
}
public static boolean addLinks(Spannable text, int mask) {
if (text != null && containsUnsupportedCharacters(text.toString()) || mask == 0) {
return false;
}
final 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<>();
if ((mask & Linkify.PHONE_NUMBERS) != 0) {
Linkify.addLinks(text, Linkify.PHONE_NUMBERS);
}
if ((mask & Linkify.WEB_URLS) != 0) {
gatherLinks(links, text, LinkifyPort.WEB_URL, new String[]{"http://", "https://", "ton://", "tg://"}, sUrlMatchFilter);
}
pruneOverlaps(links);
if (links.size() == 0) {
return false;
}
for (int a = 0, N = links.size(); a < N; a++) {
LinkSpec link = links.get(a);
URLSpan[] oldSpans = text.getSpans(link.start, link.end, URLSpan.class);
if (oldSpans != null && oldSpans.length > 0) {
for (int b = 0; b < oldSpans.length; b++) {
text.removeSpan(oldSpans[b]);
}
}
text.setSpan(new URLSpan(link.url), link.start, link.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return true;
}
private void parseLinks(String s) {
setText(s);
int mask = Linkify.WEB_URLS;
Linkify.addLinks(this, mask);
//todo this setMovementMethod(new CommentMovementMethod());
}