Example usage for android.app AppOpsManager getClass

List of usage examples for android.app AppOpsManager getClass

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

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  ww.  j  a  va2  s.co  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;
}