Android权限 - 默认授予应用运行时权限方法

 

在DefaultPermissionGrantPolicy.java文件中,进行默认授权;
 * This class is the policy for granting runtime permissions to
 * platform components and default handlers in the system such
 * that the device is usable out-of-the-box. For example, the
 * shell UID is a part of the system and the Phone app should
 * have phone related permission by default.
 此类是一种策略类,用于向系统中的平台组件和默认处理程序授予运行时权限,以便设备可以立即使用。
 例如,短信默认被赋予了CONTACTS_PERMISSIONS,SMS_PERMISSIONS,STORAGE_PERMISSIONS等权限;默认情况下,电话应用程序应该具有与电话相关的权限。
 一般是以Group形式授予,如STORAGE_PERMISSIONS,SMS_PERMISSIONS,CONTACTS_PERMISSIONS等。
 
 如:关于对STORAGE_PERMISSIONS的默认授权以及对指定的应用默认授予指定的权限:

private static final Set STORAGE_PERMISSIONS = new ArraySet<>();
 static {
     STORAGE_PERMISSIONS.add(Manifest.permission.READ_EXTERNAL_STORAGE);
     STORAGE_PERMISSIONS.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
     STORAGE_PERMISSIONS.add(Manifest.permission.ACCESS_MEDIA_LOCATION);
 }
public void grantDefaultPermissions(int userId) {
    grantPermissionsToSysComponentsAndPrivApps(userId);
    grantDefaultSystemHandlerPermissions(userId);
    grantDefaultPermissionExceptions(userId);
    synchronized (mLock) {
        mDefaultPermissionsGrantedUsers.put(userId, userId);
    }
}
private void grantDefaultSystemHandlerPermissions(int userId) {
    ......
    String[] smsAppPackageNames = (smsAppPackagesProvider != null)
                ? smsAppPackagesProvider.getPackages(userId) : null;
                
    .... 
    // SMS
    if (smsAppPackageNames == null) {
        String smsPackage = getDefaultSystemHandlerActivityPackageForCategory(
                Intent.CATEGORY_APP_MESSAGING, userId);
        grantDefaultPermissionsToDefaultSystemSmsApp(smsPackage, userId);
    } else {
        for (String smsPackage : smsAppPackageNames) {
            grantDefaultPermissionsToDefaultSystemSmsApp(smsPackage, userId);
        }
    }
    ......

    // Add begin
    // Add by mengqin.zhang,20200418,add default permission for deskclock
        Log.i(TAG, "Granted deskclock POWER_OFF_ALARM permission");
        PackageInfo deskclockPackage = getSystemPackageInfo("xxx.xxx.deskclock");
        if ((deskclockPackage != null) && doesPackageSupportRuntimePermissions(deskclockPackage)) {
            Set cusSetPL = new ArraySet();
            cusSetPL.add("xxx.xxxxxx.permission.POWER_OFF_ALARM");
            grantRuntimePermissions(deskclockPackage, cusSetPL, false, userId);
        }
    // Add end
    ......

 }
private void grantDefaultPermissionsToDefaultSystemSmsApp(String smsPackage, int userId) {
    grantPermissionsToSystemPackage(smsPackage, userId,
            PHONE_PERMISSIONS, CONTACTS_PERMISSIONS, SMS_PERMISSIONS,
            STORAGE_PERMISSIONS, MICROPHONE_PERMISSIONS, CAMERA_PERMISSIONS);
 }

最终调用到grantRuntimePermissions方法,在grantRuntimePermissions方法中,最后调用到getPackageManager().updatePermissionFlags. 最终也是调用到mPermissionManager.updatePermissionFlags.

 

 

 

你可能感兴趣的:(Android权限 - 默认授予应用运行时权限方法)