AttachmentProvider: Always return mime-types in lowercase

Intent-filter's mime-type matching is case-sensitive.  We should always use
lowercase letters.

Bug 3375709

Change-Id: Idd4abb41f94c816a5b9150aef5859dd75487a042
This commit is contained in:
Makoto Onuki 2011-01-21 14:49:39 -08:00
parent d5c6717eeb
commit 6833fd5ab6
2 changed files with 46 additions and 35 deletions

View File

@ -196,31 +196,39 @@ public class AttachmentProvider extends ContentProvider {
* @param mimeType The given mime type
* @return A likely mime type for the attachment
*/
public static String inferMimeType(String fileName, String mimeType) {
if (fileName != null && fileName.toLowerCase().endsWith(".eml")) {
return "message/rfc822";
}
// If the given mime type appears to be non-empty and non-generic - return it
if (!TextUtils.isEmpty(mimeType) &&
!"application/octet-stream".equalsIgnoreCase(mimeType)) {
return mimeType;
}
public static String inferMimeType(final String fileName, final String mimeType) {
// Try to find an extension in the filename
if (!TextUtils.isEmpty(fileName)) {
String extension = getFilenameExtension(fileName);
if (!TextUtils.isEmpty(extension)) {
// Extension found. Look up mime type, or synthesize if none found.
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
if (mimeType == null) {
mimeType = "application/" + extension;
// 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
if (!TextUtils.isEmpty(mimeType) &&
!"application/octet-stream".equalsIgnoreCase(mimeType)) {
result = mimeType;
} else {
// Try to find an extension in the filename
if (!TextUtils.isEmpty(fileName)) {
String extension = getFilenameExtension(fileName);
if (!TextUtils.isEmpty(extension)) {
// Extension found. Look up mime type, or synthesize if none found.
result = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
if (TextUtils.isEmpty(result)) {
result = "application/" + extension;
}
}
}
return mimeType;
}
}
// Fallback case - no good guess could be made.
return "application/octet-stream";
if (TextUtils.isEmpty(result)) {
// Fallback case - no good guess could be made.
result = "application/octet-stream";
}
return result.toLowerCase();
}
/**

View File

@ -277,39 +277,42 @@ public class AttachmentProviderTests extends ProviderTestCase2<AttachmentProvide
* If the filename has a recognizable extension and it converts to a mime type, return that.
* If the filename has an unrecognized extension, return "application/extension"
* Otherwise return "application/octet-stream".
*
* Also, all results should be in lowercase.
*/
public void testInferMimeType() {
final String DEFAULT = "application/octet-stream";
final String FILE_PDF = "myfile.false.pdf";
final String FILE_ABC = "myfile.false.abc";
final String DEFAULT_MIX = "Application/Octet-stream";
final String DEFAULT_LOWER = DEFAULT_MIX.toLowerCase();
final String FILE_PDF = "myfile.false.pDf";
final String FILE_ABC = "myfile.false.aBc";
final String FILE_NO_EXT = "myfile";
// If the given mime type is non-empty and anything other than "application/octet-stream",
// just return it. (This is the most common case.)
assertEquals("mime/type", AttachmentProvider.inferMimeType(null, "mime/type"));
assertEquals("mime/type", AttachmentProvider.inferMimeType("", "mime/type"));
assertEquals("mime/type", AttachmentProvider.inferMimeType(FILE_PDF, "mime/type"));
assertEquals("mime/type", AttachmentProvider.inferMimeType(null, "Mime/TyPe"));
assertEquals("mime/type", AttachmentProvider.inferMimeType("", "Mime/TyPe"));
assertEquals("mime/type", AttachmentProvider.inferMimeType(FILE_PDF, "Mime/TyPe"));
// If the filename has a recognizable extension and it converts to a mime type, return that.
assertEquals("application/pdf", AttachmentProvider.inferMimeType(FILE_PDF, null));
assertEquals("application/pdf", AttachmentProvider.inferMimeType(FILE_PDF, ""));
assertEquals("application/pdf", AttachmentProvider.inferMimeType(FILE_PDF, DEFAULT));
assertEquals("application/pdf", AttachmentProvider.inferMimeType(FILE_PDF, DEFAULT_MIX));
// If the filename has an unrecognized extension, return "application/extension"
assertEquals("application/abc", AttachmentProvider.inferMimeType(FILE_ABC, null));
assertEquals("application/abc", AttachmentProvider.inferMimeType(FILE_ABC, ""));
assertEquals("application/abc", AttachmentProvider.inferMimeType(FILE_ABC, DEFAULT));
assertEquals("application/abc", AttachmentProvider.inferMimeType(FILE_ABC, DEFAULT_MIX));
// Otherwise return "application/octet-stream".
assertEquals(DEFAULT, AttachmentProvider.inferMimeType(FILE_NO_EXT, null));
assertEquals(DEFAULT, AttachmentProvider.inferMimeType(FILE_NO_EXT, ""));
assertEquals(DEFAULT, AttachmentProvider.inferMimeType(FILE_NO_EXT, DEFAULT));
assertEquals(DEFAULT, AttachmentProvider.inferMimeType(null, null));
assertEquals(DEFAULT, AttachmentProvider.inferMimeType("", ""));
assertEquals(DEFAULT_LOWER, AttachmentProvider.inferMimeType(FILE_NO_EXT, null));
assertEquals(DEFAULT_LOWER, AttachmentProvider.inferMimeType(FILE_NO_EXT, ""));
assertEquals(DEFAULT_LOWER, AttachmentProvider.inferMimeType(FILE_NO_EXT, DEFAULT_MIX));
assertEquals(DEFAULT_LOWER, AttachmentProvider.inferMimeType(null, null));
assertEquals(DEFAULT_LOWER, AttachmentProvider.inferMimeType("", ""));
// Test for eml files.
assertEquals("message/rfc822", AttachmentProvider.inferMimeType("a.eMl", "text/plain"));
assertEquals("message/rfc822", AttachmentProvider.inferMimeType("a.eml", DEFAULT));
assertEquals("message/rfc822", AttachmentProvider.inferMimeType("a.eMl", "Text/Plain"));
assertEquals("message/rfc822", AttachmentProvider.inferMimeType("a.eml", DEFAULT_MIX));
}
/**