PermissionChecker

class PermissionChecker
kotlin.Any
   ↳ androidx.core.content.PermissionChecker

This class provides permission check APIs that verify both the permission and the associated app op for this permission if such is defined.

In the new permission model permissions with protection level dangerous are runtime permissions. For apps targeting android.os.Build.VERSION_CODES#M and above the user may not grant such permissions or revoke them at any time. For apps targeting API lower than android.os.Build.VERSION_CODES#M these permissions are always granted as such apps do not expect permission revocations and would crash. Therefore, when the user disables a permission for a legacy app in the UI the platform disables the APIs guarded by this permission making them a no-op which is doing nothing or returning an empty result or default error.

It is important that when you perform an operation on behalf of another app you use these APIs to check for permissions as the app may be a legacy app that does not participate in the new permission model for which the user had disabled the "permission" which is achieved by disallowing the corresponding app op.

Summary

Constants
static Int

Permission result: The permission is denied.

static Int

Permission result: The permission is denied because the app op is not allowed.

static Int

Permission result: The permission is granted.

Public methods
static Int
checkPermission(context: Context, permission: String, pid: Int, uid: Int, packageName: String?)

Checks whether a given package in a UID and PID has a given permission and whether the app op that corresponds to this permission is allowed.

static Int
checkSelfPermission(context: Context, permission: String)

Checks whether your app has a given permission and whether the app op that corresponds to this permission is allowed.

static Int
checkCallingPermission(context: Context, permission: String, packageName: String?)

Checks whether the IPC you are handling has a given permission and whether the app op that corresponds to this permission is allowed.

static Int

Checks whether the IPC you are handling or your app has a given permission and whether the app op that corresponds to this permission is allowed.

Constants

PERMISSION_DENIED

static val PERMISSION_DENIED: Int

Permission result: The permission is denied.

Value: -1

PERMISSION_DENIED_APP_OP

static val PERMISSION_DENIED_APP_OP: Int

Permission result: The permission is denied because the app op is not allowed.

Value: -2

PERMISSION_GRANTED

static val PERMISSION_GRANTED: Int

Permission result: The permission is granted.

Value: 0

Public methods

checkPermission

static fun checkPermission(context: Context, permission: String, pid: Int, uid: Int, packageName: String?): Int

Checks whether a given package in a UID and PID has a given permission and whether the app op that corresponds to this permission is allowed.

Parameters
context Context: Context for accessing resources.
permission Context: The permission to check.
pid Context: The process id for which to check.
uid Context: The uid for which to check.
packageName Context: The package name for which to check. If null the the first package for the calling UID will be used.
Return
Int: The permission check result which is either #PERMISSION_GRANTED or #PERMISSION_DENIED or #PERMISSION_DENIED_APP_OP.

checkSelfPermission

static fun checkSelfPermission(context: Context, permission: String): Int

Checks whether your app has a given permission and whether the app op that corresponds to this permission is allowed.

Parameters
context Context: Context for accessing resources.
permission Context: The permission to check.
Return
Int: The permission check result which is either #PERMISSION_GRANTED or #PERMISSION_DENIED or #PERMISSION_DENIED_APP_OP.

checkCallingPermission

static fun checkCallingPermission(context: Context, permission: String, packageName: String?): Int

Checks whether the IPC you are handling has a given permission and whether the app op that corresponds to this permission is allowed.

Parameters
context Context: Context for accessing resources.
permission Context: The permission to check.
packageName Context: The package name making the IPC. If null the the first package for the calling UID will be used.
Return
Int: The permission check result which is either #PERMISSION_GRANTED or #PERMISSION_DENIED or #PERMISSION_DENIED_APP_OP.

checkCallingOrSelfPermission

static fun checkCallingOrSelfPermission(context: Context, permission: String): Int

Checks whether the IPC you are handling or your app has a given permission and whether the app op that corresponds to this permission is allowed.

Parameters
context Context: Context for accessing resources.
permission Context: The permission to check.
Return
Int: The permission check result which is either #PERMISSION_GRANTED or #PERMISSION_DENIED or #PERMISSION_DENIED_APP_OP.