From 34212cc7df317a058a3f9860fc0e7de508cb7120 Mon Sep 17 00:00:00 2001 From: Todd Kennedy Date: Tue, 25 Jan 2011 08:26:27 -0800 Subject: [PATCH] Infer attachment's mime type if it's text/plain If the server reports an attachment's mime type as either text/plain or application/octet-stream, we will try to infer the real mime type using the attachment's extension. If one cannot be found, we either synthesize it (if original mime type is application-octet-stream), or, we use the server specified mime type (if the original is text/plain). bug 3379416 Change-Id: I331e767ed36e4e17756025cc816bdb7b5a8f0868 --- src/com/android/email/provider/AttachmentProvider.java | 7 ++++--- .../android/email/provider/AttachmentProviderTests.java | 6 ++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/com/android/email/provider/AttachmentProvider.java b/src/com/android/email/provider/AttachmentProvider.java index f020d1627..71a20f427 100644 --- a/src/com/android/email/provider/AttachmentProvider.java +++ b/src/com/android/email/provider/AttachmentProvider.java @@ -201,13 +201,13 @@ public class AttachmentProvider extends ContentProvider { // NOTE mime-types are case-*sensitive* on Android. // Return values from this method MUST always in lowercase. String result = null; - if (fileName != null && fileName.toLowerCase().endsWith(".eml")) { result = "message/rfc822"; } else { // If the given mime type appears to be non-empty and non-generic - return it + boolean isTextPlain = "text/plain".equalsIgnoreCase(mimeType); if (!TextUtils.isEmpty(mimeType) && - !"application/octet-stream".equalsIgnoreCase(mimeType)) { + !"application/octet-stream".equalsIgnoreCase(mimeType) && !isTextPlain) { result = mimeType; } else { // Try to find an extension in the filename @@ -217,7 +217,8 @@ public class AttachmentProvider extends ContentProvider { // Extension found. Look up mime type, or synthesize if none found. result = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); if (TextUtils.isEmpty(result)) { - result = "application/" + extension; + // If original mimetype is text/plain, us it; otherwise synthesize + result = isTextPlain ? mimeType : "application/" + extension; } } } diff --git a/tests/src/com/android/email/provider/AttachmentProviderTests.java b/tests/src/com/android/email/provider/AttachmentProviderTests.java index 7a2cf43db..9a830dbe8 100644 --- a/tests/src/com/android/email/provider/AttachmentProviderTests.java +++ b/tests/src/com/android/email/provider/AttachmentProviderTests.java @@ -366,6 +366,7 @@ public class AttachmentProviderTests extends ProviderTestCase2