Search in sources :

Example 1 with URLSpanBrowser

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

the class ChatMessageCell method getRealSpanStartAndEnd.

private int[] getRealSpanStartAndEnd(Spannable buffer, CharacterStyle link) {
    int start = 0;
    int end = 0;
    boolean ok = false;
    if (link instanceof URLSpanBrowser) {
        URLSpanBrowser span = (URLSpanBrowser) link;
        TextStyleSpan.TextStyleRun style = span.getStyle();
        if (style != null && style.urlEntity != null) {
            start = style.urlEntity.offset;
            end = style.urlEntity.offset + style.urlEntity.length;
            ok = true;
        }
    }
    if (!ok) {
        start = buffer.getSpanStart(link);
        end = buffer.getSpanEnd(link);
    }
    return new int[] { start, end };
}
Also used : TextStyleSpan(org.telegram.ui.Components.TextStyleSpan) URLSpanBrowser(org.telegram.ui.Components.URLSpanBrowser) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) SuppressLint(android.annotation.SuppressLint) Point(org.telegram.ui.Components.Point)

Example 2 with URLSpanBrowser

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

the class MessageObject method addEntitiesToText.

public static boolean addEntitiesToText(CharSequence text, ArrayList<TLRPC.MessageEntity> entities, boolean out, boolean usernames, boolean photoViewer, boolean useManualParse) {
    if (!(text instanceof Spannable)) {
        return false;
    }
    Spannable spannable = (Spannable) text;
    URLSpan[] spans = spannable.getSpans(0, text.length(), URLSpan.class);
    boolean hasUrls = spans != null && spans.length > 0;
    if (entities.isEmpty()) {
        return hasUrls;
    }
    byte t;
    if (photoViewer) {
        t = 2;
    } else if (out) {
        t = 1;
    } else {
        t = 0;
    }
    ArrayList<TextStyleSpan.TextStyleRun> runs = new ArrayList<>();
    ArrayList<TLRPC.MessageEntity> entitiesCopy = new ArrayList<>(entities);
    Collections.sort(entitiesCopy, (o1, o2) -> {
        if (o1.offset > o2.offset) {
            return 1;
        } else if (o1.offset < o2.offset) {
            return -1;
        }
        return 0;
    });
    for (int a = 0, N = entitiesCopy.size(); a < N; a++) {
        TLRPC.MessageEntity entity = entitiesCopy.get(a);
        if (entity.length <= 0 || entity.offset < 0 || entity.offset >= text.length()) {
            continue;
        } else if (entity.offset + entity.length > text.length()) {
            entity.length = text.length() - entity.offset;
        }
        if (!useManualParse || entity instanceof TLRPC.TL_messageEntityBold || entity instanceof TLRPC.TL_messageEntityItalic || entity instanceof TLRPC.TL_messageEntityStrike || entity instanceof TLRPC.TL_messageEntityUnderline || entity instanceof TLRPC.TL_messageEntityBlockquote || entity instanceof TLRPC.TL_messageEntityCode || entity instanceof TLRPC.TL_messageEntityPre || entity instanceof TLRPC.TL_messageEntityMentionName || entity instanceof TLRPC.TL_inputMessageEntityMentionName || entity instanceof TLRPC.TL_messageEntityTextUrl || entity instanceof TLRPC.TL_messageEntitySpoiler) {
            if (spans != null && spans.length > 0) {
                for (int b = 0; b < spans.length; b++) {
                    if (spans[b] == null) {
                        continue;
                    }
                    int start = spannable.getSpanStart(spans[b]);
                    int end = spannable.getSpanEnd(spans[b]);
                    if (entity.offset <= start && entity.offset + entity.length >= start || entity.offset <= end && entity.offset + entity.length >= end) {
                        spannable.removeSpan(spans[b]);
                        spans[b] = null;
                    }
                }
            }
        }
        TextStyleSpan.TextStyleRun newRun = new TextStyleSpan.TextStyleRun();
        newRun.start = entity.offset;
        newRun.end = newRun.start + entity.length;
        TLRPC.MessageEntity urlEntity = null;
        if (entity instanceof TLRPC.TL_messageEntitySpoiler) {
            newRun.flags = TextStyleSpan.FLAG_STYLE_SPOILER;
        } else if (entity instanceof TLRPC.TL_messageEntityStrike) {
            newRun.flags = TextStyleSpan.FLAG_STYLE_STRIKE;
        } else if (entity instanceof TLRPC.TL_messageEntityUnderline) {
            newRun.flags = TextStyleSpan.FLAG_STYLE_UNDERLINE;
        } else if (entity instanceof TLRPC.TL_messageEntityBlockquote) {
            newRun.flags = TextStyleSpan.FLAG_STYLE_QUOTE;
        } else if (entity instanceof TLRPC.TL_messageEntityBold) {
            newRun.flags = TextStyleSpan.FLAG_STYLE_BOLD;
        } else if (entity instanceof TLRPC.TL_messageEntityItalic) {
            newRun.flags = TextStyleSpan.FLAG_STYLE_ITALIC;
        } else if (entity instanceof TLRPC.TL_messageEntityCode || entity instanceof TLRPC.TL_messageEntityPre) {
            newRun.flags = TextStyleSpan.FLAG_STYLE_MONO;
        } else if (entity instanceof TLRPC.TL_messageEntityMentionName) {
            if (!usernames) {
                continue;
            }
            newRun.flags = TextStyleSpan.FLAG_STYLE_MENTION;
            newRun.urlEntity = entity;
        } else if (entity instanceof TLRPC.TL_inputMessageEntityMentionName) {
            if (!usernames) {
                continue;
            }
            newRun.flags = TextStyleSpan.FLAG_STYLE_MENTION;
            newRun.urlEntity = entity;
        } else {
            if (useManualParse && !(entity instanceof TLRPC.TL_messageEntityTextUrl)) {
                continue;
            }
            if ((entity instanceof TLRPC.TL_messageEntityUrl || entity instanceof TLRPC.TL_messageEntityTextUrl) && Browser.isPassportUrl(entity.url)) {
                continue;
            }
            if (entity instanceof TLRPC.TL_messageEntityMention && !usernames) {
                continue;
            }
            newRun.flags = TextStyleSpan.FLAG_STYLE_URL;
            newRun.urlEntity = entity;
        }
        for (int b = 0, N2 = runs.size(); b < N2; b++) {
            TextStyleSpan.TextStyleRun run = runs.get(b);
            if (newRun.start > run.start) {
                if (newRun.start >= run.end) {
                    continue;
                }
                if (newRun.end < run.end) {
                    TextStyleSpan.TextStyleRun r = new TextStyleSpan.TextStyleRun(newRun);
                    r.merge(run);
                    b++;
                    N2++;
                    runs.add(b, r);
                    r = new TextStyleSpan.TextStyleRun(run);
                    r.start = newRun.end;
                    b++;
                    N2++;
                    runs.add(b, r);
                } else {
                    TextStyleSpan.TextStyleRun r = new TextStyleSpan.TextStyleRun(newRun);
                    r.merge(run);
                    r.end = run.end;
                    b++;
                    N2++;
                    runs.add(b, r);
                }
                int temp = newRun.start;
                newRun.start = run.end;
                run.end = temp;
            } else {
                if (run.start >= newRun.end) {
                    continue;
                }
                int temp = run.start;
                if (newRun.end == run.end) {
                    run.merge(newRun);
                } else if (newRun.end < run.end) {
                    TextStyleSpan.TextStyleRun r = new TextStyleSpan.TextStyleRun(run);
                    r.merge(newRun);
                    r.end = newRun.end;
                    b++;
                    N2++;
                    runs.add(b, r);
                    run.start = newRun.end;
                } else {
                    TextStyleSpan.TextStyleRun r = new TextStyleSpan.TextStyleRun(newRun);
                    r.start = run.end;
                    b++;
                    N2++;
                    runs.add(b, r);
                    run.merge(newRun);
                }
                newRun.end = temp;
            }
        }
        if (newRun.start < newRun.end) {
            runs.add(newRun);
        }
    }
    int count = runs.size();
    for (int a = 0; a < count; a++) {
        TextStyleSpan.TextStyleRun run = runs.get(a);
        boolean setRun = false;
        String url = run.urlEntity != null ? TextUtils.substring(text, run.urlEntity.offset, run.urlEntity.offset + run.urlEntity.length) : null;
        if (run.urlEntity instanceof TLRPC.TL_messageEntityBotCommand) {
            spannable.setSpan(new URLSpanBotCommand(url, t, run), run.start, run.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else if (run.urlEntity instanceof TLRPC.TL_messageEntityHashtag || run.urlEntity instanceof TLRPC.TL_messageEntityMention || run.urlEntity instanceof TLRPC.TL_messageEntityCashtag) {
            spannable.setSpan(new URLSpanNoUnderline(url, run), run.start, run.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else if (run.urlEntity instanceof TLRPC.TL_messageEntityEmail) {
            spannable.setSpan(new URLSpanReplacement("mailto:" + url, run), run.start, run.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else if (run.urlEntity instanceof TLRPC.TL_messageEntityUrl) {
            hasUrls = true;
            String lowerCase = url.toLowerCase();
            if (!lowerCase.contains("://")) {
                spannable.setSpan(new URLSpanBrowser("http://" + url, run), run.start, run.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            } else {
                spannable.setSpan(new URLSpanBrowser(url, run), run.start, run.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        } else if (run.urlEntity instanceof TLRPC.TL_messageEntityBankCard) {
            hasUrls = true;
            spannable.setSpan(new URLSpanNoUnderline("card:" + url, run), run.start, run.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else if (run.urlEntity instanceof TLRPC.TL_messageEntityPhone) {
            hasUrls = true;
            String tel = PhoneFormat.stripExceptNumbers(url);
            if (url.startsWith("+")) {
                tel = "+" + tel;
            }
            spannable.setSpan(new URLSpanBrowser("tel:" + tel, run), run.start, run.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else if (run.urlEntity instanceof TLRPC.TL_messageEntityTextUrl) {
            spannable.setSpan(new URLSpanReplacement(run.urlEntity.url, run), run.start, run.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else if (run.urlEntity instanceof TLRPC.TL_messageEntityMentionName) {
            spannable.setSpan(new URLSpanUserMention("" + ((TLRPC.TL_messageEntityMentionName) run.urlEntity).user_id, t, run), run.start, run.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else if (run.urlEntity instanceof TLRPC.TL_inputMessageEntityMentionName) {
            spannable.setSpan(new URLSpanUserMention("" + ((TLRPC.TL_inputMessageEntityMentionName) run.urlEntity).user_id.user_id, t, run), run.start, run.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else if ((run.flags & TextStyleSpan.FLAG_STYLE_MONO) != 0) {
            spannable.setSpan(new URLSpanMono(spannable, run.start, run.end, t, run), run.start, run.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else {
            setRun = true;
            spannable.setSpan(new TextStyleSpan(run), run.start, run.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        if (!setRun && (run.flags & TextStyleSpan.FLAG_STYLE_SPOILER) != 0) {
            spannable.setSpan(new TextStyleSpan(run), run.start, run.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
    return hasUrls;
}
Also used : URLSpanMono(org.telegram.ui.Components.URLSpanMono) ArrayList(java.util.ArrayList) URLSpanBotCommand(org.telegram.ui.Components.URLSpanBotCommand) SpannableString(android.text.SpannableString) URLSpan(android.text.style.URLSpan) TLRPC(org.telegram.tgnet.TLRPC) TextStyleSpan(org.telegram.ui.Components.TextStyleSpan) URLSpanNoUnderline(org.telegram.ui.Components.URLSpanNoUnderline) URLSpanBrowser(org.telegram.ui.Components.URLSpanBrowser) TextPaint(android.text.TextPaint) URLSpanUserMention(org.telegram.ui.Components.URLSpanUserMention) Spannable(android.text.Spannable) URLSpanReplacement(org.telegram.ui.Components.URLSpanReplacement)

Aggregations

TextPaint (android.text.TextPaint)2 TextStyleSpan (org.telegram.ui.Components.TextStyleSpan)2 URLSpanBrowser (org.telegram.ui.Components.URLSpanBrowser)2 SuppressLint (android.annotation.SuppressLint)1 Paint (android.graphics.Paint)1 Spannable (android.text.Spannable)1 SpannableString (android.text.SpannableString)1 URLSpan (android.text.style.URLSpan)1 ArrayList (java.util.ArrayList)1 TLRPC (org.telegram.tgnet.TLRPC)1 Point (org.telegram.ui.Components.Point)1 URLSpanBotCommand (org.telegram.ui.Components.URLSpanBotCommand)1 URLSpanMono (org.telegram.ui.Components.URLSpanMono)1 URLSpanNoUnderline (org.telegram.ui.Components.URLSpanNoUnderline)1 URLSpanReplacement (org.telegram.ui.Components.URLSpanReplacement)1 URLSpanUserMention (org.telegram.ui.Components.URLSpanUserMention)1