How to get the protection level for each permission?

Im trying to list the protection level for each permission in the selected application, for the code given below. But i do not know how to get it done.

ArrayList list_permission = new ArrayList(); String[] reqp = info.requestedPermissions; if (reqp != null) < for (i = 0; i < reqp.length; i++) < k = i + 1; String a = reqp[i]; if (a.contains("android.permission.")) < String aa[] = a.split("android.permission."); list_permission.add(aa[1]); >else < list_permission.add(a); >> > 
can anyone help me with this. just want to add the protection level in front of the permission. 7,435 2 2 gold badges 37 37 silver badges 44 44 bronze badges asked Nov 23, 2012 at 6:03 119 2 2 silver badges 10 10 bronze badges

do you want to check permission level of installed package ? If yes then check this API developer.android.com/reference/android/content/pm/…

– user756706 Commented Nov 23, 2012 at 6:17

rupertrawnsley.blogspot.de/2011/11/… even this give the whole permission protection levels. but do not know how to code it for the app im doing

Commented Nov 23, 2012 at 6:20

1 Answer 1

You can use PackageManager class getPermissionInfo() method to get PermissionInfo object for any particular permission. PermissionInfo object has property Protection Lavel that can be used to check the protection level of any permission. You can check it against the constant defined in the PermissoinInfo class like PROTECTION_FLAG_SYSTEM .

Like following code :

for (PermissionInfo permission : packageInfo.permissions) < // Dump permission info String protectionLevel; switch(permission.protectionLevel) < case PermissionInfo.PROTECTION_NORMAL : protectionLevel = "normal"; break; case PermissionInfo.PROTECTION_DANGEROUS : protectionLevel = "dangerous"; break; case PermissionInfo.PROTECTION_SIGNATURE : protectionLevel = "signature"; break; case PermissionInfo.PROTECTION_SIGNATURE_OR_SYSTEM : protectionLevel = "signatureOrSystem"; break; default : protectionLevel = ""; break; > Log.i("PermissionCheck", permission.name + " " + protectionLevel); > 

UPDATE:

To get the Protection level of requestedPermissions :

String[] reqp = info.requestedPermissions; String perm = reqp[i]; if (perm.contains("android.permission.")) < try < PermissionInfo pi = getPackageManager().getPermissionInfo(perm, PackageManager.GET_META_DATA); String protctionLevel = "unknown"; switch(pi.protectionLevel) < case PermissionInfo.PROTECTION_NORMAL : protctionLevel = "normal"; break; case PermissionInfo.PROTECTION_DANGEROUS : protctionLevel = "dangerous"; break; case PermissionInfo.PROTECTION_SIGNATURE : protctionLevel = "signature"; break; case PermissionInfo.PROTECTION_SIGNATURE_OR_SYSTEM : protctionLevel = "signatureOrSystem"; break; case PermissionInfo.PROTECTION_FLAG_SYSTEM : protctionLevel = "system"; break; default : protctionLevel = ""; break; > list_permission.add(perm + " "+protctionLevel); > catch (NameNotFoundException e) < // TODO Auto-generated catch block e.printStackTrace(); >> else

Following line would only work on API level 16 or above:

 case PermissionInfo.PROTECTION_FLAG_SYSTEM : protctionLevel = "system"; break;