Example usage for android.content Context APP_OPS_SERVICE

List of usage examples for android.content Context APP_OPS_SERVICE

Introduction

In this page you can find the example usage for android.content Context APP_OPS_SERVICE.

Prototype

String APP_OPS_SERVICE

To view the source code for android.content Context APP_OPS_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.app.AppOpsManager for tracking application operations on the device.

Usage

From source file:Main.java

private static AppOpsManager getAppOpsManager(Context context) {
    return (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
}

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 {/*from w  w  w  . jav a  2 s.  c om*/
        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 {//  w w  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 {// w w  w .jav a 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  ww  w .ja v a2  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

@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;
    }/* w  w  w  .j  av  a  2s  .  c  o m*/
    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)
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  w  w w.  j  a v a2s.c  o 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

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

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 {/* w ww . j a va  2s  . c  o  m*/
        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

@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;
}