AppOpsManagerCompat

Added in 1.1.0

public final class AppOpsManagerCompat


Helper for accessing features in android.app.AppOpsManager.

Summary

Constants

static final int

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

static final int

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

static final 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 final 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
checkOrNoteProxyOp(
    @NonNull Context context,
    int proxyUid,
    @NonNull String op,
    @NonNull String proxiedPackageName
)

Check op for both proxy and proxied packages.

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

Make note of an application performing an operation.

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

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

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

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

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

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

static @Nullable String

Gets the app op name associated with a given permission.

Constants

MODE_ALLOWED

Added in 1.1.0
public static final int MODE_ALLOWED = 0

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

MODE_DEFAULT

Added in 1.1.0
public static final int MODE_DEFAULT = 3

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.

MODE_ERRORED

Added in 1.1.0
public static final int MODE_ERRORED = 2

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.

MODE_IGNORED

Added in 1.1.0
public static final int MODE_IGNORED = 1

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

checkOrNoteProxyOp

Added in 1.7.0
public static int checkOrNoteProxyOp(
    @NonNull Context context,
    int proxyUid,
    @NonNull String op,
    @NonNull String proxiedPackageName
)

Check op for both proxy and proxied packages. Do a quick check for whether an application might be able to perform an operation. This is not a security check. On API 23-28, fallback to noteProxyOpNoThrow On API 22 and lower, this method always returns MODE_IGNORED

#MODE_IGNORED} if it is not allowed and should be silently ignored (without causing the app to crash).

Parameters
@NonNull Context context

Your context.

int proxyUid

The uid of the proxy application.

@NonNull String op

The operation to note. One of the OPSTR_* constants.

@NonNull String proxiedPackageName

The name of the application calling into the proxy application.

Returns
int

Returns MODE_ALLOWED if the operation is allowed, or

noteOp

Added in 1.1.0
public static int noteOp(
    @NonNull Context context,
    @NonNull String op,
    int uid,
    @NonNull String packageName
)

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
@NonNull Context context

Your context.

@NonNull String op

The operation to note. One of the OPSTR_* constants.

int uid

The user id of the application attempting to perform the operation.

@NonNull String packageName

The name of the application attempting to perform the operation.

Returns
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).

Throws
java.lang.SecurityException

If the app has been configured to crash on this op.

noteOpNoThrow

Added in 1.1.0
public static int noteOpNoThrow(
    @NonNull Context context,
    @NonNull String op,
    int uid,
    @NonNull String packageName
)

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

Added in 1.1.0
public static int noteProxyOp(
    @NonNull Context context,
    @NonNull String op,
    @NonNull String proxiedPackageName
)

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
@NonNull Context context

Your context.

@NonNull String op

The operation to note. One of the OPSTR_* constants.

@NonNull String proxiedPackageName

The name of the application calling into the proxy application.

Returns
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).

Throws
java.lang.SecurityException

If the app has been configured to crash on this op.

noteProxyOpNoThrow

Added in 1.1.0
public static int noteProxyOpNoThrow(
    @NonNull Context context,
    @NonNull String op,
    @NonNull String proxiedPackageName
)

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

Compatibility

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

permissionToOp

Added in 1.1.0
public static @Nullable String permissionToOp(@NonNull String permission)

Gets the app op name associated with a given permission.

Compatibility

  • On API 22 and lower, this method always returns null
Parameters
@NonNull String permission

The permission.

Returns
@Nullable String

The app op associated with the permission or null.