1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-23 18:02:15 -05:00

Save attachment count

This commit is contained in:
cketti 2015-01-22 06:05:05 +01:00
parent 5e4743bf66
commit d2d85393d3
4 changed files with 56 additions and 27 deletions

View File

@ -17,7 +17,7 @@ import static org.junit.Assert.assertEquals;
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
public class MessagePreviewExtractorTest { public class MessageInfoExtractorTest {
@Test @Test
public void shouldExtractPreviewFromSinglePlainTextPart() throws MessagingException { public void shouldExtractPreviewFromSinglePlainTextPart() throws MessagingException {
@ -26,7 +26,7 @@ public class MessagePreviewExtractorTest {
TextBody body = new TextBody("Message text "); TextBody body = new TextBody("Message text ");
message.setBody(body); message.setBody(body);
String preview = MessagePreviewExtractor.extractPreview(getContext(), message); String preview = new MessageInfoExtractor(getContext(), message).getMessageTextPreview();
assertEquals("Message text", preview); assertEquals("Message text", preview);
} }
@ -49,7 +49,7 @@ public class MessagePreviewExtractorTest {
"560-------570-------580-------590-------600-------"); "560-------570-------580-------590-------600-------");
message.setBody(body); message.setBody(body);
String preview = MessagePreviewExtractor.extractPreview(getContext(), message); String preview = new MessageInfoExtractor(getContext(), message).getMessageTextPreview();
assertEquals(512, preview.length()); assertEquals(512, preview.length());
assertEquals('…', preview.charAt(511)); assertEquals('…', preview.charAt(511));
@ -62,7 +62,7 @@ public class MessagePreviewExtractorTest {
TextBody body = new TextBody("<html><body><pre>Message text</pre></body></html>"); TextBody body = new TextBody("<html><body><pre>Message text</pre></body></html>");
message.setBody(body); message.setBody(body);
String preview = MessagePreviewExtractor.extractPreview(getContext(), message); String preview = new MessageInfoExtractor(getContext(), message).getMessageTextPreview();
assertEquals("Message text", preview); assertEquals("Message text", preview);
} }
@ -83,7 +83,7 @@ public class MessagePreviewExtractorTest {
MimeBodyPart htmlPart = new MimeBodyPart(htmlBody, "text/html"); MimeBodyPart htmlPart = new MimeBodyPart(htmlBody, "text/html");
multipart.addBodyPart(htmlPart); multipart.addBodyPart(htmlPart);
String preview = MessagePreviewExtractor.extractPreview(getContext(), message); String preview = new MessageInfoExtractor(getContext(), message).getMessageTextPreview();
assertEquals("text", preview); assertEquals("text", preview);
} }
@ -104,7 +104,7 @@ public class MessagePreviewExtractorTest {
MimeBodyPart htmlPart = new MimeBodyPart(htmlBody, "text/html"); MimeBodyPart htmlPart = new MimeBodyPart(htmlBody, "text/html");
multipart.addBodyPart(htmlPart); multipart.addBodyPart(htmlPart);
String preview = MessagePreviewExtractor.extractPreview(getContext(), message); String preview = new MessageInfoExtractor(getContext(), message).getMessageTextPreview();
assertEquals("text / html", preview); assertEquals("text / html", preview);
} }
@ -130,7 +130,7 @@ public class MessagePreviewExtractorTest {
MimeBodyPart messagePart = new MimeBodyPart(innerMessage, "message/rfc822"); MimeBodyPart messagePart = new MimeBodyPart(innerMessage, "message/rfc822");
multipart.addBodyPart(messagePart); 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); assertEquals("text / Includes message titled \"inner message\" containing: html", preview);
} }

View File

@ -1247,10 +1247,10 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
parentId = threadInfo.parentId; parentId = threadInfo.parentId;
} }
//TODO: get attachment count
try { 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); long rootMessagePartId = saveMessageParts(db, message);
@ -1273,7 +1273,7 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
cv.put("bcc_list", Address.pack(message.getRecipients(RecipientType.BCC))); cv.put("bcc_list", Address.pack(message.getRecipients(RecipientType.BCC)));
cv.put("preview", preview); cv.put("preview", preview);
cv.put("reply_to_list", Address.pack(message.getReplyTo())); 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 cv.put("internal_date", message.getInternalDate() == null
? System.currentTimeMillis() : message.getInternalDate().getTime()); ? System.currentTimeMillis() : message.getInternalDate().getTime());
cv.put("mime_type", message.getMimeType()); cv.put("mime_type", message.getMimeType());

View File

@ -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<Viewable> viewables;
private List<Part> 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<Part>();
viewables = MessageExtractor.getViewables(message, attachments);
}
}
}

View File

@ -1,7 +1,6 @@
package com.fsck.k9.mailstore; package com.fsck.k9.mailstore;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import android.content.Context; import android.content.Context;
@ -9,8 +8,6 @@ import android.text.TextUtils;
import com.fsck.k9.R; import com.fsck.k9.R;
import com.fsck.k9.helper.HtmlConverter; 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.Part;
import com.fsck.k9.mail.internet.MessageExtractor; import com.fsck.k9.mail.internet.MessageExtractor;
import com.fsck.k9.mail.internet.Viewable; 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; 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_PREVIEW_LENGTH = 512;
private static final int MAX_CHARACTERS_CHECKED_FOR_PREVIEW = 8192; private static final int MAX_CHARACTERS_CHECKED_FOR_PREVIEW = 8192;
public static String extractPreview(Context context, Message message) throws MessagingException { public static String extractPreview(Context context, List<Viewable> viewables) {
try {
List<Part> attachments = new ArrayList<Part>();
List<Viewable> 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<Viewable> viewables) throws MessagingException {
StringBuilder text = new StringBuilder(); StringBuilder text = new StringBuilder();
boolean divider = false; boolean divider = false;