From 144a82ada2391d65063866063950560af29758d0 Mon Sep 17 00:00:00 2001 From: steckbrief Date: Mon, 5 Mar 2018 19:47:47 +0100 Subject: moves xmpp url linkify to TextViewUtil --- .../conversationsplus/utils/ui/TextViewUtil.java | 54 ++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'src/main/java/de/thedevstack/conversationsplus/utils/ui/TextViewUtil.java') diff --git a/src/main/java/de/thedevstack/conversationsplus/utils/ui/TextViewUtil.java b/src/main/java/de/thedevstack/conversationsplus/utils/ui/TextViewUtil.java index 0d4b065a..9840ce41 100644 --- a/src/main/java/de/thedevstack/conversationsplus/utils/ui/TextViewUtil.java +++ b/src/main/java/de/thedevstack/conversationsplus/utils/ui/TextViewUtil.java @@ -2,15 +2,47 @@ package de.thedevstack.conversationsplus.utils.ui; import android.support.annotation.ColorRes; import android.support.annotation.StringRes; +import android.text.util.Linkify; +import android.util.Patterns; import android.view.View; import android.widget.TextView; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + import de.thedevstack.conversationsplus.ConversationsPlusColors; /** * */ public final class TextViewUtil extends ViewUtil { + public static void linkifyXmpp(TextView tv) { + int patternMatchCount = 0; + int oldAutoLinkMask = tv.getAutoLinkMask(); + CharSequence body = tv.getText(); + + // first check if we have a match on XMPP_PATTERN so we do not have to check for EMAIL_ADDRESSES + patternMatchCount += countMatches(XMPP_PATTERN, body); + if ((Linkify.EMAIL_ADDRESSES & oldAutoLinkMask) != 0 && patternMatchCount > 0) { + oldAutoLinkMask -= Linkify.EMAIL_ADDRESSES; + } + + // count matches for all patterns + if ((Linkify.WEB_URLS & oldAutoLinkMask) != 0) { + patternMatchCount += countMatches(Patterns.WEB_URL, body); + } + if ((Linkify.EMAIL_ADDRESSES & oldAutoLinkMask) != 0) { + patternMatchCount += countMatches(Patterns.EMAIL_ADDRESS, body); + } + if ((Linkify.PHONE_NUMBERS & oldAutoLinkMask) != 0) { + patternMatchCount += countMatches(Patterns.PHONE, body); + } + + tv.setTextIsSelectable(patternMatchCount <= 1); + tv.setAutoLinkMask(0); + Linkify.addLinks(tv, XMPP_PATTERN, "xmpp"); + tv.setAutoLinkMask(oldAutoLinkMask); + } public static void setTextWithoutAutoLink(TextView tv, CharSequence text) { int oldAutoLinkMask = tv.getAutoLinkMask(); @@ -103,6 +135,28 @@ public final class TextViewUtil extends ViewUtil { } } + /** + * Counts the number of occurrences of the pattern in body. + * + * @param pattern the pattern to match + * @param body the body to find the pattern + * @return the number of occurrences + */ + private static int countMatches(Pattern pattern, CharSequence body) { + Matcher matcher = pattern.matcher(body); + int count = 0; + while (matcher.find()) { + count++; + } + return count; + } + + private static final Pattern XMPP_PATTERN = Pattern + .compile("xmpp\\:(?:(?:[" + + Patterns.GOOD_IRI_CHAR + + "\\;\\/\\?\\@\\&\\=\\#\\~\\-\\.\\+\\!\\*\\'\\(\\)\\,\\_])" + + "|(?:\\%[a-fA-F0-9]{2}))+"); + private TextViewUtil() { // avoid instantiation - helper class } -- cgit v1.2.3