Example usage for android.text StaticLayout getLineCount

List of usage examples for android.text StaticLayout getLineCount

Introduction

In this page you can find the example usage for android.text StaticLayout getLineCount.

Prototype

@Override
    public int getLineCount() 

Source Link

Usage

From source file:org.telegram.ui.Cells.SharedLinkCell.java

@SuppressLint("DrawAllocation")
@Override/*from  w  w w .j av  a 2s .  com*/
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    drawLinkImageView = false;
    descriptionLayout = null;
    titleLayout = null;
    descriptionLayout2 = null;
    description2Y = descriptionY;
    linkLayout.clear();
    links.clear();

    int maxWidth = MeasureSpec.getSize(widthMeasureSpec) - AndroidUtilities.dp(AndroidUtilities.leftBaseline)
            - AndroidUtilities.dp(8);

    String title = null;
    String description = null;
    String description2 = null;
    String webPageLink = null;
    boolean hasPhoto = false;

    if (message.messageOwner.media instanceof TLRPC.TL_messageMediaWebPage
            && message.messageOwner.media.webpage instanceof TLRPC.TL_webPage) {
        TLRPC.WebPage webPage = message.messageOwner.media.webpage;
        if (message.photoThumbs == null && webPage.photo != null) {
            message.generateThumbs(true);
        }
        hasPhoto = webPage.photo != null && message.photoThumbs != null;
        title = webPage.title;
        if (title == null) {
            title = webPage.site_name;
        }
        description = webPage.description;
        webPageLink = webPage.url;
    }
    if (message != null && !message.messageOwner.entities.isEmpty()) {
        for (int a = 0; a < message.messageOwner.entities.size(); a++) {
            TLRPC.MessageEntity entity = message.messageOwner.entities.get(a);
            if (entity.length <= 0 || entity.offset < 0
                    || entity.offset >= message.messageOwner.message.length()) {
                continue;
            } else if (entity.offset + entity.length > message.messageOwner.message.length()) {
                entity.length = message.messageOwner.message.length() - entity.offset;
            }
            if (a == 0 && webPageLink != null
                    && !(entity.offset == 0 && entity.length == message.messageOwner.message.length())) {
                if (message.messageOwner.entities.size() == 1) {
                    if (description == null) {
                        description2 = message.messageOwner.message;
                    }
                } else {
                    description2 = message.messageOwner.message;
                }
            }
            try {
                String link = null;
                if (entity instanceof TLRPC.TL_messageEntityTextUrl
                        || entity instanceof TLRPC.TL_messageEntityUrl) {
                    if (entity instanceof TLRPC.TL_messageEntityUrl) {
                        link = message.messageOwner.message.substring(entity.offset,
                                entity.offset + entity.length);
                    } else {
                        link = entity.url;
                    }
                    if (title == null || title.length() == 0) {
                        title = link;
                        Uri uri = Uri.parse(title);
                        title = uri.getHost();
                        if (title == null) {
                            title = link;
                        }
                        int index;
                        if (title != null && (index = title.lastIndexOf('.')) >= 0) {
                            title = title.substring(0, index);
                            if ((index = title.lastIndexOf('.')) >= 0) {
                                title = title.substring(index + 1);
                            }
                            title = title.substring(0, 1).toUpperCase() + title.substring(1);
                        }
                        if (entity.offset != 0 || entity.length != message.messageOwner.message.length()) {
                            description = message.messageOwner.message;
                        }
                    }
                } else if (entity instanceof TLRPC.TL_messageEntityEmail) {
                    if (title == null || title.length() == 0) {
                        link = "mailto:" + message.messageOwner.message.substring(entity.offset,
                                entity.offset + entity.length);
                        title = message.messageOwner.message.substring(entity.offset,
                                entity.offset + entity.length);
                        if (entity.offset != 0 || entity.length != message.messageOwner.message.length()) {
                            description = message.messageOwner.message;
                        }
                    }
                }
                if (link != null) {
                    if (link.toLowerCase().indexOf("http") != 0 && link.toLowerCase().indexOf("mailto") != 0) {
                        links.add("http://" + link);
                    } else {
                        links.add(link);
                    }
                }
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
        }
    }
    if (webPageLink != null && links.isEmpty()) {
        links.add(webPageLink);
    }

    if (title != null) {
        try {
            int width = (int) Math.ceil(titleTextPaint.measureText(title));
            CharSequence titleFinal = TextUtils.ellipsize(title.replace('\n', ' '), titleTextPaint,
                    Math.min(width, maxWidth), TextUtils.TruncateAt.END);
            titleLayout = new StaticLayout(titleFinal, titleTextPaint, maxWidth, Layout.Alignment.ALIGN_NORMAL,
                    1.0f, 0.0f, false);
        } catch (Exception e) {
            FileLog.e("tmessages", e);
        }
        letterDrawable.setTitle(title);
    }

    if (description != null) {
        try {
            descriptionLayout = ChatMessageCell.generateStaticLayout(description, descriptionTextPaint,
                    maxWidth, maxWidth, 0, 3);
            if (descriptionLayout.getLineCount() > 0) {
                description2Y = descriptionY
                        + descriptionLayout.getLineBottom(descriptionLayout.getLineCount() - 1)
                        + AndroidUtilities.dp(1);
            }
        } catch (Exception e) {
            FileLog.e("tmessages", e);
        }
    }

    if (description2 != null) {
        try {
            descriptionLayout2 = ChatMessageCell.generateStaticLayout(description2, descriptionTextPaint,
                    maxWidth, maxWidth, 0, 3);
            int height = descriptionLayout2.getLineBottom(descriptionLayout2.getLineCount() - 1);
            if (descriptionLayout != null) {
                description2Y += AndroidUtilities.dp(10);
            }
        } catch (Exception e) {
            FileLog.e("tmessages", e);
        }
    }

    if (!links.isEmpty()) {
        for (int a = 0; a < links.size(); a++) {
            try {
                String link = links.get(a);
                int width = (int) Math.ceil(descriptionTextPaint.measureText(link));
                CharSequence linkFinal = TextUtils.ellipsize(link.replace('\n', ' '), descriptionTextPaint,
                        Math.min(width, maxWidth), TextUtils.TruncateAt.MIDDLE);
                StaticLayout layout = new StaticLayout(linkFinal, descriptionTextPaint, maxWidth,
                        Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
                linkY = description2Y;
                if (descriptionLayout2 != null && descriptionLayout2.getLineCount() != 0) {
                    linkY += descriptionLayout2.getLineBottom(descriptionLayout2.getLineCount() - 1)
                            + AndroidUtilities.dp(1);
                }
                linkLayout.add(layout);
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
        }
    }

    int maxPhotoWidth = AndroidUtilities.dp(52);
    int x = LocaleController.isRTL
            ? MeasureSpec.getSize(widthMeasureSpec) - AndroidUtilities.dp(10) - maxPhotoWidth
            : AndroidUtilities.dp(10);
    letterDrawable.setBounds(x, AndroidUtilities.dp(10), x + maxPhotoWidth, AndroidUtilities.dp(62));

    if (hasPhoto) {
        TLRPC.PhotoSize currentPhotoObject = FileLoader.getClosestPhotoSizeWithSize(message.photoThumbs,
                maxPhotoWidth, true);
        TLRPC.PhotoSize currentPhotoObjectThumb = FileLoader.getClosestPhotoSizeWithSize(message.photoThumbs,
                80);
        if (currentPhotoObjectThumb == currentPhotoObject) {
            currentPhotoObjectThumb = null;
        }
        currentPhotoObject.size = -1;
        if (currentPhotoObjectThumb != null) {
            currentPhotoObjectThumb.size = -1;
        }
        linkImageView.setImageCoords(x, AndroidUtilities.dp(10), maxPhotoWidth, maxPhotoWidth);
        String fileName = FileLoader.getAttachFileName(currentPhotoObject);
        boolean photoExist = true;
        File cacheFile = FileLoader.getPathToAttach(currentPhotoObject, true);
        if (!cacheFile.exists()) {
            photoExist = false;
        }
        String filter = String.format(Locale.US, "%d_%d", maxPhotoWidth, maxPhotoWidth);
        if (photoExist
                || MediaController.getInstance().canDownloadMedia(MediaController.AUTODOWNLOAD_MASK_PHOTO)
                || FileLoader.getInstance().isLoadingFile(fileName)) {
            linkImageView.setImage(currentPhotoObject.location, filter,
                    currentPhotoObjectThumb != null ? currentPhotoObjectThumb.location : null,
                    String.format(Locale.US, "%d_%d_b", maxPhotoWidth, maxPhotoWidth), 0, null, false);
        } else {
            if (currentPhotoObjectThumb != null) {
                linkImageView.setImage(null, null, currentPhotoObjectThumb.location,
                        String.format(Locale.US, "%d_%d_b", maxPhotoWidth, maxPhotoWidth), 0, null, false);
            } else {
                linkImageView.setImageBitmap((Drawable) null);
            }
        }
        drawLinkImageView = true;
    }

    int height = 0;
    if (titleLayout != null && titleLayout.getLineCount() != 0) {
        height += titleLayout.getLineBottom(titleLayout.getLineCount() - 1);
    }
    if (descriptionLayout != null && descriptionLayout.getLineCount() != 0) {
        height += descriptionLayout.getLineBottom(descriptionLayout.getLineCount() - 1);
    }
    if (descriptionLayout2 != null && descriptionLayout2.getLineCount() != 0) {
        height += descriptionLayout2.getLineBottom(descriptionLayout2.getLineCount() - 1);
        if (descriptionLayout != null) {
            height += AndroidUtilities.dp(10);
        }
    }
    for (int a = 0; a < linkLayout.size(); a++) {
        StaticLayout layout = linkLayout.get(a);
        if (layout.getLineCount() > 0) {
            height += layout.getLineBottom(layout.getLineCount() - 1);
        }
    }
    if (hasPhoto) {
        height = Math.max(AndroidUtilities.dp(48), height);
    }
    checkBox.measure(MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(22), MeasureSpec.EXACTLY),
            MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(22), MeasureSpec.EXACTLY));
    setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
            Math.max(AndroidUtilities.dp(72), height + AndroidUtilities.dp(16)) + (needDivider ? 1 : 0));
}