实现自定义用户类型

Android 11 引入了定义明确的用户类型的概念,代表了 Android 多用户功能允许的所有不同类型的用户。借助此功能,OEM 可以自定义预定义的 AOSP 用户类型并定义新的配置文件类型。有关详细信息,请参阅有关用户类型的部分。

本页详细介绍了自定义用户类型所需的实施指南。

定制

为了自定义 AOSP 用户类型并定义新的配置文件类型,OEM 必须使用所需的自定义覆盖config_user_types.xmlconfig_user_types.xml文件包含参考实现和可配置属性的综合列表。

config_user_types.xml文件中指定的任何属性(例如default-restrictions )都会覆盖 AOSP 默认值。任何未指定的属性都遵循 AOSP 默认值。更改大多数属性(例如配置文件类型的徽章属性)会影响该用户类型的预先存在的用户。但是,由于default-restrictions仅在创建用户时应用,因此修改此特定属性(如果 OTA 更改config_user_types.xml文件)对预先存在的用户没有影响。同样,指定最大用户数仅适用于创建新用户时;现有用户不会被删除。

每种用户类型的当前自定义限制如下:

  • 配置文件可以完全定制和定义。在这种情况下,OEM 负责根据需要进行平台修改,以便在 Android 中支持其自定义配置文件,因为 AOSP 仅支持预定义的 AOSP 用户类型。
  • 无法定义完整用户,只能自定义其default-restrictions属性。
  • 无法使用此机制自定义系统用户。在这种情况下,可以使用com.android.internal.R.array.config_defaultFirstUserRestrictions设置default-restrictions 。有关详细信息,请参阅config.xml

修改现有用户类型

可以通过覆盖其属性来自定义现有用户类型,如以下代码示例所示。

<user-types version="0">
    <full-type name="android.os.usertype.full.SECONDARY" >
        <default-restrictions no_sms="true" />
    </full-type>

    <profile-type
        name='android.os.usertype.profile.MANAGED'
        max-allowed-per-parent='2'
        icon-badge='@android:drawable/ic_corp_icon_badge_case'
        badge-plain='@android:drawable/ic_corp_badge_case'
        badge-no-background='@android:drawable/ic_corp_badge_no_background' >
        <badge-labels>
            <item res='@android:string/managed_profile_label_badge' />
            <item res='@android:string/managed_profile_label_badge_2' />
        </badge-labels>
        <badge-colors>
            <item res='@android:color/profile_badge_1' />
            <item res='@android:color/profile_badge_2' />
        </badge-colors>
        <default-restrictions no_sms="true" no_outgoing_calls="true" />
    </profile-type>
</user-types>

在此代码示例中,通过修改支持的属性来自定义以下 AOSP 用户类型。

  • 完整用户android.os.usertype.full.SECONDARY

    • no_sms的默认限制通过指定default-restrictions no_sms="true"设置为 true。
  • 配置文件用户android.os.usertype.profile.MANAGED

    • 通过设置max-allowed-per-parent='2'为每个父用户允许两个配置文件。
    • 使用icon-badgebadge-plainbadge-no-backgroundbadge-labelsbadge-colors将徽章属性设置为所需的值。
    • no_smsno_outgoing_calls的默认限制通过指定default-restrictions no_sms="true" no_outgoing_calls="true"

有关这些属性的含义和默认值,请参阅UserTypeFactory.javaUserTypeDetails.java

定义自定义配置文件类型

以下代码示例显示了如何定义新的自定义配置文件类型。

<user-types version="1">
    <profile-type
        name="com.example.profilename"
        max-allowed-per-parent="2" />

    <change-user-type
        from="android.os.usertype.profile.MANAGED"
        to="com.example.profilename"
        whenVersionLeq="1" />
</user-types>

在此代码示例中, com.example.profilename配置文件类型定义如下:

  • 对于每个父用户的两个配置文件, max-allowed-per-parents设置为2

  • change-user-type :通过 OTA 从<= 1user-type版本升级设备时,将所有现有的android.os.usertype.profile.MANAGED类型的托管配置文件转换为新的com.example.profilename类型。