下面列出了android.app.AppOpsManager#MODE_IGNORED 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public int checkOperation(int code, int uid, String packageName) {
verifyIncomingUid(uid);
verifyIncomingOp(code);
String resolvedPackageName = resolvePackageName(uid, packageName);
if (resolvedPackageName == null) {
return AppOpsManager.MODE_IGNORED;
}
synchronized (this) {
if (isOpRestrictedLocked(uid, code, resolvedPackageName)) {
return AppOpsManager.MODE_IGNORED;
}
code = AppOpsManager.opToSwitch(code);
UidState uidState = getUidStateLocked(uid, false);
if (uidState != null && uidState.opModes != null
&& uidState.opModes.indexOfKey(code) >= 0) {
return uidState.opModes.get(code);
}
Op op = getOpLocked(code, uid, resolvedPackageName, false);
if (op == null) {
return AppOpsManager.opToDefaultMode(code);
}
return op.mode;
}
}
@Override
public int checkAudioOperation(int code, int usage, int uid, String packageName) {
boolean suspended;
try {
suspended = isPackageSuspendedForUser(packageName, uid);
} catch (IllegalArgumentException ex) {
// Package not found.
suspended = false;
}
if (suspended) {
Slog.i(TAG, "Audio disabled for suspended package=" + packageName + " for uid=" + uid);
return AppOpsManager.MODE_IGNORED;
}
synchronized (this) {
final int mode = checkRestrictionLocked(code, usage, uid, packageName);
if (mode != AppOpsManager.MODE_ALLOWED) {
return mode;
}
}
return checkOperation(code, uid, packageName);
}
@Override
public int noteProxyOperation(int code, String proxyPackageName,
int proxiedUid, String proxiedPackageName) {
verifyIncomingOp(code);
final int proxyUid = Binder.getCallingUid();
String resolveProxyPackageName = resolvePackageName(proxyUid, proxyPackageName);
if (resolveProxyPackageName == null) {
return AppOpsManager.MODE_IGNORED;
}
final int proxyMode = noteOperationUnchecked(code, proxyUid,
resolveProxyPackageName, -1, null);
if (proxyMode != AppOpsManager.MODE_ALLOWED || Binder.getCallingUid() == proxiedUid) {
return proxyMode;
}
String resolveProxiedPackageName = resolvePackageName(proxiedUid, proxiedPackageName);
if (resolveProxiedPackageName == null) {
return AppOpsManager.MODE_IGNORED;
}
return noteOperationUnchecked(code, proxiedUid, resolveProxiedPackageName,
proxyMode, resolveProxyPackageName);
}
/**
* Check if application can draw over other apps
* @param context Context
* @return Boolean
*/
public static boolean canDrawOverlays(@NonNull Context context) {
final int sdkInt = Build.VERSION.SDK_INT;
if (sdkInt >= Build.VERSION_CODES.M) {
if (sdkInt == Build.VERSION_CODES.O) {
// Sometimes Settings.canDrawOverlays returns false after allowing permission.
// Google Issue Tracker: https://issuetracker.google.com/issues/66072795
AppOpsManager appOpsMgr = context.getSystemService(AppOpsManager.class);
if (appOpsMgr != null) {
int mode = appOpsMgr.checkOpNoThrow(
"android:system_alert_window",
android.os.Process.myUid(),
context.getPackageName()
);
return mode == AppOpsManager.MODE_ALLOWED || mode == AppOpsManager.MODE_IGNORED;
} else {
return false;
}
}
// Default
return android.provider.Settings.canDrawOverlays(context);
}
return true; // This fallback may returns a incorrect result.
}
private void checkPermissionCamera() {
int checkPermission = 0;
if (Build.VERSION.SDK_INT >= 23) {
// checkPermission =ContextCompat.checkSelfPermission(this,Manifest.permission.CAMERA);
checkPermission = PermissionChecker.checkSelfPermission(this, Manifest.permission.CAMERA);
if (checkPermission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
} else {
isOpenCamera = true;
}
} else {
checkPermission = checkPermission(26);
if (checkPermission == AppOpsManager.MODE_ALLOWED) {
isOpenCamera = true;
} else if (checkPermission == AppOpsManager.MODE_IGNORED) {
isOpenCamera = false;
displayFrameworkBugMessageAndExit();
}
}
}
@Override
public void onConnected(@NonNull PushController controller,
Bundle savedInstanceState) {
super.onConnected(controller, savedInstanceState);
int result = controller.checkOp(AppOpsManagerOverride.OP_RUN_IN_BACKGROUND);
allow = (result != AppOpsManager.MODE_IGNORED);
if (allow) {
nextPage();
finish();
return;
}
layout.getNavigationBar()
.setNavigationBarListener(this);
mText.setText(Html.fromHtml(getString(R.string.wizard_descr_run_in_background, (Utils.isAppOpsInstalled() ? getString(R.string.run_in_background_rikka_appops) :
getString(R.string.run_in_background_appops_root)))));
layout.setHeaderText(R.string.wizard_title_run_in_background);
setContentView(layout);
}
public OpsResult disableAllPermission(final String packageName) throws Exception {
OpsResult opsForPackage = getOpsForPackage(packageName);
if (opsForPackage != null) {
if (opsForPackage.getException() == null) {
List<PackageOps> list = opsForPackage.getList();
if (list != null && !list.isEmpty()) {
for (PackageOps packageOps : list) {
List<OpEntry> ops = packageOps.getOps();
if (ops != null) {
for (OpEntry op : ops) {
if (op.getMode() != AppOpsManager.MODE_IGNORED) {
setOpsMode(packageName, op.getOp(), AppOpsManager.MODE_IGNORED);
}
}
}
}
}
} else {
throw new Exception(opsForPackage.getException());
}
}
return opsForPackage;
}
/**
* Check if application can draw over other apps
* @param context Context
* @return Boolean
*/
public static boolean canDrawOverlays(Context context) {
int sdkInt = Build.VERSION.SDK_INT;
if (sdkInt >= Build.VERSION_CODES.M) {
if (sdkInt == Build.VERSION_CODES.O) {
// Sometimes Settings.canDrawOverlays returns false after allowing permission.
// Google Issue Tracker: https://issuetracker.google.com/issues/66072795
AppOpsManager appOpsMgr = context.getSystemService(AppOpsManager.class);
if (appOpsMgr != null) {
int mode = appOpsMgr.checkOpNoThrow(
"android:system_alert_window",
android.os.Process.myUid(),
context.getPackageName()
);
return mode == AppOpsManager.MODE_ALLOWED || mode == AppOpsManager.MODE_IGNORED;
} else {
return false;
}
}
// Default
return android.provider.Settings.canDrawOverlays(context);
}
return true; // This fallback may returns a incorrect result.
}
private void checkPermissionCamera() {
int checkPermission = 0;
if (Build.VERSION.SDK_INT >= 23) {
// checkPermission =ContextCompat.checkSelfPermission(this,Manifest.permission.CAMERA);
checkPermission = PermissionChecker.checkSelfPermission(this, Manifest.permission.CAMERA);
if (checkPermission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
} else {
isOpenCamera = true;
}
} else {
checkPermission = checkPermission(26);
if (checkPermission == AppOpsManager.MODE_ALLOWED) {
isOpenCamera = true;
} else if (checkPermission == AppOpsManager.MODE_IGNORED) {
isOpenCamera = false;
displayFrameworkBugMessageAndExit();
}
}
}
int evalMode(int mode) {
if (mode == AppOpsManager.MODE_FOREGROUND) {
return state <= UID_STATE_LAST_NON_RESTRICTED
? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED;
}
return mode;
}
@Override
public int noteOperation(int code, int uid, String packageName) {
verifyIncomingUid(uid);
verifyIncomingOp(code);
String resolvedPackageName = resolvePackageName(uid, packageName);
if (resolvedPackageName == null) {
return AppOpsManager.MODE_IGNORED;
}
return noteOperationUnchecked(code, uid, resolvedPackageName, 0, null);
}
void switchMode(OpEntryInfo info, boolean v) {
if (v) {
info.mode = AppOpsManager.MODE_ALLOWED;
} else {
info.mode = AppOpsManager.MODE_IGNORED;
}
Map<String, String> map = new HashMap<String, String>(2);
map.put("new_mode", String.valueOf(info.mode));
map.put("op_name", info.opName);
setMode(info);
}
public void changeStatus() {
if (isAllowed()) {
this.mode = AppOpsManager.MODE_IGNORED;
} else {
this.mode = AppOpsManager.MODE_ALLOWED;
}
}
public static Observable<OpsResult> setMode(final Context context, final String pkgName,
final OpEntryInfo opEntryInfo,boolean isAllow) {
if (isAllow) {
opEntryInfo.mode = AppOpsManager.MODE_ALLOWED;
} else {
opEntryInfo.mode = AppOpsManager.MODE_IGNORED;
}
Map<String, String> map = new HashMap<String, String>(2);
map.put("new_mode", String.valueOf(opEntryInfo.mode));
map.put("op_name", opEntryInfo.opName);
return setMode(context, pkgName, opEntryInfo);
}
private int noteOperationUnchecked(int code, int uid, String packageName,
int proxyUid, String proxyPackageName) {
synchronized (this) {
final Ops ops = getOpsRawLocked(uid, packageName, true /* edit */,
false /* uidMismatchExpected */);
if (ops == null) {
if (DEBUG) Slog.d(TAG, "noteOperation: no op for code " + code + " uid " + uid
+ " package " + packageName);
return AppOpsManager.MODE_ERRORED;
}
final Op op = getOpLocked(ops, code, true);
if (isOpRestrictedLocked(uid, code, packageName)) {
return AppOpsManager.MODE_IGNORED;
}
final UidState uidState = ops.uidState;
if (op.duration == -1) {
Slog.w(TAG, "Noting op not finished: uid " + uid + " pkg " + packageName
+ " code " + code + " time=" + op.time[uidState.state]
+ " duration=" + op.duration);
}
op.duration = 0;
final int switchCode = AppOpsManager.opToSwitch(code);
// If there is a non-default per UID policy (we set UID op mode only if
// non-default) it takes over, otherwise use the per package policy.
if (uidState.opModes != null && uidState.opModes.indexOfKey(switchCode) >= 0) {
final int uidMode = uidState.evalMode(uidState.opModes.get(switchCode));
if (uidMode != AppOpsManager.MODE_ALLOWED) {
if (DEBUG) Slog.d(TAG, "noteOperation: uid reject #" + uidMode + " for code "
+ switchCode + " (" + code + ") uid " + uid + " package "
+ packageName);
op.rejectTime[uidState.state] = System.currentTimeMillis();
return uidMode;
}
} else {
final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, true) : op;
final int mode = switchOp.getMode();
if (mode != AppOpsManager.MODE_ALLOWED) {
if (DEBUG) Slog.d(TAG, "noteOperation: reject #" + mode + " for code "
+ switchCode + " (" + code + ") uid " + uid + " package "
+ packageName);
op.rejectTime[uidState.state] = System.currentTimeMillis();
return mode;
}
}
if (DEBUG) Slog.d(TAG, "noteOperation: allowing code " + code + " uid " + uid
+ " package " + packageName);
op.time[uidState.state] = System.currentTimeMillis();
op.rejectTime[uidState.state] = 0;
op.proxyUid = proxyUid;
op.proxyPackageName = proxyPackageName;
return AppOpsManager.MODE_ALLOWED;
}
}
@Override
public int startOperation(IBinder token, int code, int uid, String packageName,
boolean startIfModeDefault) {
verifyIncomingUid(uid);
verifyIncomingOp(code);
String resolvedPackageName = resolvePackageName(uid, packageName);
if (resolvedPackageName == null) {
return AppOpsManager.MODE_IGNORED;
}
ClientState client = (ClientState)token;
synchronized (this) {
final Ops ops = getOpsRawLocked(uid, resolvedPackageName, true /* edit */,
false /* uidMismatchExpected */);
if (ops == null) {
if (DEBUG) Slog.d(TAG, "startOperation: no op for code " + code + " uid " + uid
+ " package " + resolvedPackageName);
return AppOpsManager.MODE_ERRORED;
}
final Op op = getOpLocked(ops, code, true);
if (isOpRestrictedLocked(uid, code, resolvedPackageName)) {
return AppOpsManager.MODE_IGNORED;
}
final int switchCode = AppOpsManager.opToSwitch(code);
final UidState uidState = ops.uidState;
// If there is a non-default per UID policy (we set UID op mode only if
// non-default) it takes over, otherwise use the per package policy.
if (uidState.opModes != null && uidState.opModes.indexOfKey(switchCode) >= 0) {
final int uidMode = uidState.evalMode(uidState.opModes.get(switchCode));
if (uidMode != AppOpsManager.MODE_ALLOWED
&& (!startIfModeDefault || uidMode != AppOpsManager.MODE_DEFAULT)) {
if (DEBUG) Slog.d(TAG, "noteOperation: uid reject #" + uidMode + " for code "
+ switchCode + " (" + code + ") uid " + uid + " package "
+ resolvedPackageName);
op.rejectTime[uidState.state] = System.currentTimeMillis();
return uidMode;
}
} else {
final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, true) : op;
final int mode = switchOp.getMode();
if (mode != AppOpsManager.MODE_ALLOWED
&& (!startIfModeDefault || mode != AppOpsManager.MODE_DEFAULT)) {
if (DEBUG) Slog.d(TAG, "startOperation: reject #" + mode + " for code "
+ switchCode + " (" + code + ") uid " + uid + " package "
+ resolvedPackageName);
op.rejectTime[uidState.state] = System.currentTimeMillis();
return mode;
}
}
if (DEBUG) Slog.d(TAG, "startOperation: allowing code " + code + " uid " + uid
+ " package " + resolvedPackageName);
if (op.startNesting == 0) {
op.startRealtime = SystemClock.elapsedRealtime();
op.time[uidState.state] = System.currentTimeMillis();
op.rejectTime[uidState.state] = 0;
op.duration = -1;
scheduleOpActiveChangedIfNeededLocked(code, uid, packageName, true);
}
op.startNesting++;
uidState.startNesting++;
if (client.mStartedOps != null) {
client.mStartedOps.add(op);
}
}
return AppOpsManager.MODE_ALLOWED;
}