MODE_ALLOWED源码实例Demo

android.app.AppOpsManager#MODE_ALLOWED源码实例Demo

下面列出了android.app.AppOpsManager#MODE_ALLOWED 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

/**
 * 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.
}
 
源代码2 项目: GrabQQPWD   文件: MiuiUtils.java
@TargetApi(Build.VERSION_CODES.KITKAT)
private static boolean checkOp(Context context, int op) {
    final int version = Build.VERSION.SDK_INT;
    if (version >= 19) {
        AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        try {
            Class clazz = AppOpsManager.class;
            Method method = clazz.getDeclaredMethod("checkOp", int.class, int.class, String.class);
            return AppOpsManager.MODE_ALLOWED == method.invoke(manager, op, Binder.getCallingUid(),
                    context.getPackageName());
        } catch (Exception e) {
            Log.e("", e.getMessage());
        }
    } else {
        Log.e("", "Below API 19 cannot invoke!");
    }
    return false;
}
 
源代码3 项目: android_9.0.0_r45   文件: AppOpsService.java
@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);
}
 
源代码4 项目: Telegram   文件: XiaomiUtilities.java
@SuppressWarnings("JavaReflectionMemberAccess")
@TargetApi(19)
public static boolean isCustomPermissionGranted(int permission) {
	try {
		AppOpsManager mgr = (AppOpsManager) ApplicationLoader.applicationContext.getSystemService(Context.APP_OPS_SERVICE);
		Method m = AppOpsManager.class.getMethod("checkOpNoThrow", int.class, int.class, String.class);
		int result = (int) m.invoke(mgr, permission, android.os.Process.myUid(), ApplicationLoader.applicationContext.getPackageName());
		return result == AppOpsManager.MODE_ALLOWED;
	} catch (Exception x) {
		FileLog.e(x);
	}
	return true;
}
 
源代码5 项目: kcanotify   文件: MainPreferenceFragment.java
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static boolean hasUsageStatPermission(Context context) {
    AppOpsManager appOps = (AppOpsManager)
            context.getSystemService(Context.APP_OPS_SERVICE);
    int mode = appOps.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,
            android.os.Process.myUid(), context.getPackageName());
    return mode == AppOpsManager.MODE_ALLOWED;
}
 
源代码6 项目: foregroundappchecker   文件: Utils.java
@TargetApi(Build.VERSION_CODES.KITKAT)
public static boolean hasUsageStatsPermission(Context context) {
    AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
    int mode = appOps.checkOpNoThrow("android:get_usage_stats",
            android.os.Process.myUid(), context.getPackageName());
    boolean granted = mode == AppOpsManager.MODE_ALLOWED;
    return granted;
}
 
源代码7 项目: ScreenShift   文件: ProfilesActivity.java
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private boolean hasUsageAccess() {
    try {
        PackageManager packageManager = getPackageManager();
        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(getPackageName(), 0);
        AppOpsManager appOpsManager = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
        int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,
                applicationInfo.uid, applicationInfo.packageName);
        return (mode == AppOpsManager.MODE_ALLOWED);

    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}
 
源代码8 项目: android_9.0.0_r45   文件: ContentProvider.java
@Override
public int delete(String callingPkg, Uri uri, String selection, String[] selectionArgs) {
    uri = validateIncomingUri(uri);
    uri = maybeGetUriWithoutUserId(uri);
    if (enforceWritePermission(callingPkg, uri, null) != AppOpsManager.MODE_ALLOWED) {
        return 0;
    }
    final String original = setCallingPackage(callingPkg);
    try {
        return ContentProvider.this.delete(uri, selection, selectionArgs);
    } finally {
        setCallingPackage(original);
    }
}
 
源代码9 项目: AppOpsX   文件: Helper.java
public static List<OpEntryInfo> getLocalOpEntryInfos(Context context) {
  if (sOpEntryInfoList.isEmpty()) {
    int[] sOpToSwitch = FixCompat.sOpToSwitch();
    String[] sOpNames = FixCompat.sOpNames();
    String[] sOpPerms = FixCompat.sOpPerms();
    int len = sOpPerms.length;
    PackageManager pm = context.getPackageManager();
    for (int i = 0; i < len; i++) {
      OpEntry entry = new OpEntry(sOpToSwitch[i], AppOpsManager.MODE_ALLOWED, 0, 0, 0, 0, null);
      OpEntryInfo opEntryInfo = new OpEntryInfo(entry);
      opEntryInfo.opName = sOpNames[i];
      try {
        PermissionInfo permissionInfo = pm.getPermissionInfo(sOpPerms[i], 0);
        opEntryInfo.opPermsLab = String.valueOf(permissionInfo.loadLabel(pm));
        opEntryInfo.opPermsDesc = String.valueOf(permissionInfo.loadDescription(pm));
      } catch (PackageManager.NameNotFoundException e) {
        //ignore
        Integer resId = sPermI18N.get(opEntryInfo.opName);
        if (resId != null) {
          opEntryInfo.opPermsLab = context.getString(resId);
          opEntryInfo.opPermsDesc = opEntryInfo.opName;
        } else {
          opEntryInfo.opPermsLab = opEntryInfo.opName;
        }
      }
      sOpEntryInfo.put(entry.getOp(), opEntryInfo);
      sAllOps.put(entry.getOp(), entry.getOp());
      sOpEntryInfoList.add(opEntryInfo);
    }
  }
  return new ArrayList<OpEntryInfo>(sOpEntryInfoList);
}
 
源代码10 项目: GrabQQPWD   文件: MainActivity.java
private boolean checkUsagePermission() {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
        AppOpsManager appOps = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
        int mode = 0;
        mode = appOps.checkOpNoThrow("android:get_usage_stats", android.os.Process.myUid(), getPackageName());
        boolean granted = mode == AppOpsManager.MODE_ALLOWED;
        if (!granted) {
            Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
            startActivityForResult(intent, 2);
            return false;
        }
    }
    return true;
}
 
源代码11 项目: AndroidComponentPlugin   文件: ContentProvider.java
@Override
public int delete(String callingPkg, Uri uri, String selection, String[] selectionArgs) {
    validateIncomingUri(uri);
    uri = maybeGetUriWithoutUserId(uri);
    if (enforceWritePermission(callingPkg, uri, null) != AppOpsManager.MODE_ALLOWED) {
        return 0;
    }
    final String original = setCallingPackage(callingPkg);
    try {
        return ContentProvider.this.delete(uri, selection, selectionArgs);
    } finally {
        setCallingPackage(original);
    }
}
 
源代码12 项目: AndroidComponentPlugin   文件: ContentProvider.java
@Override
public Uri canonicalize(String callingPkg, Uri uri) {
    validateIncomingUri(uri);
    int userId = getUserIdFromUri(uri);
    uri = getUriWithoutUserId(uri);
    if (enforceReadPermission(callingPkg, uri, null) != AppOpsManager.MODE_ALLOWED) {
        return null;
    }
    final String original = setCallingPackage(callingPkg);
    try {
        return maybeAddUserId(ContentProvider.this.canonicalize(uri), userId);
    } finally {
        setCallingPackage(original);
    }
}
 
源代码13 项目: android_9.0.0_r45   文件: MmsServiceBroker.java
@Override
public void setAutoPersisting(String callingPkg, boolean enabled) throws RemoteException {
    if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(),
            callingPkg) != AppOpsManager.MODE_ALLOWED) {
        return;
    }
    getServiceGuarded().setAutoPersisting(callingPkg, enabled);
}
 
源代码14 项目: GotoSleep   文件: SettingsFragment.java
public static boolean isUsageAccessGranted(Context context) {
    try {
        PackageManager packageManager = context.getPackageManager();
        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
        AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        int mode = 0;
        mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,
                applicationInfo.uid, applicationInfo.packageName);
        return (mode == AppOpsManager.MODE_ALLOWED);

    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}
 
源代码15 项目: MobileGuard   文件: ProcessManagerEngine.java
/**
 * check whether has permission to get usage stats
 * @param context
 * @return true if have, false otherwise
 */
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static boolean hasGetUsageStatsPermission(Context context) {
    AppOpsManager appOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
    int mode = 0;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mode = appOps.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,
                android.os.Process.myUid(), context.getPackageName());
    }
    return mode == AppOpsManager.MODE_ALLOWED;
}
 
源代码16 项目: android_9.0.0_r45   文件: MmsServiceBroker.java
@Override
public Uri importMultimediaMessage(String callingPkg, Uri contentUri,
        String messageId, long timestampSecs, boolean seen, boolean read)
                throws RemoteException {
    if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(),
            callingPkg) != AppOpsManager.MODE_ALLOWED) {
        // Silently fail AppOps failure due to not being the default SMS app
        // while writing the TelephonyProvider
        return FAKE_MMS_SENT_URI;
    }
    return getServiceGuarded().importMultimediaMessage(
            callingPkg, contentUri, messageId, timestampSecs, seen, read);
}
 
源代码17 项目: FloatWindow   文件: PermissionUtil.java
/**
 * 6.0以下判断是否有权限
 * 理论上6.0以上才需处理权限,但有的国内rom在6.0以下就添加了权限
 * 其实此方式也可以用于判断6.0以上版本,只不过有更简单的canDrawOverlays代替
 */
static boolean hasPermissionBelowMarshmallow(Context context) {
    try {
        AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        Method dispatchMethod = AppOpsManager.class.getMethod("checkOp", int.class, int.class, String.class);
        //AppOpsManager.OP_SYSTEM_ALERT_WINDOW = 24
        return AppOpsManager.MODE_ALLOWED == (Integer) dispatchMethod.invoke(
                manager, 24, Binder.getCallingUid(), context.getApplicationContext().getPackageName());
    } catch (Exception e) {
        return false;
    }
}
 
源代码18 项目: NekoSMS   文件: AppOpsUtils.java
public static boolean noteOp(Context context, int opCode, int uid, String packageName) {
    AppOpsManager appOpsManager = getAppOpsManager(context);
    int result = (Integer)ReflectionUtils.invoke(sNoteOpMethod, appOpsManager, opCode, uid, packageName);
    return result == AppOpsManager.MODE_ALLOWED;
}
 
private void check (PushController controller) {
    int result = controller.checkOp(AppOpsManagerOverride.OP_GET_USAGE_STATS);
    allow = (result == AppOpsManager.MODE_ALLOWED);
}
 
源代码20 项目: android_9.0.0_r45   文件: AppOpsService.java
@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;
}
 
如果文章对您有帮助,请点击页面广告。以鼓励作者做出的努力。
 方法所在类