am adbd22c9: Merge "Infer attachment\'s mime type if it\'s text/plain" into honeycomb

* commit 'adbd22c9bed1e45e26130f32a684aa29676537dc':
  Infer attachment's mime type if it's text/plain
This commit is contained in:
Todd Kennedy 2011-01-25 10:40:08 -08:00 committed by Android Git Automerger
commit b82239c192
2 changed files with 10 additions and 3 deletions

View File

@ -201,13 +201,13 @@ public class AttachmentProvider extends ContentProvider {
// NOTE mime-types are case-*sensitive* on Android. // NOTE mime-types are case-*sensitive* on Android.
// Return values from this method MUST always in lowercase. // Return values from this method MUST always in lowercase.
String result = null; String result = null;
if (fileName != null && fileName.toLowerCase().endsWith(".eml")) { if (fileName != null && fileName.toLowerCase().endsWith(".eml")) {
result = "message/rfc822"; result = "message/rfc822";
} else { } else {
// If the given mime type appears to be non-empty and non-generic - return it // 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) && if (!TextUtils.isEmpty(mimeType) &&
!"application/octet-stream".equalsIgnoreCase(mimeType)) { !"application/octet-stream".equalsIgnoreCase(mimeType) && !isTextPlain) {
result = mimeType; result = mimeType;
} else { } else {
// Try to find an extension in the filename // 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. // Extension found. Look up mime type, or synthesize if none found.
result = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); result = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
if (TextUtils.isEmpty(result)) { if (TextUtils.isEmpty(result)) {
result = "application/" + extension; // If original mimetype is text/plain, us it; otherwise synthesize
result = isTextPlain ? mimeType : "application/" + extension;
} }
} }
} }

View File

@ -366,6 +366,7 @@ public class AttachmentProviderTests extends ProviderTestCase2<AttachmentProvide
public void testInferMimeType() { public void testInferMimeType() {
final String DEFAULT_MIX = "Application/Octet-stream"; final String DEFAULT_MIX = "Application/Octet-stream";
final String DEFAULT_LOWER = DEFAULT_MIX.toLowerCase(); final String DEFAULT_LOWER = DEFAULT_MIX.toLowerCase();
final String TEXT_PLAIN = "text/plain";
final String FILE_PDF = "myfile.false.pDf"; final String FILE_PDF = "myfile.false.pDf";
final String FILE_ABC = "myfile.false.aBc"; final String FILE_ABC = "myfile.false.aBc";
final String FILE_NO_EXT = "myfile"; final String FILE_NO_EXT = "myfile";
@ -393,6 +394,11 @@ public class AttachmentProviderTests extends ProviderTestCase2<AttachmentProvide
assertEquals(DEFAULT_LOWER, AttachmentProvider.inferMimeType(null, null)); assertEquals(DEFAULT_LOWER, AttachmentProvider.inferMimeType(null, null));
assertEquals(DEFAULT_LOWER, AttachmentProvider.inferMimeType("", "")); assertEquals(DEFAULT_LOWER, AttachmentProvider.inferMimeType("", ""));
// If mime type can be inferred, return it; otherwise return text/plain
assertEquals("application/pdf", AttachmentProvider.inferMimeType(FILE_PDF, TEXT_PLAIN));
assertEquals(DEFAULT_LOWER, AttachmentProvider.inferMimeType(FILE_NO_EXT, TEXT_PLAIN));
assertEquals(TEXT_PLAIN, AttachmentProvider.inferMimeType(FILE_ABC, TEXT_PLAIN));
// Test for eml files. // Test for eml files.
assertEquals("message/rfc822", AttachmentProvider.inferMimeType("a.eMl", "Text/Plain")); assertEquals("message/rfc822", AttachmentProvider.inferMimeType("a.eMl", "Text/Plain"));
assertEquals("message/rfc822", AttachmentProvider.inferMimeType("a.eml", DEFAULT_MIX)); assertEquals("message/rfc822", AttachmentProvider.inferMimeType("a.eml", DEFAULT_MIX));