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

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

Exposed canSwitchUsers() and added an API in UserManager for SUW.

UserManager#canSwitchUsers() is now exposed as a SystemApi for SUW use.
It requires either the MANAGE_USERS or INTERACT_ACROSS_USERS permission.

Added UserManager#removeUser(UserHandle) which mirrors
UserManager#removeUser(int). It requires the MANAGE_USERS permission.

Bug: 120457727
Test: atest com.android.server.pm.UserManagerTest#testRemoveUserByHandle
Test: atest com.android.server.pm.UserManagerTest#testRemoveUser
Test: manual (run setup wizard)
Change-Id: Ie7a641da8ada789931b2b116c907a3ca298490f4
parent 9d7964b9
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -4269,6 +4269,7 @@ package android.os {
  }

  public class UserManager {
    method public boolean canSwitchUsers();
    method public void clearSeedAccountData();
    method public java.lang.String getSeedAccountName();
    method public android.os.PersistableBundle getSeedAccountOptions();
@@ -4283,6 +4284,7 @@ package android.os {
    method public boolean isManagedProfile(int);
    method public boolean isPrimaryUser();
    method public boolean isRestrictedProfile();
    method public boolean removeUser(android.os.UserHandle);
    field public static final java.lang.String ACTION_USER_RESTRICTIONS_CHANGED = "android.os.action.USER_RESTRICTIONS_CHANGED";
    field public static final deprecated java.lang.String DISALLOW_OEM_UNLOCK = "no_oem_unlock";
    field public static final java.lang.String DISALLOW_RUN_IN_BACKGROUND = "no_run_in_background";
+16 −0
Original line number Diff line number Diff line
@@ -1225,6 +1225,9 @@ public class UserManager {
     * system user hasn't been unlocked yet, or {@link #DISALLOW_USER_SWITCH} is set.
     * @hide
     */
    @SystemApi
    @RequiresPermission(anyOf = {android.Manifest.permission.MANAGE_USERS,
            android.Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true)
    public boolean canSwitchUsers() {
        boolean allowUserSwitchingWhenSystemUserLocked = Settings.Global.getInt(
                mContext.getContentResolver(),
@@ -2624,6 +2627,19 @@ public class UserManager {
        }
    }

    /**
     * Removes a user and all associated data.
     *
     * @param user the user that needs to be removed.
     * @hide
     */
    @SystemApi
    @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
    public boolean removeUser(UserHandle user) {
        return removeUser(user.getIdentifier());
    }


    /**
     * Similar to {@link #removeUser(int)} except bypassing the checking of
     * {@link UserManager#DISALLOW_REMOVE_USER}
+24 −1
Original line number Diff line number Diff line
@@ -16,13 +16,13 @@

package com.android.server.pm;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.pm.UserInfo;
import android.app.ActivityManager;
import android.os.Bundle;
import android.os.UserHandle;
import android.os.UserManager;
@@ -161,6 +161,29 @@ public class UserManagerTest extends AndroidTestCase {
        assertFalse(findUser(userInfo.id));
    }

    @MediumTest
    public void testRemoveUserByHandle() {
        UserInfo userInfo = createUser("Guest 1", UserInfo.FLAG_GUEST);
        final UserHandle user = userInfo.getUserHandle();
        synchronized (mUserRemoveLock) {
            mUserManager.removeUser(user);
            long time = System.currentTimeMillis();
            while (mUserManager.getUserInfo(user.getIdentifier()) != null) {
                try {
                    mUserRemoveLock.wait(REMOVE_CHECK_INTERVAL_MILLIS);
                } catch (InterruptedException ie) {
                    Thread.currentThread().interrupt();
                    return;
                }
                if (System.currentTimeMillis() - time > REMOVE_TIMEOUT_MILLIS) {
                    fail("Timeout waiting for removeUser. userId = " + user.getIdentifier());
                }
            }
        }

        assertFalse(findUser(userInfo.id));
    }

    @MediumTest
    public void testAddGuest() throws Exception {
        UserInfo userInfo1 = createUser("Guest 1", UserInfo.FLAG_GUEST);