From dabd1ade44c561340d37e5e4b3a0827124a4a230 Mon Sep 17 00:00:00 2001 From: shreyacsingh Date: Fri, 6 Jan 2023 00:40:38 +0000 Subject: [PATCH] Disallow work profiles on non-initial users in headless Modified 'canHaveProfile' method to ensure we allow work profiles only on initial user and not additional users- in both headless and non-headless environment.For that we can use isMain() as on non-headless, the system user(initial) is main,and on headless the first secondary user(initial) is main. Test: atest Bug: 266053489 Fix: 266053489 Change-Id: Ia959213b03ea7b36d113fd13e23fca187fe3f59d --- core/java/android/content/pm/UserInfo.java | 6 +----- .../android/server/pm/UserManagerServiceUserInfoTest.java | 8 ++++++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/core/java/android/content/pm/UserInfo.java b/core/java/android/content/pm/UserInfo.java index e38cb65f991f8..333066bc4d8f7 100644 --- a/core/java/android/content/pm/UserInfo.java +++ b/core/java/android/content/pm/UserInfo.java @@ -436,11 +436,7 @@ public class UserInfo implements Parcelable { if (isProfile() || isGuest() || isRestricted()) { return false; } - if (UserManager.isHeadlessSystemUserMode()) { - return id != UserHandle.USER_SYSTEM; - } else { - return id == UserHandle.USER_SYSTEM; - } + return isMain(); } // TODO(b/142482943): Get rid of this (after removing it from all tests) if feasible. diff --git a/services/tests/servicestests/src/com/android/server/pm/UserManagerServiceUserInfoTest.java b/services/tests/servicestests/src/com/android/server/pm/UserManagerServiceUserInfoTest.java index 92fddc76343d5..d999aa3159408 100644 --- a/services/tests/servicestests/src/com/android/server/pm/UserManagerServiceUserInfoTest.java +++ b/services/tests/servicestests/src/com/android/server/pm/UserManagerServiceUserInfoTest.java @@ -22,6 +22,7 @@ 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_INITIALIZED; +import static android.content.pm.UserInfo.FLAG_MAIN; 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; @@ -206,6 +207,13 @@ public class UserManagerServiceUserInfoTest { assertFalse("Switching to a profiles should be disabled", userInfo.supportsSwitchTo()); } + /** Test UserInfo.canHaveProfile for main user */ + @Test + public void testCanHaveProfile() throws Exception { + UserInfo userInfo = createUser(100, FLAG_MAIN, null); + assertTrue("Main users can have profile", userInfo.canHaveProfile()); + } + /** Tests upgradeIfNecessaryLP (but without locking) for upgrading from version 8 to 9+. */ @Test public void testUpgradeIfNecessaryLP_9() {