diff --git a/core/java/android/os/UserManager.java b/core/java/android/os/UserManager.java
index db3550542086c..a715c6961359a 100644
--- a/core/java/android/os/UserManager.java
+++ b/core/java/android/os/UserManager.java
@@ -3328,7 +3328,7 @@ public class UserManager {
*
*
This method can be used by OEMs to "warm" up the user creation by pre-creating some users
* at the first boot, so they when the "real" user is created (for example,
- * by {@link #createUser(String, String, int)} or {@link #createGuest(Context, String)}), it
+ * by {@link #createUser(String, String, int)} or {@link #createGuest(Context)}), it
* takes less time.
*
*
This method completes the majority of work necessary for user creation: it
@@ -3365,7 +3365,6 @@ public class UserManager {
/**
* Creates a guest user and configures it.
* @param context an application context
- * @param name the name to set for the user
* @return the {@link UserInfo} object for the created user, or {@code null} if the user
* could not be created.
*
@@ -3373,20 +3372,19 @@ public class UserManager {
*/
@RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
Manifest.permission.CREATE_USERS})
- public UserInfo createGuest(Context context, String name) {
- UserInfo guest = null;
+ public UserInfo createGuest(Context context) {
try {
- guest = mService.createUserWithThrow(name, USER_TYPE_FULL_GUEST, 0);
+ final UserInfo guest = mService.createUserWithThrow(null, USER_TYPE_FULL_GUEST, 0);
if (guest != null) {
Settings.Secure.putStringForUser(context.getContentResolver(),
Settings.Secure.SKIP_FIRST_USE_HINTS, "1", guest.id);
}
+ return guest;
} catch (ServiceSpecificException e) {
return null;
} catch (RemoteException re) {
throw re.rethrowFromSystemServer();
}
- return guest;
}
/**
diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml
index e64756119478a..34bd2fbad576b 100644
--- a/core/res/res/values/strings.xml
+++ b/core/res/res/values/strings.xml
@@ -4894,6 +4894,8 @@
Logging out %1$s\u2026
Owner
+
+ Guest
Error
diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml
index 520878b91c2a2..df3cbf34c8f01 100644
--- a/core/res/res/values/symbols.xml
+++ b/core/res/res/values/symbols.xml
@@ -1108,6 +1108,7 @@
+
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java
index d1c9b3f1c2c50..d7c8a91608072 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java
@@ -855,8 +855,7 @@ public class UserSwitcherController implements Dumpable {
public @UserIdInt int createGuest() {
UserInfo guest;
try {
- guest = mUserManager.createGuest(mContext,
- mContext.getString(com.android.settingslib.R.string.guest_nickname));
+ guest = mUserManager.createGuest(mContext);
} catch (UserManager.UserOperationException e) {
Log.e(TAG, "Couldn't create guest user", e);
return UserHandle.USER_NULL;
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/UserSwitcherControllerTest.kt b/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/UserSwitcherControllerTest.kt
index 896dab6c060d2..91c347fc4685a 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/UserSwitcherControllerTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/UserSwitcherControllerTest.kt
@@ -64,7 +64,6 @@ import org.mockito.ArgumentMatchers.eq
import org.mockito.Mock
import org.mockito.Mockito.`when`
import org.mockito.Mockito.any
-import org.mockito.Mockito.anyString
import org.mockito.Mockito.doNothing
import org.mockito.Mockito.doReturn
import org.mockito.Mockito.mock
@@ -207,7 +206,7 @@ class UserSwitcherControllerTest : SysuiTestCase() {
`when`(userTracker.userId).thenReturn(ownerId)
`when`(userTracker.userInfo).thenReturn(ownerInfo)
- `when`(userManager.createGuest(any(), anyString())).thenReturn(guestInfo)
+ `when`(userManager.createGuest(any())).thenReturn(guestInfo)
userSwitcherController.onUserListItemClicked(emptyGuestUserRecord, null)
testableLooper.processAllMessages()
@@ -232,7 +231,7 @@ class UserSwitcherControllerTest : SysuiTestCase() {
`when`(userTracker.userId).thenReturn(ownerId)
`when`(userTracker.userInfo).thenReturn(ownerInfo)
- `when`(userManager.createGuest(any(), anyString())).thenReturn(guestInfo)
+ `when`(userManager.createGuest(any())).thenReturn(guestInfo)
userSwitcherController.onUserListItemClicked(emptyGuestUserRecord, dialogShower)
testableLooper.processAllMessages()
diff --git a/services/core/java/com/android/server/pm/UserManagerService.java b/services/core/java/com/android/server/pm/UserManagerService.java
index ad0f9a183c435..d99305d728b98 100644
--- a/services/core/java/com/android/server/pm/UserManagerService.java
+++ b/services/core/java/com/android/server/pm/UserManagerService.java
@@ -1399,21 +1399,28 @@ public class UserManagerService extends IUserManager.Stub {
}
/**
- * Returns a UserInfo object with the name filled in, for Owner, or the original
+ * Returns a UserInfo object with the name filled in, for Owner and Guest, or the original
* if the name is already set.
*/
private UserInfo userWithName(UserInfo orig) {
- if (orig != null && orig.name == null && orig.id == UserHandle.USER_SYSTEM) {
- if (DBG_ALLOCATION) {
- final int number = mUser0Allocations.incrementAndGet();
- Slog.w(LOG_TAG, "System user instantiated at least " + number + " times");
+ if (orig != null && orig.name == null) {
+ String name = null;
+ if (orig.id == UserHandle.USER_SYSTEM) {
+ if (DBG_ALLOCATION) {
+ final int number = mUser0Allocations.incrementAndGet();
+ Slog.w(LOG_TAG, "System user instantiated at least " + number + " times");
+ }
+ name = getOwnerName();
+ } else if (orig.isGuest()) {
+ name = getGuestName();
+ }
+ if (name != null) {
+ final UserInfo withName = new UserInfo(orig);
+ withName.name = name;
+ return withName;
}
- UserInfo withName = new UserInfo(orig);
- withName.name = getOwnerName();
- return withName;
- } else {
- return orig;
}
+ return orig;
}
/** Returns whether the given user type is one of the FULL user types. */
@@ -3259,6 +3266,10 @@ public class UserManagerService extends IUserManager.Stub {
return mOwnerName.get();
}
+ private String getGuestName() {
+ return mContext.getString(com.android.internal.R.string.guest_name);
+ }
+
private void invalidateOwnerNameIfNecessary(@NonNull Resources res, boolean forceUpdate) {
final int configChanges = mLastConfiguration.updateFrom(res.getConfiguration());
if (forceUpdate || (configChanges & mOwnerNameTypedValue.changingConfigurations) != 0) {
diff --git a/services/tests/servicestests/src/com/android/server/pm/UserManagerTest.java b/services/tests/servicestests/src/com/android/server/pm/UserManagerTest.java
index 401cd7f0b67e5..c7b5547170d08 100644
--- a/services/tests/servicestests/src/com/android/server/pm/UserManagerTest.java
+++ b/services/tests/servicestests/src/com/android/server/pm/UserManagerTest.java
@@ -1078,6 +1078,19 @@ public final class UserManagerTest {
assertThat(um.getUserName()).isEqualTo(userName);
}
+ @Test
+ public void testGetUserName_shouldReturnTranslatedTextForNullNamedGuestUser() throws Exception {
+ UserInfo guestWithNullName = createUser(null, UserManager.USER_TYPE_FULL_GUEST, 0);
+ assertThat(guestWithNullName).isNotNull();
+
+ UserManager um = (UserManager) mContext.createPackageContextAsUser(
+ "android", 0, guestWithNullName.getUserHandle())
+ .getSystemService(Context.USER_SERVICE);
+
+ assertThat(um.getUserName()).isEqualTo(
+ mContext.getString(com.android.internal.R.string.guest_name));
+ }
+
@Test
public void testGetUserIcon_withContextUserId() throws Exception {
assumeManagedUsersSupported();