Search in sources :

Example 16 with AppOpsManager

use of android.app.AppOpsManager in project AndroidChromium by JackyAndroid.

the class NotificationSystemStatusUtil method determineAppNotificationStatus.

/**
 * Determines whether notifications are enabled for the app represented by |context|.
 * Notifications may be disabled because either the user, or a management tool, has explicitly
 * disallowed the Chrome App to display notifications.
 *
 * This check requires Android KitKat or later. Earlier versions will return an INDETERMINABLE
 * status. When an exception occurs, an EXCEPTION status will be returned instead.
 *
 * @param context The context to check of whether it can show notifications.
 * @return One of the APP_NOTIFICATION_STATUS_* constants defined in this class.
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
static int determineAppNotificationStatus(Context context) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        return APP_NOTIFICATIONS_STATUS_UNDETERMINABLE;
    }
    final String packageName = context.getPackageName();
    final int uid = context.getApplicationInfo().uid;
    final AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
    try {
        Class appOpsManagerClass = Class.forName(AppOpsManager.class.getName());
        @SuppressWarnings("unchecked") final Method checkOpNoThrowMethod = appOpsManagerClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String.class);
        final Field opPostNotificationField = appOpsManagerClass.getDeclaredField(OP_POST_NOTIFICATION);
        int value = (int) opPostNotificationField.get(Integer.class);
        int status = (int) checkOpNoThrowMethod.invoke(appOpsManager, value, uid, packageName);
        return status == AppOpsManager.MODE_ALLOWED ? APP_NOTIFICATIONS_STATUS_ENABLED : APP_NOTIFICATIONS_STATUS_DISABLED;
    } catch (RuntimeException e) {
    } catch (Exception e) {
    // Silently fail here, since this is just collecting statistics. The histogram includes
    // a count for thrown exceptions, if that proves to be significant we can revisit.
    }
    return APP_NOTIFICATIONS_STATUS_EXCEPTION;
}
Also used : Field(java.lang.reflect.Field) AppOpsManager(android.app.AppOpsManager) Method(java.lang.reflect.Method) TargetApi(android.annotation.TargetApi)

Example 17 with AppOpsManager

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

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);
    }
    if (!mVrModeEnabled) {
        return;
    }
    // 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, true, mOverlayToken, exemptions, newUserId);
}
Also used : AppOpsManager(android.app.AppOpsManager)

Example 18 with AppOpsManager

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

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

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

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

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

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);
    }
    if (!mVrModeEnabled) {
        return;
    }
    // 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, true, mOverlayToken, exemptions, newUserId);
}
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