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.os.Build;
 33import android.text.Editable;
 34import android.text.util.Linkify;
 35
 36import java.util.Locale;
 37
 38import eu.siacs.conversations.ui.text.FixedURLSpan;
 39import eu.siacs.conversations.utils.GeoHelper;
 40import eu.siacs.conversations.utils.Patterns;
 41import eu.siacs.conversations.utils.XmppUri;
 42
 43public class MyLinkify {
 44
 45    private static final Linkify.TransformFilter WEBURL_TRANSFORM_FILTER = (matcher, url) -> {
 46        if (url == null) {
 47            return null;
 48        }
 49        final String lcUrl = url.toLowerCase(Locale.US);
 50        if (lcUrl.startsWith("http://") || lcUrl.startsWith("https://")) {
 51            return removeTrailingBracket(url);
 52        } else {
 53            return "http://" + removeTrailingBracket(url);
 54        }
 55    };
 56
 57    private static String removeTrailingBracket(final String url) {
 58        int numOpenBrackets = 0;
 59        for (char c : url.toCharArray()) {
 60            if (c == '(') {
 61                ++numOpenBrackets;
 62            } else if (c == ')') {
 63                --numOpenBrackets;
 64            }
 65        }
 66        if (numOpenBrackets != 0 && url.charAt(url.length() - 1) == ')') {
 67            return url.substring(0, url.length() - 1);
 68        } else {
 69            return url;
 70        }
 71    }
 72
 73    private static final Linkify.MatchFilter WEBURL_MATCH_FILTER = (cs, start, end) -> {
 74        if (start > 0) {
 75            if (cs.charAt(start - 1) == '@' || cs.charAt(start - 1) == '.'
 76                    || cs.subSequence(Math.max(0, start - 3), start).equals("://")) {
 77                return false;
 78            }
 79        }
 80
 81        if (end < cs.length()) {
 82            // Reject strings that were probably matched only because they contain a dot followed by
 83            // by some known TLD (see also comment for WORD_BOUNDARY in Patterns.java)
 84            return !isAlphabetic(cs.charAt(end - 1)) || !isAlphabetic(cs.charAt(end));
 85        }
 86
 87        return true;
 88    };
 89
 90    private static final Linkify.MatchFilter XMPPURI_MATCH_FILTER = (s, start, end) -> {
 91        XmppUri uri = new XmppUri(s.subSequence(start, end).toString());
 92        return uri.isValidJid();
 93    };
 94
 95    private static boolean isAlphabetic(final int code) {
 96        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
 97            return Character.isAlphabetic(code);
 98        }
 99
100        switch (Character.getType(code)) {
101            case Character.UPPERCASE_LETTER:
102            case Character.LOWERCASE_LETTER:
103            case Character.TITLECASE_LETTER:
104            case Character.MODIFIER_LETTER:
105            case Character.OTHER_LETTER:
106            case Character.LETTER_NUMBER:
107                return true;
108            default:
109                return false;
110        }
111    }
112
113    public static void addLinks(Editable body, boolean includeGeo) {
114        Linkify.addLinks(body, Patterns.XMPP_PATTERN, "xmpp", XMPPURI_MATCH_FILTER, null);
115        Linkify.addLinks(body, Patterns.AUTOLINK_WEB_URL, "http", WEBURL_MATCH_FILTER, WEBURL_TRANSFORM_FILTER);
116        if (includeGeo) {
117            Linkify.addLinks(body, GeoHelper.GEO_URI, "geo");
118        }
119        FixedURLSpan.fix(body);
120    }
121}