Allow providers block folders in ACTION_OPEN_DOCUMENT_TREE

DocumentsContract
- Add new flag FLAG_DIR_BLOCKS_TREE in Document

ExternalStorageProvider
- Add flag into DocumentInfo for blocking folder

Change-Id: Ib557fe99d330788a3bd968bffd43b6658761514f
Bug: 32370759
Test: atest DocumentsTest
This commit is contained in:
Ivan Chiang
2019-08-21 16:12:54 +08:00
parent 92818ea47c
commit 730b3a3a45
4 changed files with 87 additions and 4 deletions

View File

@@ -16,6 +16,7 @@
package com.android.externalstorage;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.usage.StorageStatsManager;
import android.content.ContentResolver;
@@ -298,6 +299,53 @@ public class ExternalStorageProvider extends FileSystemProvider {
return projection != null ? projection : DEFAULT_ROOT_PROJECTION;
}
/**
* Check that the directory is the root of storage or blocked file from tree.
*
* @param docId the docId of the directory to be checked
* @return true, should be blocked from tree. Otherwise, false.
*/
@Override
protected boolean shouldBlockFromTree(@NonNull String docId) {
try {
final File dir = getFileForDocId(docId, true /* visible */).getCanonicalFile();
if (!dir.isDirectory()) {
return false;
}
final String path = dir.getAbsolutePath();
// Block Download folder from tree
if (MediaStore.Downloads.isDownloadDir(path)) {
return true;
}
final ArrayMap<String, RootInfo> roots = new ArrayMap<>();
synchronized (mRootsLock) {
roots.putAll(mRoots);
}
// 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(
"Failed to determine if " + docId + " should block from tree " + ": " + e);
}
}
@Override
protected String getDocIdForFile(File file) throws FileNotFoundException {
return getDocIdForFileMaybeCreate(file, false);