MyLinkify.java

  1/*
  2 * Copyright (c) 2018, Daniel Gultsch All rights reserved.
  3 *
  4 * Redistribution and use in source and binary forms, with or without modification,
  5 * are permitted provided that the following conditions are met:
  6 *
  7 * 1. Redistributions of source code must retain the above copyright notice, this
  8 * list of conditions and the following disclaimer.
  9 *
 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
 11 * this list of conditions and the following disclaimer in the documentation and/or
 12 * other materials provided with the distribution.
 13 *
 14 * 3. Neither the name of the copyright holder nor the names of its contributors
 15 * may be used to endorse or promote products derived from this software without
 16 * specific prior written permission.
 17 *
 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28 */
 29
 30package eu.siacs.conversations.ui.util;
 31
 32import android.net.Uri;
 33import android.os.Build;
 34import android.text.Editable;
 35import android.text.Spanned;
 36import android.text.style.TypefaceSpan;
 37import android.text.style.URLSpan;
 38import android.text.util.Linkify;
 39
 40import com.google.common.collect.Collections2;
 41import com.google.common.collect.ImmutableList;
 42import com.google.common.collect.Lists;
 43
 44import java.lang.IndexOutOfBoundsException;
 45import java.util.Arrays;
 46import java.util.Collection;
 47import java.util.List;
 48import java.util.Locale;
 49import java.util.Objects;
 50
 51import eu.siacs.conversations.entities.Account;
 52import eu.siacs.conversations.entities.ListItem;
 53import eu.siacs.conversations.entities.Roster;
 54import eu.siacs.conversations.ui.text.FixedURLSpan;
 55import eu.siacs.conversations.utils.GeoHelper;
 56import eu.siacs.conversations.utils.Patterns;
 57import eu.siacs.conversations.utils.StylingHelper;
 58import eu.siacs.conversations.utils.XmppUri;
 59import eu.siacs.conversations.xmpp.Jid;
 60
 61public class MyLinkify {
 62
 63    private static final Linkify.TransformFilter WEBURL_TRANSFORM_FILTER = (matcher, url) -> {
 64        if (url == null) {
 65            return null;
 66        }
 67        final String lcUrl = url.toLowerCase(Locale.US);
 68        if (lcUrl.startsWith("http://") || lcUrl.startsWith("https://")) {
 69            return removeTrailingBracket(url);
 70        } else {
 71            return "http://" + removeTrailingBracket(url);
 72        }
 73    };
 74
 75    private static String removeTrailingBracket(final String url) {
 76        int numOpenBrackets = 0;
 77        for (char c : url.toCharArray()) {
 78            if (c == '(') {
 79                ++numOpenBrackets;
 80            } else if (c == ')') {
 81                --numOpenBrackets;
 82            }
 83        }
 84        if (numOpenBrackets != 0 && url.charAt(url.length() - 1) == ')') {
 85            return url.substring(0, url.length() - 1);
 86        } else {
 87            return url;
 88        }
 89    }
 90
 91    private static final Linkify.MatchFilter WEBURL_MATCH_FILTER = (cs, start, end) -> {
 92        if (start > 0) {
 93            if (cs.charAt(start - 1) == '@' || cs.charAt(start - 1) == '.'
 94                    || cs.subSequence(Math.max(0, start - 3), start).equals("://")) {
 95                return false;
 96            }
 97        }
 98
 99        if (end < cs.length()) {
100            // Reject strings that were probably matched only because they contain a dot followed by
101            // by some known TLD (see also comment for WORD_BOUNDARY in Patterns.java)
102            return !isAlphabetic(cs.charAt(end - 1)) || !isAlphabetic(cs.charAt(end));
103        }
104
105        return true;
106    };
107
108    private static final Linkify.MatchFilter XMPPURI_MATCH_FILTER = (s, start, end) -> {
109        XmppUri uri = new XmppUri(s.subSequence(start, end).toString());
110        return uri.isValidJid();
111    };
112
113    private static boolean isAlphabetic(final int code) {
114        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
115            return Character.isAlphabetic(code);
116        }
117
118        switch (Character.getType(code)) {
119            case Character.UPPERCASE_LETTER:
120            case Character.LOWERCASE_LETTER:
121            case Character.TITLECASE_LETTER:
122            case Character.MODIFIER_LETTER:
123            case Character.OTHER_LETTER:
124            case Character.LETTER_NUMBER:
125                return true;
126            default:
127                return false;
128        }
129    }
130
131    public static void addLinks(Editable body, boolean includeGeo) {
132        Linkify.addLinks(body, Patterns.XMPP_PATTERN, "xmpp", XMPPURI_MATCH_FILTER, null);
133        Linkify.addLinks(body, Patterns.TEL_URI, "tel");
134        Linkify.addLinks(body, Patterns.SMS_URI, "sms");
135        Linkify.addLinks(body, Patterns.BITCOIN_URI, "bitcoin");
136        Linkify.addLinks(body, Patterns.BITCOINCASH_URI, "bitcoincash");
137        Linkify.addLinks(body, Patterns.MONERO_URI, "monero");
138        Linkify.addLinks(body, Patterns.AUTOLINK_WEB_URL, "http", WEBURL_MATCH_FILTER, WEBURL_TRANSFORM_FILTER);
139        if (includeGeo) {
140            Linkify.addLinks(body, GeoHelper.GEO_URI, "geo");
141        }
142        FixedURLSpan.fix(body);
143    }
144
145    public static void addLinks(Editable body, Account account, Jid context) {
146        addLinks(body, true);
147        Roster roster = account.getRoster();
148        urlspan:
149        for (final URLSpan urlspan : body.getSpans(0, body.length() - 1, URLSpan.class)) {
150            final var start = body.getSpanStart(urlspan);
151            for (final var span : body.getSpans(start, start, Object.class))  {
152                // instanceof TypefaceSpan is to block in XHTML code blocks. Probably a bit heavy-handed but works for now
153                if ((body.getSpanFlags(span) & Spanned.SPAN_USER) >> Spanned.SPAN_USER_SHIFT == StylingHelper.NOLINKIFY || span instanceof TypefaceSpan) {
154                    body.removeSpan(urlspan);
155                    continue urlspan;
156                }
157            }
158            Uri uri = Uri.parse(urlspan.getURL());
159            if ("xmpp".equals(uri.getScheme())) {
160                try {
161                    if (!body.subSequence(body.getSpanStart(urlspan), body.getSpanEnd(urlspan)).toString().startsWith("xmpp:")) {
162                        // Already customized
163                        continue;
164                    }
165
166                    XmppUri xmppUri = new XmppUri(uri);
167                    Jid jid = xmppUri.getJid();
168                    String display = xmppUri.toString();
169                    if (jid.asBareJid().equals(context) && xmppUri.isAction("message") && xmppUri.getBody() != null) {
170                        display = xmppUri.getBody();
171                    } else if (jid.asBareJid().equals(context) && xmppUri.parameterString().length() > 0) {
172                        display = xmppUri.parameterString();
173                    } else {
174                        ListItem item = account.getBookmark(jid);
175                        if (item == null) item = roster.getContact(jid);
176                        display = item.getDisplayName() + xmppUri.displayParameterString();
177                    }
178                    body.replace(
179                        body.getSpanStart(urlspan),
180                        body.getSpanEnd(urlspan),
181                        display
182                    );
183                } catch (final IllegalArgumentException | IndexOutOfBoundsException e) { /* bad JID or span gone */ }
184            }
185        }
186    }
187
188    public static List<String> extractLinks(final Editable body) {
189        MyLinkify.addLinks(body, false);
190        final Collection<URLSpan> spans =
191                Arrays.asList(body.getSpans(0, body.length() - 1, URLSpan.class));
192        final Collection<UrlWrapper> urlWrappers =
193                Collections2.filter(
194                        Collections2.transform(
195                                spans,
196                                s ->
197                                        s == null
198                                                ? null
199                                                : new UrlWrapper(body.getSpanStart(s), s.getURL())),
200                        uw -> uw != null);
201        List<UrlWrapper> sorted = ImmutableList.sortedCopyOf(
202                (a, b) -> Integer.compare(a.position, b.position), urlWrappers);
203        return Lists.transform(sorted, uw -> uw.url);
204
205    }
206
207    private static class UrlWrapper {
208        private final int position;
209        private final String url;
210
211        private UrlWrapper(int position, String url) {
212            this.position = position;
213            this.url = url;
214        }
215    }
216}