diff options
| author | Jules Laplace <jules@okfoc.us> | 2017-02-16 01:24:12 +0100 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2017-02-16 01:24:12 +0100 |
| commit | 30c49550c89c1b69c680170d2dc247eac76bd463 (patch) | |
| tree | 8732652298b630b9ba15def97e59738f1c9bf7b6 /StoneIsland/plugins/phonegap-plugin-push/src/android/com/adobe/phonegap/push/PermissionUtils.java | |
| parent | 8f1f626384e6ba75f4fb24c27e0973260a74421b (diff) | |
push plugin
Diffstat (limited to 'StoneIsland/plugins/phonegap-plugin-push/src/android/com/adobe/phonegap/push/PermissionUtils.java')
| -rw-r--r-- | StoneIsland/plugins/phonegap-plugin-push/src/android/com/adobe/phonegap/push/PermissionUtils.java | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/StoneIsland/plugins/phonegap-plugin-push/src/android/com/adobe/phonegap/push/PermissionUtils.java b/StoneIsland/plugins/phonegap-plugin-push/src/android/com/adobe/phonegap/push/PermissionUtils.java new file mode 100644 index 00000000..6aa5c9bf --- /dev/null +++ b/StoneIsland/plugins/phonegap-plugin-push/src/android/com/adobe/phonegap/push/PermissionUtils.java @@ -0,0 +1,55 @@ +package com.adobe.phonegap.push; + +import android.content.Context; +import android.content.pm.ApplicationInfo; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class PermissionUtils { + + private static final String CHECK_OP_NO_THROW = "checkOpNoThrow"; + + public static boolean hasPermission(Context appContext, String appOpsServiceId) throws UnknownError { + + ApplicationInfo appInfo = appContext.getApplicationInfo(); + + String pkg = appContext.getPackageName(); + int uid = appInfo.uid; + Class appOpsClass = null; + Object appOps = appContext.getSystemService("appops"); + + try { + + appOpsClass = Class.forName("android.app.AppOpsManager"); + + Method checkOpNoThrowMethod = appOpsClass.getMethod( + CHECK_OP_NO_THROW, + Integer.TYPE, + Integer.TYPE, + String.class + ); + + Field opValue = appOpsClass.getDeclaredField(appOpsServiceId); + + int value = (int) opValue.getInt(Integer.class); + Object result = checkOpNoThrowMethod.invoke(appOps, value, uid, pkg); + + return Integer.parseInt(result.toString()) == 0; // AppOpsManager.MODE_ALLOWED + + } catch (ClassNotFoundException e) { + throw new UnknownError("class not found"); + } catch (NoSuchMethodException e) { + throw new UnknownError("no such method"); + } catch (NoSuchFieldException e) { + throw new UnknownError("no such field"); + } catch (InvocationTargetException e) { + throw new UnknownError("invocation target"); + } catch (IllegalAccessException e) { + throw new UnknownError("illegal access"); + } + + } + +} |
