diff options
author | steckbrief <steckbrief@chefmail.de> | 2018-03-05 19:47:47 +0100 |
---|---|---|
committer | steckbrief <steckbrief@chefmail.de> | 2018-03-05 19:47:47 +0100 |
commit | 144a82ada2391d65063866063950560af29758d0 (patch) | |
tree | 9f71faea128befe9a8c29358a7960db224f4f2aa /src | |
parent | fb9817091a8722782801a34b40e5baaceb777521 (diff) |
moves xmpp url linkify to TextViewUtil
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/de/thedevstack/conversationsplus/ui/adapter/MessageAdapter.java | 51 | ||||
-rw-r--r-- | src/main/java/de/thedevstack/conversationsplus/utils/ui/TextViewUtil.java | 54 |
2 files changed, 56 insertions, 49 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/ui/adapter/MessageAdapter.java b/src/main/java/de/thedevstack/conversationsplus/ui/adapter/MessageAdapter.java index 4991c512..7c7a6b57 100644 --- a/src/main/java/de/thedevstack/conversationsplus/ui/adapter/MessageAdapter.java +++ b/src/main/java/de/thedevstack/conversationsplus/ui/adapter/MessageAdapter.java @@ -8,9 +8,7 @@ import android.text.SpannableString; import android.text.style.ForegroundColorSpan; import android.text.style.StyleSpan; -import android.text.util.Linkify; -import android.util.Patterns; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnLongClickListener; @@ -22,8 +20,6 @@ import android.widget.LinearLayout; import android.widget.TextView; import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import de.thedevstack.conversationsplus.ConversationsPlusColors; import de.thedevstack.conversationsplus.ConversationsPlusPreferences; @@ -58,11 +54,6 @@ public class MessageAdapter extends ArrayAdapter<Message> { private static final int RECEIVED = 1; private static final int STATUS = 2; private static final int ME_COMMAND = 3; - private static final Pattern XMPP_PATTERN = Pattern - .compile("xmpp\\:(?:(?:[" - + Patterns.GOOD_IRI_CHAR - + "\\;\\/\\?\\@\\&\\=\\#\\~\\-\\.\\+\\!\\*\\'\\(\\)\\,\\_])" - + "|(?:\\%[a-fA-F0-9]{2}))+"); private ConversationActivity activity; @@ -304,6 +295,7 @@ public class MessageAdapter extends ArrayAdapter<Message> { viewHolder.messageBody.setVisibility(View.VISIBLE); viewHolder.messageBody.setIncludeFontPadding(true); + if (message.getBody() != null) { final String nick = UIHelper.getMessageDisplayName(message); String body; @@ -350,30 +342,7 @@ public class MessageAdapter extends ArrayAdapter<Message> { } viewHolder.messageBody.setText(span); } - int patternMatchCount = 0; - int oldAutoLinkMask = viewHolder.messageBody.getAutoLinkMask(); - - // 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); - } - - viewHolder.messageBody.setTextIsSelectable(patternMatchCount <= 1); - viewHolder.messageBody.setAutoLinkMask(0); - Linkify.addLinks(viewHolder.messageBody, XMPP_PATTERN, "xmpp"); - viewHolder.messageBody.setAutoLinkMask(oldAutoLinkMask); + TextViewUtil.linkifyXmpp(viewHolder.messageBody); } else { viewHolder.messageBody.setText(""); viewHolder.messageBody.setTextIsSelectable(false); @@ -383,22 +352,6 @@ public class MessageAdapter extends ArrayAdapter<Message> { viewHolder.messageBody.setOnLongClickListener(openContextMenu); } - /** - * 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 int countMatches(Pattern pattern, String body) { - Matcher matcher = pattern.matcher(body); - int count = 0; - while (matcher.find()) { - count++; - } - return count; - } - private void displayDownloadButton(ViewHolder viewHolder, String btnText, OnClickListener onClickListener) { viewHolder.download_button.setVisibility(View.VISIBLE); viewHolder.download_button.setText(btnText); 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 } |