Merge "Move UserTypeDetails & UserTypeFactory to services"

This commit is contained in:
TreeHugger Robot
2019-11-21 00:43:29 +00:00
committed by Android (Google) Code Review
3 changed files with 5 additions and 6 deletions

View File

@@ -26,8 +26,6 @@ import android.os.UserHandle;
import android.os.UserManager;
import android.util.DebugUtils;
import com.android.server.pm.UserTypeDetails;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@@ -181,7 +179,7 @@ public class UserInfo implements Parcelable {
/**
* Type of user, such as {@link UserManager#USER_TYPE_PROFILE_MANAGED}, corresponding to
* {@link UserTypeDetails#getName()}.
* {@link com.android.server.pm.UserTypeDetails#getName()}.
*/
public String userType;
@@ -195,9 +193,10 @@ public class UserInfo implements Parcelable {
public int restrictedProfileParentId;
/**
* Which badge color/label to use within a particular {@link UserTypeDetails}, i.e.
* the badgeIndex.
* This is an index for distinguishing different profiles with the same parent and user type.
* Index for distinguishing different profiles with the same parent and user type for the
* purpose of badging.
* It is used for determining which badge color/label to use (if applicable) from
* the options available for a particular user type.
*/
public int profileBadge;

View File

@@ -1,388 +0,0 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.pm;
import android.annotation.ColorRes;
import android.annotation.DrawableRes;
import android.annotation.NonNull;
import android.annotation.StringRes;
import android.content.pm.UserInfo;
import android.content.pm.UserInfo.UserInfoFlag;
import android.content.res.Resources;
import android.os.UserManager;
import com.android.internal.util.Preconditions;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Contains the details about a multiuser "user type", such as a
* {@link UserManager#USER_TYPE_PROFILE_MANAGED}.
*
* Tests are located in UserManagerServiceUserTypeTest.java.
* @hide
*/
public final class UserTypeDetails {
/** Indicates that there is no limit to the number of users allowed. */
public static final int UNLIMITED_NUMBER_OF_USERS = -1;
/** Name of the user type, such as {@link UserManager#USER_TYPE_PROFILE_MANAGED}. */
private final @NonNull String mName;
// TODO(b/142482943): Currently unused. Hook this up.
private final boolean mEnabled;
// TODO(b/142482943): Currently unused and not set. Hook this up.
private final int mLabel;
/**
* Maximum number of this user type allowed on the device.
* Use {@link #UNLIMITED_NUMBER_OF_USERS} to indicate that there is no hard limit.
*/
private final int mMaxAllowed;
/**
* Maximum number of this user type allowed per parent (for user types, like profiles, that
* have parents).
* Use {@link #UNLIMITED_NUMBER_OF_USERS} to indicate that there is no hard limit.
*/
// TODO(b/142482943): Should this also apply to restricted profiles?
private final int mMaxAllowedPerParent;
// TODO(b/143784345): Update doc when we clean up UserInfo.
/** The {@link UserInfo.UserInfoFlag} representing the base type of this user. */
private final @UserInfoFlag int mBaseType;
// TODO(b/143784345): Update doc/name when we clean up UserInfo.
/** The {@link UserInfo.UserInfoFlag}s that all users of this type will automatically have. */
private final @UserInfoFlag int mDefaultUserInfoPropertyFlags;
// TODO(b/142482943): Hook these up to something and set them for each type.
private final List<String> mDefaultRestrictions;
// Fields for profiles only, controlling the nature of their badges.
// All badge information should be set if {@link #hasBadge()} is true.
/** Resource ID of the badge put on icons. */
private @DrawableRes final int mIconBadge;
/** Resource ID of the badge. Should be set if mIconBadge is set. */
private @DrawableRes final int mBadgePlain;
/** Resource ID of the badge without a background. Should be set if mIconBadge is set. */
private @DrawableRes final int mBadgeNoBackground;
/**
* Resource ID ({@link StringRes}) of the of the labels to describe badged apps; should be the
* same format as com.android.internal.R.color.profile_badge_1. These are used for accessibility
* services.
*
* <p>This is an array because, in general, there may be multiple users of the same user type.
* In this case, the user is indexed according to its {@link UserInfo#profileBadge}.
*
* <p>Must be set if mIconBadge is set.
*/
private final int[] mBadgeLabels;
/**
* Resource ID ({@link ColorRes}) of the colors badge put on icons.
* (The value is a resource ID referring to the color; it is not the color value itself).
*
* <p>This is an array because, in general, there may be multiple users of the same user type.
* In this case, the user is indexed according to its {@link UserInfo#profileBadge}.
*
* <p>Must be set if mIconBadge is set.
*/
private final int[] mBadgeColors;
private UserTypeDetails(@NonNull String name, boolean enabled, int maxAllowed,
@UserInfoFlag int baseType, @UserInfoFlag int defaultUserInfoPropertyFlags, int label,
int maxAllowedPerParent,
int iconBadge, int badgePlain, int badgeNoBackground,
int[] badgeLabels, int[] badgeColors,
ArrayList<String> defaultRestrictions) {
this.mName = name;
this.mEnabled = enabled;
this.mMaxAllowed = maxAllowed;
this.mMaxAllowedPerParent = maxAllowedPerParent;
this.mBaseType = baseType;
this.mDefaultUserInfoPropertyFlags = defaultUserInfoPropertyFlags;
this.mDefaultRestrictions =
Collections.unmodifiableList(new ArrayList<>(defaultRestrictions));
this.mIconBadge = iconBadge;
this.mBadgePlain = badgePlain;
this.mBadgeNoBackground = badgeNoBackground;
this.mLabel = label;
this.mBadgeLabels = badgeLabels;
this.mBadgeColors = badgeColors;
}
/**
* Returns the name of the user type, such as {@link UserManager#USER_TYPE_PROFILE_MANAGED}.
*/
public String getName() {
return mName;
}
// TODO(b/142482943) Hook this up or delete it.
public boolean isEnabled() {
return mEnabled;
}
/**
* Returns the maximum number of this user type allowed on the device.
* <p>Returns {@link #UNLIMITED_NUMBER_OF_USERS} to indicate that there is no hard limit.
*/
public int getMaxAllowed() {
return mMaxAllowed;
}
/**
* Returns the maximum number of this user type allowed per parent (for user types, like
* profiles, that have parents).
* <p>Returns {@link #UNLIMITED_NUMBER_OF_USERS} to indicate that there is no hard limit.
*/
public int getMaxAllowedPerParent() {
return mMaxAllowedPerParent;
}
// TODO(b/143784345): Update comment when UserInfo is reorganized.
/** The {@link UserInfo.UserInfoFlag}s that all users of this type will automatically have. */
public int getDefaultUserInfoFlags() {
return mDefaultUserInfoPropertyFlags | mBaseType;
}
// TODO(b/142482943) Hook this up; it is currently unused.
public int getLabel() {
return mLabel;
}
/** Returns whether users of this user type should be badged. */
public boolean hasBadge() {
return mIconBadge != Resources.ID_NULL;
}
/** Resource ID of the badge put on icons. */
public @DrawableRes int getIconBadge() {
return mIconBadge;
}
/** Resource ID of the badge. Used for {@link UserManager#getUserBadgeResId(int)}. */
public @DrawableRes int getBadgePlain() {
return mBadgePlain;
}
/** Resource ID of the badge without a background. */
public @DrawableRes int getBadgeNoBackground() {
return mBadgeNoBackground;
}
/**
* Returns the Resource ID of the badgeIndexth badge label, where the badgeIndex is expected
* to be the {@link UserInfo#profileBadge} of the user.
* If badgeIndex exceeds the number of labels, returns the label for the highest index.
*/
public @StringRes int getBadgeLabel(int badgeIndex) {
if (mBadgeLabels == null || mBadgeLabels.length == 0 || badgeIndex < 0) {
return Resources.ID_NULL;
}
return mBadgeLabels[Math.min(badgeIndex, mBadgeLabels.length - 1)];
}
/**
* Returns the Resource ID of the badgeIndexth badge color, where the badgeIndex is expected
* to be the {@link UserInfo#profileBadge} of the user.
* If badgeIndex exceeds the number of colors, returns the color for the highest index.
*/
public @ColorRes int getBadgeColor(int badgeIndex) {
if (mBadgeColors == null || mBadgeColors.length == 0 || badgeIndex < 0) {
return Resources.ID_NULL;
}
return mBadgeColors[Math.min(badgeIndex, mBadgeColors.length - 1)];
}
public boolean isProfile() {
return (mBaseType & UserInfo.FLAG_PROFILE) != 0;
}
// TODO(b/142482943): Hook this up and don't return the original.
public List<String> getDefaultRestrictions() {
return mDefaultRestrictions;
}
/** Dumps details of the UserTypeDetails. Do not parse this. */
public void dump(PrintWriter pw) {
final String prefix = " ";
pw.print(prefix); pw.print("mName: "); pw.println(mName);
pw.print(prefix); pw.print("mBaseType: "); pw.println(UserInfo.flagsToString(mBaseType));
pw.print(prefix); pw.print("mEnabled: "); pw.println(mEnabled);
pw.print(prefix); pw.print("mMaxAllowed: "); pw.println(mMaxAllowed);
pw.print(prefix); pw.print("mMaxAllowedPerParent: "); pw.println(mMaxAllowedPerParent);
pw.print(prefix); pw.print("mDefaultUserInfoFlags: ");
pw.println(UserInfo.flagsToString(mDefaultUserInfoPropertyFlags));
pw.print(prefix); pw.print("mLabel: "); pw.println(mLabel);
pw.print(prefix); pw.print("mDefaultRestrictions: "); pw.println(mDefaultRestrictions);
pw.print(prefix); pw.print("mIconBadge: "); pw.println(mIconBadge);
pw.print(prefix); pw.print("mBadgePlain: "); pw.println(mBadgePlain);
pw.print(prefix); pw.print("mBadgeNoBackground: "); pw.println(mBadgeNoBackground);
pw.print(prefix); pw.print("mBadgeLabels.length: ");
pw.println(mBadgeLabels != null ? mBadgeLabels.length : "0(null)");
pw.print(prefix); pw.print("mBadgeColors.length: ");
pw.println(mBadgeColors != null ? mBadgeColors.length : "0(null)");
}
/** Builder for a {@link UserTypeDetails}; see that class for documentation. */
public static final class Builder {
// UserTypeDetails properties and their default values.
private String mName; // This MUST be explicitly set.
private int mBaseType; // This MUST be explicitly set.
private int mMaxAllowed = UNLIMITED_NUMBER_OF_USERS;
private int mMaxAllowedPerParent = UNLIMITED_NUMBER_OF_USERS;
private int mDefaultUserInfoPropertyFlags = 0;
private ArrayList<String> mDefaultRestrictions = new ArrayList<>();
private boolean mEnabled = true;
private int mLabel = Resources.ID_NULL;
private int[] mBadgeLabels = null;
private int[] mBadgeColors = null;
private int mIconBadge = Resources.ID_NULL;
private int mBadgePlain = Resources.ID_NULL;
private int mBadgeNoBackground = Resources.ID_NULL;
public Builder setName(String name) {
mName = name;
return this;
}
public Builder setEnabled(boolean enabled) {
mEnabled = enabled;
return this;
}
public Builder setMaxAllowed(int maxAllowed) {
mMaxAllowed = maxAllowed;
return this;
}
public Builder setMaxAllowedPerParent(int maxAllowedPerParent) {
mMaxAllowedPerParent = maxAllowedPerParent;
return this;
}
public Builder setBaseType(@UserInfoFlag int baseType) {
mBaseType = baseType;
return this;
}
public Builder setDefaultUserInfoPropertyFlags(@UserInfoFlag int flags) {
mDefaultUserInfoPropertyFlags = flags;
return this;
}
public Builder setBadgeLabels(int ... badgeLabels) {
mBadgeLabels = badgeLabels;
return this;
}
public Builder setBadgeColors(int ... badgeColors) {
mBadgeColors = badgeColors;
return this;
}
public Builder setIconBadge(int badgeIcon) {
mIconBadge = badgeIcon;
return this;
}
public Builder setBadgePlain(int badgePlain) {
mBadgePlain = badgePlain;
return this;
}
public Builder setBadgeNoBackground(int badgeNoBackground) {
mBadgeNoBackground = badgeNoBackground;
return this;
}
public Builder setLabel(int label) {
mLabel = label;
return this;
}
public Builder setDefaultRestrictions(ArrayList<String> restrictions) {
mDefaultRestrictions = restrictions;
return this;
}
public UserTypeDetails createUserTypeDetails() {
Preconditions.checkArgument(mName != null,
"Cannot create a UserTypeDetails with no name.");
Preconditions.checkArgument(hasValidBaseType(),
"UserTypeDetails " + mName + " has invalid baseType: " + mBaseType);
Preconditions.checkArgument(hasValidPropertyFlags(),
"UserTypeDetails " + mName + " has invalid flags: "
+ Integer.toHexString(mDefaultUserInfoPropertyFlags));
if (hasBadge()) {
Preconditions.checkArgument(mBadgeLabels != null && mBadgeLabels.length != 0,
"UserTypeDetails " + mName + " has badge but no badgeLabels.");
Preconditions.checkArgument(mBadgeColors != null && mBadgeColors.length != 0,
"UserTypeDetails " + mName + " has badge but no badgeColors.");
}
return new UserTypeDetails(mName, mEnabled, mMaxAllowed, mBaseType,
mDefaultUserInfoPropertyFlags, mLabel, mMaxAllowedPerParent,
mIconBadge, mBadgePlain, mBadgeNoBackground, mBadgeLabels, mBadgeColors,
mDefaultRestrictions);
}
private boolean hasBadge() {
return mIconBadge != Resources.ID_NULL;
}
// TODO(b/143784345): Refactor this when we clean up UserInfo.
private boolean hasValidBaseType() {
return mBaseType == UserInfo.FLAG_FULL
|| mBaseType == UserInfo.FLAG_PROFILE
|| mBaseType == UserInfo.FLAG_SYSTEM
|| mBaseType == (UserInfo.FLAG_FULL | UserInfo.FLAG_SYSTEM);
}
// TODO(b/143784345): Refactor this when we clean up UserInfo.
private boolean hasValidPropertyFlags() {
final int forbiddenMask =
UserInfo.FLAG_PRIMARY |
UserInfo.FLAG_ADMIN |
UserInfo.FLAG_INITIALIZED |
UserInfo.FLAG_QUIET_MODE |
UserInfo.FLAG_FULL |
UserInfo.FLAG_SYSTEM |
UserInfo.FLAG_PROFILE;
return (mDefaultUserInfoPropertyFlags & forbiddenMask) == 0;
}
}
/**
* Returns whether the user type is a managed profile
* (i.e. {@link UserManager#USER_TYPE_PROFILE_MANAGED}).
*/
public boolean isManagedProfile() {
return UserManager.isUserTypeManagedProfile(mName);
}
}

View File

@@ -1,168 +0,0 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.pm;
import static android.content.pm.UserInfo.FLAG_DEMO;
import static android.content.pm.UserInfo.FLAG_EPHEMERAL;
import static android.content.pm.UserInfo.FLAG_FULL;
import static android.content.pm.UserInfo.FLAG_GUEST;
import static android.content.pm.UserInfo.FLAG_MANAGED_PROFILE;
import static android.content.pm.UserInfo.FLAG_PROFILE;
import static android.content.pm.UserInfo.FLAG_RESTRICTED;
import static android.content.pm.UserInfo.FLAG_SYSTEM;
import static android.os.UserManager.USER_TYPE_FULL_DEMO;
import static android.os.UserManager.USER_TYPE_FULL_GUEST;
import static android.os.UserManager.USER_TYPE_FULL_RESTRICTED;
import static android.os.UserManager.USER_TYPE_FULL_SECONDARY;
import static android.os.UserManager.USER_TYPE_FULL_SYSTEM;
import static android.os.UserManager.USER_TYPE_PROFILE_MANAGED;
import static android.os.UserManager.USER_TYPE_SYSTEM_HEADLESS;
import static com.android.server.pm.UserTypeDetails.UNLIMITED_NUMBER_OF_USERS;
import android.content.res.Resources;
import android.os.UserManager;
import android.util.ArrayMap;
/**
* Class for creating all {@link UserTypeDetails} on the device.
*
* Tests are located in UserManagerServiceUserTypeTest.java.
* @hide
*/
public final class UserTypeFactory {
/** This is a utility class, so no instantiable constructor. */
private UserTypeFactory() {}
/**
* Obtains the user types (built-in and customized) for this device.
*
* @return mapping from the name of each user type to its {@link UserTypeDetails} object
*/
public static ArrayMap<String, UserTypeDetails> getUserTypes() {
final ArrayMap<String, UserTypeDetails> map = new ArrayMap<>();
// TODO(b/142482943): Read an xml file for OEM customized types.
// Remember to disallow "android." namespace
// TODO(b/142482943): Read an xml file to get any overrides for the built-in types.
final int maxManagedProfiles = 1;
map.put(USER_TYPE_PROFILE_MANAGED,
getDefaultTypeProfileManaged().setMaxAllowedPerParent(maxManagedProfiles)
.createUserTypeDetails());
map.put(USER_TYPE_FULL_SYSTEM, getDefaultTypeSystemFull().createUserTypeDetails());
map.put(USER_TYPE_FULL_SECONDARY, getDefaultTypeFullSecondary().createUserTypeDetails());
map.put(USER_TYPE_FULL_GUEST, getDefaultTypeFullGuest().createUserTypeDetails());
map.put(USER_TYPE_FULL_DEMO, getDefaultTypeFullDemo().createUserTypeDetails());
map.put(USER_TYPE_FULL_RESTRICTED, getDefaultTypeFullRestricted().createUserTypeDetails());
map.put(USER_TYPE_SYSTEM_HEADLESS, getDefaultTypeSystemHeadless().createUserTypeDetails());
return map;
}
/**
* Returns the Builder for the default {@link UserManager#USER_TYPE_PROFILE_MANAGED}
* configuration.
*/
private static UserTypeDetails.Builder getDefaultTypeProfileManaged() {
return new UserTypeDetails.Builder()
.setName(USER_TYPE_PROFILE_MANAGED)
.setBaseType(FLAG_PROFILE)
.setDefaultUserInfoPropertyFlags(FLAG_MANAGED_PROFILE)
.setMaxAllowedPerParent(1)
.setLabel(0)
.setIconBadge(com.android.internal.R.drawable.ic_corp_icon_badge_case)
.setBadgePlain(com.android.internal.R.drawable.ic_corp_badge_case)
.setBadgeNoBackground(com.android.internal.R.drawable.ic_corp_badge_no_background)
.setBadgeLabels(
com.android.internal.R.string.managed_profile_label_badge,
com.android.internal.R.string.managed_profile_label_badge_2,
com.android.internal.R.string.managed_profile_label_badge_3)
.setBadgeColors(
com.android.internal.R.color.profile_badge_1,
com.android.internal.R.color.profile_badge_2,
com.android.internal.R.color.profile_badge_3);
}
/**
* Returns the Builder for the default {@link UserManager#USER_TYPE_FULL_SECONDARY}
* configuration.
*/
private static UserTypeDetails.Builder getDefaultTypeFullSecondary() {
return new UserTypeDetails.Builder()
.setName(USER_TYPE_FULL_SECONDARY)
.setBaseType(FLAG_FULL)
.setMaxAllowed(UNLIMITED_NUMBER_OF_USERS);
}
/**
* Returns the Builder for the default {@link UserManager#USER_TYPE_FULL_GUEST} configuration.
*/
private static UserTypeDetails.Builder getDefaultTypeFullGuest() {
final boolean ephemeralGuests = Resources.getSystem()
.getBoolean(com.android.internal.R.bool.config_guestUserEphemeral);
final int flags = FLAG_GUEST | (ephemeralGuests ? FLAG_EPHEMERAL : 0);
// TODO(b/142482943): Put UMS.initDefaultGuestRestrictions() here; then fetch them from here
return new UserTypeDetails.Builder()
.setName(USER_TYPE_FULL_GUEST)
.setBaseType(FLAG_FULL)
.setDefaultUserInfoPropertyFlags(flags)
.setMaxAllowed(1);
}
/**
* Returns the Builder for the default {@link UserManager#USER_TYPE_FULL_DEMO} configuration.
*/
private static UserTypeDetails.Builder getDefaultTypeFullDemo() {
return new UserTypeDetails.Builder()
.setName(USER_TYPE_FULL_DEMO)
.setBaseType(FLAG_FULL)
.setDefaultUserInfoPropertyFlags(FLAG_DEMO)
.setMaxAllowed(UNLIMITED_NUMBER_OF_USERS);
}
/**
* Returns the Builder for the default {@link UserManager#USER_TYPE_FULL_RESTRICTED}
* configuration.
*/
private static UserTypeDetails.Builder getDefaultTypeFullRestricted() {
return new UserTypeDetails.Builder()
.setName(USER_TYPE_FULL_RESTRICTED)
.setBaseType(FLAG_FULL)
.setDefaultUserInfoPropertyFlags(FLAG_RESTRICTED)
.setMaxAllowed(UNLIMITED_NUMBER_OF_USERS);
}
/**
* Returns the Builder for the default {@link UserManager#USER_TYPE_FULL_SYSTEM} configuration.
*/
private static UserTypeDetails.Builder getDefaultTypeSystemFull() {
return new UserTypeDetails.Builder()
.setName(USER_TYPE_FULL_SYSTEM)
.setBaseType(FLAG_SYSTEM | FLAG_FULL);
}
/**
* Returns the Builder for the default {@link UserManager#USER_TYPE_SYSTEM_HEADLESS}
* configuration.
*/
private static UserTypeDetails.Builder getDefaultTypeSystemHeadless() {
return new UserTypeDetails.Builder()
.setName(USER_TYPE_SYSTEM_HEADLESS)
.setBaseType(FLAG_SYSTEM);
}
}