Example usage for android.app AppOpsManager MODE_ALLOWED

List of usage examples for android.app AppOpsManager MODE_ALLOWED

Introduction

In this page you can find the example usage for android.app AppOpsManager MODE_ALLOWED.

Prototype

int MODE_ALLOWED

To view the source code for android.app AppOpsManager MODE_ALLOWED.

Click Source Link

Document

Result from #checkOp , #noteOp , #startOp : the given caller is allowed to perform the given operation.

Usage

From source file:Main.java

@SuppressWarnings("IncompatibleBitwiseMaskOperation")
public static boolean isFloatWindowOpAllowed(Context context) {
    if (Build.VERSION.SDK_INT >= 19) { // 19, 4.4, KITKAT
        final AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        final int mode = manager.checkOp(AppOpsManager.OPSTR_SYSTEM_ALERT_WINDOW, Binder.getCallingUid(),
                context.getPackageName());
        return AppOpsManager.MODE_ALLOWED == mode;
    } else {// w  w w  .  ja v  a2  s .c  o  m
        return (context.getApplicationInfo().flags & 1 << 27) == 1;
    }
}

From source file:Main.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 {/*from ww  w. j  a  v  a 2 s  . c om*/
            Class clazz = AppOpsManager.class;
            Method method = clazz.getDeclaredMethod("checkOp", int.class, int.class, String.class);
            return AppOpsManager.MODE_ALLOWED == (int) method.invoke(manager, op, Binder.getCallingUid(),
                    context.getPackageName());
        } catch (Exception e) {
            Log.e(TAG, Log.getStackTraceString(e));
        }
    } else {
        Log.e("", "Below API 19 cannot invoke!");
    }
    return false;
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.KITKAT)
public static boolean havePermission(Context context) {
    try {/*from  w  w w . j a  va  2s  . co m*/
        PackageManager packageManager = context.getPackageManager();
        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
        AppOpsManager appOpsManager = (AppOpsManager) context.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;
    }
}

From source file:Main.java

public static boolean isAppUsageEnabled(Context context) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        return true;
    }/*from w  w  w.j av  a 2 s. co  m*/
    try {
        PackageManager packageManager = context.getPackageManager();
        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
        AppOpsManager appOpsManager = (AppOpsManager) context.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;
    }
}

From source file:Main.java

public static boolean hasAppOp(Context context, String appOp) {
    final AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
    final int mode = appOpsManager.checkOpNoThrow(appOp, Process.myUid(), context.getPackageName());
    return mode == AppOpsManager.MODE_ALLOWED;
}

From source file:Main.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 {/* ww  w.j  a  v a 2 s . co m*/
            Class clazz = AppOpsManager.class;
            Method method = clazz.getDeclaredMethod("checkOp", int.class, int.class, String.class);
            return AppOpsManager.MODE_ALLOWED == (int) method.invoke(manager, op, Binder.getCallingUid(),
                    context.getPackageName());
        } catch (Exception e) {
            Log.e(TAG, Log.getStackTraceString(e));
        }
    } else {
        Log.e(TAG, "Below API 19 cannot invoke!");
    }
    return false;
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.KITKAT)
public static boolean isAllowed(Context context, int op) {
    Log.d(TAG, "api level: " + Build.VERSION.SDK_INT);
    if (Build.VERSION.SDK_INT < 19) {
        return true;
    }/*from  ww  w. j a  v a2  s .c om*/
    Log.d(TAG, "op is " + op);
    String packageName = context.getApplicationContext().getPackageName();
    AppOpsManager aom = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
    Class<?>[] types = new Class[] { int.class, int.class, String.class };
    Object[] args = new Object[] { op, Binder.getCallingUid(), packageName };
    try {
        Method method = aom.getClass().getDeclaredMethod("checkOpNoThrow", types);
        Object mode = method.invoke(aom, args);
        Log.d(TAG, "invoke checkOpNoThrow: " + mode);
        if ((mode instanceof Integer) && ((Integer) mode == AppOpsManager.MODE_ALLOWED)) {
            Log.d(TAG, "allowed");
            return true;
        }
    } catch (Exception e) {
        Log.e(TAG, "invoke error: " + e);
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.KITKAT)
public static boolean isAppOpsAllowed(Context context, String appOpsPermission) {
    AppOpsManager aom = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
    return aom.checkOpNoThrow(appOpsPermission, Process.myUid(),
            context.getPackageName()) == AppOpsManager.MODE_ALLOWED;
}

From source file:Main.java

public static boolean areNotificationsEnabled(Context context) {
    AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
    ApplicationInfo appInfo = context.getApplicationInfo();
    String pkg = context.getApplicationContext().getPackageName();
    int uid = appInfo.uid;
    try {/*from  w ww  . j  av a  2  s  .  com*/
        Class<?> 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 = (Integer) opPostNotificationValue.get(Integer.class);
        return ((Integer) checkOpNoThrowMethod.invoke(appOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED);
    } catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException | InvocationTargetException
            | IllegalAccessException | RuntimeException e) {
        return true;
    }
}

From source file:Main.java

/**
 * 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.
 *//*  ww  w.j a v a  2  s. com*/
@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;
}