Merge "Iteration on findPath API. Address Jeff's comment in ag/1542773."

This commit is contained in:
TreeHugger Robot
2016-10-29 01:36:13 +00:00
committed by Android (Google) Code Review
3 changed files with 19 additions and 20 deletions

View File

@@ -1412,7 +1412,7 @@ public final class DocumentsContract {
* @param path the list of document ids from the parent document at
* position 0 to the child document.
*/
public Path(String rootId, List<String> path) {
public Path(@Nullable String rootId, List<String> path) {
checkCollectionNotEmpty(path, "path");
checkCollectionElementsNotNull(path, "path");

View File

@@ -927,14 +927,20 @@ public abstract class DocumentsProvider extends ContentProvider {
? DocumentsContract.getTreeDocumentId(documentUri)
: null;
final Path path = findPath(documentId, parentDocumentId);
Path path = findPath(documentId, parentDocumentId);
// Ensure provider doesn't leak information to unprivileged callers.
if (isTreeUri
&& (path.getRootId() != null
|| !Objects.equals(path.getPath().get(0), parentDocumentId))) {
throw new IllegalStateException(
"Provider returns an invalid result for findPath.");
if (isTreeUri) {
if (!Objects.equals(path.getPath().get(0), parentDocumentId)) {
Log.wtf(TAG, "Provider doesn't return path from the tree root. Expected: "
+ parentDocumentId + " found: " + path.getPath().get(0));
}
if (path.getRootId() != null) {
Log.wtf(TAG, "Provider returns root id :"
+ path.getRootId() + " unexpectedly. Erase root id.");
path = new Path(null, path.getPath());
}
}
out.putParcelable(DocumentsContract.EXTRA_RESULT, path);

View File

@@ -86,25 +86,18 @@ public class DocumentsProviderTest extends ProviderTestCase2<TestDocumentsProvid
assertNull(DocumentsContract.findPath(mResolver, docUri));
}
public void testFindPath_treeUri_throwsOnNonNullRootId() throws Exception {
public void testFindPath_treeUri_erasesNonNullRootId() throws Exception {
mProvider.nextIsChildDocument = true;
mProvider.nextPath = new Path(ROOT_ID, Arrays.asList(PARENT_DOCUMENT_ID, DOCUMENT_ID));
final Uri docUri = buildTreeDocumentUri(
TestDocumentsProvider.AUTHORITY, PARENT_DOCUMENT_ID, DOCUMENT_ID);
assertNull(DocumentsContract.findPath(mResolver, docUri));
}
public void testFindPath_treeUri_throwsOnDifferentParentDocId() throws Exception {
mProvider.nextIsChildDocument = true;
mProvider.nextPath = new Path(
null, Arrays.asList(ANCESTOR_DOCUMENT_ID, PARENT_DOCUMENT_ID, DOCUMENT_ID));
final Uri docUri = buildTreeDocumentUri(
TestDocumentsProvider.AUTHORITY, PARENT_DOCUMENT_ID, DOCUMENT_ID);
assertNull(DocumentsContract.findPath(mResolver, docUri));
try (ContentProviderClient client =
mResolver.acquireUnstableContentProviderClient(docUri)) {
Path path = DocumentsContract.findPath(client, docUri);
assertNull(path.getRootId());
}
}
private static Uri buildTreeDocumentUri(String authority, String parentDocId, String docId) {