Remove MtpDocument.
Change-Id: Ie4b0e55f6ff2c7b323cf767a5ed3d3c61c12734d
diff --git a/packages/MtpDocumentsProvider/src/com/android/mtp/CursorHelper.java b/packages/MtpDocumentsProvider/src/com/android/mtp/CursorHelper.java
new file mode 100644
index 0000000..48d5dcf
--- /dev/null
+++ b/packages/MtpDocumentsProvider/src/com/android/mtp/CursorHelper.java
@@ -0,0 +1,99 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.mtp;
+
+import android.database.MatrixCursor;
+import android.mtp.MtpObjectInfo;
+import android.provider.DocumentsContract;
+import android.provider.DocumentsContract.Document;
+
+import java.util.Date;
+
+final class CursorHelper {
+ static final int DUMMY_HANDLE_FOR_ROOT = 0;
+
+ private CursorHelper() {
+ }
+
+ static void addToCursor(MtpRoot root, MatrixCursor.RowBuilder builder) {
+ final Identifier identifier = new Identifier(
+ root.mDeviceId, root.mStorageId, DUMMY_HANDLE_FOR_ROOT);
+ builder.add(Document.COLUMN_DOCUMENT_ID, identifier.toDocumentId());
+ builder.add(Document.COLUMN_DISPLAY_NAME, root.mDescription);
+ builder.add(Document.COLUMN_MIME_TYPE, DocumentsContract.Document.MIME_TYPE_DIR);
+ builder.add(Document.COLUMN_LAST_MODIFIED, null);
+ builder.add(Document.COLUMN_FLAGS, 0);
+ builder.add(Document.COLUMN_SIZE,
+ (int) Math.min(root.mMaxCapacity - root.mFreeSpace, Integer.MAX_VALUE));
+ }
+
+ static void addToCursor(MtpObjectInfo objectInfo, Identifier rootIdentifier,
+ MatrixCursor.RowBuilder builder) {
+ final Identifier identifier = new Identifier(
+ rootIdentifier.mDeviceId, rootIdentifier.mStorageId, objectInfo.getObjectHandle());
+ final String mimeType = formatTypeToMimeType(objectInfo.getFormat());
+
+ int flag = 0;
+ if (objectInfo.getProtectionStatus() == 0) {
+ flag |= DocumentsContract.Document.FLAG_SUPPORTS_DELETE |
+ DocumentsContract.Document.FLAG_SUPPORTS_WRITE;
+ if (mimeType == DocumentsContract.Document.MIME_TYPE_DIR) {
+ flag |= DocumentsContract.Document.FLAG_DIR_SUPPORTS_CREATE;
+ }
+ }
+ if (objectInfo.getThumbCompressedSize() > 0) {
+ flag |= DocumentsContract.Document.FLAG_SUPPORTS_THUMBNAIL;
+ }
+
+ builder.add(Document.COLUMN_DOCUMENT_ID, identifier.toDocumentId());
+ builder.add(Document.COLUMN_DISPLAY_NAME, objectInfo.getName());
+ builder.add(Document.COLUMN_MIME_TYPE, mimeType);
+ builder.add(
+ Document.COLUMN_LAST_MODIFIED,
+ objectInfo.getDateModified() != 0 ? objectInfo.getDateModified() : null);
+ builder.add(Document.COLUMN_FLAGS, flag);
+ builder.add(Document.COLUMN_SIZE, objectInfo.getCompressedSize());
+ }
+
+ static String formatTypeToMimeType(int format) {
+ // TODO: Add complete list of mime types.
+ switch (format) {
+ case 0x3001:
+ return DocumentsContract.Document.MIME_TYPE_DIR;
+ case 0x3009:
+ return "audio/mp3";
+ case 0x3801:
+ return "image/jpeg";
+ default:
+ return "application/octet-stream";
+ }
+ }
+
+ static int mimeTypeToFormatType(String mimeType) {
+ // TODO: Add complete list of mime types.
+ switch (mimeType.toLowerCase()) {
+ case Document.MIME_TYPE_DIR:
+ return 0x3001;
+ case "audio/mp3":
+ return 0x3009;
+ case "image/jpeg":
+ return 0x3801;
+ default:
+ return 0x3000; // Undefined object.
+ }
+ }
+}
diff --git a/packages/MtpDocumentsProvider/src/com/android/mtp/DocumentLoader.java b/packages/MtpDocumentsProvider/src/com/android/mtp/DocumentLoader.java
index c430def..eef173c 100644
--- a/packages/MtpDocumentsProvider/src/com/android/mtp/DocumentLoader.java
+++ b/packages/MtpDocumentsProvider/src/com/android/mtp/DocumentLoader.java
@@ -19,6 +19,7 @@
import android.content.ContentResolver;
import android.database.Cursor;
import android.database.MatrixCursor;
+import android.mtp.MtpObjectInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Process;
@@ -34,6 +35,7 @@
* Loader for MTP document.
* At the first request, the loader returns only first NUM_INITIAL_ENTRIES. Then it launches
* background thread to load the rest documents and caches its result for next requests.
+ * TODO: Rename this class to ObjectInfoLoader
*/
class DocumentLoader {
static final int NUM_INITIAL_ENTRIES = 10;
@@ -50,13 +52,13 @@
mResolver = resolver;
}
- private static MtpDocument[] loadDocuments(MtpManager manager, int deviceId, int[] handles)
+ private static MtpObjectInfo[] loadDocuments(MtpManager manager, int deviceId, int[] handles)
throws IOException {
- final MtpDocument[] documents = new MtpDocument[handles.length];
+ final MtpObjectInfo[] objectInfos = new MtpObjectInfo[handles.length];
for (int i = 0; i < handles.length; i++) {
- documents[i] = manager.getDocument(deviceId, handles[i]);
+ objectInfos[i] = manager.getObjectInfo(deviceId, handles[i]);
}
- return documents;
+ return objectInfos;
}
synchronized Cursor queryChildDocuments(String[] columnNames, Identifier parent)
@@ -66,7 +68,7 @@
int parentHandle = parent.mObjectHandle;
// Need to pass the special value MtpManager.OBJECT_HANDLE_ROOT_CHILDREN to
// getObjectHandles if we would like to obtain children under the root.
- if (parentHandle == MtpDocument.DUMMY_HANDLE_FOR_ROOT) {
+ if (parentHandle == CursorHelper.DUMMY_HANDLE_FOR_ROOT) {
parentHandle = MtpManager.OBJECT_HANDLE_ROOT_CHILDREN;
}
task = new LoaderTask(parent, mMtpManager.getObjectHandles(
@@ -114,16 +116,16 @@
deviceId = task.mIdentifier.mDeviceId;
handles = task.getUnloadedObjectHandles(NUM_LOADING_ENTRIES);
}
- MtpDocument[] documents;
+ MtpObjectInfo[] objectInfos;
try {
- documents = loadDocuments(mMtpManager, deviceId, handles);
+ objectInfos = loadDocuments(mMtpManager, deviceId, handles);
} catch (IOException exception) {
- documents = null;
+ objectInfos = null;
Log.d(MtpDocumentsProvider.TAG, exception.getMessage());
}
synchronized (DocumentLoader.this) {
- if (documents != null) {
- task.fillDocuments(documents);
+ if (objectInfos != null) {
+ task.fillDocuments(objectInfos);
final boolean shouldNotify =
task.mLastNotified.getTime() <
new Date().getTime() - NOTIFY_PERIOD_MS ||
@@ -182,14 +184,14 @@
private static class LoaderTask {
final Identifier mIdentifier;
final int[] mObjectHandles;
- final MtpDocument[] mDocuments;
+ final MtpObjectInfo[] mObjectInfos;
Date mLastNotified;
int mNumLoaded;
LoaderTask(Identifier identifier, int[] objectHandles) {
mIdentifier = identifier;
mObjectHandles = objectHandles;
- mDocuments = new MtpDocument[mObjectHandles.length];
+ mObjectInfos = new MtpObjectInfo[mObjectHandles.length];
mNumLoaded = 0;
mLastNotified = new Date();
}
@@ -199,7 +201,7 @@
final Identifier rootIdentifier = new Identifier(
mIdentifier.mDeviceId, mIdentifier.mStorageId);
for (int i = 0; i < mNumLoaded; i++) {
- mDocuments[i].addToCursor(rootIdentifier, cursor.newRow());
+ CursorHelper.addToCursor(mObjectInfos[i], rootIdentifier, cursor.newRow());
}
final Bundle extras = new Bundle();
extras.putBoolean(DocumentsContract.EXTRA_LOADING, !completed());
@@ -209,7 +211,7 @@
}
boolean completed() {
- return mNumLoaded == mDocuments.length;
+ return mNumLoaded == mObjectInfos.length;
}
int[] getUnloadedObjectHandles(int count) {
@@ -224,9 +226,9 @@
mLastNotified = new Date();
}
- void fillDocuments(MtpDocument[] documents) {
- for (int i = 0; i < documents.length; i++) {
- mDocuments[mNumLoaded++] = documents[i];
+ void fillDocuments(MtpObjectInfo[] objectInfos) {
+ for (int i = 0; i < objectInfos.length; i++) {
+ mObjectInfos[mNumLoaded++] = objectInfos[i];
}
}
diff --git a/packages/MtpDocumentsProvider/src/com/android/mtp/Identifier.java b/packages/MtpDocumentsProvider/src/com/android/mtp/Identifier.java
index bd0c837..ae29f52 100644
--- a/packages/MtpDocumentsProvider/src/com/android/mtp/Identifier.java
+++ b/packages/MtpDocumentsProvider/src/com/android/mtp/Identifier.java
@@ -41,7 +41,7 @@
Identifier(int deviceId, int storageId) {
- this(deviceId, storageId, MtpDocument.DUMMY_HANDLE_FOR_ROOT);
+ this(deviceId, storageId, CursorHelper.DUMMY_HANDLE_FOR_ROOT);
}
Identifier(int deviceId, int storageId, int objectHandle) {
diff --git a/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocument.java b/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocument.java
deleted file mode 100644
index c1d9609..0000000
--- a/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocument.java
+++ /dev/null
@@ -1,132 +0,0 @@
-/*
- * Copyright (C) 2015 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.mtp;
-
-import android.database.MatrixCursor;
-import android.mtp.MtpObjectInfo;
-import android.provider.DocumentsContract;
-import android.provider.DocumentsContract.Document;
-
-import java.util.Date;
-
-class MtpDocument {
- static final int DUMMY_HANDLE_FOR_ROOT = 0;
-
- private final int mObjectHandle;
- private final int mFormat;
- private final String mName;
- private final Date mDateModified;
- private final int mSize;
- private final int mThumbSize;
- private final boolean mReadOnly;
-
- /**
- * Constructor for root document.
- */
- MtpDocument(MtpRoot root) {
- this(DUMMY_HANDLE_FOR_ROOT,
- 0x3001, // Directory.
- root.mDescription,
- null, // Unknown name.
- (int) Math.min(root.mMaxCapacity - root.mFreeSpace, Integer.MAX_VALUE),
- 0, // Total size.
- true); // Writable.
- }
-
- MtpDocument(MtpObjectInfo objectInfo) {
- this(objectInfo.getObjectHandle(),
- objectInfo.getFormat(),
- objectInfo.getName(),
- objectInfo.getDateModified() != 0 ? new Date(objectInfo.getDateModified()) : null,
- objectInfo.getCompressedSize(),
- objectInfo.getThumbCompressedSize(),
- objectInfo.getProtectionStatus() != 0);
- }
-
- MtpDocument(int objectHandle,
- int format,
- String name,
- Date dateModified,
- int size,
- int thumbSize,
- boolean readOnly) {
- this.mObjectHandle = objectHandle;
- this.mFormat = format;
- this.mName = name;
- this.mDateModified = dateModified;
- this.mSize = size;
- this.mThumbSize = thumbSize;
- this.mReadOnly = readOnly;
- }
-
- void addToCursor(Identifier rootIdentifier, MatrixCursor.RowBuilder builder) {
- final Identifier identifier = new Identifier(
- rootIdentifier.mDeviceId, rootIdentifier.mStorageId, mObjectHandle);
- final String mimeType = formatTypeToMimeType(mFormat);
-
- int flag = 0;
- if (mObjectHandle != DUMMY_HANDLE_FOR_ROOT) {
- if (mThumbSize > 0) {
- flag |= DocumentsContract.Document.FLAG_SUPPORTS_THUMBNAIL;
- }
- if (!mReadOnly) {
- flag |= DocumentsContract.Document.FLAG_SUPPORTS_DELETE |
- DocumentsContract.Document.FLAG_SUPPORTS_WRITE;
- }
- }
- if (mimeType == DocumentsContract.Document.MIME_TYPE_DIR && !mReadOnly) {
- flag |= DocumentsContract.Document.FLAG_DIR_SUPPORTS_CREATE;
- }
-
- builder.add(Document.COLUMN_DOCUMENT_ID, identifier.toDocumentId());
- builder.add(Document.COLUMN_DISPLAY_NAME, mName);
- builder.add(Document.COLUMN_MIME_TYPE, mimeType);
- builder.add(
- Document.COLUMN_LAST_MODIFIED,
- mDateModified != null ? mDateModified.getTime() : null);
- builder.add(Document.COLUMN_FLAGS, flag);
- builder.add(Document.COLUMN_SIZE, mSize);
- }
-
- static String formatTypeToMimeType(int format) {
- // TODO: Add complete list of mime types.
- switch (format) {
- case 0x3001:
- return DocumentsContract.Document.MIME_TYPE_DIR;
- case 0x3009:
- return "audio/mp3";
- case 0x3801:
- return "image/jpeg";
- default:
- return "application/octet-stream";
- }
- }
-
- static int mimeTypeToFormatType(String mimeType) {
- // TODO: Add complete list of mime types.
- switch (mimeType.toLowerCase()) {
- case Document.MIME_TYPE_DIR:
- return 0x3001;
- case "audio/mp3":
- return 0x3009;
- case "image/jpeg":
- return 0x3801;
- default:
- return 0x3000; // Undefined object.
- }
- }
-}
diff --git a/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsProvider.java b/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsProvider.java
index a3cf3e2..4ba0c1f 100644
--- a/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsProvider.java
+++ b/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsProvider.java
@@ -21,11 +21,12 @@
import android.database.Cursor;
import android.database.MatrixCursor;
import android.graphics.Point;
+import android.mtp.MtpObjectInfo;
import android.os.CancellationSignal;
import android.os.ParcelFileDescriptor;
-import android.provider.DocumentsContract;
import android.provider.DocumentsContract.Document;
import android.provider.DocumentsContract.Root;
+import android.provider.DocumentsContract;
import android.provider.DocumentsProvider;
import android.util.Log;
@@ -116,37 +117,37 @@
}
final Identifier identifier = Identifier.createFromDocumentId(documentId);
- MtpDocument document = null;
- if (identifier.mObjectHandle != MtpDocument.DUMMY_HANDLE_FOR_ROOT) {
+ if (identifier.mObjectHandle != CursorHelper.DUMMY_HANDLE_FOR_ROOT) {
+ MtpObjectInfo objectInfo;
try {
- document = mMtpManager.getDocument(identifier.mDeviceId, identifier.mObjectHandle);
+ objectInfo = mMtpManager.getObjectInfo(
+ identifier.mDeviceId, identifier.mObjectHandle);
} catch (IOException e) {
throw new FileNotFoundException(e.getMessage());
}
+ final MatrixCursor cursor = new MatrixCursor(projection);
+ CursorHelper.addToCursor(
+ objectInfo,
+ new Identifier(identifier.mDeviceId, identifier.mStorageId),
+ cursor.newRow());
+ return cursor;
} else {
MtpRoot[] roots;
try {
roots = mMtpManager.getRoots(identifier.mDeviceId);
- if (roots != null) {
- for (final MtpRoot root : roots) {
- if (identifier.mStorageId == root.mStorageId) {
- document = new MtpDocument(root);
- break;
- }
- }
- }
- if (document == null) {
- throw new FileNotFoundException();
- }
} catch (IOException e) {
throw new FileNotFoundException(e.getMessage());
}
+ for (final MtpRoot root : roots) {
+ if (identifier.mStorageId != root.mStorageId)
+ continue;
+ final MatrixCursor cursor = new MatrixCursor(projection);
+ CursorHelper.addToCursor(root, cursor.newRow());
+ return cursor;
+ }
}
- final MatrixCursor cursor = new MatrixCursor(projection);
- document.addToCursor(
- new Identifier(identifier.mDeviceId, identifier.mStorageId), cursor.newRow());
- return cursor;
+ throw new FileNotFoundException();
}
@Override
@@ -225,8 +226,13 @@
try {
final Identifier parentId = Identifier.createFromDocumentId(parentDocumentId);
final int objectHandle = mMtpManager.createDocument(
- parentId.mDeviceId, parentId.mStorageId, parentId.mObjectHandle, mimeType,
- displayName);
+ parentId.mDeviceId,
+ new MtpObjectInfo.Builder()
+ .setStorageId(parentId.mStorageId)
+ .setParent(parentId.mObjectHandle)
+ .setFormat(CursorHelper.mimeTypeToFormatType(mimeType))
+ .setName(displayName)
+ .build());
final String documentId = new Identifier(parentId.mDeviceId, parentId.mStorageId,
objectHandle).toDocumentId();
notifyChildDocumentsChange(parentDocumentId);
diff --git a/packages/MtpDocumentsProvider/src/com/android/mtp/MtpManager.java b/packages/MtpDocumentsProvider/src/com/android/mtp/MtpManager.java
index 6647009..7cfc679 100644
--- a/packages/MtpDocumentsProvider/src/com/android/mtp/MtpManager.java
+++ b/packages/MtpDocumentsProvider/src/com/android/mtp/MtpManager.java
@@ -108,16 +108,12 @@
return results;
}
- synchronized MtpObjectInfo getObjectInfo(int deviceId, int objectHandle) throws IOException {
+ synchronized MtpObjectInfo getObjectInfo(int deviceId, int objectHandle)
+ throws IOException {
final MtpDevice device = getDevice(deviceId);
return device.getObjectInfo(objectHandle);
}
- synchronized MtpDocument getDocument(int deviceId, int objectHandle) throws IOException {
- final MtpDevice device = getDevice(deviceId);
- return new MtpDocument(device.getObjectInfo(objectHandle));
- }
-
synchronized int[] getObjectHandles(int deviceId, int storageId, int parentObjectHandle)
throws IOException {
final MtpDevice device = getDevice(deviceId);
@@ -142,18 +138,6 @@
}
}
- // TODO: Remove this method.
- synchronized int createDocument(int deviceId, int storageId, int parentObjectHandle,
- String mimeType, String name) throws IOException {
- final MtpObjectInfo objectInfo = new MtpObjectInfo.Builder()
- .setName(name)
- .setStorageId(storageId)
- .setParent(parentObjectHandle)
- .setFormat(MtpDocument.mimeTypeToFormatType(mimeType))
- .build();
- return createDocument(deviceId, objectInfo);
- }
-
synchronized int createDocument(int deviceId, MtpObjectInfo objectInfo) throws IOException {
final MtpDevice device = getDevice(deviceId);
final MtpObjectInfo result = device.sendObjectInfo(objectInfo);