diff options
author | lookshe <github@lookshe.org> | 2016-03-08 20:34:18 +0100 |
---|---|---|
committer | lookshe <github@lookshe.org> | 2016-03-08 20:36:41 +0100 |
commit | f051dd0bcdbd35d101420fb1f75f33a565a1e0f8 (patch) | |
tree | 13a51964c2c5cc73e7140fec685b664361596759 | |
parent | d80e433e4286f4eef3bf416b83b40b31e5cdf0c9 (diff) |
Fixes FS#153 - Check if url, phone and email are linkified correctly
-rw-r--r-- | libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconHandler.java | 10 | ||||
-rw-r--r-- | src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java | 37 |
2 files changed, 41 insertions, 6 deletions
diff --git a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconHandler.java b/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconHandler.java index 5523314b..f611a263 100644 --- a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconHandler.java +++ b/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconHandler.java @@ -1407,6 +1407,11 @@ public final class EmojiconHandler { public static final Map<Pattern, Integer> ANDROID_EMOTICONS = new HashMap<Pattern, Integer>(); private static final Pattern TIME_AND_SCORING_PATTERN = Pattern.compile("[0-9]{1,3}:[0-9]{1,3}"); + private static final Pattern XMPP_PATTERN = Pattern + .compile("xmpp\\:(?:(?:[" + + Patterns.GOOD_IRI_CHAR + + "\\;\\/\\?\\@\\&\\=\\#\\~\\-\\.\\+\\!\\*\\'\\(\\)\\,\\_])" + + "|(?:\\%[a-fA-F0-9]{2}))+"); private static final Spannable.Factory spannableFactory = Spannable.Factory .getInstance(); @@ -1459,6 +1464,11 @@ public final class EmojiconHandler { falsePositives.add(Pair.create(timeAndScoringMatcher.start(), timeAndScoringMatcher.end())); } + Matcher xmppMatcher = XMPP_PATTERN.matcher(spannable); + while (xmppMatcher.find()) { + falsePositives.add(Pair.create(xmppMatcher.start(), xmppMatcher.end())); + } + Map<Pattern, Integer> emoticons = ANDROID_EMOTICONS; for (Map.Entry<Pattern, Integer> entry : emoticons.entrySet()) { Matcher matcher = entry.getKey().matcher(spannable); diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java index 53611d37..36790041 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java @@ -330,15 +330,40 @@ public class MessageAdapter extends ArrayAdapter<Message> { } viewHolder.messageBody.setText(span); } - int urlCount = 0; - Matcher matcher = Patterns.WEB_URL.matcher(body); - while (matcher.find()) { - urlCount++; + int patternMatchCount = 0; + int oldAutoLinkMask = viewHolder.messageBody.getAutoLinkMask(); + Matcher matcher = null; + + // first check if we have a match on XMPP_PATTERN so we do not have to check for EMAIL_ADDRESSES + matcher = XMPP_PATTERN.matcher(body); + if ((Linkify.EMAIL_ADDRESSES & oldAutoLinkMask) != 0 && matcher.find()) { + oldAutoLinkMask -= Linkify.EMAIL_ADDRESSES; } - viewHolder.messageBody.setTextIsSelectable(urlCount <= 1); + + // count matches for all patterns + if ((Linkify.WEB_URLS & oldAutoLinkMask) != 0) { + matcher = Patterns.WEB_URL.matcher(body); + while (matcher.find()) { + patternMatchCount++; + } + } + if ((Linkify.EMAIL_ADDRESSES & oldAutoLinkMask) != 0) { + matcher = Patterns.EMAIL_ADDRESS.matcher(body); + while (matcher.find()) { + patternMatchCount++; + } + } + if ((Linkify.PHONE_NUMBERS & oldAutoLinkMask) != 0) { + matcher = Patterns.PHONE.matcher(body); + while (matcher.find()) { + patternMatchCount++; + } + } + + viewHolder.messageBody.setTextIsSelectable(patternMatchCount <= 1); viewHolder.messageBody.setAutoLinkMask(0); - Linkify.addLinks(viewHolder.messageBody, Linkify.WEB_URLS); Linkify.addLinks(viewHolder.messageBody, XMPP_PATTERN, "xmpp"); + viewHolder.messageBody.setAutoLinkMask(oldAutoLinkMask); } else { viewHolder.messageBody.setText(""); viewHolder.messageBody.setTextIsSelectable(false); |