Java Code Examples for android.text.util.Linkify#PHONE_NUMBERS

The following examples show how to use android.text.util.Linkify#PHONE_NUMBERS . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConversationItem.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: TextLinksParams.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example 3
Source File: TextLinks.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/** 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));
}
 
Example 4
Source File: LongMessageActivity.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 5
Source File: TextClassifier.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@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;
    }
}
 
Example 6
Source File: AndroidUtilities.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 7
Source File: AndroidUtilities.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
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;
}