System UserInfo flags are set in UserTypeFactory

The System's default UserInfo flags are set in two possible places. We
move the "user-type" flags to UserTypeFactory, consistent with other
user types. This is just aesthetics. There is no functional change here.

We also mark FLAG_PRIMARY and isPrimary as deprecated. They are merely
synonymous with FLAG_SYSTEM and userId == 0.

Bug: 267536424
Test: Flash HSUM and non-HSUM builds and ensure system has correct flags
Change-Id: I759409d907120043f20b2341655a65cb372a0d21
This commit is contained in:
Adam Bookatz
2023-02-02 18:51:43 -08:00
parent 51a18cab4f
commit 00034aa376
6 changed files with 53 additions and 19 deletions

View File

@@ -915,7 +915,7 @@ package android.content.pm {
method public boolean isInitialized(); method public boolean isInitialized();
method public boolean isMain(); method public boolean isMain();
method public boolean isManagedProfile(); method public boolean isManagedProfile();
method public boolean isPrimary(); method @Deprecated public boolean isPrimary();
method public boolean isProfile(); method public boolean isProfile();
method public boolean isQuietModeEnabled(); method public boolean isQuietModeEnabled();
method public boolean isRestricted(); method public boolean isRestricted();
@@ -932,7 +932,7 @@ package android.content.pm {
field public static final int FLAG_INITIALIZED = 16; // 0x10 field public static final int FLAG_INITIALIZED = 16; // 0x10
field public static final int FLAG_MAIN = 16384; // 0x4000 field public static final int FLAG_MAIN = 16384; // 0x4000
field @Deprecated public static final int FLAG_MANAGED_PROFILE = 32; // 0x20 field @Deprecated public static final int FLAG_MANAGED_PROFILE = 32; // 0x20
field public static final int FLAG_PRIMARY = 1; // 0x1 field @Deprecated public static final int FLAG_PRIMARY = 1; // 0x1
field public static final int FLAG_PROFILE = 4096; // 0x1000 field public static final int FLAG_PROFILE = 4096; // 0x1000
field public static final int FLAG_QUIET_MODE = 128; // 0x80 field public static final int FLAG_QUIET_MODE = 128; // 0x80
field @Deprecated public static final int FLAG_RESTRICTED = 8; // 0x8 field @Deprecated public static final int FLAG_RESTRICTED = 8; // 0x8

View File

@@ -59,10 +59,17 @@ public class UserInfo implements Parcelable {
*/ */
/** /**
* Primary user. Only one user can have this flag set. It identifies the first human user * Primary user. In practice, this is just synonymous with {@link #FLAG_SYSTEM}.
* on a device. This flag is not supported in headless system user mode. *
* <p>On many devices, this will also be the first human user.
* However, in {@link UserManager#isHeadlessSystemUserMode() headless system user mode}, this
* should be regarded as unsupported since the system user may not be a human.
*
* @deprecated For checking for user 0, use {@link #FLAG_SYSTEM}.
* For checking for the designated "main human user", use {@link #FLAG_MAIN}.
*/ */
@UnsupportedAppUsage @UnsupportedAppUsage
@Deprecated
public static final int FLAG_PRIMARY = 0x00000001; public static final int FLAG_PRIMARY = 0x00000001;
/** /**
@@ -334,7 +341,12 @@ public class UserInfo implements Parcelable {
} }
} }
/**
* @deprecated For checking for user 0, compare {@link #id} to {@link UserHandle#USER_SYSTEM}.
* For checking for the designated "main human user", use {@link #isMain()}.
*/
@UnsupportedAppUsage @UnsupportedAppUsage
@Deprecated
public boolean isPrimary() { public boolean isPrimary() {
return (flags & FLAG_PRIMARY) == FLAG_PRIMARY; return (flags & FLAG_PRIMARY) == FLAG_PRIMARY;
} }

View File

@@ -4372,11 +4372,16 @@ public class UserManager {
} }
/** /**
* Returns information for Primary user. * Returns information for Primary user (which in practice is the same as the System user).
* *
* @return the Primary user, null if not found. * @return the Primary user, null if not found.
* @deprecated For the system user, call {@link #getUserInfo} on {@link UserHandle#USER_SYSTEM},
* or just use {@link UserHandle#SYSTEM} or {@link UserHandle#USER_SYSTEM}.
* For the designated MainUser, use {@link #getMainUser()}.
*
* @hide * @hide
*/ */
@Deprecated
@RequiresPermission(android.Manifest.permission.MANAGE_USERS) @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
public @Nullable UserInfo getPrimaryUser() { public @Nullable UserInfo getPrimaryUser() {
try { try {

View File

@@ -4001,22 +4001,22 @@ public class UserManagerService extends IUserManager.Stub {
@GuardedBy({"mPackagesLock"}) @GuardedBy({"mPackagesLock"})
private void fallbackToSingleUserLP() { private void fallbackToSingleUserLP() {
int flags = UserInfo.FLAG_SYSTEM | UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_ADMIN
| UserInfo.FLAG_PRIMARY;
// Create the system user // Create the system user
String systemUserType = isDefaultHeadlessSystemUserMode() final String systemUserType = isDefaultHeadlessSystemUserMode()
? UserManager.USER_TYPE_SYSTEM_HEADLESS ? UserManager.USER_TYPE_SYSTEM_HEADLESS
: UserManager.USER_TYPE_FULL_SYSTEM; : UserManager.USER_TYPE_FULL_SYSTEM;
flags |= mUserTypes.get(systemUserType).getDefaultUserInfoFlags(); final int flags = mUserTypes.get(systemUserType).getDefaultUserInfoFlags()
UserInfo system = new UserInfo(UserHandle.USER_SYSTEM, null, null, flags, systemUserType); | UserInfo.FLAG_INITIALIZED;
UserData userData = putUserInfo(system); final UserInfo system = new UserInfo(UserHandle.USER_SYSTEM,
/* name= */ null, /* iconPath= */ null, flags, systemUserType);
final UserData userData = putUserInfo(system);
userData.userProperties = new UserProperties( userData.userProperties = new UserProperties(
mUserTypes.get(userData.info.userType).getDefaultUserPropertiesReference()); mUserTypes.get(userData.info.userType).getDefaultUserPropertiesReference());
mNextSerialNumber = MIN_USER_ID; mNextSerialNumber = MIN_USER_ID;
mUserVersion = USER_VERSION; mUserVersion = USER_VERSION;
mUserTypeVersion = UserTypeFactory.getUserTypeVersion(); mUserTypeVersion = UserTypeFactory.getUserTypeVersion();
Bundle restrictions = new Bundle(); final Bundle restrictions = new Bundle();
try { try {
final String[] defaultFirstUserRestrictions = mContext.getResources().getStringArray( final String[] defaultFirstUserRestrictions = mContext.getResources().getStringArray(
com.android.internal.R.array.config_defaultFirstUserRestrictions); com.android.internal.R.array.config_defaultFirstUserRestrictions);

View File

@@ -76,7 +76,7 @@ public final class UserTypeDetails {
private final @UserInfoFlag int mBaseType; private final @UserInfoFlag int mBaseType;
// TODO(b/143784345): Update doc/name when we clean up UserInfo. // 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. */ /** The {@link UserInfoFlag}s to apply by default to newly created users of this type. */
private final @UserInfoFlag int mDefaultUserInfoPropertyFlags; private final @UserInfoFlag int mDefaultUserInfoPropertyFlags;
/** /**
@@ -224,7 +224,7 @@ public final class UserTypeDetails {
} }
// TODO(b/143784345): Update comment when UserInfo is reorganized. // TODO(b/143784345): Update comment when UserInfo is reorganized.
/** The {@link UserInfo.UserInfoFlag}s that all users of this type will automatically have. */ /** The {@link UserInfoFlag}s to apply by default to newly created users of this type. */
public int getDefaultUserInfoFlags() { public int getDefaultUserInfoFlags() {
return mDefaultUserInfoPropertyFlags | mBaseType; return mDefaultUserInfoPropertyFlags | mBaseType;
} }
@@ -526,6 +526,7 @@ public final class UserTypeDetails {
Preconditions.checkArgument(hasValidPropertyFlags(), Preconditions.checkArgument(hasValidPropertyFlags(),
"UserTypeDetails " + mName + " has invalid flags: " "UserTypeDetails " + mName + " has invalid flags: "
+ Integer.toHexString(mDefaultUserInfoPropertyFlags)); + Integer.toHexString(mDefaultUserInfoPropertyFlags));
checkSystemAndMainUserPreconditions();
if (hasBadge()) { if (hasBadge()) {
Preconditions.checkArgument(mBadgeLabels != null && mBadgeLabels.length != 0, Preconditions.checkArgument(mBadgeLabels != null && mBadgeLabels.length != 0,
"UserTypeDetails " + mName + " has badge but no badgeLabels."); "UserTypeDetails " + mName + " has badge but no badgeLabels.");
@@ -578,8 +579,6 @@ public final class UserTypeDetails {
// TODO(b/143784345): Refactor this when we clean up UserInfo. // TODO(b/143784345): Refactor this when we clean up UserInfo.
private boolean hasValidPropertyFlags() { private boolean hasValidPropertyFlags() {
final int forbiddenMask = final int forbiddenMask =
UserInfo.FLAG_PRIMARY |
UserInfo.FLAG_ADMIN |
UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_INITIALIZED |
UserInfo.FLAG_QUIET_MODE | UserInfo.FLAG_QUIET_MODE |
UserInfo.FLAG_FULL | UserInfo.FLAG_FULL |
@@ -587,6 +586,18 @@ public final class UserTypeDetails {
UserInfo.FLAG_PROFILE; UserInfo.FLAG_PROFILE;
return (mDefaultUserInfoPropertyFlags & forbiddenMask) == 0; return (mDefaultUserInfoPropertyFlags & forbiddenMask) == 0;
} }
private void checkSystemAndMainUserPreconditions() {
// Primary must be synonymous with System.
Preconditions.checkArgument(
((mBaseType & UserInfo.FLAG_SYSTEM) != 0) ==
((mDefaultUserInfoPropertyFlags & UserInfo.FLAG_PRIMARY) != 0),
"UserTypeDetails " + mName + " cannot be SYSTEM xor PRIMARY.");
// At most one MainUser is ever allowed at a time.
Preconditions.checkArgument(
((mDefaultUserInfoPropertyFlags & UserInfo.FLAG_MAIN) == 0) || mMaxAllowed == 1,
"UserTypeDetails " + mName + " must not sanction more than one MainUser.");
}
} }
/** /**

View File

@@ -16,11 +16,14 @@
package com.android.server.pm; package com.android.server.pm;
import static android.content.pm.UserInfo.FLAG_ADMIN;
import static android.content.pm.UserInfo.FLAG_DEMO; import static android.content.pm.UserInfo.FLAG_DEMO;
import static android.content.pm.UserInfo.FLAG_EPHEMERAL; import static android.content.pm.UserInfo.FLAG_EPHEMERAL;
import static android.content.pm.UserInfo.FLAG_FULL; import static android.content.pm.UserInfo.FLAG_FULL;
import static android.content.pm.UserInfo.FLAG_GUEST; import static android.content.pm.UserInfo.FLAG_GUEST;
import static android.content.pm.UserInfo.FLAG_MAIN;
import static android.content.pm.UserInfo.FLAG_MANAGED_PROFILE; import static android.content.pm.UserInfo.FLAG_MANAGED_PROFILE;
import static android.content.pm.UserInfo.FLAG_PRIMARY;
import static android.content.pm.UserInfo.FLAG_PROFILE; import static android.content.pm.UserInfo.FLAG_PROFILE;
import static android.content.pm.UserInfo.FLAG_RESTRICTED; import static android.content.pm.UserInfo.FLAG_RESTRICTED;
import static android.content.pm.UserInfo.FLAG_SYSTEM; import static android.content.pm.UserInfo.FLAG_SYSTEM;
@@ -62,7 +65,7 @@ import java.util.function.Consumer;
* This class is responsible both for defining the AOSP use types, as well as reading in customized * This class is responsible both for defining the AOSP use types, as well as reading in customized
* user types from {@link com.android.internal.R.xml#config_user_types}. * user types from {@link com.android.internal.R.xml#config_user_types}.
* *
* Tests are located in UserManagerServiceUserTypeTest.java. * Tests are located in {@link UserManagerServiceUserTypeTest}.
* @hide * @hide
*/ */
public final class UserTypeFactory { public final class UserTypeFactory {
@@ -277,7 +280,8 @@ public final class UserTypeFactory {
return new UserTypeDetails.Builder() return new UserTypeDetails.Builder()
.setName(USER_TYPE_FULL_SYSTEM) .setName(USER_TYPE_FULL_SYSTEM)
.setBaseType(FLAG_SYSTEM | FLAG_FULL) .setBaseType(FLAG_SYSTEM | FLAG_FULL)
.setDefaultUserInfoPropertyFlags(UserInfo.FLAG_MAIN); .setDefaultUserInfoPropertyFlags(FLAG_PRIMARY | FLAG_ADMIN | FLAG_MAIN)
.setMaxAllowed(1);
} }
/** /**
@@ -287,7 +291,9 @@ public final class UserTypeFactory {
private static UserTypeDetails.Builder getDefaultTypeSystemHeadless() { private static UserTypeDetails.Builder getDefaultTypeSystemHeadless() {
return new UserTypeDetails.Builder() return new UserTypeDetails.Builder()
.setName(USER_TYPE_SYSTEM_HEADLESS) .setName(USER_TYPE_SYSTEM_HEADLESS)
.setBaseType(FLAG_SYSTEM); .setBaseType(FLAG_SYSTEM)
.setDefaultUserInfoPropertyFlags(FLAG_PRIMARY | FLAG_ADMIN)
.setMaxAllowed(1);
} }
private static Bundle getDefaultSecondaryUserRestrictions() { private static Bundle getDefaultSecondaryUserRestrictions() {