Merge "Add API for apps to query whether they have All Files Access" into rvc-dev

This commit is contained in:
Zimuzo Ezeozue
2020-03-10 17:03:16 +00:00
committed by Android (Google) Code Review
3 changed files with 87 additions and 2 deletions

View File

@@ -39,6 +39,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
/**
* Provides access to environment variables.
@@ -1253,6 +1254,50 @@ public class Environment {
uid, context.getOpPackageName()) == AppOpsManager.MODE_ALLOWED;
}
/**
* Returns whether the calling app has All Files Access on the primary shared/external storage
* media.
* <p>Declaring the permission {@link android.Manifest.permission#MANAGE_EXTERNAL_STORAGE} isn't
* enough to gain the access.
* <p>To request access, use
* {@link android.provider.Settings#ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION}.
*/
public static boolean isExternalStorageManager() {
final File externalDir = sCurrentUser.getExternalDirs()[0];
return isExternalStorageManager(externalDir);
}
/**
* Returns whether the calling app has All Files Access at the given {@code path}
* <p>Declaring the permission {@link android.Manifest.permission#MANAGE_EXTERNAL_STORAGE} isn't
* enough to gain the access.
* <p>To request access, use
* {@link android.provider.Settings#ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION}.
*/
public static boolean isExternalStorageManager(@NonNull File path) {
final Context context = Objects.requireNonNull(AppGlobals.getInitialApplication());
String packageName = Objects.requireNonNull(context.getPackageName());
int uid = context.getApplicationInfo().uid;
final AppOpsManager appOps = context.getSystemService(AppOpsManager.class);
final int opMode =
appOps.checkOpNoThrow(AppOpsManager.OP_MANAGE_EXTERNAL_STORAGE, uid, packageName);
switch (opMode) {
case AppOpsManager.MODE_DEFAULT:
return PackageManager.PERMISSION_GRANTED
== context.checkPermission(
Manifest.permission.MANAGE_EXTERNAL_STORAGE, Process.myPid(), uid);
case AppOpsManager.MODE_ALLOWED:
return true;
case AppOpsManager.MODE_ERRORED:
case AppOpsManager.MODE_IGNORED:
return false;
default:
throw new IllegalStateException("Unknown AppOpsManager mode " + opMode);
}
}
static File getDirectory(String variableName, String defaultPath) {
String path = System.getenv(variableName);
return path == null ? new File(defaultPath) : new File(path);