Search in sources :

Example 1 with WPImageGetter

use of org.wordpress.android.util.helpers.WPImageGetter in project WordPress-Android by wordpress-mobile.

the class CommentUtils method displayHtmlComment.

/*
     * displays comment text as html, including retrieving images
     */
public static void displayHtmlComment(TextView textView, String content, int maxImageSize, ImageLoader imageLoader) {
    if (textView == null) {
        return;
    }
    if (content == null) {
        textView.setText(null);
        return;
    }
    // skip performance hit of html conversion if content doesn't contain html
    if (!content.contains("<") && !content.contains("&")) {
        content = content.trim();
        textView.setText(content);
        // make sure unnamed links are clickable
        if (content.contains("://")) {
            Linkify.addLinks(textView, Linkify.WEB_URLS);
        }
        return;
    }
    // convert emoticons first (otherwise they'll be downloaded)
    content = EmoticonsUtils.replaceEmoticonsWithEmoji(content);
    // now convert to HTML with an image getter that enforces a max image size
    final Spanned html;
    if (maxImageSize > 0 && content.contains("<img")) {
        Drawable loading = ContextCompat.getDrawable(textView.getContext(), R.drawable.legacy_dashicon_format_image_big_grey);
        Drawable failed = ContextCompat.getDrawable(textView.getContext(), R.drawable.ic_notice_grey_500_48dp);
        html = HtmlUtils.fromHtml(content, new WPImageGetter(textView, maxImageSize, imageLoader, loading, failed));
    } else {
        html = HtmlUtils.fromHtml(content);
    }
    // remove extra \n\n added by Html.convert()
    int start = 0;
    int end = html.length();
    while (start < end && Character.isWhitespace(html.charAt(start))) {
        start++;
    }
    while (end > start && Character.isWhitespace(html.charAt(end - 1))) {
        end--;
    }
    textView.setText(html.subSequence(start, end));
}
Also used : Drawable(android.graphics.drawable.Drawable) WPImageGetter(org.wordpress.android.util.helpers.WPImageGetter) Spanned(android.text.Spanned) Paint(android.graphics.Paint)

Example 2 with WPImageGetter

use of org.wordpress.android.util.helpers.WPImageGetter in project WordPress-Android by wordpress-mobile.

the class NotificationsUtils method addImageSpansForBlockMedia.

/**
     * Adds ImageSpans to the passed SpannableStringBuilder
     */
private static void addImageSpansForBlockMedia(TextView textView, JSONObject subject, SpannableStringBuilder spannableStringBuilder) {
    if (textView == null || subject == null || spannableStringBuilder == null)
        return;
    Context context = textView.getContext();
    JSONArray mediaArray = subject.optJSONArray("media");
    if (context == null || mediaArray == null) {
        return;
    }
    Drawable loading = context.getResources().getDrawable(org.wordpress.android.editor.R.drawable.legacy_dashicon_format_image_big_grey);
    Drawable failed = context.getResources().getDrawable(R.drawable.ic_notice_grey_500_48dp);
    // Note: notifications_max_image_size seems to be the max size an ImageSpan can handle,
    // otherwise it would load blank white
    WPImageGetter imageGetter = new WPImageGetter(textView, context.getResources().getDimensionPixelSize(R.dimen.notifications_max_image_size), WordPress.sImageLoader, loading, failed);
    int indexAdjustment = 0;
    String imagePlaceholder;
    for (int i = 0; i < mediaArray.length(); i++) {
        JSONObject mediaObject = mediaArray.optJSONObject(i);
        if (mediaObject == null) {
            continue;
        }
        final Drawable remoteDrawable = imageGetter.getDrawable(mediaObject.optString("url", ""));
        ImageSpan noteImageSpan = new ImageSpan(remoteDrawable, mediaObject.optString("url", ""));
        int startIndex = JSONUtils.queryJSON(mediaObject, "indices[0]", -1);
        int endIndex = JSONUtils.queryJSON(mediaObject, "indices[1]", -1);
        if (startIndex >= 0) {
            startIndex += indexAdjustment;
            endIndex += indexAdjustment;
            if (startIndex > spannableStringBuilder.length()) {
                continue;
            }
            // If we have a range, it means there is alt text that should be removed
            if (endIndex > startIndex && endIndex <= spannableStringBuilder.length()) {
                spannableStringBuilder.replace(startIndex, endIndex, "");
            }
            // We need an empty space to insert the ImageSpan into
            imagePlaceholder = " ";
            // Move the image to a new line if needed
            int previousCharIndex = (startIndex > 0) ? startIndex - 1 : 0;
            if (!spannableHasCharacterAtIndex(spannableStringBuilder, '\n', previousCharIndex) || spannableStringBuilder.getSpans(startIndex, startIndex, ImageSpan.class).length > 0) {
                imagePlaceholder = "\n ";
            }
            int spanIndex = startIndex + imagePlaceholder.length() - 1;
            // Add a newline after the image if needed
            if (!spannableHasCharacterAtIndex(spannableStringBuilder, '\n', startIndex) && !spannableHasCharacterAtIndex(spannableStringBuilder, '\r', startIndex)) {
                imagePlaceholder += "\n";
            }
            spannableStringBuilder.insert(startIndex, imagePlaceholder);
            // Add the image span
            spannableStringBuilder.setSpan(noteImageSpan, spanIndex, spanIndex + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            // Add an AlignmentSpan to center the image
            spannableStringBuilder.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER), spanIndex, spanIndex + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            indexAdjustment += imagePlaceholder.length();
        }
    }
}
Also used : Context(android.content.Context) AlignmentSpan(android.text.style.AlignmentSpan) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) Drawable(android.graphics.drawable.Drawable) WPImageGetter(org.wordpress.android.util.helpers.WPImageGetter) ImageSpan(android.text.style.ImageSpan)

Aggregations

Drawable (android.graphics.drawable.Drawable)2 WPImageGetter (org.wordpress.android.util.helpers.WPImageGetter)2 Context (android.content.Context)1 Paint (android.graphics.Paint)1 Spanned (android.text.Spanned)1 AlignmentSpan (android.text.style.AlignmentSpan)1 ImageSpan (android.text.style.ImageSpan)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1