Dan Baruch
Dan Baruch

Reputation: 1093

Checking if a DIFFERENT app is granted a permission

I've read around the internet for two options to check if an app is granted a permission or not.

Option 1:

getPackageManager().checkPermission(permission_string, packageName);

Option 2:

(PackageInfo.requestedPermissionsFlag[i] & PackageInfo.REQUESTED_PERMISSION_GRANTED) != 0

I'm running on Android 11. I'm implementing a mechanism that upon clicking an app, the permissions state will be checked and if a permission is not allowed, the user will be prompt to allow it. I'm only checking this for "advanced" permissions, meaning, permissions that requires the user to allow them from settings screen, such as manage external storage (for android 11), drawOverlay, writeSettings and such. Anyway, this is the code I'm using:

try {
            PackageInfo pi = getPackageManager().getPackageInfo(currAppInfo.getName(), PackageManager.GET_PERMISSIONS);
            for(int i=0; i<pi.requestedPermissions.length; i++)
            {
                String perm = pi.requestedPermissions[i];
                
                PermissionInfo permi = getPackageManager().getPermissionInfo(perm, PackageManager.GET_META_DATA);
                if(getPackageManager().checkPermission(perm, currAppInfo.getName()) == 0)
                    continue;
                if(AdvancedPermissionHandler.isAdvancedPermission(permi))
                {
                    AdvancedPermissionHandler.openSettingsPage(permi, currAppInfo.getName(), MainActivity.this);
                    return;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

The only problem I'm facing is that even if I'm using option 1 and even if I'm using option 2, I'm ALWAYS getting false on the check. Meaning, say I click an app that requires manage external storage and it's state is currently not allowed. I click the app, I get moved to the appropriate screen, I allow the permission, I go back to the main screen, when I click the app again, instead of opening, I'm being moved to the same permission screen. Debugger shows that

getPackageManager().checkPermission(permission_string, packageName);

is returning false, even though the permission is given. Same for when I'm using option 2. So my question is, what other methods are available to determine if a different app is granted a permission or, what am I doing wrong here in this code.

Upvotes: 1

Views: 1112

Answers (2)

agnostic-apollo
agnostic-apollo

Reputation: 76

This is due to Android 11 restrictions to prevent installed apps fingerprinting.

You need to add target package name (e.g com.termux) to queries element or declare QUERY_ALL_PACKAGES permission in AndroidManifest.xml if targetSdkVersion is 30+. Check Package Visibility or this article for more info. Otherwise, you will will get PackageSetting{...... com.termux/......} BLOCKED errors in logcat.

<manifest
    <queries>
        <package android:name="com.termux" />
   </queries>
</manifest>

Upvotes: 1

Dan Baruch
Dan Baruch

Reputation: 1093

After digging some more I've found AppOps. This is the code I've used to work, Android 11:

AppOpsManager appOps = (AppOpsManager)getSystemService(Context.APP_OPS_SERVICE);
            ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo(currAppInfo.getName(), 0);

            PackageInfo pi = getPackageManager().getPackageInfo(currAppInfo.getName(), PackageManager.GET_PERMISSIONS);
            for(int i=0; i<pi.requestedPermissions.length; i++)
            {
                String perm = pi.requestedPermissions[i];
                PermissionInfo permi = getPackageManager().getPermissionInfo(perm, PackageManager.GET_META_DATA);
                if(AppOpsManager.permissionToOp(permi.name) == null)
                    continue;
                boolean granted = (appOps.unsafeCheckOpNoThrow(AppOpsManager.permissionToOp(permi.name),applicationInfo.uid,currAppInfo.getName()) == AppOpsManager.MODE_ALLOWED);
                if(granted)
                    continue;
            }

Upvotes: 4

Related Questions