aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/ui/util
diff options
context:
space:
mode:
authorChristian Schneppe <christian@pix-art.de>2018-06-29 22:34:45 +0200
committerChristian Schneppe <christian@pix-art.de>2018-06-29 22:34:45 +0200
commit70477c9fc9c6c6df45d2b921d920ec4070535bb4 (patch)
tree9e53c29f86894cb46a7e79c523b26f839192153b /src/main/java/de/pixart/messenger/ui/util
parentdda1f862f0d96571e1353e4deefe10bb57a7d269 (diff)
linkify subject + open xmpp directly w/o going through start conv activity
Diffstat (limited to 'src/main/java/de/pixart/messenger/ui/util')
-rw-r--r--src/main/java/de/pixart/messenger/ui/util/MyLinkify.java106
1 files changed, 106 insertions, 0 deletions
diff --git a/src/main/java/de/pixart/messenger/ui/util/MyLinkify.java b/src/main/java/de/pixart/messenger/ui/util/MyLinkify.java
new file mode 100644
index 000000000..2817c55c7
--- /dev/null
+++ b/src/main/java/de/pixart/messenger/ui/util/MyLinkify.java
@@ -0,0 +1,106 @@
+/*
+ * Copyright (c) 2018, Daniel Gultsch All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation and/or
+ * other materials provided with the distribution.
+ *
+ * 3. Neither the name of the copyright holder nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package de.pixart.messenger.ui.util;
+
+import android.os.Build;
+import android.text.Editable;
+import android.text.util.Linkify;
+
+import java.util.Locale;
+
+import de.pixart.messenger.ui.text.FixedURLSpan;
+import de.pixart.messenger.utils.GeoHelper;
+import de.pixart.messenger.utils.Patterns;
+import de.pixart.messenger.utils.XmppUri;
+
+public class MyLinkify {
+
+ private static final Linkify.TransformFilter WEBURL_TRANSFORM_FILTER = (matcher, url) -> {
+ if (url == null) {
+ return null;
+ }
+ final String lcUrl = url.toLowerCase(Locale.US);
+ if (lcUrl.startsWith("http://") || lcUrl.startsWith("https://")) {
+ return removeTrailingBracket(url);
+ } else {
+ return "http://" + removeTrailingBracket(url);
+ }
+ };
+
+ private static String removeTrailingBracket(final String url) {
+ int numOpenBrackets = 0;
+ for (char c : url.toCharArray()) {
+ if (c == '(') {
+ ++numOpenBrackets;
+ } else if (c == ')') {
+ --numOpenBrackets;
+ }
+ }
+ if (numOpenBrackets != 0 && url.charAt(url.length() - 1) == ')') {
+ return url.substring(0, url.length() - 1);
+ } else {
+ return url;
+ }
+ }
+
+ private static final Linkify.MatchFilter WEBURL_MATCH_FILTER = (cs, start, end) -> {
+ if (start > 0) {
+ if (cs.charAt(start - 1) == '@' || cs.charAt(start - 1) == '.'
+ || cs.subSequence(Math.max(0, start - 3), start).equals("://")) {
+ return false;
+ }
+ }
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
+ if (end < cs.length()) {
+ // Reject strings that were probably matched only because they contain a dot followed by
+ // by some known TLD (see also comment for WORD_BOUNDARY in Patterns.java)
+ if (Character.isAlphabetic(cs.charAt(end - 1)) && Character.isAlphabetic(cs.charAt(end))) {
+ return false;
+ }
+ }
+ }
+
+ return true;
+ };
+
+ private static final Linkify.MatchFilter XMPPURI_MATCH_FILTER = (s, start, end) -> {
+ XmppUri uri = new XmppUri(s.subSequence(start, end).toString());
+ return uri.isJidValid();
+ };
+
+ public static void addLinks(Editable body, boolean includeGeo) {
+ Linkify.addLinks(body, Patterns.XMPP_PATTERN, "xmpp", XMPPURI_MATCH_FILTER, null);
+ Linkify.addLinks(body, Patterns.AUTOLINK_WEB_URL, "http", WEBURL_MATCH_FILTER, WEBURL_TRANSFORM_FILTER);
+ if (includeGeo) {
+ Linkify.addLinks(body, GeoHelper.GEO_URI, "geo");
+ }
+ FixedURLSpan.fix(body);
+ }
+} \ No newline at end of file