From 0aa03ccdc42043c7c7752883c31166ff2679c541 Mon Sep 17 00:00:00 2001 From: cketti Date: Fri, 25 Mar 2011 00:37:53 +0100 Subject: [PATCH] Extract MIME type fixup code to method MimeUtility.getMimeTypeForViewing() --- .../fsck/k9/mail/internet/MimeUtility.java | 20 +++++++++++++++++++ .../fsck/k9/provider/AttachmentProvider.java | 11 +--------- src/com/fsck/k9/view/AttachmentView.java | 5 +---- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/com/fsck/k9/mail/internet/MimeUtility.java b/src/com/fsck/k9/mail/internet/MimeUtility.java index 7eb4fa2ed..24f9da2da 100644 --- a/src/com/fsck/k9/mail/internet/MimeUtility.java +++ b/src/com/fsck/k9/mail/internet/MimeUtility.java @@ -1220,6 +1220,26 @@ public class MimeUtility { return mimeType; } + /** + * When viewing the attachment we want the MIME type to be as sensible as + * possible. So we fix it up if necessary. + * + * @param mimeType The original MIME type of the attachment. + * @param name The (file)name of the attachment. + * + * @return The best MIME type we can come up with. + */ + public static String getMimeTypeForViewing(String mimeType, String name) { + if (DEFAULT_ATTACHMENT_MIME_TYPE.equalsIgnoreCase(mimeType)) { + // If the MIME type is the generic "application/octet-stream" + // we try to find a better one by looking at the file extension. + return getMimeTypeByExtension(name); + } else { + // Some messages contain wrong MIME types. See if we know better. + return canonicalizeMimeType(mimeType); + } + } + private static Message getMessageFromPart(Part part) { while (part != null) { if (part instanceof Message) diff --git a/src/com/fsck/k9/provider/AttachmentProvider.java b/src/com/fsck/k9/provider/AttachmentProvider.java index a2f466b10..126223d72 100644 --- a/src/com/fsck/k9/provider/AttachmentProvider.java +++ b/src/com/fsck/k9/provider/AttachmentProvider.java @@ -118,16 +118,7 @@ public class AttachmentProvider extends ContentProvider { AttachmentInfo attachmentInfo = localStore.getAttachmentInfo(id); if (FORMAT_VIEW.equals(format)) { - // When viewing the attachment we want the MIME type to be - // as sensible as possible. So we fix it up if necessary. - if (MimeUtility.DEFAULT_ATTACHMENT_MIME_TYPE.equalsIgnoreCase(attachmentInfo.type)) { - // If the MIME type is the generic "application/octet-stream" - // we try to find a better one by looking at the file extension. - return MimeUtility.getMimeTypeByExtension(attachmentInfo.name); - } else { - // Some messages contain wrong MIME types. See if we know better. - return MimeUtility.canonicalizeMimeType(attachmentInfo.type); - } + return MimeUtility.getMimeTypeForViewing(attachmentInfo.type, attachmentInfo.name); } else { // When accessing the "raw" message we deliver the original MIME type. return attachmentInfo.type; diff --git a/src/com/fsck/k9/view/AttachmentView.java b/src/com/fsck/k9/view/AttachmentView.java index ef911c897..1fff23319 100644 --- a/src/com/fsck/k9/view/AttachmentView.java +++ b/src/com/fsck/k9/view/AttachmentView.java @@ -78,10 +78,7 @@ public class AttachmentView extends FrameLayout { mListener = listener; size = Integer.parseInt(MimeUtility.getHeaderParameter(contentDisposition, "size")); - contentType = part.getMimeType(); - if (MimeUtility.DEFAULT_ATTACHMENT_MIME_TYPE.equalsIgnoreCase(contentType)) { - contentType = MimeUtility.getMimeTypeByExtension(name); - } + contentType = MimeUtility.getMimeTypeForViewing(part.getMimeType(), name); TextView attachmentName = (TextView) findViewById(R.id.attachment_name); TextView attachmentInfo = (TextView) findViewById(R.id.attachment_info); ImageView attachmentIcon = (ImageView) findViewById(R.id.attachment_icon);