From ba797797589f7999346a0524f011eac49145a289 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Thu, 29 Jan 2015 16:10:03 +0100 Subject: [PATCH] break message into multiple MessageViewContainers --- .../k9/mailstore/LocalMessageExtractor.java | 62 ++++++++++++++++--- .../fsck/k9/mailstore/MessageViewInfo.java | 8 ++- 2 files changed, 61 insertions(+), 9 deletions(-) diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalMessageExtractor.java b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalMessageExtractor.java index a021bcede..75139527c 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalMessageExtractor.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalMessageExtractor.java @@ -1,12 +1,19 @@ package com.fsck.k9.mailstore; +import android.app.PendingIntent; import android.content.Context; import android.net.Uri; +import android.util.Log; +import com.fsck.k9.K9; import com.fsck.k9.R; +import com.fsck.k9.crypto.MessageDecryptor; import com.fsck.k9.mail.Address; +import com.fsck.k9.mail.Body; +import com.fsck.k9.mail.BodyPart; import com.fsck.k9.mail.Message; import com.fsck.k9.mail.MessagingException; +import com.fsck.k9.mail.Multipart; import com.fsck.k9.mail.Part; import com.fsck.k9.helper.HtmlConverter; import com.fsck.k9.mail.internet.MessageExtractor; @@ -15,6 +22,7 @@ import com.fsck.k9.mail.internet.MimeUtility; import com.fsck.k9.mail.internet.Viewable; import com.fsck.k9.mailstore.MessageViewInfo.MessageViewContainer; import com.fsck.k9.provider.AttachmentProvider; +import org.openintents.openpgp.OpenPgpError; import org.openintents.openpgp.OpenPgpSignatureResult; import java.util.ArrayList; @@ -417,10 +425,7 @@ public class LocalMessageExtractor { public static MessageViewInfo decodeMessageForView(Context context, Message message) throws MessagingException { // 1. break mime structure on encryption/signature boundaries - ArrayList parts = new ArrayList(); - // TODO: actually break it down - parts.add(message); -// parts.add(message); + List parts = getCryptPieces(message); // 2. extract viewables/attachments of parts ArrayList containers = new ArrayList(); @@ -433,15 +438,58 @@ public class LocalMessageExtractor { attachments); List attachmentInfos = extractAttachmentInfos(attachments); - // TODO correctly extract OpenPgpSignatureResult and add to MessageViewContainer - OpenPgpSignatureResult result = null; - containers.add(new MessageViewContainer(viewable.html, attachmentInfos, result, false, null)); + // TODO fill from part + OpenPgpSignatureResult pgpResult = null; + OpenPgpError pgpError = null; + boolean wasEncrypted = false; + PendingIntent pendingIntent = null; + + containers.add(new MessageViewContainer( + viewable.html, attachmentInfos, pgpResult, pgpError, wasEncrypted, pendingIntent)); } return new MessageViewInfo(containers, message); } + public static List getCryptPieces(Part part) throws MessagingException { + ArrayList parts = new ArrayList(); + if (!getCryptSubPieces(part, parts)) { + parts.add(part); + } + + return parts; + } + + public static boolean getCryptSubPieces(Part part, ArrayList parts) throws MessagingException { + + Body body = part.getBody(); + if (body instanceof Multipart) { + Multipart multi = (Multipart) body; + if ("multipart/mixed".equals(part.getMimeType())) { + boolean foundSome = false; + for (BodyPart sub : multi.getBodyParts()) { + foundSome |= getCryptSubPieces(sub, parts); + } + if (!foundSome) { + parts.add(part); + return true; + } + } else if (isPgpMimeDecryptedPart(part)) { + parts.add(multi.getBodyPart(2)); + return true; + } + } + return false; + } + + public static boolean isPgpMimeDecryptedPart (Part part) { + Body body = part.getBody(); + return (body instanceof Multipart) + && MessageDecryptor.isPgpMimeEncryptedPart(part) + && ((Multipart) part.getBody()).getBodyParts().size() == 3; + } + private static List extractAttachmentInfos(List attachmentParts) throws MessagingException { diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/MessageViewInfo.java b/k9mail/src/main/java/com/fsck/k9/mailstore/MessageViewInfo.java index 39509fd0e..522119b03 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/MessageViewInfo.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/MessageViewInfo.java @@ -7,6 +7,7 @@ import java.util.ArrayList; import java.util.List; import com.fsck.k9.mail.Message; +import org.openintents.openpgp.OpenPgpError; import org.openintents.openpgp.OpenPgpSignatureResult; @@ -33,22 +34,25 @@ public class MessageViewInfo { final public List attachments; final public boolean encrypted; final public OpenPgpSignatureResult signatureResult; + final public OpenPgpError pgpError; final public PendingIntent pgpPendingIntent; MessageViewContainer(String text, List attachments) { this.text = text; this.attachments = attachments; this.signatureResult = null; + this.pgpError = null; this.encrypted = false; this.pgpPendingIntent = null; } MessageViewContainer(String text, List attachments, - OpenPgpSignatureResult signatureResult, boolean encrypted, - PendingIntent pgpPendingIntent) { + OpenPgpSignatureResult signatureResult, OpenPgpError pgpError, + boolean encrypted, PendingIntent pgpPendingIntent) { this.text = text; this.attachments = attachments; this.signatureResult = signatureResult; + this.pgpError = pgpError; this.encrypted = encrypted; this.pgpPendingIntent = pgpPendingIntent; }