diff options
author | Garfield Tan <xutan@google.com> | 2016-10-07 16:03:17 -0700 |
---|---|---|
committer | Garfield Tan <xutan@google.com> | 2016-10-14 19:18:46 +0000 |
commit | 06940e1246467e96392ae2b7effcb1f9d3e4d273 (patch) | |
tree | bf26eb4342b722388f337c3e4a38152e014aaad7 /packages/ExternalStorageProvider/src | |
parent | 5025bd3a1fd14ed785afb3392109449c0783f066 (diff) |
Iteration on findPath API.
* Extend this API to take tree URI
* Add toString(), equals() and hashCode() to Path
* Address Jeff's comments in ag/1513538
* Add unit tests for findPath
Bug: 30948740
Change-Id: Iaf852d0e40fae37623e9bb9ffa1c6fbe334c1b21
(cherry picked from commit d4ab7ade7171a4382ef4f61f2a5f078a17800e83)
Diffstat (limited to 'packages/ExternalStorageProvider/src')
-rw-r--r-- | packages/ExternalStorageProvider/src/com/android/externalstorage/ExternalStorageProvider.java | 50 |
1 files changed, 36 insertions, 14 deletions
diff --git a/packages/ExternalStorageProvider/src/com/android/externalstorage/ExternalStorageProvider.java b/packages/ExternalStorageProvider/src/com/android/externalstorage/ExternalStorageProvider.java index 3b575a8a03a6..662a1cde53cd 100644 --- a/packages/ExternalStorageProvider/src/com/android/externalstorage/ExternalStorageProvider.java +++ b/packages/ExternalStorageProvider/src/com/android/externalstorage/ExternalStorageProvider.java @@ -16,6 +16,7 @@ package com.android.externalstorage; +import android.annotation.Nullable; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; @@ -40,8 +41,8 @@ import android.os.storage.StorageManager; import android.os.storage.VolumeInfo; import android.provider.DocumentsContract; import android.provider.DocumentsContract.Document; -import android.provider.DocumentsContract.Root; import android.provider.DocumentsContract.Path; +import android.provider.DocumentsContract.Root; import android.provider.DocumentsProvider; import android.provider.MediaStore; import android.provider.Settings; @@ -325,14 +326,19 @@ public class ExternalStorageProvider extends DocumentsProvider { } private File getFileForDocId(String docId, boolean visible) throws FileNotFoundException { - return resolveDocId(docId, visible).second; + RootInfo root = getRootFromDocId(docId); + return buildFile(root, docId, visible); } private Pair<RootInfo, File> resolveDocId(String docId, boolean visible) throws FileNotFoundException { + RootInfo root = getRootFromDocId(docId); + return Pair.create(root, buildFile(root, docId, visible)); + } + + private RootInfo getRootFromDocId(String docId) throws FileNotFoundException { final int splitIndex = docId.indexOf(':', 1); final String tag = docId.substring(0, splitIndex); - final String path = docId.substring(splitIndex + 1); RootInfo root; synchronized (mRootsLock) { @@ -342,6 +348,14 @@ public class ExternalStorageProvider extends DocumentsProvider { throw new FileNotFoundException("No root for " + tag); } + return root; + } + + private File buildFile(RootInfo root, String docId, boolean visible) + throws FileNotFoundException { + final int splitIndex = docId.indexOf(':', 1); + final String path = docId.substring(splitIndex + 1); + File target = visible ? root.visiblePath : root.path; if (target == null) { return null; @@ -353,7 +367,7 @@ public class ExternalStorageProvider extends DocumentsProvider { if (!target.exists()) { throw new FileNotFoundException("Missing file for " + docId + " at " + target); } - return Pair.create(root, target); + return target; } private void includeFile(MatrixCursor result, String docId, File file) @@ -430,25 +444,33 @@ public class ExternalStorageProvider extends DocumentsProvider { } @Override - public Path findPath(String documentId) + public Path findPath(String childDocId, @Nullable String parentDocId) throws FileNotFoundException { LinkedList<String> path = new LinkedList<>(); - final Pair<RootInfo, File> resolvedDocId = resolveDocId(documentId, false); - RootInfo root = resolvedDocId.first; - File file = resolvedDocId.second; + final Pair<RootInfo, File> resolvedDocId = resolveDocId(childDocId, false); + final RootInfo root = resolvedDocId.first; + File child = resolvedDocId.second; + + final File parent = TextUtils.isEmpty(parentDocId) + ? root.path + : getFileForDocId(parentDocId); + + if (!child.exists()) { + throw new FileNotFoundException(childDocId + " is not found."); + } - if (!file.exists()) { - throw new FileNotFoundException(); + if (!child.getAbsolutePath().startsWith(parent.getAbsolutePath())) { + throw new FileNotFoundException(childDocId + " is not found under " + parentDocId); } - while (file != null && file.getAbsolutePath().startsWith(root.path.getAbsolutePath())) { - path.addFirst(getDocIdForFile(file)); + while (child != null && child.getAbsolutePath().startsWith(parent.getAbsolutePath())) { + path.addFirst(getDocIdForFile(child)); - file = file.getParentFile(); + child = child.getParentFile(); } - return new Path(root.rootId, path); + return new Path(parentDocId == null ? root.rootId : null, path); } @Override |