Search in sources :

Example 1 with TextPaintUrlSpan

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

the class ArticleViewer method getText.

private CharSequence getText(TLRPC.WebPage page, View parentView, TLRPC.RichText parentRichText, TLRPC.RichText richText, TLRPC.PageBlock parentBlock, int maxWidth) {
    if (richText == null) {
        return null;
    }
    if (richText instanceof TLRPC.TL_textFixed) {
        return getText(page, parentView, parentRichText, ((TLRPC.TL_textFixed) richText).text, parentBlock, maxWidth);
    } else if (richText instanceof TLRPC.TL_textItalic) {
        return getText(page, parentView, parentRichText, ((TLRPC.TL_textItalic) richText).text, parentBlock, maxWidth);
    } else if (richText instanceof TLRPC.TL_textBold) {
        return getText(page, parentView, parentRichText, ((TLRPC.TL_textBold) richText).text, parentBlock, maxWidth);
    } else if (richText instanceof TLRPC.TL_textUnderline) {
        return getText(page, parentView, parentRichText, ((TLRPC.TL_textUnderline) richText).text, parentBlock, maxWidth);
    } else if (richText instanceof TLRPC.TL_textStrike) {
        return getText(page, parentView, parentRichText, ((TLRPC.TL_textStrike) richText).text, parentBlock, maxWidth);
    } else if (richText instanceof TLRPC.TL_textEmail) {
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(getText(page, parentView, parentRichText, ((TLRPC.TL_textEmail) richText).text, parentBlock, maxWidth));
        MetricAffectingSpan[] innerSpans = spannableStringBuilder.getSpans(0, spannableStringBuilder.length(), MetricAffectingSpan.class);
        if (spannableStringBuilder.length() != 0) {
            spannableStringBuilder.setSpan(new TextPaintUrlSpan(innerSpans == null || innerSpans.length == 0 ? getTextPaint(parentRichText, richText, parentBlock) : null, "mailto:" + getUrl(richText)), 0, spannableStringBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannableStringBuilder;
    } else if (richText instanceof TLRPC.TL_textUrl) {
        TLRPC.TL_textUrl textUrl = (TLRPC.TL_textUrl) richText;
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(getText(page, parentView, parentRichText, ((TLRPC.TL_textUrl) richText).text, parentBlock, maxWidth));
        MetricAffectingSpan[] innerSpans = spannableStringBuilder.getSpans(0, spannableStringBuilder.length(), MetricAffectingSpan.class);
        TextPaint paint = innerSpans == null || innerSpans.length == 0 ? getTextPaint(parentRichText, richText, parentBlock) : null;
        MetricAffectingSpan span;
        if (textUrl.webpage_id != 0) {
            span = new TextPaintWebpageUrlSpan(paint, getUrl(richText));
        } else {
            span = new TextPaintUrlSpan(paint, getUrl(richText));
        }
        if (spannableStringBuilder.length() != 0) {
            spannableStringBuilder.setSpan(span, 0, spannableStringBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannableStringBuilder;
    } else if (richText instanceof TLRPC.TL_textPlain) {
        return ((TLRPC.TL_textPlain) richText).text;
    } else if (richText instanceof TLRPC.TL_textAnchor) {
        TLRPC.TL_textAnchor textAnchor = (TLRPC.TL_textAnchor) richText;
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(getText(page, parentView, parentRichText, textAnchor.text, parentBlock, maxWidth));
        spannableStringBuilder.setSpan(new AnchorSpan(textAnchor.name), 0, spannableStringBuilder.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableStringBuilder;
    } else if (richText instanceof TLRPC.TL_textEmpty) {
        return "";
    } else if (richText instanceof TLRPC.TL_textConcat) {
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
        int count = richText.texts.size();
        for (int a = 0; a < count; a++) {
            TLRPC.RichText innerRichText = richText.texts.get(a);
            TLRPC.RichText lastRichText = getLastRichText(innerRichText);
            boolean extraSpace = maxWidth >= 0 && innerRichText instanceof TLRPC.TL_textUrl && ((TLRPC.TL_textUrl) innerRichText).webpage_id != 0;
            if (extraSpace && spannableStringBuilder.length() != 0 && spannableStringBuilder.charAt(spannableStringBuilder.length() - 1) != '\n') {
                spannableStringBuilder.append(" ");
                spannableStringBuilder.setSpan(new TextSelectionHelper.IgnoreCopySpannable(), spannableStringBuilder.length() - 1, spannableStringBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            CharSequence innerText = getText(page, parentView, parentRichText, innerRichText, parentBlock, maxWidth);
            int flags = getTextFlags(lastRichText);
            int startLength = spannableStringBuilder.length();
            spannableStringBuilder.append(innerText);
            if (flags != 0 && !(innerText instanceof SpannableStringBuilder)) {
                if ((flags & TEXT_FLAG_URL) != 0 || (flags & TEXT_FLAG_WEBPAGE_URL) != 0) {
                    String url = getUrl(innerRichText);
                    if (url == null) {
                        url = getUrl(parentRichText);
                    }
                    MetricAffectingSpan span;
                    if ((flags & TEXT_FLAG_WEBPAGE_URL) != 0) {
                        span = new TextPaintWebpageUrlSpan(getTextPaint(parentRichText, lastRichText, parentBlock), url);
                    } else {
                        span = new TextPaintUrlSpan(getTextPaint(parentRichText, lastRichText, parentBlock), url);
                    }
                    if (startLength != spannableStringBuilder.length()) {
                        spannableStringBuilder.setSpan(span, startLength, spannableStringBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }
                } else {
                    if (startLength != spannableStringBuilder.length()) {
                        spannableStringBuilder.setSpan(new TextPaintSpan(getTextPaint(parentRichText, lastRichText, parentBlock)), startLength, spannableStringBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }
                }
            }
            if (extraSpace && a != count - 1) {
                spannableStringBuilder.append(" ");
                spannableStringBuilder.setSpan(new TextSelectionHelper.IgnoreCopySpannable(), spannableStringBuilder.length() - 1, spannableStringBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        return spannableStringBuilder;
    } else if (richText instanceof TLRPC.TL_textSubscript) {
        return getText(page, parentView, parentRichText, ((TLRPC.TL_textSubscript) richText).text, parentBlock, maxWidth);
    } else if (richText instanceof TLRPC.TL_textSuperscript) {
        return getText(page, parentView, parentRichText, ((TLRPC.TL_textSuperscript) richText).text, parentBlock, maxWidth);
    } else if (richText instanceof TLRPC.TL_textMarked) {
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(getText(page, parentView, parentRichText, ((TLRPC.TL_textMarked) richText).text, parentBlock, maxWidth));
        MetricAffectingSpan[] innerSpans = spannableStringBuilder.getSpans(0, spannableStringBuilder.length(), MetricAffectingSpan.class);
        if (spannableStringBuilder.length() != 0) {
            spannableStringBuilder.setSpan(new TextPaintMarkSpan(innerSpans == null || innerSpans.length == 0 ? getTextPaint(parentRichText, richText, parentBlock) : null), 0, spannableStringBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannableStringBuilder;
    } else if (richText instanceof TLRPC.TL_textPhone) {
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(getText(page, parentView, parentRichText, ((TLRPC.TL_textPhone) richText).text, parentBlock, maxWidth));
        MetricAffectingSpan[] innerSpans = spannableStringBuilder.getSpans(0, spannableStringBuilder.length(), MetricAffectingSpan.class);
        if (spannableStringBuilder.length() != 0) {
            spannableStringBuilder.setSpan(new TextPaintUrlSpan(innerSpans == null || innerSpans.length == 0 ? getTextPaint(parentRichText, richText, parentBlock) : null, "tel:" + getUrl(richText)), 0, spannableStringBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannableStringBuilder;
    } else if (richText instanceof TLRPC.TL_textImage) {
        TLRPC.TL_textImage textImage = (TLRPC.TL_textImage) richText;
        TLRPC.Document document = WebPageUtils.getDocumentWithId(page, textImage.document_id);
        if (document != null) {
            SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder("*");
            int w = AndroidUtilities.dp(textImage.w);
            int h = AndroidUtilities.dp(textImage.h);
            maxWidth = Math.abs(maxWidth);
            if (w > maxWidth) {
                float scale = maxWidth / (float) w;
                w = maxWidth;
                h *= scale;
            }
            if (parentView != null) {
                int color = Theme.getColor(Theme.key_windowBackgroundWhite);
                float lightness = (0.2126f * Color.red(color) + 0.7152f * Color.green(color) + 0.0722f * Color.blue(color)) / 255.0f;
                spannableStringBuilder.setSpan(new TextPaintImageReceiverSpan(parentView, document, page, w, h, false, lightness <= 0.705f), 0, spannableStringBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            return spannableStringBuilder;
        } else {
            return "";
        }
    }
    return "not supported " + richText;
}
Also used : TextPaintSpan(org.telegram.ui.Components.TextPaintSpan) TLRPC(org.telegram.tgnet.TLRPC) TextPaintUrlSpan(org.telegram.ui.Components.TextPaintUrlSpan) TextPaintWebpageUrlSpan(org.telegram.ui.Components.TextPaintWebpageUrlSpan) TextSelectionHelper(org.telegram.ui.Cells.TextSelectionHelper) TextPaintMarkSpan(org.telegram.ui.Components.TextPaintMarkSpan) TextPaintImageReceiverSpan(org.telegram.ui.Components.TextPaintImageReceiverSpan) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) SuppressLint(android.annotation.SuppressLint) TextPaint(android.text.TextPaint) SpannableStringBuilder(android.text.SpannableStringBuilder) MetricAffectingSpan(android.text.style.MetricAffectingSpan) AnchorSpan(org.telegram.ui.Components.AnchorSpan)

Example 2 with TextPaintUrlSpan

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

the class ArticleViewer method checkLayoutForLinks.

private boolean checkLayoutForLinks(WebpageAdapter adapter, MotionEvent event, View parentView, DrawingText drawingText, int layoutX, int layoutY) {
    if (pageSwitchAnimation != null || parentView == null || !textSelectionHelper.isSelectable(parentView)) {
        return false;
    }
    pressedLinkOwnerView = parentView;
    if (drawingText != null) {
        StaticLayout layout = drawingText.textLayout;
        int x = (int) event.getX();
        int y = (int) event.getY();
        boolean removeLink = false;
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            float width = 0;
            float left = Integer.MAX_VALUE;
            for (int a = 0, N = layout.getLineCount(); a < N; a++) {
                width = Math.max(layout.getLineWidth(a), width);
                left = Math.min(layout.getLineLeft(a), left);
            }
            if (x >= layoutX + left && x <= left + layoutX + width && y >= layoutY && y <= layoutY + layout.getHeight()) {
                pressedLinkOwnerLayout = drawingText;
                pressedLinkOwnerView = parentView;
                pressedLayoutY = layoutY;
                CharSequence text = layout.getText();
                if (text instanceof Spannable) {
                    try {
                        int checkX = x - layoutX;
                        int checkY = y - layoutY;
                        final int line = layout.getLineForVertical(checkY);
                        final int off = layout.getOffsetForHorizontal(line, checkX);
                        left = layout.getLineLeft(line);
                        if (left <= checkX && left + layout.getLineWidth(line) >= checkX) {
                            Spannable buffer = (Spannable) layout.getText();
                            TextPaintUrlSpan[] link = buffer.getSpans(off, off, TextPaintUrlSpan.class);
                            if (link != null && link.length > 0) {
                                pressedLink = link[0];
                                int pressedStart = buffer.getSpanStart(pressedLink);
                                int pressedEnd = buffer.getSpanEnd(pressedLink);
                                for (int a = 1; a < link.length; a++) {
                                    TextPaintUrlSpan span = link[a];
                                    int start = buffer.getSpanStart(span);
                                    int end = buffer.getSpanEnd(span);
                                    if (pressedStart > start || end > pressedEnd) {
                                        pressedLink = span;
                                        pressedStart = start;
                                        pressedEnd = end;
                                    }
                                }
                                try {
                                    urlPath.setUseRoundRect(true);
                                    urlPath.setCurrentLayout(layout, pressedStart, 0);
                                    int shift = pressedLink.getTextPaint() != null ? pressedLink.getTextPaint().baselineShift : 0;
                                    urlPath.setBaselineShift(shift != 0 ? shift + AndroidUtilities.dp(shift > 0 ? 5 : -2) : 0);
                                    layout.getSelectionPath(pressedStart, pressedEnd, urlPath);
                                    parentView.invalidate();
                                } catch (Exception e) {
                                    FileLog.e(e);
                                }
                            }
                        }
                    } catch (Exception e) {
                        FileLog.e(e);
                    }
                }
            }
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            if (pressedLink != null) {
                removeLink = true;
                String url = pressedLink.getUrl();
                if (url != null) {
                    if (linkSheet != null) {
                        linkSheet.dismiss();
                        linkSheet = null;
                    }
                    int index;
                    boolean isAnchor = false;
                    String anchor;
                    if ((index = url.lastIndexOf('#')) != -1) {
                        String webPageUrl;
                        if (!TextUtils.isEmpty(adapter.currentPage.cached_page.url)) {
                            webPageUrl = adapter.currentPage.cached_page.url.toLowerCase();
                        } else {
                            webPageUrl = adapter.currentPage.url.toLowerCase();
                        }
                        try {
                            anchor = URLDecoder.decode(url.substring(index + 1), "UTF-8");
                        } catch (Exception ignore) {
                            anchor = "";
                        }
                        if (url.toLowerCase().contains(webPageUrl)) {
                            if (TextUtils.isEmpty(anchor)) {
                                layoutManager[0].scrollToPositionWithOffset(0, 0);
                                checkScrollAnimated();
                            } else {
                                scrollToAnchor(anchor);
                            }
                            isAnchor = true;
                        }
                    } else {
                        anchor = null;
                    }
                    if (!isAnchor) {
                        openWebpageUrl(pressedLink.getUrl(), anchor);
                    }
                }
            }
        } else if (event.getAction() == MotionEvent.ACTION_CANCEL && (popupWindow == null || !popupWindow.isShowing())) {
            removeLink = true;
        }
        if (removeLink) {
            removePressedLink();
        }
    }
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        startCheckLongPress(event.getX(), event.getY(), parentView);
    }
    if (event.getAction() != MotionEvent.ACTION_DOWN && event.getAction() != MotionEvent.ACTION_MOVE) {
        cancelCheckLongPress();
    }
    if (parentView instanceof BlockDetailsCell) {
        return pressedLink != null;
    } else {
        return pressedLinkOwnerLayout != null;
    }
}
Also used : TextPaintUrlSpan(org.telegram.ui.Components.TextPaintUrlSpan) StaticLayout(android.text.StaticLayout) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) SuppressLint(android.annotation.SuppressLint) Spannable(android.text.Spannable)

Aggregations

SuppressLint (android.annotation.SuppressLint)2 Paint (android.graphics.Paint)2 TextPaint (android.text.TextPaint)2 TextPaintUrlSpan (org.telegram.ui.Components.TextPaintUrlSpan)2 Spannable (android.text.Spannable)1 SpannableStringBuilder (android.text.SpannableStringBuilder)1 StaticLayout (android.text.StaticLayout)1 MetricAffectingSpan (android.text.style.MetricAffectingSpan)1 TLRPC (org.telegram.tgnet.TLRPC)1 TextSelectionHelper (org.telegram.ui.Cells.TextSelectionHelper)1 AnchorSpan (org.telegram.ui.Components.AnchorSpan)1 TextPaintImageReceiverSpan (org.telegram.ui.Components.TextPaintImageReceiverSpan)1 TextPaintMarkSpan (org.telegram.ui.Components.TextPaintMarkSpan)1 TextPaintSpan (org.telegram.ui.Components.TextPaintSpan)1 TextPaintWebpageUrlSpan (org.telegram.ui.Components.TextPaintWebpageUrlSpan)1