Merge "Add isChildDocument to MtpDocumentsProvider." into nyc-mr2-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
32a16bada4
@@ -442,6 +442,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 {
|
||||
synchronized (mDeviceListLock) {
|
||||
if (mDeviceToolkits.containsKey(deviceId)) {
|
||||
|
||||
@@ -34,6 +34,8 @@ import android.test.suitebuilder.annotation.MediumTest;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import static com.android.mtp.MtpDatabase.strings;
|
||||
@@ -792,6 +794,18 @@ public class MtpDocumentsProviderTest extends AndroidTestCase {
|
||||
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) {
|
||||
mDatabase = new MtpDatabase(getContext(), flag);
|
||||
mProvider = new MtpDocumentsProvider();
|
||||
@@ -845,4 +859,63 @@ public class MtpDocumentsProviderTest extends AndroidTestCase {
|
||||
return getStrings(mProvider.queryChildDocuments(
|
||||
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