Android权限和权限组之间的链接

时间:2014-02-08 10:05:37

标签: android permissions

我是Android开发的新手。我正在查看android清单权限。有两件事。权限和权限组。

我知道这个清单

http://developer.android.com/reference/android/Manifest.permission.html

不全面,您也可以添加自己的权限。但是,我想问一下,可以将每个权限映射到

中提到的权限组。

http://developer.android.com/reference/android/Manifest.permission_group.html

4 个答案:

答案 0 :(得分: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 repo上有一些教程。

项目页面:https://ensoftcorp.github.io/android-essentials-toolbox/

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

答案 1 :(得分:6)

可以使用PackageManager getAllPermissionGroups()和queryPermissionsByGroup() 枚举整个Android权限层次结构。下面的代码生成了5.1.1(SDK 22)设备末尾显示的输出。这些团体标记为" personal" groupInfo.flags == 1,并且看起来对应于被称为" dangerous"的权限组。在Marshmallow。

由于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;
}

/** * 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; }

* android.permission-group.ACCESSIBILITY_FEATURES
   no permissions under group

* android.permission-group.ACCOUNTS (personal)
   no permissions under group

* android.permission-group.AFFECTS_BATTERY
   android.permission.CHANGE_WIFI_MULTICAST_STATE
   android.permission.FLASHLIGHT
   android.permission.TRANSMIT_IR
   android.permission.VIBRATE
   android.permission.WAKE_LOCK

* android.permission-group.APP_INFO
   android.permission.GET_TASKS
   android.permission.KILL_BACKGROUND_PROCESSES
   android.permission.MANAGE_ACTIVITY_STACKS
   android.permission.PERSISTENT_ACTIVITY
   android.permission.REAL_GET_TASKS
   android.permission.RECEIVE_BOOT_COMPLETED
   android.permission.REMOVE_TASKS
   android.permission.REORDER_TASKS
   android.permission.RESTART_PACKAGES

* android.permission-group.AUDIO_SETTINGS
   android.permission.MODIFY_AUDIO_SETTINGS

* android.permission-group.BLUETOOTH_NETWORK
   android.permission.BLUETOOTH
   android.permission.BLUETOOTH_ADMIN
   android.permission.BLUETOOTH_MAP
   android.permission.BLUETOOTH_PRIVILEGED

* android.permission-group.BOOKMARKS (personal)
   no permissions under group

* android.permission-group.CALENDAR (personal)
   no permissions under group

* android.permission-group.CAMERA (personal)
   no permissions under group

* android.permission-group.COST_MONEY
   no permissions under group

* android.permission-group.DEVELOPMENT_TOOLS
   android.permission.ACCESS_ALL_EXTERNAL_STORAGE
   android.permission.CHANGE_CONFIGURATION
   android.permission.DUMP
   android.permission.READ_LOGS
   android.permission.SET_ALWAYS_FINISH
   android.permission.SET_DEBUG_APP
   android.permission.SET_PROCESS_LIMIT
   android.permission.SIGNAL_PERSISTENT_PROCESSES
   android.permission.WRITE_SECURE_SETTINGS
   com.android.chrome.permission.DEBUG

* android.permission-group.DEVICE_ALARMS (personal)
   no permissions under group

* android.permission-group.DISPLAY
   android.permission.SYSTEM_ALERT_WINDOW

* android.permission-group.HARDWARE_CONTROLS
   android.permission.ACCESS_FM_RADIO
   android.permission.ACCESS_MTP
   android.permission.HARDWARE_TEST
   android.permission.MANAGE_USB

* android.permission-group.LOCATION (personal)
   no permissions under group

* android.permission-group.MESSAGES (personal)
   no permissions under group

* android.permission-group.MICROPHONE (personal)
   no permissions under group

* android.permission-group.NETWORK
   android.permission.ACCESS_NETWORK_STATE
   android.permission.ACCESS_WIFI_STATE
   android.permission.ACCESS_WIMAX_STATE
   android.permission.BROADCAST_NETWORK_PRIVILEGED
   android.permission.CHANGE_NETWORK_STATE
   android.permission.CHANGE_WIFI_STATE
   android.permission.CHANGE_WIMAX_STATE
   android.permission.CONNECTIVITY_INTERNAL
   android.permission.DOWNLOAD_WITHOUT_NOTIFICATION
   android.permission.INTERNET
   android.permission.LOOP_RADIO
   android.permission.NFC
   android.permission.READ_WIFI_CREDENTIAL
   android.permission.RECEIVE_DATA_ACTIVITY_CHANGE
   android.permission.SCORE_NETWORKS
   com.android.vending.BILLING
   com.android.vending.CHECK_LICENSE
   com.google.android.c2dm.permission.RECEIVE
   com.google.android.c2dm.permission.SEND
   com.google.android.gtalkservice.permission.SEND_HEARTBEAT
   com.google.android.permission.BROADCAST_DATA_MESSAGE
   com.google.android.xmpp.permission.BROADCAST
   com.google.android.xmpp.permission.SEND_RECEIVE
   com.google.android.xmpp.permission.USE_XMPP_ENDPOINT
   com.google.android.xmpp.permission.XMPP_ENDPOINT_BROADCAST
   com.gsma.services.nfc.permission.TRANSACTION_EVENT

* android.permission-group.PERSONAL_INFO (personal)
   no permissions under group

* android.permission-group.PHONE_CALLS (personal)
   no permissions under group

* android.permission-group.SCREENLOCK (personal)
   no permissions under group

* android.permission-group.SOCIAL_INFO (personal)
   no permissions under group

* android.permission-group.STATUS_BAR
   android.permission.EXPAND_STATUS_BAR

* android.permission-group.STORAGE (personal)
   no permissions under group

* android.permission-group.SYNC_SETTINGS
   android.permission.READ_SYNC_SETTINGS
   android.permission.READ_SYNC_STATS
   android.permission.WRITE_SYNC_SETTINGS

* android.permission-group.SYSTEM_CLOCK
   android.permission.SET_TIME_ZONE

* android.permission-group.SYSTEM_TOOLS
   android.permission.ACCESS_LOCATION_EXTRA_COMMANDS
   android.permission.ACCESS_MOCK_LOCATION
   android.permission.ACCESS_PDB_STATE
   android.permission.ASEC_ACCESS
   android.permission.ASEC_CREATE
   android.permission.ASEC_DESTROY
   android.permission.ASEC_MOUNT_UNMOUNT
   android.permission.ASEC_RENAME
   android.permission.BATTERY_STATS
   android.permission.BLUETOOTH_STACK
   android.permission.BROADCAST_PACKAGE_REMOVED
   android.permission.BROADCAST_STICKY
   android.permission.CHANGE_BACKGROUND_DATA_SETTING
   android.permission.CLEAR_APP_CACHE
   android.permission.DIAGNOSTIC
   android.permission.FORCE_STOP_PACKAGES
   android.permission.GET_APP_OPS_STATS
   android.permission.GET_DETAILED_TASKS
   android.permission.GET_PACKAGE_SIZE
   android.permission.GLOBAL_SEARCH
   android.permission.GLOBAL_SEARCH_CONTROL
   android.permission.INTERACT_ACROSS_USERS
   android.permission.INTERACT_ACROSS_USERS_FULL
   android.permission.MANAGE_USERS
   android.permission.MODIFY_APPWIDGET_BIND_PERMISSIONS
   android.permission.MOUNT_FORMAT_FILESYSTEMS
   android.permission.MOUNT_UNMOUNT_FILESYSTEMS
   android.permission.NET_ADMIN
   android.permission.NET_TUNNELING
   android.permission.OEM_UNLOCK_STATE
   android.permission.READ_DREAM_STATE
   android.permission.READ_SEARCH_INDEXABLES
   android.permission.RECOVERY
   android.permission.REMOTE_AUDIO_PLAYBACK
   android.permission.SET_ANIMATION_SCALE
   android.permission.SET_PREFERRED_APPLICATIONS
   android.permission.SET_SCREEN_COMPATIBILITY
   android.permission.SET_WALLPAPER_COMPONENT
   android.permission.START_ANY_ACTIVITY
   android.permission.START_TASKS_FROM_RECENTS
   android.permission.SUBSCRIBED_FEEDS_READ
   android.permission.SUBSCRIBED_FEEDS_WRITE
   android.permission.WRITE_APN_SETTINGS
   android.permission.WRITE_DREAM_STATE
   android.permission.WRITE_SETTINGS
   com.android.launcher.permission.INSTALL_SHORTCUT
   com.android.launcher.permission.UNINSTALL_SHORTCUT
   com.android.launcher3.permission.READ_SETTINGS
   com.android.launcher3.permission.WRITE_SETTINGS
   com.google.android.launcher.permission.READ_SETTINGS
   com.google.android.launcher.permission.WRITE_SETTINGS
   com.motorola.permission.ACCESS_COGRADIO

* android.permission-group.USER_DICTIONARY (personal)
   no permissions under group

* android.permission-group.VOICEMAIL (personal)
   no permissions under group

* android.permission-group.WALLPAPER
   android.permission.SET_WALLPAPER
   android.permission.SET_WALLPAPER_HINTS

* android.permission-group.WRITE_USER_DICTIONARY
   android.permission.WRITE_USER_DICTIONARY

* com.google.android.gms.permission.CAR_INFORMATION
   com.google.android.gms.permission.CAR_FUEL
   com.google.android.gms.permission.CAR_MILEAGE
   com.google.android.gms.permission.CAR_VENDOR_EXTENSION

* null
   no permissions under group

答案 2 :(得分:2)

API指南中有一个表,但仅适用于危险权限:

https://developer.android.com/guide/topics/security/permissions.html#perm-groups

答案 3 :(得分:0)

据我所知:Manifest.permission_group * AB * = Manifest.permission * A * + Manifest.permission * B * 看看这个例子:

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

我希望你明白这一点。