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

Commit abc9c875 authored by Felipe Leme's avatar Felipe Leme
Browse files

Fixed DPM.logoutUser() methods.

The initial implementation was setting the logout user to be SYSTEM
on non-headless system user mode, but recently it was changed to
return null if the user was not switched or started by the device
admin.

Test: atest com.android.cts.devicepolicy.DeviceOwnerTest#testCreateAndManageUser_LogoutUser_systemApi com.android.cts.devicepolicy.DeviceOwnerTest#testCreateAndManageUser_LogoutUser # on phones and automotive
Fixes: 216648012

Change-Id: I1f8f754167260a41df13dca13cb62467723c99b1
parent 2bbbc9a2
Loading
Loading
Loading
Loading
+13 −3
Original line number Diff line number Diff line
@@ -10063,7 +10063,9 @@ public class DevicePolicyManager {
    /**
     * Called by a profile owner of secondary user that is affiliated with the device to stop the
     * calling user and switch back to primary user.
     * calling user and switch back to primary user (when the user was
     * {@link #switchUser(ComponentName, UserHandle)} switched to) or stop the user (when it was
     * {@link #startUserInBackground(ComponentName, UserHandle) started in background}.
     *
     * <p>Notice that on devices running with
     * {@link UserManager#isHeadlessSystemUserMode() headless system user mode}, there is no primary
@@ -10091,7 +10093,12 @@ public class DevicePolicyManager {
    }
    /**
     * Same as {@link #logoutUser(ComponentName)}, but called by system (like Settings), not admin.
     * Similar to {@link #logoutUser(ComponentName)}, except:
     *
     * <ul>
     *   <li>Called by system (like Settings), not admin.
     *   <li>It logs out the current user, not the caller.
     * </ul>
     *
     * @hide
     */
@@ -10108,7 +10115,10 @@ public class DevicePolicyManager {
    }
    /**
     * Gets the user a {@link #logoutUser(ComponentName)} call would switch to,
     * or {@code null} if the current user is not in a session.
     * or {@code null} if the current user is not in a session (i.e., if it was not
     * {@link #switchUser(ComponentName, UserHandle) switched} or
     * {@link #startUserInBackground(ComponentName, UserHandle) started in background} by the
     * device admin.
     *
     * @hide
     */
+13 −13
Original line number Diff line number Diff line
@@ -11026,6 +11026,8 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
                switched = mInjector.getIActivityManager().switchUser(userId);
                if (!switched) {
                    Slogf.w(LOG_TAG, "Failed to switch to user %d", userId);
                } else {
                    Slogf.d(LOG_TAG, "Switched");
                }
                return switched;
            } catch (RemoteException e) {
@@ -11049,18 +11051,12 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
    }
    private @UserIdInt int getLogoutUserIdUnchecked() {
        if (!mInjector.userManagerIsHeadlessSystemUserMode()) {
            // mLogoutUserId is USER_SYSTEM as well, but there's no need to acquire the lock
            return UserHandle.USER_SYSTEM;
        }
        synchronized (getLockObject()) {
            return mLogoutUserId;
        }
    }
    private void clearLogoutUser() {
        if (!mInjector.userManagerIsHeadlessSystemUserMode()) return; // ignore
        synchronized (getLockObject()) {
            setLogoutUserIdLocked(UserHandle.USER_NULL);
        }
@@ -11068,8 +11064,6 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
    @GuardedBy("getLockObject()")
    private void setLogoutUserIdLocked(@UserIdInt int userId) {
        if (!mInjector.userManagerIsHeadlessSystemUserMode()) return; // ignore
        if (userId == UserHandle.USER_CURRENT) {
            userId = getCurrentForegroundUserId();
        }
@@ -11151,8 +11145,7 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
            return UserManager.USER_OPERATION_ERROR_MANAGED_PROFILE;
        }
        // TODO(b/204585343): remove the headless system user check?
        if (mInjector.userManagerIsHeadlessSystemUserMode() && callingUserId != mInjector
        if (callingUserId != mInjector
                .binderWithCleanCallingIdentity(() -> getCurrentForegroundUserId())) {
            Slogf.d(LOG_TAG, "logoutUser(): user %d is in background, just stopping, not switching",
                    callingUserId);
@@ -11168,8 +11161,15 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
        Preconditions.checkCallAuthorization(
                canManageUsers(caller) || hasCallingOrSelfPermission(permission.CREATE_USERS));
        int result = logoutUserUnchecked(getCurrentForegroundUserId());
        Slogf.d(LOG_TAG, "logout called by uid %d. Result: %d", caller.getUid(), result);
        int currentUserId = getCurrentForegroundUserId();
        if (VERBOSE_LOG) {
            Slogf.v(LOG_TAG, "logout() called by uid %d; current user is %d", caller.getUid(),
                    currentUserId);
        }
        int result = logoutUserUnchecked(currentUserId);
        if (VERBOSE_LOG) {
            Slogf.v(LOG_TAG, "Result of logout(): %d", result);
        }
        return result;
    }