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

Extract MIME type fixup code to method MimeUtility.getMimeTypeForViewing()

This commit is contained in:
cketti 2011-03-25 00:37:53 +01:00
parent 522634fba7
commit 0aa03ccdc4
3 changed files with 22 additions and 14 deletions

View File

@ -1220,6 +1220,26 @@ public class MimeUtility {
return mimeType; 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) { private static Message getMessageFromPart(Part part) {
while (part != null) { while (part != null) {
if (part instanceof Message) if (part instanceof Message)

View File

@ -118,16 +118,7 @@ public class AttachmentProvider extends ContentProvider {
AttachmentInfo attachmentInfo = localStore.getAttachmentInfo(id); AttachmentInfo attachmentInfo = localStore.getAttachmentInfo(id);
if (FORMAT_VIEW.equals(format)) { if (FORMAT_VIEW.equals(format)) {
// When viewing the attachment we want the MIME type to be return MimeUtility.getMimeTypeForViewing(attachmentInfo.type, attachmentInfo.name);
// 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);
}
} else { } else {
// When accessing the "raw" message we deliver the original MIME type. // When accessing the "raw" message we deliver the original MIME type.
return attachmentInfo.type; return attachmentInfo.type;

View File

@ -78,10 +78,7 @@ public class AttachmentView extends FrameLayout {
mListener = listener; mListener = listener;
size = Integer.parseInt(MimeUtility.getHeaderParameter(contentDisposition, "size")); size = Integer.parseInt(MimeUtility.getHeaderParameter(contentDisposition, "size"));
contentType = part.getMimeType(); contentType = MimeUtility.getMimeTypeForViewing(part.getMimeType(), name);
if (MimeUtility.DEFAULT_ATTACHMENT_MIME_TYPE.equalsIgnoreCase(contentType)) {
contentType = MimeUtility.getMimeTypeByExtension(name);
}
TextView attachmentName = (TextView) findViewById(R.id.attachment_name); TextView attachmentName = (TextView) findViewById(R.id.attachment_name);
TextView attachmentInfo = (TextView) findViewById(R.id.attachment_info); TextView attachmentInfo = (TextView) findViewById(R.id.attachment_info);
ImageView attachmentIcon = (ImageView) findViewById(R.id.attachment_icon); ImageView attachmentIcon = (ImageView) findViewById(R.id.attachment_icon);