am 04cf5e4b
: Merge "AttachmentProvider: Always return mime-types in lowercase" into honeycomb
* commit '04cf5e4bdf091f490f2e3bfb40f80bab44a05e25': AttachmentProvider: Always return mime-types in lowercase
This commit is contained in:
commit
2aee3a1002
@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user