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
This commit is contained in:
Felipe Leme
2022-01-31 11:48:20 -08:00
parent 2bbbc9a2e8
commit abc9c87519
2 changed files with 26 additions and 16 deletions

View File

@@ -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
*/

View File

@@ -10907,7 +10907,7 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
if (mPendingUserCreatedCallbackTokens.contains(token)) {
// Ignore because it was triggered by createAndManageUser()
Slogf.d(LOG_TAG, "handleNewUserCreated(): ignoring for user " + userId
+ " due to token" + token);
+ " due to token " + token);
mPendingUserCreatedCallbackTokens.remove(token);
return;
}
@@ -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;
}