Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Overload hasAllPermissions  #11

@robertoestivill

Description

@robertoestivill

I was thinking on a variation of PermissionsManager.hasAllPermissions that only takes a context and retrieves all the app permissions out of the package info, just like PermissionsManager.requestAllManifestPermissionsIfNecessary

Just by looking at the code, it could be something like the following

public static boolean hasAllPermissions( @Nullable Context context ) { 
    if (context == null) {
        return false;
    }
    PackageInfo packageInfo = null;
    try {
        Log.d(TAG, context.getPackageName());
        packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS);
    } catch (PackageManager.NameNotFoundException e) {
        Log.e(TAG, "A problem occurred when retrieving permissions", e);
    }
    boolean hasAllPermissions = false;
    if (packageInfo != null) {
        hasAllPermissions = true;
        String[] permissions = packageInfo.requestedPermissions;
        if (permissions != null) {
            for (String perm : permissions) {
                hasAllPermissions &= ActivityCompat.checkSelfPermission(context, perm)
                    == PackageManager.PERMISSION_GRANTED;
            }
        }
    }
    return hasAllPermissions;
}

Disclaimer: I have not tested the code nor even compiled it, this is just an idea. I will be happy to contribute properly if the idea is good enough.

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions