summaryrefslogtreecommitdiff
path: root/packages/ExternalStorageProvider/src
diff options
context:
space:
mode:
authorIvan Chiang <chiangi@google.com>2019-11-15 19:02:46 +0800
committerIvan Chiang <chiangi@google.com>2019-11-26 17:11:02 +0800
commit698340fd5ce268542dff85411577a196c4908dd2 (patch)
tree3763076ce1bb66c5a38f5f04676b7b01bbddc36d /packages/ExternalStorageProvider/src
parent58bea80fd242676a0303de4c9c387be9949ed465 (diff)
Parse relative path from docId
Remove the logic of using file path directly. Parse the docId to get the relative path. Test: atest ExternalStorageProviderTest Change-Id: Ic218813acc73e247ee5593ed9e8e7688760e6780 Fix: 144467519
Diffstat (limited to 'packages/ExternalStorageProvider/src')
-rw-r--r--packages/ExternalStorageProvider/src/com/android/externalstorage/ExternalStorageProvider.java51
1 files changed, 29 insertions, 22 deletions
diff --git a/packages/ExternalStorageProvider/src/com/android/externalstorage/ExternalStorageProvider.java b/packages/ExternalStorageProvider/src/com/android/externalstorage/ExternalStorageProvider.java
index 4a50210d1a60..7b2922ba961b 100644
--- a/packages/ExternalStorageProvider/src/com/android/externalstorage/ExternalStorageProvider.java
+++ b/packages/ExternalStorageProvider/src/com/android/externalstorage/ExternalStorageProvider.java
@@ -50,6 +50,7 @@ import android.util.Log;
import android.util.Pair;
import com.android.internal.annotations.GuardedBy;
+import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.content.FileSystemProvider;
import com.android.internal.util.IndentingPrintWriter;
@@ -308,37 +309,26 @@ public class ExternalStorageProvider extends FileSystemProvider {
@Override
protected boolean shouldBlockFromTree(@NonNull String docId) {
try {
- final File dir = getFileForDocId(docId, true /* visible */).getCanonicalFile();
- if (!dir.isDirectory()) {
+ final File dir = getFileForDocId(docId, false /* visible */);
+
+ // the file is null or it is not a directory
+ if (dir == null || !dir.isDirectory()) {
return false;
}
- final String path = dir.getAbsolutePath();
+ final String path = getPathFromDocId(docId);
- // Block Download folder from tree
- if (MediaStore.Downloads.isDownloadDir(path)) {
+ // Block the root of the storage
+ if (path.isEmpty()) {
return true;
}
- final ArrayMap<String, RootInfo> roots = new ArrayMap<>();
-
- synchronized (mRootsLock) {
- roots.putAll(mRoots);
+ // Block Download folder from tree
+ if (TextUtils.equals(Environment.DIRECTORY_DOWNLOADS.toLowerCase(),
+ path.toLowerCase())) {
+ return true;
}
- // block root of storage
- for (int i = 0; i < roots.size(); i++) {
- RootInfo rootInfo = roots.valueAt(i);
- // skip home root
- if (TextUtils.equals(rootInfo.rootId, ROOT_ID_HOME)) {
- continue;
- }
-
- // block the root of storage
- if (TextUtils.equals(path, rootInfo.visiblePath.getAbsolutePath())) {
- return true;
- }
- }
return false;
} catch (IOException e) {
throw new IllegalArgumentException(
@@ -430,6 +420,23 @@ public class ExternalStorageProvider extends FileSystemProvider {
return Pair.create(root, buildFile(root, docId, visible, true));
}
+ @VisibleForTesting
+ static String getPathFromDocId(String docId) {
+ final int splitIndex = docId.indexOf(':', 1);
+ final String path = docId.substring(splitIndex + 1);
+
+ if (path.isEmpty()) {
+ return path;
+ }
+
+ // remove trailing "/"
+ if (path.charAt(path.length() - 1) == '/') {
+ return path.substring(0, path.length() - 1);
+ } else {
+ return path;
+ }
+ }
+
private RootInfo getRootFromDocId(String docId) throws FileNotFoundException {
final int splitIndex = docId.indexOf(':', 1);
final String tag = docId.substring(0, splitIndex);