Implement remove user for user hal
Bug: 150409600
Bug: 158195639
Test: atest CarServiceUnitTest:com.android.car.user.CarUserManagerUnitTest
Test: atest CarServiceUnitTest:com.android.car.hal.UserHalServiceTest
Test: atest CarServiceUnitTest:com.android.car.user.CarUserServiceTest
Test: atest CarServiceUnitTest:android.car.userlib.UserHalHelperTest
Test: atest CarSecurityPermissionTest:com.android.car.user.CarUserManagerPermissionTest
Change-Id: I6e5a67464fecf087994a19458def4ab7f538e1ce
diff --git a/service/src/com/android/car/user/CarUserService.java b/service/src/com/android/car/user/CarUserService.java
index 81d1b6c..bd45abb 100644
--- a/service/src/com/android/car/user/CarUserService.java
+++ b/service/src/com/android/car/user/CarUserService.java
@@ -35,6 +35,7 @@
import android.car.user.CarUserManager.UserLifecycleListener;
import android.car.user.UserCreationResult;
import android.car.user.UserIdentificationAssociationResponse;
+import android.car.user.UserRemovalResult;
import android.car.user.UserSwitchResult;
import android.car.userlib.CarUserManagerHelper;
import android.car.userlib.CommonConstants.CarUserServiceConstants;
@@ -53,6 +54,7 @@
import android.hardware.automotive.vehicle.V2_0.CreateUserStatus;
import android.hardware.automotive.vehicle.V2_0.InitialUserInfoResponse;
import android.hardware.automotive.vehicle.V2_0.InitialUserInfoResponseAction;
+import android.hardware.automotive.vehicle.V2_0.RemoveUserRequest;
import android.hardware.automotive.vehicle.V2_0.SwitchUserRequest;
import android.hardware.automotive.vehicle.V2_0.SwitchUserStatus;
import android.hardware.automotive.vehicle.V2_0.UserIdentificationGetRequest;
@@ -879,6 +881,62 @@
});
}
+ @Override
+ public UserRemovalResult removeUser(@UserIdInt int userId) {
+ checkManageUsersPermission("removeUser");
+ EventLog.writeEvent(EventLogTags.CAR_USER_SVC_REMOVE_USER_REQ, userId);
+ // If the requested user is the current user, return error.
+ if (ActivityManager.getCurrentUser() == userId) {
+ return logAndGetResults(userId,
+ UserRemovalResult.STATUS_TARGET_USER_IS_CURRENT_USER);
+ }
+
+ // If requested user is the only admin user, return error.
+ UserInfo userInfo = mUserManager.getUserInfo(userId);
+ if (userInfo == null) {
+ return logAndGetResults(userId, UserRemovalResult.STATUS_USER_DOES_NOT_EXIST);
+ }
+
+ android.hardware.automotive.vehicle.V2_0.UserInfo halUser =
+ new android.hardware.automotive.vehicle.V2_0.UserInfo();
+ halUser.userId = userInfo.id;
+ halUser.flags = UserHalHelper.convertFlags(userInfo);
+ UsersInfo usersInfo = UserHalHelper.newUsersInfo(mUserManager);
+
+ // Do not delete last admin user.
+ if (UserHalHelper.isAdmin(halUser.flags)) {
+ int size = usersInfo.existingUsers.size();
+ int totalAdminUsers = 0;
+ for (int i = 0; i < size; i++) {
+ if (UserHalHelper.isAdmin(usersInfo.existingUsers.get(i).flags)) {
+ totalAdminUsers++;
+ }
+ }
+ if (totalAdminUsers == 1) {
+ return logAndGetResults(userId,
+ UserRemovalResult.STATUS_TARGET_USER_IS_LAST_ADMIN_USER);
+ }
+ }
+
+ // First remove user from android and then remove from HAL because HAL remove user is one
+ // way call.
+ if (!mUserManager.removeUser(userId)) {
+ return logAndGetResults(userId, UserRemovalResult.STATUS_ANDROID_FAILURE);
+ }
+
+ RemoveUserRequest request = new RemoveUserRequest();
+ request.removedUserInfo = halUser;
+ request.usersInfo = usersInfo;
+ mHal.removeUser(request);
+ return logAndGetResults(userId, UserRemovalResult.STATUS_SUCCESSFUL);
+ }
+
+ private UserRemovalResult logAndGetResults(@UserIdInt int userId,
+ @UserRemovalResult.Status int result) {
+ EventLog.writeEvent(EventLogTags.CAR_USER_SVC_REMOVE_USER_RESP, userId, result);
+ return new UserRemovalResult(result);
+ }
+
private void sendUserSwitchUiCallback(@UserIdInt int targetUserId) {
if (mUserSwitchUiReceiver == null) {
Log.w(TAG_USER, "No User switch UI receiver.");