Search code examples
androidpermissionsandroid-external-storageandroid-11

Check if external storage Manager is granted for ANOTHER app


I need to check if another app has the permission to External Storage Manager. With

Environment.isExternalStorageManager()

I check if the current app is granted. How can I do this for another app? I already tried to check the permission status with

getPackageManager().checkPermission(Manifest.permission.MANAGE_EXTERNAL_STORAGE, "dk.tacit.android.foldersync.lite")

but it return always -1, which means PERMISSION_DENIED, even if the permission is granted.


Solution

  • You can try using AppOpsManager class like this :

    try {
            PackageManager packageManager = this.getPackageManager();
            ApplicationInfo info = packageManager.getApplicationInfo("package name",0);
            Log.d("TAG", "onCreate: "+hasPermission(this,info)); 
            
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    

    Now the hasPermission function :

    @RequiresApi(api = Build.VERSION_CODES.R)
    public static boolean hasPermission(Context context,ApplicationInfo info) {
        boolean granted = false;
        AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        int mode = appOps.unsafeCheckOpNoThrow(AppOpsManager.permissionToOp("android.permission.MANAGE_EXTERNAL_STORAGE"), info.uid, info.packageName);
        if (mode == AppOpsManager.MODE_DEFAULT)
            granted = (context.checkCallingOrSelfPermission(Manifest.permission.MANAGE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
        else
            granted = (mode == AppOpsManager.MODE_ALLOWED);
        return granted;
    }
    

    You also have to declare QUERY_ALL_PACKAGES permission in the manifest :

    <uses-permission
        android:name="android.permission.QUERY_ALL_PACKAGES"
        tools:ignore="QueryAllPackagesPermission" />