Search in sources :

Example 46 with AppOpsManager

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

the class VrManagerService method updateOverlayStateLocked.

private void updateOverlayStateLocked(String exemptedPackage, int newUserId, int oldUserId) {
    AppOpsManager appOpsManager = getContext().getSystemService(AppOpsManager.class);
    // If user changed drop restrictions for the old user.
    if (oldUserId != newUserId) {
        appOpsManager.setUserRestrictionForUser(AppOpsManager.OP_SYSTEM_ALERT_WINDOW, false, mOverlayToken, null, oldUserId);
    }
    // Apply the restrictions for the current user based on vr state
    String[] exemptions = (exemptedPackage == null) ? new String[0] : new String[] { exemptedPackage };
    appOpsManager.setUserRestrictionForUser(AppOpsManager.OP_SYSTEM_ALERT_WINDOW, mVrModeEnabled, mOverlayToken, exemptions, newUserId);
}
Also used : AppOpsManager(android.app.AppOpsManager)

Example 47 with AppOpsManager

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

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 48 with AppOpsManager

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

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)

Example 49 with AppOpsManager

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

the class RecentLocationApps method getAppList.

/**
     * Fills a list of applications which queried location recently within specified time.
     */
public List<Request> getAppList() {
    // Retrieve a location usage list from AppOps
    AppOpsManager aoManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
    List<AppOpsManager.PackageOps> appOps = aoManager.getPackagesForOps(LOCATION_OPS);
    final int appOpsCount = appOps != null ? appOps.size() : 0;
    // Process the AppOps list and generate a preference list.
    ArrayList<Request> requests = new ArrayList<>(appOpsCount);
    final long now = System.currentTimeMillis();
    final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
    final List<UserHandle> profiles = um.getUserProfiles();
    for (int i = 0; i < appOpsCount; ++i) {
        AppOpsManager.PackageOps ops = appOps.get(i);
        // Don't show the Android System in the list - it's not actionable for the user.
        // Also don't show apps belonging to background users except managed users.
        String packageName = ops.getPackageName();
        int uid = ops.getUid();
        int userId = UserHandle.getUserId(uid);
        boolean isAndroidOs = (uid == Process.SYSTEM_UID) && ANDROID_SYSTEM_PACKAGE_NAME.equals(packageName);
        if (isAndroidOs || !profiles.contains(new UserHandle(userId))) {
            continue;
        }
        Request request = getRequestFromOps(now, ops);
        if (request != null) {
            requests.add(request);
        }
    }
    return requests;
}
Also used : ArrayList(java.util.ArrayList) AppOpsManager(android.app.AppOpsManager) UserManager(android.os.UserManager) UserHandle(android.os.UserHandle)

Example 50 with AppOpsManager

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

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)

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