From d2d85393d38d85972cd0a190477024d359dbf368 Mon Sep 17 00:00:00 2001 From: cketti Date: Thu, 22 Jan 2015 06:05:05 +0100 Subject: [PATCH] Save attachment count --- ...est.java => MessageInfoExtractorTest.java} | 14 +++--- .../com/fsck/k9/mailstore/LocalFolder.java | 8 ++-- .../k9/mailstore/MessageInfoExtractor.java | 43 +++++++++++++++++++ .../k9/mailstore/MessagePreviewExtractor.java | 18 +------- 4 files changed, 56 insertions(+), 27 deletions(-) rename k9mail/src/androidTest/java/com/fsck/k9/mailstore/{MessagePreviewExtractorTest.java => MessageInfoExtractorTest.java} (89%) create mode 100644 k9mail/src/main/java/com/fsck/k9/mailstore/MessageInfoExtractor.java diff --git a/k9mail/src/androidTest/java/com/fsck/k9/mailstore/MessagePreviewExtractorTest.java b/k9mail/src/androidTest/java/com/fsck/k9/mailstore/MessageInfoExtractorTest.java similarity index 89% rename from k9mail/src/androidTest/java/com/fsck/k9/mailstore/MessagePreviewExtractorTest.java rename to k9mail/src/androidTest/java/com/fsck/k9/mailstore/MessageInfoExtractorTest.java index dcbd437cc..3a7539240 100644 --- a/k9mail/src/androidTest/java/com/fsck/k9/mailstore/MessagePreviewExtractorTest.java +++ b/k9mail/src/androidTest/java/com/fsck/k9/mailstore/MessageInfoExtractorTest.java @@ -17,7 +17,7 @@ import static org.junit.Assert.assertEquals; @RunWith(AndroidJUnit4.class) -public class MessagePreviewExtractorTest { +public class MessageInfoExtractorTest { @Test public void shouldExtractPreviewFromSinglePlainTextPart() throws MessagingException { @@ -26,7 +26,7 @@ public class MessagePreviewExtractorTest { TextBody body = new TextBody("Message text "); message.setBody(body); - String preview = MessagePreviewExtractor.extractPreview(getContext(), message); + String preview = new MessageInfoExtractor(getContext(), message).getMessageTextPreview(); assertEquals("Message text", preview); } @@ -49,7 +49,7 @@ public class MessagePreviewExtractorTest { "560-------570-------580-------590-------600-------"); message.setBody(body); - String preview = MessagePreviewExtractor.extractPreview(getContext(), message); + String preview = new MessageInfoExtractor(getContext(), message).getMessageTextPreview(); assertEquals(512, preview.length()); assertEquals('…', preview.charAt(511)); @@ -62,7 +62,7 @@ public class MessagePreviewExtractorTest { TextBody body = new TextBody("
Message text
"); message.setBody(body); - String preview = MessagePreviewExtractor.extractPreview(getContext(), message); + String preview = new MessageInfoExtractor(getContext(), message).getMessageTextPreview(); assertEquals("Message text", preview); } @@ -83,7 +83,7 @@ public class MessagePreviewExtractorTest { MimeBodyPart htmlPart = new MimeBodyPart(htmlBody, "text/html"); multipart.addBodyPart(htmlPart); - String preview = MessagePreviewExtractor.extractPreview(getContext(), message); + String preview = new MessageInfoExtractor(getContext(), message).getMessageTextPreview(); assertEquals("text", preview); } @@ -104,7 +104,7 @@ public class MessagePreviewExtractorTest { MimeBodyPart htmlPart = new MimeBodyPart(htmlBody, "text/html"); multipart.addBodyPart(htmlPart); - String preview = MessagePreviewExtractor.extractPreview(getContext(), message); + String preview = new MessageInfoExtractor(getContext(), message).getMessageTextPreview(); assertEquals("text / html", preview); } @@ -130,7 +130,7 @@ public class MessagePreviewExtractorTest { MimeBodyPart messagePart = new MimeBodyPart(innerMessage, "message/rfc822"); multipart.addBodyPart(messagePart); - String preview = MessagePreviewExtractor.extractPreview(getContext(), message); + String preview = new MessageInfoExtractor(getContext(), message).getMessageTextPreview(); assertEquals("text / Includes message titled \"inner message\" containing: html", preview); } diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalFolder.java b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalFolder.java index df0f899bd..0ed6db404 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalFolder.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalFolder.java @@ -1247,10 +1247,10 @@ public class LocalFolder extends Folder implements Serializable { parentId = threadInfo.parentId; } - //TODO: get attachment count - try { - String preview = MessagePreviewExtractor.extractPreview(localStore.context, message); + MessageInfoExtractor messageExtractor = new MessageInfoExtractor(localStore.context, message); + String preview = messageExtractor.getMessageTextPreview(); + int attachmentCount = messageExtractor.getAttachmentCount(); long rootMessagePartId = saveMessageParts(db, message); @@ -1273,7 +1273,7 @@ public class LocalFolder extends Folder implements Serializable { cv.put("bcc_list", Address.pack(message.getRecipients(RecipientType.BCC))); cv.put("preview", preview); cv.put("reply_to_list", Address.pack(message.getReplyTo())); - cv.put("attachment_count", 0); //FIXME + cv.put("attachment_count", attachmentCount); cv.put("internal_date", message.getInternalDate() == null ? System.currentTimeMillis() : message.getInternalDate().getTime()); cv.put("mime_type", message.getMimeType()); diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/MessageInfoExtractor.java b/k9mail/src/main/java/com/fsck/k9/mailstore/MessageInfoExtractor.java new file mode 100644 index 000000000..c0e5f66dc --- /dev/null +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/MessageInfoExtractor.java @@ -0,0 +1,43 @@ +package com.fsck.k9.mailstore; + + +import java.util.ArrayList; +import java.util.List; + +import android.content.Context; + +import com.fsck.k9.mail.Message; +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.Viewable; + + +class MessageInfoExtractor { + private final Context context; + private final Message message; + private List viewables; + private List attachments; + + public MessageInfoExtractor(Context context, Message message) { + this.context = context; + this.message = message; + } + + public String getMessageTextPreview() throws MessagingException { + getViewablesIfNecessary(); + return MessagePreviewExtractor.extractPreview(context, viewables); + } + + public int getAttachmentCount() throws MessagingException { + getViewablesIfNecessary(); + return attachments.size(); + } + + private void getViewablesIfNecessary() throws MessagingException { + if (viewables == null) { + attachments = new ArrayList(); + viewables = MessageExtractor.getViewables(message, attachments); + } + } +} diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/MessagePreviewExtractor.java b/k9mail/src/main/java/com/fsck/k9/mailstore/MessagePreviewExtractor.java index 60be77618..bafa7c82f 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/MessagePreviewExtractor.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/MessagePreviewExtractor.java @@ -1,7 +1,6 @@ package com.fsck.k9.mailstore; -import java.util.ArrayList; import java.util.List; import android.content.Context; @@ -9,8 +8,6 @@ import android.text.TextUtils; import com.fsck.k9.R; import com.fsck.k9.helper.HtmlConverter; -import com.fsck.k9.mail.Message; -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.Viewable; @@ -20,22 +17,11 @@ import com.fsck.k9.mail.internet.Viewable.MessageHeader; import com.fsck.k9.mail.internet.Viewable.Textual; -public class MessagePreviewExtractor { +class MessagePreviewExtractor { private static final int MAX_PREVIEW_LENGTH = 512; private static final int MAX_CHARACTERS_CHECKED_FOR_PREVIEW = 8192; - public static String extractPreview(Context context, Message message) throws MessagingException { - try { - List attachments = new ArrayList(); - List viewables = MessageExtractor.getViewables(message, attachments); - - return buildPreview(context, viewables); - } catch (Exception e) { - throw new MessagingException("Couldn't extract viewable parts", e); - } - } - - private static String buildPreview(Context context, List viewables) throws MessagingException { + public static String extractPreview(Context context, List viewables) { StringBuilder text = new StringBuilder(); boolean divider = false;