Extract MIME type fixup code to method MimeUtility.getMimeTypeForViewing()
This commit is contained in:
parent
522634fba7
commit
0aa03ccdc4
3 changed files with 22 additions and 14 deletions
|
@ -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)
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue