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