Sets the removed user flags on REMOVE_USER UserHal message.

Test: atest CarUserServiceTest
Test: manual verification
Fixes: 155913815

Change-Id: I8a9e3e2008dd03545e1f0a8562754040d87813da
diff --git a/service/src/com/android/car/user/CarUserService.java b/service/src/com/android/car/user/CarUserService.java
index a49cbdf..5f3daf5 100644
--- a/service/src/com/android/car/user/CarUserService.java
+++ b/service/src/com/android/car/user/CarUserService.java
@@ -49,11 +49,8 @@
 import android.car.userlib.HalCallback;
 import android.car.userlib.UserHalHelper;
 import android.car.userlib.UserHelper;
-import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.Context;
-import android.content.Intent;
-import android.content.IntentFilter;
 import android.content.pm.PackageManager;
 import android.content.pm.PackageManager.NameNotFoundException;
 import android.content.pm.UserInfo;
@@ -275,23 +272,6 @@
     @GuardedBy("mLockHelper")
     private ZoneUserBindingHelper mZoneUserBindingHelper;
 
-    private final BroadcastReceiver mUserLifecycleReceiver = new BroadcastReceiver() {
-        @Override
-        public void onReceive(Context context, Intent intent) {
-            if (Log.isLoggable(TAG_USER, Log.DEBUG)) {
-                Log.d(TAG_USER, "onReceive: " + intent);
-            }
-            switch(intent.getAction()) {
-                case Intent.ACTION_USER_REMOVED:
-                    int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL);
-                    notifyHalUserRemoved(userId);
-                    break;
-                default:
-                    Log.w(TAG, "received unexpected intent: " + intent);
-            }
-        }
-    };
-
     /** Map used to avoid calling UserHAL when a user was removed because HAL creation failed. */
     @GuardedBy("mLockUser")
     private final SparseBooleanArray mFailedToCreateUserIds = new SparseBooleanArray(1);
@@ -337,8 +317,6 @@
         if (Log.isLoggable(TAG_USER, Log.DEBUG)) {
             Log.d(TAG_USER, "init()");
         }
-        mContext.registerReceiver(mUserLifecycleReceiver,
-                new IntentFilter(Intent.ACTION_USER_REMOVED));
         mCarUxRestrictionService.registerUxRestrictionsChangeListener(
                 mCarUxRestrictionsChangeListener, Display.DEFAULT_DISPLAY);
 
@@ -350,7 +328,6 @@
         if (Log.isLoggable(TAG_USER, Log.DEBUG)) {
             Log.d(TAG_USER, "release()");
         }
-        mContext.unregisterReceiver(mUserLifecycleReceiver);
         mCarUxRestrictionService
                 .unregisterUxRestrictionsChangeListener(mCarUxRestrictionsChangeListener);
     }
@@ -1192,11 +1169,28 @@
         }
     }
 
-    private void notifyHalUserRemoved(@UserIdInt int userId) {
+    /**
+     * Should be called by {@code ICarImpl} only.
+     */
+    public void onUserRemoved(@NonNull UserInfo user) {
+        if (Log.isLoggable(TAG_USER, Log.DEBUG)) {
+            Log.d(TAG_USER, "onUserRemoved: " + user.toFullString());
+        }
+        notifyHalUserRemoved(user);
+    }
+
+    private void notifyHalUserRemoved(@NonNull UserInfo user) {
         if (!isUserHalSupported()) return;
 
+        if (user == null) {
+            Log.wtf(TAG, "notifyHalUserRemoved() called for null user");
+            return;
+        }
+
+        int userId = user.id;
+
         if (userId == UserHandle.USER_NULL) {
-            Log.wtf(TAG, "notifyHalUserRemoved() Called for UserHandle.USER_NULL");
+            Log.wtf(TAG, "notifyHalUserRemoved() called for UserHandle.USER_NULL");
             return;
         }
 
@@ -1213,10 +1207,7 @@
         android.hardware.automotive.vehicle.V2_0.UserInfo halUser =
                 new android.hardware.automotive.vehicle.V2_0.UserInfo();
         halUser.userId = userId;
-        // TODO(b/155913815): per the REMOVE_USER API, the userFlags should be set as well, but it's
-        // gone. We'll need to either update the documentation, or if that's not possible (as it's
-        // breaking the contract), either change the intent to contain the flags, or keep track of
-        // the flags internally.
+        halUser.flags = UserHalHelper.convertFlags(user);
 
         RemoveUserRequest request = new RemoveUserRequest();
         request.removedUserInfo = halUser;
@@ -1415,6 +1406,8 @@
     }
 
     private void removeCreatedUser(@NonNull UserInfo user, @NonNull String reason) {
+        Log.i(TAG, "removing " + user.toFullString() + "; reason: " + reason);
+
         int userId = user.id;
         EventLog.writeEvent(EventLogTags.CAR_USER_SVC_CREATE_USER_USER_REMOVED, userId, reason);