From 6833fd5ab6c1e23c0bf2005dec2950796a040781 Mon Sep 17 00:00:00 2001 From: Makoto Onuki Date: Fri, 21 Jan 2011 14:49:39 -0800 Subject: [PATCH] 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 --- .../email/provider/AttachmentProvider.java | 48 +++++++++++-------- .../provider/AttachmentProviderTests.java | 33 +++++++------ 2 files changed, 46 insertions(+), 35 deletions(-) diff --git a/src/com/android/email/provider/AttachmentProvider.java b/src/com/android/email/provider/AttachmentProvider.java index 8f85a57e1..f020d1627 100644 --- a/src/com/android/email/provider/AttachmentProvider.java +++ b/src/com/android/email/provider/AttachmentProvider.java @@ -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(); } /** diff --git a/tests/src/com/android/email/provider/AttachmentProviderTests.java b/tests/src/com/android/email/provider/AttachmentProviderTests.java index 5286be9ed..66cad9617 100644 --- a/tests/src/com/android/email/provider/AttachmentProviderTests.java +++ b/tests/src/com/android/email/provider/AttachmentProviderTests.java @@ -277,39 +277,42 @@ public class AttachmentProviderTests extends ProviderTestCase2