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.style.URLSpan;
 36import android.text.util.Linkify;
 37
 38import com.google.common.collect.Collections2;
 39import com.google.common.collect.ImmutableList;
 40import com.google.common.collect.Lists;
 41
 42import java.lang.IndexOutOfBoundsException;
 43import java.util.Arrays;
 44import java.util.Collection;
 45import java.util.List;
 46import java.util.Locale;
 47import java.util.Objects;
 48
 49import eu.siacs.conversations.entities.Account;
 50import eu.siacs.conversations.entities.ListItem;
 51import eu.siacs.conversations.entities.Roster;
 52import eu.siacs.conversations.ui.text.FixedURLSpan;
 53import eu.siacs.conversations.utils.GeoHelper;
 54import eu.siacs.conversations.utils.Patterns;
 55import eu.siacs.conversations.utils.XmppUri;
 56import eu.siacs.conversations.xmpp.Jid;
 57
 58public class MyLinkify {
 59
 60    private static final Linkify.TransformFilter WEBURL_TRANSFORM_FILTER = (matcher, url) -> {
 61        if (url == null) {
 62            return null;
 63        }
 64        final String lcUrl = url.toLowerCase(Locale.US);
 65        if (lcUrl.startsWith("http://") || lcUrl.startsWith("https://")) {
 66            return removeTrailingBracket(url);
 67        } else {
 68            return "http://" + removeTrailingBracket(url);
 69        }
 70    };
 71
 72    private static String removeTrailingBracket(final String url) {
 73        int numOpenBrackets = 0;
 74        for (char c : url.toCharArray()) {
 75            if (c == '(') {
 76                ++numOpenBrackets;
 77            } else if (c == ')') {
 78                --numOpenBrackets;
 79            }
 80        }
 81        if (numOpenBrackets != 0 && url.charAt(url.length() - 1) == ')') {
 82            return url.substring(0, url.length() - 1);
 83        } else {
 84            return url;
 85        }
 86    }
 87
 88    private static final Linkify.MatchFilter WEBURL_MATCH_FILTER = (cs, start, end) -> {
 89        if (start > 0) {
 90            if (cs.charAt(start - 1) == '@' || cs.charAt(start - 1) == '.'
 91                    || cs.subSequence(Math.max(0, start - 3), start).equals("://")) {
 92                return false;
 93            }
 94        }
 95
 96        if (end < cs.length()) {
 97            // Reject strings that were probably matched only because they contain a dot followed by
 98            // by some known TLD (see also comment for WORD_BOUNDARY in Patterns.java)
 99            return !isAlphabetic(cs.charAt(end - 1)) || !isAlphabetic(cs.charAt(end));
100        }
101
102        return true;
103    };
104
105    private static final Linkify.MatchFilter XMPPURI_MATCH_FILTER = (s, start, end) -> {
106        XmppUri uri = new XmppUri(s.subSequence(start, end).toString());
107        return uri.isValidJid();
108    };
109
110    private static boolean isAlphabetic(final int code) {
111        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
112            return Character.isAlphabetic(code);
113        }
114
115        switch (Character.getType(code)) {
116            case Character.UPPERCASE_LETTER:
117            case Character.LOWERCASE_LETTER:
118            case Character.TITLECASE_LETTER:
119            case Character.MODIFIER_LETTER:
120            case Character.OTHER_LETTER:
121            case Character.LETTER_NUMBER:
122                return true;
123            default:
124                return false;
125        }
126    }
127
128    public static void addLinks(Editable body, boolean includeGeo) {
129        Linkify.addLinks(body, Patterns.XMPP_PATTERN, "xmpp", XMPPURI_MATCH_FILTER, null);
130        Linkify.addLinks(body, Patterns.TEL_URI, "tel");
131        Linkify.addLinks(body, Patterns.SMS_URI, "sms");
132        Linkify.addLinks(body, Patterns.AUTOLINK_WEB_URL, "http", WEBURL_MATCH_FILTER, WEBURL_TRANSFORM_FILTER);
133        if (includeGeo) {
134            Linkify.addLinks(body, GeoHelper.GEO_URI, "geo");
135        }
136        FixedURLSpan.fix(body);
137    }
138
139    public static void addLinks(Editable body, Account account, Jid context) {
140        addLinks(body, true);
141        Roster roster = account.getRoster();
142        for (final URLSpan urlspan : body.getSpans(0, body.length() - 1, URLSpan.class)) {
143            Uri uri = Uri.parse(urlspan.getURL());
144            if ("xmpp".equals(uri.getScheme())) {
145                try {
146                    if (!body.subSequence(body.getSpanStart(urlspan), body.getSpanEnd(urlspan)).toString().startsWith("xmpp:")) {
147                        // Already customized
148                        continue;
149                    }
150
151                    XmppUri xmppUri = new XmppUri(uri);
152                    Jid jid = xmppUri.getJid();
153                    String display = xmppUri.toString();
154                    if (jid.asBareJid().equals(context) && xmppUri.isAction("message") && xmppUri.getBody() != null) {
155                        display = xmppUri.getBody();
156                    } else if (jid.asBareJid().equals(context)) {
157                        display = xmppUri.parameterString();
158                    } else {
159                        ListItem item = account.getBookmark(jid);
160                        if (item == null) item = roster.getContact(jid);
161                        display = item.getDisplayName() + xmppUri.parameterString();
162                    }
163                    body.replace(
164                        body.getSpanStart(urlspan),
165                        body.getSpanEnd(urlspan),
166                        display
167                    );
168                } catch (final IllegalArgumentException | IndexOutOfBoundsException e) { /* bad JID or span gone */ }
169            }
170        }
171    }
172
173    public static List<String> extractLinks(final Editable body) {
174        MyLinkify.addLinks(body, false);
175        final Collection<URLSpan> spans =
176                Arrays.asList(body.getSpans(0, body.length() - 1, URLSpan.class));
177        final Collection<UrlWrapper> urlWrappers =
178                Collections2.filter(
179                        Collections2.transform(
180                                spans,
181                                s ->
182                                        s == null
183                                                ? null
184                                                : new UrlWrapper(body.getSpanStart(s), s.getURL())),
185                        uw -> uw != null);
186        List<UrlWrapper> sorted = ImmutableList.sortedCopyOf(
187                (a, b) -> Integer.compare(a.position, b.position), urlWrappers);
188        return Lists.transform(sorted, uw -> uw.url);
189
190    }
191
192    private static class UrlWrapper {
193        private final int position;
194        private final String url;
195
196        private UrlWrapper(int position, String url) {
197            this.position = position;
198            this.url = url;
199        }
200    }
201}