Add isChildDocument to MtpDocumentsProvider.
Bug: 32687127 Test: adb shell am instrument -w -e class com.android.mtp.MtpDocumentsProviderTest#testIsChildDocument com.android.mtp.tests/com.android.mtp.TestResultInstrumentation Change-Id: I44ded3c73443735229a8b3465122f7e10c4da178
This commit is contained in:
@@ -414,6 +414,24 @@ public class MtpDocumentsProvider extends DocumentsProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isChildDocument(String parentDocumentId, String documentId) {
|
||||||
|
try {
|
||||||
|
Identifier identifier = mDatabase.createIdentifier(documentId);
|
||||||
|
while (true) {
|
||||||
|
if (parentDocumentId.equals(identifier.mDocumentId)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (identifier.mDocumentType == MtpDatabaseConstants.DOCUMENT_TYPE_DEVICE) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
identifier = mDatabase.getParentIdentifier(identifier.mDocumentId);
|
||||||
|
}
|
||||||
|
} catch (FileNotFoundException error) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void openDevice(int deviceId) throws IOException {
|
void openDevice(int deviceId) throws IOException {
|
||||||
synchronized (mDeviceListLock) {
|
synchronized (mDeviceListLock) {
|
||||||
if (mDeviceToolkits.containsKey(deviceId)) {
|
if (mDeviceToolkits.containsKey(deviceId)) {
|
||||||
|
|||||||
@@ -34,6 +34,8 @@ import android.test.suitebuilder.annotation.MediumTest;
|
|||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.Queue;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
import static com.android.mtp.MtpDatabase.strings;
|
import static com.android.mtp.MtpDatabase.strings;
|
||||||
@@ -769,6 +771,18 @@ public class MtpDocumentsProviderTest extends AndroidTestCase {
|
|||||||
assertEquals(0x400000000L, cursor.getLong(0));
|
assertEquals(0x400000000L, cursor.getLong(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testIsChildDocument() throws Exception {
|
||||||
|
setupProvider(MtpDatabaseConstants.FLAG_DATABASE_IN_MEMORY);
|
||||||
|
setupRoots(0, new MtpRoot[] { new MtpRoot(0, 0, "Storage", 1000, 1000, "") });
|
||||||
|
setupHierarchyDocuments("1");
|
||||||
|
assertTrue(mProvider.isChildDocument("1", "1"));
|
||||||
|
assertTrue(mProvider.isChildDocument("1", "14"));
|
||||||
|
assertTrue(mProvider.isChildDocument("2", "14"));
|
||||||
|
assertTrue(mProvider.isChildDocument("5", "14"));
|
||||||
|
assertFalse(mProvider.isChildDocument("3", "14"));
|
||||||
|
assertFalse(mProvider.isChildDocument("6", "14"));
|
||||||
|
}
|
||||||
|
|
||||||
private void setupProvider(int flag) {
|
private void setupProvider(int flag) {
|
||||||
mDatabase = new MtpDatabase(getContext(), flag);
|
mDatabase = new MtpDatabase(getContext(), flag);
|
||||||
mProvider = new MtpDocumentsProvider();
|
mProvider = new MtpDocumentsProvider();
|
||||||
@@ -822,4 +836,63 @@ public class MtpDocumentsProviderTest extends AndroidTestCase {
|
|||||||
return getStrings(mProvider.queryChildDocuments(
|
return getStrings(mProvider.queryChildDocuments(
|
||||||
parentDocumentId, strings(DocumentsContract.Document.COLUMN_DOCUMENT_ID), null));
|
parentDocumentId, strings(DocumentsContract.Document.COLUMN_DOCUMENT_ID), null));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class HierarchyDocument {
|
||||||
|
int depth;
|
||||||
|
String documentId;
|
||||||
|
int objectHandle;
|
||||||
|
int parentHandle;
|
||||||
|
|
||||||
|
HierarchyDocument createChildDocument(int newHandle) {
|
||||||
|
final HierarchyDocument doc = new HierarchyDocument();
|
||||||
|
doc.depth = depth - 1;
|
||||||
|
doc.objectHandle = newHandle;
|
||||||
|
doc.parentHandle = objectHandle;
|
||||||
|
return doc;
|
||||||
|
}
|
||||||
|
|
||||||
|
MtpObjectInfo toObjectInfo() {
|
||||||
|
return new MtpObjectInfo.Builder()
|
||||||
|
.setName("doc_" + documentId)
|
||||||
|
.setFormat(depth > 0 ?
|
||||||
|
MtpConstants.FORMAT_ASSOCIATION : MtpConstants.FORMAT_TEXT)
|
||||||
|
.setObjectHandle(objectHandle)
|
||||||
|
.setParent(parentHandle)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupHierarchyDocuments(String documentId) throws Exception {
|
||||||
|
final Queue<HierarchyDocument> ids = new LinkedList<>();
|
||||||
|
final HierarchyDocument firstDocument = new HierarchyDocument();
|
||||||
|
firstDocument.depth = 3;
|
||||||
|
firstDocument.documentId = documentId;
|
||||||
|
firstDocument.objectHandle = MtpManager.OBJECT_HANDLE_ROOT_CHILDREN;
|
||||||
|
ids.add(firstDocument);
|
||||||
|
|
||||||
|
int objectHandle = 100;
|
||||||
|
while (!ids.isEmpty()) {
|
||||||
|
final HierarchyDocument document = ids.remove();
|
||||||
|
final HierarchyDocument[] children = new HierarchyDocument[] {
|
||||||
|
document.createChildDocument(objectHandle++),
|
||||||
|
document.createChildDocument(objectHandle++),
|
||||||
|
document.createChildDocument(objectHandle++),
|
||||||
|
};
|
||||||
|
final String[] childDocIds = setupDocuments(
|
||||||
|
0, 0, document.objectHandle, document.documentId, new MtpObjectInfo[] {
|
||||||
|
children[0].toObjectInfo(),
|
||||||
|
children[1].toObjectInfo(),
|
||||||
|
children[2].toObjectInfo(),
|
||||||
|
});
|
||||||
|
children[0].documentId = childDocIds[0];
|
||||||
|
children[1].documentId = childDocIds[1];
|
||||||
|
children[2].documentId = childDocIds[2];
|
||||||
|
|
||||||
|
if (children[0].depth > 0) {
|
||||||
|
ids.add(children[0]);
|
||||||
|
ids.add(children[1]);
|
||||||
|
ids.add(children[2]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user