Add additional constraint in DPMS for Automotive

Restricts setting DO for headless system user when there are only two users

Bug: 170333009
Test: atest CustomDeviceOwnerTest#testCannotSetDeviceOwnerWhenSecondaryUserPresent

Change-Id: I8a826fedf4faa17266dea039288342b91e142ee5
This commit is contained in:
Yan Zhu
2021-02-11 12:59:25 -08:00
committed by Felipe Leme
parent 0845888808
commit 0cb930a35f

View File

@@ -10062,7 +10062,6 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
final int userId = user.id;
// TODO(b/177547285): add CTS test
if (mInjector.userManagerIsHeadlessSystemUserMode()) {
ComponentName admin = mOwners.getDeviceOwnerComponent();
Slog.i(LOG_TAG, "Automatically setting profile owner (" + admin + ") on new user "
@@ -12834,8 +12833,10 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
if (mOwners.hasProfileOwner(deviceOwnerUserId)) {
return CODE_USER_HAS_PROFILE_OWNER;
}
boolean isHeadlessSystemUserMode = mInjector.userManagerIsHeadlessSystemUserMode();
// System user is always running in headless system user mode.
if (!mInjector.userManagerIsHeadlessSystemUserMode()
if (!isHeadlessSystemUserMode
&& !mUserManager.isUserRunning(new UserHandle(deviceOwnerUserId))) {
return CODE_USER_NOT_RUNNING;
}
@@ -12843,7 +12844,7 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
return CODE_HAS_PAIRED;
}
if (mInjector.userManagerIsHeadlessSystemUserMode()) {
if (isHeadlessSystemUserMode) {
if (deviceOwnerUserId != UserHandle.USER_SYSTEM) {
Slog.e(LOG_TAG, "In headless system user mode, "
+ "device owner can only be set on headless system user.");
@@ -12855,9 +12856,13 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
// If shell command runs after user setup completed check device status. Otherwise, OK.
if (mIsWatch || hasUserSetupCompleted(UserHandle.USER_SYSTEM)) {
// In non-headless system user mode, DO can be setup only if
// there's no non-system user
if (!mInjector.userManagerIsHeadlessSystemUserMode()
&& mUserManager.getUserCount() > 1) {
// there's no non-system user.
// In headless system user mode, DO can be setup only if there are
// two users: the headless system user and the foreground user.
// If there could be multiple foreground users, this constraint should be modified.
int maxNumberOfExistingUsers = isHeadlessSystemUserMode ? 2 : 1;
if (mUserManager.getUserCount() > maxNumberOfExistingUsers) {
return CODE_NONSYSTEM_USER_EXISTS;
}