diff options
Diffstat (limited to 'core/java')
| -rw-r--r-- | core/java/android/os/Environment.java | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/core/java/android/os/Environment.java b/core/java/android/os/Environment.java index 21a1e0f0a108..f2fb5b246f39 100644 --- a/core/java/android/os/Environment.java +++ b/core/java/android/os/Environment.java @@ -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); |
