Fetch mime type from file name when format code does not help.

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
This commit is contained in:
Daichi Hirono
2016-02-05 17:21:13 +09:00
parent 9640477e3c
commit 6213cefbc0
2 changed files with 18 additions and 3 deletions

View 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++) {

View 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);