2014-02-08 36 views

回答

7

Android源包含默認權限組和權限之間的實際映射。

https://github.com/android/platform_frameworks_base/blob/master/core/res/AndroidManifest.xml

如果你想從權限組映射允許你可以採取你正在使用的Android版本AndroidMainifest.xml文件,並解析XML創建映射。所以要回答你的問題是的,這是可能的,但這聽起來像這可能是爲你的任務矯枉過正。


更新:因爲我實際上需要這個映射我自己的另一個項目,我決定分享我的代碼來創建此映射。您可以在github存儲庫中查看結果。

鏈接:https://github.com/benjholla/AndroidPermissionAttributeMapper


更新2:

我的組開源了我們的解決方案產生這種映射和周圍Android文檔一些其他的對象包裝的權限。在項目頁面和github回購中有一些教程。

項目頁:https://ensoftcorp.github.io/android-essentials-toolbox/

來源:https://github.com/EnSoftCorp/android-essentials-toolbox

0

據我所知:Manifest.permission_group * AB * = * Manifest.permission一個* + Manifest.permission * * 看看這個例子:

android.permission-group.MESSAGES = SEND_SMS + WRITE_SMS + RECEIVE_SMS + READ_SMS + BROADCAST_SMS

我希望你明白了。

+0

這是對個人權限組合成一組的方式。我想知道,開發人員論壇中提到的組和個人權限之間已經存在映射。例如,有一個名爲「BLUETOOTH_NETWORK」的許可組。該組最有可能包含三個權限,即「BLUETOOTH」,「BLUETOOTH_ADMIN」和「BLUETOOTH_PRIVILEGED」。我自己繪製了這些圖。如果有任何資源可用,我想完成映射。 – Faheem

+0

@Faheem我不知道用法,但任何人都可以自由地將權限映射到權限組。使用頁面中給出的描述來搜索關鍵字。例如;搜索「短信」,你會發現相關的權限。 – Shervin

6

它可以使用PackageManager getAllPermissionGroups( )和queryPermissionsByGroup() 來枚舉整個Android權限層次結構。下面的代碼在5.1.1(SDK 22)設備的末尾生成了輸出結果。標記爲「個人」的組具有groupInfo.flags == 1,並且看起來與在棉花糖中被稱爲「危險」的權限組相對應。

由於SDK級別的差異以及應用程序可以定義自定義權限的事實,您將在不同設備上獲得不同的權限層次結構。

/** 
* Uses PackageManager getAllPermissionGroups() and queryPermissionsByGroup() 
* to enumerate the Android permission hierarchy. 
*/ 
private void showPermissionTree() 
{ 
    final PackageManager pm = m_context.getPackageManager(); 
    if (pm == null) 
     return; 

    /* 
    * Get a list of all permission groups and sort them alphabetically. 
    * Then add to the end of the list the special case of a null group name. There can be 
    * numerous permissions that are not listed under a group name. 
    */ 
    List<PermissionGroupInfo> groupInfoList = pm.getAllPermissionGroups(0); 
    if (groupInfoList == null) 
     return; 

    ArrayList<String> groupNameList = new ArrayList<>(); 
    for (PermissionGroupInfo groupInfo : groupInfoList) { 
     String groupName = groupInfo.name; 
     if (groupName != null) { 
      if (Build.VERSION.SDK_INT >= 17) { 
       /* 
       * SDK 17 added the flags field. If non-zero, the permission group contains 
       * permissions that control access to user personal data. 
       * N.B. These are the permissions groups that are called "dangerous" in 
       * Marshmallow. 
       */ 
       if (groupInfo.flags != 0) { 
        groupName += " (personal)"; 
       } 
      } 
      groupNameList.add(groupName); 
     } 
    } 

    Collections.sort(groupNameList); 
    groupNameList.add(null); 

    /* 
    * Loop though each permission group, adding to the StringBuilder the group name and 
    * the list of all permissions under that group. 
    */ 
    StringBuilder sb = new StringBuilder(10000); 
    final String INDENT = " "; 

    for (String groupName : groupNameList) { 
     if (groupName == null) 
      groupName = "null"; 

     sb.append("* ").append(groupName).append("\n"); 

     ArrayList<String> permissionNameList = getPermissionsForGroup(groupName); 
     if (permissionNameList.size() > 0) { 
      for (String permission : permissionNameList) { 
       sb.append(INDENT).append(permission).append("\n"); 
      } 
     } else { 
      sb.append(INDENT).append("no permissions under group\n"); 
     } 

     sb.append("\n"); 
    } 

    m_textView.setText(sb.toString()); 
} 


/* 
* Gets and returns a list of all permission under the specified group, sorted alphabetically. 
* 
* N.B. groupName can be null. The docs for PackageManager.queryPermissionsByGroup() say 
* "Use null to find all of the permissions not associated with a group." 
*/ 
private ArrayList<String> getPermissionsForGroup(String groupName) 
{ 
    final PackageManager pm = m_context.getPackageManager(); 
    final ArrayList<String> permissionNameList = new ArrayList<>(); 

    try { 
     List<PermissionInfo> permissionInfoList = 
       pm.queryPermissionsByGroup(groupName, PackageManager.GET_META_DATA); 
     if (permissionInfoList != null) { 
      for (PermissionInfo permInfo : permissionInfoList) { 
       String permName = permInfo.name; 
       if (permName == null) { 
        permName = "null"; 
       } else if (permName.isEmpty()) { 
        permName = "empty"; 
       } 
       permissionNameList.add(permName); 
      } 
     } 
    } 
    catch (PackageManager.NameNotFoundException e) { 
     // e.printStackTrace(); 
     Log.d(TAG, "permissions not found for group = " + groupName); 
    } 

    Collections.sort(permissionNameList); 

    return permissionNameList; 
} 

​​3210