Search in sources :

Example 6 with URLSpanBotCommand

use of org.telegram.ui.Components.URLSpanBotCommand in project Telegram-FOSS by Telegram-FOSS-Team.

the class MessageObject method addUrlsByPattern.

public static void addUrlsByPattern(boolean isOut, CharSequence charSequence, boolean botCommands, int patternType, int duration, boolean check) {
    try {
        Matcher matcher;
        if (patternType == 3 || patternType == 4) {
            if (videoTimeUrlPattern == null) {
                videoTimeUrlPattern = Pattern.compile("\\b(?:(\\d{1,2}):)?(\\d{1,3}):([0-5][0-9])\\b");
            }
            matcher = videoTimeUrlPattern.matcher(charSequence);
        } else if (patternType == 1) {
            if (instagramUrlPattern == null) {
                instagramUrlPattern = Pattern.compile("(^|\\s|\\()@[a-zA-Z\\d_.]{1,32}|(^|\\s|\\()#[\\w.]+");
            }
            matcher = instagramUrlPattern.matcher(charSequence);
        } else {
            if (urlPattern == null) {
                urlPattern = Pattern.compile("(^|\\s)/[a-zA-Z@\\d_]{1,255}|(^|\\s|\\()@[a-zA-Z\\d_]{1,32}|(^|\\s|\\()#[^0-9][\\w.]+|(^|\\s)\\$[A-Z]{3,8}([ ,.]|$)");
            }
            matcher = urlPattern.matcher(charSequence);
        }
        Spannable spannable = (Spannable) charSequence;
        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            URLSpanNoUnderline url = null;
            if (patternType == 3 || patternType == 4) {
                URLSpan[] spans = spannable.getSpans(start, end, URLSpan.class);
                if (spans != null && spans.length > 0) {
                    continue;
                }
                int count = matcher.groupCount();
                int s1 = matcher.start(1);
                int e1 = matcher.end(1);
                int s2 = matcher.start(2);
                int e2 = matcher.end(2);
                int s3 = matcher.start(3);
                int e3 = matcher.end(3);
                int minutes = Utilities.parseInt(charSequence.subSequence(s2, e2));
                int seconds = Utilities.parseInt(charSequence.subSequence(s3, e3));
                int hours = s1 >= 0 && e1 >= 0 ? Utilities.parseInt(charSequence.subSequence(s1, e1)) : -1;
                seconds += minutes * 60;
                if (hours > 0) {
                    seconds += hours * 60 * 60;
                }
                if (seconds > duration) {
                    continue;
                }
                if (patternType == 3) {
                    url = new URLSpanNoUnderline("video?" + seconds);
                } else {
                    url = new URLSpanNoUnderline("audio?" + seconds);
                }
            } else {
                char ch = charSequence.charAt(start);
                if (patternType != 0) {
                    if (ch != '@' && ch != '#') {
                        start++;
                    }
                    ch = charSequence.charAt(start);
                    if (ch != '@' && ch != '#') {
                        continue;
                    }
                } else {
                    if (ch != '@' && ch != '#' && ch != '/' && ch != '$') {
                        start++;
                    }
                }
                if (patternType == 1) {
                    if (ch == '@') {
                        url = new URLSpanNoUnderline("https://instagram.com/" + charSequence.subSequence(start + 1, end).toString());
                    } else {
                        url = new URLSpanNoUnderline("https://www.instagram.com/explore/tags/" + charSequence.subSequence(start + 1, end).toString());
                    }
                } else if (patternType == 2) {
                    if (ch == '@') {
                        url = new URLSpanNoUnderline("https://twitter.com/" + charSequence.subSequence(start + 1, end).toString());
                    } else {
                        url = new URLSpanNoUnderline("https://twitter.com/hashtag/" + charSequence.subSequence(start + 1, end).toString());
                    }
                } else {
                    if (charSequence.charAt(start) == '/') {
                        if (botCommands) {
                            url = new URLSpanBotCommand(charSequence.subSequence(start, end).toString(), isOut ? 1 : 0);
                        }
                    } else {
                        url = new URLSpanNoUnderline(charSequence.subSequence(start, end).toString());
                    }
                }
            }
            if (url != null) {
                if (check) {
                    ClickableSpan[] spans = spannable.getSpans(start, end, ClickableSpan.class);
                    if (spans != null && spans.length > 0) {
                        spannable.removeSpan(spans[0]);
                    }
                }
                spannable.setSpan(url, start, end, 0);
            }
        }
    } catch (Exception e) {
        FileLog.e(e);
    }
}
Also used : Matcher(java.util.regex.Matcher) URLSpanBotCommand(org.telegram.ui.Components.URLSpanBotCommand) URLSpan(android.text.style.URLSpan) URLSpanNoUnderline(org.telegram.ui.Components.URLSpanNoUnderline) ClickableSpan(android.text.style.ClickableSpan) Spannable(android.text.Spannable) TextPaint(android.text.TextPaint)

Aggregations

Spannable (android.text.Spannable)6 TextPaint (android.text.TextPaint)6 URLSpanBotCommand (org.telegram.ui.Components.URLSpanBotCommand)6 SuppressLint (android.annotation.SuppressLint)4 Paint (android.graphics.Paint)4 ClickableSpan (android.text.style.ClickableSpan)4 URLSpan (android.text.style.URLSpan)4 LinkPath (org.telegram.ui.Components.LinkPath)4 Point (org.telegram.ui.Components.Point)4 URLSpanMono (org.telegram.ui.Components.URLSpanMono)3 CharacterStyle (android.text.style.CharacterStyle)2 TLRPC (org.telegram.tgnet.TLRPC)2 URLSpanNoUnderline (org.telegram.ui.Components.URLSpanNoUnderline)2 SpannableString (android.text.SpannableString)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 MessageObject (org.telegram.messenger.MessageObject)1 TextStyleSpan (org.telegram.ui.Components.TextStyleSpan)1 URLSpanBrowser (org.telegram.ui.Components.URLSpanBrowser)1 URLSpanReplacement (org.telegram.ui.Components.URLSpanReplacement)1