Java Code Examples for android.app.AppOpsManager#MODE_ALLOWED

The following examples show how to use android.app.AppOpsManager#MODE_ALLOWED . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AppOpsService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 2
Source File: MiuiUtils.java    From GrabQQPWD with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 3
Source File: PermissionUtils.java    From EnhancedScreenshotNotification with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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.
}
 
Example 4
Source File: MainActivity.java    From GrabQQPWD with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 5
Source File: PermissionUtil.java    From FloatWindow with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
    }
}
 
Example 6
Source File: MmsServiceBroker.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 7
Source File: ProcessManagerEngine.java    From MobileGuard with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example 8
Source File: SettingsFragment.java    From GotoSleep with GNU General Public License v3.0 5 votes vote down vote up
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;
    }
}
 
Example 9
Source File: MmsServiceBroker.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 10
Source File: ContentProvider.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 11
Source File: ContentProvider.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 12
Source File: XiaomiUtilities.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example 13
Source File: Helper.java    From AppOpsX with MIT License 5 votes vote down vote up
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);
}
 
Example 14
Source File: ContentProvider.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 15
Source File: ProfilesActivity.java    From ScreenShift with Apache License 2.0 5 votes vote down vote up
@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;
    }
}
 
Example 16
Source File: Utils.java    From foregroundappchecker with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 17
Source File: MainPreferenceFragment.java    From kcanotify with GNU General Public License v3.0 5 votes vote down vote up
@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;
}
 
Example 18
Source File: AppOpsService.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@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;
}
 
Example 19
Source File: UsageStatsPermissionActivity.java    From MiPushFramework with GNU General Public License v3.0 4 votes vote down vote up
private void check (PushController controller) {
    int result = controller.checkOp(AppOpsManagerOverride.OP_GET_USAGE_STATS);
    allow = (result == AppOpsManager.MODE_ALLOWED);
}
 
Example 20
Source File: AppOpsUtils.java    From NekoSMS with GNU General Public License v3.0 4 votes vote down vote up
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;
}