Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9d1970d7 authored by cketti's avatar cketti Committed by GitHub
Browse files

Merge pull request #1465 from k9mail/extract-webview-resource-handling

Encapsulate attachment resource handling into AttachmentResolver
parents dca9d20f 2c4f6981
Loading
Loading
Loading
Loading
+81 −0
Original line number Diff line number Diff line
package com.fsck.k9.mailstore;


import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.annotation.WorkerThread;
import android.util.Log;

import com.fsck.k9.K9;
import com.fsck.k9.mail.Body;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.Multipart;
import com.fsck.k9.mail.Part;
import com.fsck.k9.message.extractors.AttachmentInfoExtractor;


/**
 * This class is used to encapsulate a message part, providing an interface to
 * get relevant info for a given Content-ID URI.
 *
 * The point of this class is to keep the Content-ID loading code agnostic of
 * the underlying part structure.
 */
public class AttachmentResolver {
    Map<String,Uri> contentIdToAttachmentUriMap;


    private AttachmentResolver(Map<String, Uri> contentIdToAttachmentUriMap) {
        this.contentIdToAttachmentUriMap = contentIdToAttachmentUriMap;
    }

    @Nullable
    public Uri getAttachmentUriForContentId(String cid) {
        return contentIdToAttachmentUriMap.get(cid);
    }

    @WorkerThread
    public static AttachmentResolver createFromPart(Context context, Part part) {
        Map<String,Uri> contentIdToAttachmentUriMap = buildCidToAttachmentUriMap(context, part);

        return new AttachmentResolver(contentIdToAttachmentUriMap);
    }

    private static Map<String,Uri> buildCidToAttachmentUriMap(Context context, Part rootPart) {
        HashMap<String,Uri> result = new HashMap<>();

        Stack<Part> partsToCheck = new Stack<>();
        partsToCheck.push(rootPart);

        while (!partsToCheck.isEmpty()) {
            Part part = partsToCheck.pop();

            Body body = part.getBody();
            if (body instanceof Multipart) {
                Multipart multipart = (Multipart) body;
                for (Part bodyPart : multipart.getBodyParts()) {
                    partsToCheck.push(bodyPart);
                }
            } else {
                try {
                    String contentId = part.getContentId();
                    if (contentId != null) {
                        AttachmentViewInfo attachmentInfo = AttachmentInfoExtractor.extractAttachmentInfo(context, part);
                        result.put(contentId, attachmentInfo.uri);
                    }
                } catch (MessagingException e) {
                    Log.e(K9.LOG_TAG, "Error extracting attachment info", e);
                }
            }
        }

        return Collections.unmodifiableMap(result);
    }

}
+4 −2
Original line number Diff line number Diff line
@@ -20,17 +20,19 @@ public class MessageViewInfo {

    public static class MessageViewContainer {
        public final String text;
        public final AttachmentResolver attachmentResolver;
        public final Part rootPart;
        public final List<AttachmentViewInfo> attachments;
        public final CryptoResultAnnotation cryptoAnnotation;


        MessageViewContainer(String text, Part rootPart, List<AttachmentViewInfo> attachments,
                CryptoResultAnnotation cryptoAnnotation) {
        MessageViewContainer(String text, AttachmentResolver attachmentResolver, Part rootPart,
                List<AttachmentViewInfo> attachments, CryptoResultAnnotation cryptoAnnotation) {
            this.text = text;
            this.rootPart = rootPart;
            this.attachments = attachments;
            this.cryptoAnnotation = cryptoAnnotation;
            this.attachmentResolver = attachmentResolver;
        }
    }
}
+5 −1
Original line number Diff line number Diff line
@@ -68,8 +68,12 @@ public class MessageViewInfoExtractor {
            ViewableExtractedText viewable = MessageViewInfoExtractor.extractTextFromViewables(context, viewableParts);
            List<AttachmentViewInfo> attachmentInfos = AttachmentInfoExtractor.extractAttachmentInfos(context, attachments);

            AttachmentResolver attachmentResolver =
                    AttachmentResolver.createFromPart(context, part);

            MessageViewContainer messageViewContainer =
                    new MessageViewContainer(viewable.html, part, attachmentInfos, pgpAnnotation);
                    new MessageViewContainer(viewable.html, attachmentResolver,
                            part, attachmentInfos, pgpAnnotation);

            containers.add(messageViewContainer);
        }
+3 −2
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ import android.widget.ImageButton;
import com.fsck.k9.FontSizes;
import com.fsck.k9.R;
import com.fsck.k9.activity.MessageCompose;
import com.fsck.k9.mailstore.AttachmentResolver;
import com.fsck.k9.message.QuotedTextMode;
import com.fsck.k9.message.SimpleMessageFormat;
import com.fsck.k9.ui.EolConvertingEditText;
@@ -117,8 +118,8 @@ public class QuotedMessageMvpView {
        mFontSizes.setViewTextSize(mQuotedText, fontSize);
    }

    public void setQuotedHtml(String quotedContent) {
        mQuotedHTML.setText(quotedContent);
    public void setQuotedHtml(String quotedContent, AttachmentResolver attachmentResolver) {
        mQuotedHTML.displayHtmlContentWithInlineAttachments(quotedContent, attachmentResolver);
    }

    public void setQuotedText(String quotedText) {
+9 −4
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.Part;
import com.fsck.k9.mail.internet.MessageExtractor;
import com.fsck.k9.mail.internet.MimeUtility;
import com.fsck.k9.mailstore.AttachmentResolver;
import com.fsck.k9.mailstore.LocalMessage;
import com.fsck.k9.message.IdentityField;
import com.fsck.k9.message.InsertableHtmlContent;
@@ -130,8 +131,9 @@ public class QuotedMessagePresenter {
            quotedHtmlContent = QuotedMessageHelper.quoteOriginalHtmlMessage(
                    resources, sourceMessage, content, quoteStyle);

            // Load the message with the reply header.
            view.setQuotedHtml(quotedHtmlContent.getQuotedContent());
            // Load the message with the reply header. TODO replace with MessageViewInfo data
            view.setQuotedHtml(quotedHtmlContent.getQuotedContent(), AttachmentResolver
                    .createFromPart(messageCompose, sourceMessage));

            // TODO: Also strip the signature from the text/plain part
            view.setQuotedText(QuotedMessageHelper.quoteOriginalTextMessage(resources, sourceMessage,
@@ -173,7 +175,8 @@ public class QuotedMessagePresenter {
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        quotedHtmlContent = (InsertableHtmlContent) savedInstanceState.getSerializable(STATE_KEY_HTML_QUOTE);
        if (quotedHtmlContent != null && quotedHtmlContent.getQuotedContent() != null) {
            view.setQuotedHtml(quotedHtmlContent.getQuotedContent());
            // we don't have the part here, but inline-displayed images are cached by the webview
            view.setQuotedHtml(quotedHtmlContent.getQuotedContent(), null);
        }
        quotedTextFormat = (SimpleMessageFormat) savedInstanceState.getSerializable(
                STATE_KEY_QUOTED_TEXT_FORMAT);
@@ -293,7 +296,9 @@ public class QuotedMessagePresenter {
                    } else {
                        quotedHtmlContent.setFooterInsertionPoint(bodyOffset);
                    }
                    view.setQuotedHtml(quotedHtmlContent.getQuotedContent());
                    // TODO replace with MessageViewInfo data
                    view.setQuotedHtml(quotedHtmlContent.getQuotedContent(),
                            AttachmentResolver.createFromPart(messageCompose, message));
                }
            }
            if (bodyPlainOffset != null && bodyPlainLength != null) {
Loading