Search in sources :

Example 11 with AppOpsManager

use of android.app.AppOpsManager in project android_frameworks_base by AOSPA.

the class NetworkStatsAccess method hasAppOpsPermission.

private static boolean hasAppOpsPermission(Context context, int callingUid, String callingPackage) {
    if (callingPackage != null) {
        AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        final int mode = appOps.checkOp(AppOpsManager.OP_GET_USAGE_STATS, callingUid, callingPackage);
        if (mode == AppOpsManager.MODE_DEFAULT) {
            // The default behavior here is to check if PackageManager has given the app
            // permission.
            final int permissionCheck = context.checkCallingPermission(Manifest.permission.PACKAGE_USAGE_STATS);
            return permissionCheck == PackageManager.PERMISSION_GRANTED;
        }
        return (mode == AppOpsManager.MODE_ALLOWED);
    }
    return false;
}
Also used : AppOpsManager(android.app.AppOpsManager)

Example 12 with AppOpsManager

use of android.app.AppOpsManager in project WordPress-Android by wordpress-mobile.

the class NotificationsUtils method isNotificationsEnabled.

// Checks if global notifications toggle is enabled in the Android app settings
// See: https://code.google.com/p/android/issues/detail?id=38482#c15
@SuppressWarnings("unchecked")
@TargetApi(19)
public static boolean isNotificationsEnabled(Context context) {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        ApplicationInfo appInfo = context.getApplicationInfo();
        String pkg = context.getApplicationContext().getPackageName();
        int uid = appInfo.uid;
        Class appOpsClass;
        try {
            appOpsClass = Class.forName(AppOpsManager.class.getName());
            Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String.class);
            Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
            int value = (int) opPostNotificationValue.get(Integer.class);
            return ((int) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED);
        } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            AppLog.e(T.NOTIFS, e.getMessage());
        }
    }
    // Default to assuming notifications are enabled
    return true;
}
Also used : ApplicationInfo(android.content.pm.ApplicationInfo) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Field(java.lang.reflect.Field) AppOpsManager(android.app.AppOpsManager) TargetApi(android.annotation.TargetApi)

Example 13 with AppOpsManager

use of android.app.AppOpsManager in project platform_frameworks_base by android.

the class MountService method mkdirs.

@Override
public int mkdirs(String callingPkg, String appPath) {
    final int userId = UserHandle.getUserId(Binder.getCallingUid());
    final UserEnvironment userEnv = new UserEnvironment(userId);
    // Validate that reported package name belongs to caller
    final AppOpsManager appOps = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
    appOps.checkPackage(Binder.getCallingUid(), callingPkg);
    File appFile = null;
    try {
        appFile = new File(appPath).getCanonicalFile();
    } catch (IOException e) {
        Slog.e(TAG, "Failed to resolve " + appPath + ": " + e);
        return -1;
    }
    // belong to the calling package.
    if (FileUtils.contains(userEnv.buildExternalStorageAppDataDirs(callingPkg), appFile) || FileUtils.contains(userEnv.buildExternalStorageAppObbDirs(callingPkg), appFile) || FileUtils.contains(userEnv.buildExternalStorageAppMediaDirs(callingPkg), appFile)) {
        appPath = appFile.getAbsolutePath();
        if (!appPath.endsWith("/")) {
            appPath = appPath + "/";
        }
        try {
            mConnector.execute("volume", "mkdirs", appPath);
            return 0;
        } catch (NativeDaemonConnectorException e) {
            return e.getCode();
        }
    }
    throw new SecurityException("Invalid mkdirs path: " + appFile);
}
Also used : AppOpsManager(android.app.AppOpsManager) UserEnvironment(android.os.Environment.UserEnvironment) IOException(java.io.IOException) File(java.io.File) AtomicFile(android.util.AtomicFile)

Example 14 with AppOpsManager

use of android.app.AppOpsManager in project platform_frameworks_base by android.

the class Vpn method setPackageAuthorization.

/**
     * Set whether a package has the ability to launch VPNs without user intervention.
     */
public boolean setPackageAuthorization(String packageName, boolean authorized) {
    // Check if the caller is authorized.
    enforceControlPermissionOrInternalCaller();
    int uid = getAppUid(packageName, mUserHandle);
    if (uid == -1 || VpnConfig.LEGACY_VPN.equals(packageName)) {
        // Authorization for nonexistent packages (or fake ones) can't be updated.
        return false;
    }
    long token = Binder.clearCallingIdentity();
    try {
        AppOpsManager appOps = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
        appOps.setMode(AppOpsManager.OP_ACTIVATE_VPN, uid, packageName, authorized ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED);
        return true;
    } catch (Exception e) {
        Log.wtf(TAG, "Failed to set app ops for package " + packageName + ", uid " + uid, e);
    } finally {
        Binder.restoreCallingIdentity(token);
    }
    return false;
}
Also used : AppOpsManager(android.app.AppOpsManager) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) RemoteException(android.os.RemoteException) IOException(java.io.IOException)

Example 15 with AppOpsManager

use of android.app.AppOpsManager in project platform_frameworks_base by android.

the class Settings method isCallingPackageAllowedToPerformAppOpsProtectedOperation.

/**
     * Helper method to perform a general and comprehensive check of whether an operation that is
     * protected by appops can be performed by a caller or not. e.g. OP_SYSTEM_ALERT_WINDOW and
     * OP_WRITE_SETTINGS
     * @hide
     */
public static boolean isCallingPackageAllowedToPerformAppOpsProtectedOperation(Context context, int uid, String callingPackage, boolean throwException, int appOpsOpCode, String[] permissions, boolean makeNote) {
    if (callingPackage == null) {
        return false;
    }
    AppOpsManager appOpsMgr = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
    int mode = AppOpsManager.MODE_DEFAULT;
    if (makeNote) {
        mode = appOpsMgr.noteOpNoThrow(appOpsOpCode, uid, callingPackage);
    } else {
        mode = appOpsMgr.checkOpNoThrow(appOpsOpCode, uid, callingPackage);
    }
    switch(mode) {
        case AppOpsManager.MODE_ALLOWED:
            return true;
        case AppOpsManager.MODE_DEFAULT:
            // if it is granted during install time.
            for (String permission : permissions) {
                if (context.checkCallingOrSelfPermission(permission) == PackageManager.PERMISSION_GRANTED) {
                    // if either of the permissions are granted, we will allow it
                    return true;
                }
            }
        default:
            // this is for all other cases trickled down here...
            if (!throwException) {
                return false;
            }
    }
    // prepare string to throw SecurityException
    StringBuilder exceptionMessage = new StringBuilder();
    exceptionMessage.append(callingPackage);
    exceptionMessage.append(" was not granted ");
    if (permissions.length > 1) {
        exceptionMessage.append(" either of these permissions: ");
    } else {
        exceptionMessage.append(" this permission: ");
    }
    for (int i = 0; i < permissions.length; i++) {
        exceptionMessage.append(permissions[i]);
        exceptionMessage.append((i == permissions.length - 1) ? "." : ", ");
    }
    throw new SecurityException(exceptionMessage.toString());
}
Also used : AppOpsManager(android.app.AppOpsManager)

Aggregations

AppOpsManager (android.app.AppOpsManager)101 ApplicationInfo (android.content.pm.ApplicationInfo)28 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)27 PackageManager (android.content.pm.PackageManager)18 PackageOps (android.app.AppOpsManager.PackageOps)14 ArrayList (java.util.ArrayList)14 UserHandle (android.os.UserHandle)13 IOException (java.io.IOException)10 Context (android.content.Context)9 PackageInfo (android.content.pm.PackageInfo)9 Method (java.lang.reflect.Method)9 TargetApi (android.annotation.TargetApi)8 HashMap (java.util.HashMap)8 RemoteException (android.os.RemoteException)7 ActivityManager (android.app.ActivityManager)6 Intent (android.content.Intent)6 ResolveInfo (android.content.pm.ResolveInfo)6 UserManager (android.os.UserManager)6 VisibleForTesting (com.android.internal.annotations.VisibleForTesting)6 Field (java.lang.reflect.Field)6