use 7.1 web url pattern matching on old platforms as well. fixes #2228

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java | 21 
src/main/java/eu/siacs/conversations/utils/Patterns.java            | 62 
2 files changed, 81 insertions(+), 2 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java 🔗

@@ -23,7 +23,6 @@ import android.text.style.RelativeSizeSpan;
 import android.text.style.StyleSpan;
 import android.text.util.Linkify;
 import android.util.DisplayMetrics;
-import android.util.Patterns;
 import android.view.ActionMode;
 import android.view.Menu;
 import android.view.MenuItem;
@@ -41,7 +40,9 @@ import android.widget.Toast;
 import java.lang.ref.WeakReference;
 import java.net.URL;
 import java.util.List;
+import java.util.Locale;
 import java.util.concurrent.RejectedExecutionException;
+import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 import eu.siacs.conversations.Config;
@@ -63,6 +64,7 @@ import eu.siacs.conversations.ui.widget.CopyTextView;
 import eu.siacs.conversations.ui.widget.ListSelectionManager;
 import eu.siacs.conversations.utils.CryptoHelper;
 import eu.siacs.conversations.utils.GeoHelper;
+import eu.siacs.conversations.utils.Patterns;
 import eu.siacs.conversations.utils.UIHelper;
 
 public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextView.CopyHandler {
@@ -76,6 +78,21 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
 					+ "\\;\\/\\?\\@\\&\\=\\#\\~\\-\\.\\+\\!\\*\\'\\(\\)\\,\\_])"
 					+ "|(?:\\%[a-fA-F0-9]{2}))+");
 
+	private static final Linkify.TransformFilter WEBURL_TRANSOFRM_FILTER = new Linkify.TransformFilter() {
+		@Override
+		public String transformUrl(Matcher matcher, String url) {
+			if (url == null) {
+				return null;
+			}
+			final String lcUrl = url.toLowerCase(Locale.US);
+			if (lcUrl.startsWith("http://") || lcUrl.startsWith("https://")) {
+				return url;
+			} else {
+				return "http://"+url;
+			}
+		}
+	};
+
 	private ConversationActivity activity;
 
 	private DisplayMetrics metrics;
@@ -429,7 +446,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
 							privateMarkerIndex + 1 + nick.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 				}
 			}
-			Linkify.addLinks(body, Linkify.WEB_URLS);
+			Linkify.addLinks(body, Patterns.AUTOLINK_WEB_URL, "http", null, WEBURL_TRANSOFRM_FILTER);
 			Linkify.addLinks(body, XMPP_PATTERN, "xmpp");
 			Linkify.addLinks(body, GeoHelper.GEO_URI, "geo");
 			viewHolder.messageBody.setAutoLinkMask(0);

src/main/java/eu/siacs/conversations/utils/Patterns.java 🔗

@@ -0,0 +1,474 @@
+/*
+ * Copyright (C) 2007 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ *
+ * Download latest version here: https://android.googlesource.com/platform/frameworks/base.git/+/master
+ *
+ *
+ */
+package eu.siacs.conversations.utils;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+/**
+ * Commonly used regular expression patterns.
+ */
+public class Patterns {
+    /**
+     *  Regular expression to match all IANA top-level domains.
+     *  List accurate as of 2011/07/18.  List taken from:
+     *  http://data.iana.org/TLD/tlds-alpha-by-domain.txt
+     *  This pattern is auto-generated by frameworks/ex/common/tools/make-iana-tld-pattern.py
+     *
+     *  @deprecated Due to the recent profileration of gTLDs, this API is
+     *  expected to become out-of-date very quickly. Therefore it is now
+     *  deprecated.
+     */
+    @Deprecated
+    public static final String TOP_LEVEL_DOMAIN_STR =
+            "((aero|arpa|asia|a[cdefgilmnoqrstuwxz])"
+                    + "|(biz|b[abdefghijmnorstvwyz])"
+                    + "|(cat|com|coop|c[acdfghiklmnoruvxyz])"
+                    + "|d[ejkmoz]"
+                    + "|(edu|e[cegrstu])"
+                    + "|f[ijkmor]"
+                    + "|(gov|g[abdefghilmnpqrstuwy])"
+                    + "|h[kmnrtu]"
+                    + "|(info|int|i[delmnoqrst])"
+                    + "|(jobs|j[emop])"
+                    + "|k[eghimnprwyz]"
+                    + "|l[abcikrstuvy]"
+                    + "|(mil|mobi|museum|m[acdeghklmnopqrstuvwxyz])"
+                    + "|(name|net|n[acefgilopruz])"
+                    + "|(org|om)"
+                    + "|(pro|p[aefghklmnrstwy])"
+                    + "|qa"
+                    + "|r[eosuw]"
+                    + "|s[abcdeghijklmnortuvyz]"
+                    + "|(tel|travel|t[cdfghjklmnoprtvwz])"
+                    + "|u[agksyz]"
+                    + "|v[aceginu]"
+                    + "|w[fs]"