blob: 41bc6a6f6f62e91479621e740acac66971d92321 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
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";
private static final int MIN_API_LEVEL = 19; // required by AppOpsManager
public static boolean hasPermission(Context appContext, String appOpsServiceId) throws UnknownError {
if (android.os.Build.VERSION.SDK_INT < MIN_API_LEVEL) {
return true;
}
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");
}
}
}
|