OSDN Git Service

Fetch mime type from file name when format code does not help.
authorDaichi Hirono <hirono@google.com>
Fri, 5 Feb 2016 08:21:13 +0000 (17:21 +0900)
committerDaichi Hirono <hirono@google.com>
Fri, 5 Feb 2016 08:21:13 +0000 (17:21 +0900)
MTP spec defines format code as a file type of object, but we don't have
format code for some file types like PDF. This CL adds fallback that
tries to obtain mime type from file name in such case.

BUG=27004957

Change-Id: Id61352bf4726f4e044e57edadcefbf179fe3f882

packages/MtpDocumentsProvider/src/com/android/mtp/MtpDatabase.java
packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsProvider.java

index b44f9af..8a3ebef 100644 (file)
@@ -577,9 +577,7 @@ class MtpDatabase {
     static void getObjectDocumentValues(
             ContentValues values, int deviceId, String parentId, MtpObjectInfo info) {
         values.clear();
-        final String mimeType = info.getFormat() == MtpConstants.FORMAT_ASSOCIATION ?
-                DocumentsContract.Document.MIME_TYPE_DIR :
-                MediaFile.getMimeTypeForFormatCode(info.getFormat());
+        final String mimeType = getMimeType(info);
         int flag = 0;
         if (info.getProtectionStatus() == 0) {
             flag |= Document.FLAG_SUPPORTS_DELETE |
@@ -608,6 +606,17 @@ class MtpDatabase {
         values.put(Document.COLUMN_SIZE, info.getCompressedSize());
     }
 
+    private static String getMimeType(MtpObjectInfo info) {
+        if (info.getFormat() == MtpConstants.FORMAT_ASSOCIATION) {
+            return DocumentsContract.Document.MIME_TYPE_DIR;
+        }
+        final String formatCodeMimeType = MediaFile.getMimeTypeForFormatCode(info.getFormat());
+        if (formatCodeMimeType != null) {
+            return formatCodeMimeType;
+        }
+        return MediaFile.getMimeTypeForFile(info.getName());
+    }
+
     static String[] strings(Object... args) {
         final String[] results = new String[args.length];
         for (int i = 0; i < args.length; i++) {
index 70a1aae..0338454 100644 (file)
@@ -184,6 +184,9 @@ public class MtpDocumentsProvider extends DocumentsProvider {
     public ParcelFileDescriptor openDocument(
             String documentId, String mode, CancellationSignal signal)
                     throws FileNotFoundException {
+        if (DEBUG) {
+            Log.d(TAG, "openDocument: " + documentId);
+        }
         final Identifier identifier = mDatabase.createIdentifier(documentId);
         try {
             openDevice(identifier.mDeviceId);
@@ -270,6 +273,9 @@ public class MtpDocumentsProvider extends DocumentsProvider {
     @Override
     public String createDocument(String parentDocumentId, String mimeType, String displayName)
             throws FileNotFoundException {
+        if (DEBUG) {
+            Log.d(TAG, "createDocument: " + displayName);
+        }
         try {
             final Identifier parentId = mDatabase.createIdentifier(parentDocumentId);
             openDevice(parentId.mDeviceId);