AppOpsManagerCompat

class AppOpsManagerCompat
kotlin.Any
   ↳ androidx.core.app.AppOpsManagerCompat

Helper for accessing features in android.app.AppOpsManager.

Summary

Constants

static Int

Result from noteOp: the given caller is allowed to perform the given operation.

static Int

Result from noteOp: the given caller should use its default security check.

static Int

Result from noteOpNoThrow: the given caller is not allowed to perform the given operation, and this attempt should cause it to have a fatal error, typically a SecurityException.

static Int

Result from noteOp: the given caller is not allowed to perform the given operation, and this attempt should silently fail (it should not cause the app to crash).

Public methods

static Int
noteOp(@NonNull context: Context, @NonNull op: String, uid: Int, @NonNull packageName: String)

Make note of an application performing an operation.

static Int
noteOpNoThrow(@NonNull context: Context, @NonNull op: String, uid: Int, @NonNull packageName: String)

Like noteOp but instead of throwing a SecurityException it returns MODE_ERRORED.

static Int
noteProxyOp(@NonNull context: Context, @NonNull op: String, @NonNull proxiedPackageName: String)

Make note of an application performing an operation on behalf of another application when handling an IPC.

static Int
noteProxyOpNoThrow(@NonNull context: Context, @NonNull op: String, @NonNull proxiedPackageName: String)

Like noteProxyOp(Context, String, String) but instead of throwing a SecurityException it returns MODE_ERRORED.

static String?
permissionToOp(@NonNull permission: String)

Gets the app op name associated with a given permission.

Constants

MODE_ALLOWED

static val MODE_ALLOWED: Int

Result from noteOp: the given caller is allowed to perform the given operation.

Value: AppOpsManager.MODE_ALLOWED

MODE_DEFAULT

static val MODE_DEFAULT: Int

Result from noteOp: the given caller should use its default security check. This mode is not normally used; it should only be used with appop permissions, and callers must explicitly check for it and deal with it.

Value: AppOpsManager.MODE_DEFAULT

MODE_ERRORED

static val MODE_ERRORED: Int

Result from noteOpNoThrow: the given caller is not allowed to perform the given operation, and this attempt should cause it to have a fatal error, typically a SecurityException.

Value: AppOpsManager.MODE_ERRORED

MODE_IGNORED

static val MODE_IGNORED: Int

Result from noteOp: the given caller is not allowed to perform the given operation, and this attempt should silently fail (it should not cause the app to crash).

Value: AppOpsManager.MODE_IGNORED

Public methods

noteOp

static fun noteOp(@NonNull context: Context, @NonNull op: String, uid: Int, @NonNull packageName: String): Int

Make note of an application performing an operation. Note that you must pass in both the uid and name of the application to be checked; this function will verify that these two match, and if not, return MODE_IGNORED. If this call succeeds, the last execution time of the operation for this app will be updated to the current time.

Compatibility

  • On API 18 and lower, this method always returns MODE_IGNORED
Parameters
context Context: Your context.
op Context: The operation to note. One of the OPSTR_* constants.
uid Context: The user id of the application attempting to perform the operation.
packageName Context: The name of the application attempting to perform the operation.
Return
Int: Returns MODE_ALLOWED if the operation is allowed, or MODE_IGNORED if it is not allowed and should be silently ignored (without causing the app to crash).
Exceptions
SecurityException If the app has been configured to crash on this op.

noteOpNoThrow

static fun noteOpNoThrow(@NonNull context: Context, @NonNull op: String, uid: Int, @NonNull packageName: String): Int

Like noteOp but instead of throwing a SecurityException it returns MODE_ERRORED.

Compatibility

  • On API 18 and lower, this method always returns MODE_IGNORED

noteProxyOp

static fun noteProxyOp(@NonNull context: Context, @NonNull op: String, @NonNull proxiedPackageName: String): Int

Make note of an application performing an operation on behalf of another application when handling an IPC. Note that you must pass the package name of the application that is being proxied while its UID will be inferred from the IPC state; this function will verify that the calling uid and proxied package name match, and if not, return MODE_IGNORED. If this call succeeds, the last execution time of the operation for the proxied app and your app will be updated to the current time.

Compatibility

  • On API 22 and lower, this method always returns MODE_IGNORED
Parameters
context Context: Your context.
op Context: The operation to note. One of the OPSTR_* constants.
proxiedPackageName Context: The name of the application calling into the proxy application.
Return
Int: Returns MODE_ALLOWED if the operation is allowed, or MODE_IGNORED if it is not allowed and should be silently ignored (without causing the app to crash).
Exceptions
SecurityException If the app has been configured to crash on this op.

noteProxyOpNoThrow

static fun noteProxyOpNoThrow(@NonNull context: Context, @NonNull op: String, @NonNull proxiedPackageName: String): Int

Like noteProxyOp(Context, String, String) but instead of throwing a SecurityException it returns MODE_ERRORED.

Compatibility

  • On API 22 and lower, this method always returns MODE_IGNORED

permissionToOp

@Nullable static fun permissionToOp(@NonNull permission: String): String?

Gets the app op name associated with a given permission.

Compatibility

  • On API 22 and lower, this method always returns null
Parameters
permission String: The permission.
Return
String?: The app op associated with the permission or null.