Skip to content

Instantly share code, notes, and snippets.

@chanphiromsok
Forked from thoinv/PermissionUtil.java
Created June 16, 2023 00:17
Show Gist options
  • Save chanphiromsok/242f804b60473657157b3e1f353d88ee to your computer and use it in GitHub Desktop.
Save chanphiromsok/242f804b60473657157b3e1f353d88ee to your computer and use it in GitHub Desktop.
Permission Util #android #utils #permission
@SuppressLint("WrongConstant")
public static boolean canDrawOverlays(Context context) {
if (Build.VERSION.SDK_INT >= 23) {
return Settings.canDrawOverlays(context);
} else {
if (Build.MANUFACTURER.equalsIgnoreCase("xiaomi")) {
return true;
}
AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService("appops");
Method method = null;
try {
if (Build.VERSION.SDK_INT >= 19) {
method = AppOpsManager.class.getMethod("checkOpNoThrow", Integer.TYPE, Integer.TYPE, String.class);
}
method.setAccessible(true);
return (Integer) method.invoke(appOpsManager, 24, Binder.getCallingUid(), context.getPackageName()) == 0;
} catch (Exception e) {
e.printStackTrace();
return true;
}
}
}
public static boolean openSettingPermission(Context context, Intent intent) {
if (context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY).size() > 0) {
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
return true;
}
new StringBuilder("Intent is not available! ").append(intent);
return false;
}
public static boolean allow(Context context, String... permissions) {
if (Build.VERSION.SDK_INT < 23) {
return true;
}
return PermissionUtils.hasSelfPermissions(context, permissions);
}
public static List<String> getPermissionsNotAllow(Context context, String... permissions) {
List<String> permissionList = new ArrayList<>();
for (String str : permissions) {
if (!allow(context, str)) {
permissionList.add(str);
}
}
return permissionList;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment