From d03224f32646e6bcb68f0c9fa12fe63d2502e88c Mon Sep 17 00:00:00 2001 From: Yasin Kilicdere Date: Thu, 10 Feb 2022 23:04:56 +0000 Subject: [PATCH] Fix Guest string was not translated in UMS.userWithName() Guest users name was stuck with the language when it was created, and it wasn't changing with the active language setting. To solve that, removed name parameter from UM.createGuest() and created the guest user with a null name, then made changes in UMS.userWithName() to fill the name of guest user with the active language's translation when it has a null name. Creating a guest with a specific name is still supported through UM.createUser API. Bug: 185309160 Test: atest com.android.server.pm.UserManagerTest Change-Id: I2745aed0ea722765a11c6da40fb4159146da54c7 Merged-In: I2745aed0ea722765a11c6da40fb4159146da54c7 (cherry picked from commit c556777ca4af81c2dde2fa383baae20398e3029e) --- core/java/android/os/UserManager.java | 10 +++--- core/res/res/values/strings.xml | 2 ++ core/res/res/values/symbols.xml | 1 + .../policy/UserSwitcherController.java | 3 +- .../policy/UserSwitcherControllerTest.kt | 5 ++- .../android/server/pm/UserManagerService.java | 31 +++++++++++++------ .../android/server/pm/UserManagerTest.java | 13 ++++++++ 7 files changed, 44 insertions(+), 21 deletions(-) diff --git a/core/java/android/os/UserManager.java b/core/java/android/os/UserManager.java index e56f214e30d9a..5c6a75c7af224 100644 --- a/core/java/android/os/UserManager.java +++ b/core/java/android/os/UserManager.java @@ -3322,7 +3322,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 @@ -3359,7 +3359,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. * @@ -3367,20 +3366,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 2112953d69623..9b677b3682653 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();