Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e7f6b543 authored by Varun Shah's avatar Varun Shah
Browse files

Adding a null check for ActivityManager#switchUser(UserHandle).

Also updated documentation and added the relevant test.

Bug: 122887441
Test: atest com.android.server.pm.UserManagerTest#testSwitchUserByHandle_ThrowsException
Change-Id: Ic936570ff24d4879732017c717d8c81f38356553
parent 42edcbfa
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -3705,11 +3705,16 @@ public class ActivityManager {
     * Returns whether switching to provided user was successful.
     *
     * @param user the user to switch to.
     *
     * @throws IllegalArgumentException if the user is null.
     * @hide
     */
    @SystemApi
    @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
    public boolean switchUser(UserHandle user) {
    public boolean switchUser(@NonNull UserHandle user) {
        if (user == null) {
            throw new IllegalArgumentException("UserHandle cannot be null.");
        }
        return switchUser(user.getIdentifier());
    }

+12 −0
Original line number Diff line number Diff line
@@ -544,6 +544,18 @@ public class UserManagerTest extends AndroidTestCase {
        switchUser(-1, UserHandle.of(startUser), false);
    }

    public void testSwitchUserByHandle_ThrowsException() {
        synchronized (mUserSwitchLock) {
            try {
                ActivityManager am = getContext().getSystemService(ActivityManager.class);
                am.switchUser(null);
                fail("Expected IllegalArgumentException on passing in a null UserHandle.");
            } catch (IllegalArgumentException expected) {
                // Do nothing - exception is expected.
            }
        }
    }

    @MediumTest
    public void testConcurrentUserCreate() throws Exception {
        int userCount = mUserManager.getUserCount();