Limit background user starting to max running users

- startUserInBackground can allow more uses that is configured.
  So restrict it inside car service. Eviction is based on LRU.
- Fix mishandling of background user in stopped state: It is auto-unlocked
  and unlockUser fails. This leads into not stopping the user.
  Add more logic to check such state so that such case is handled.
- Clean up confusing user lock / unlock check in CarUserService
  It could go wrong if any user is unlocked before user 0.
  (Not true in car now)

Bug: 130158283
Test: run unit tests, check started background users when garage mode starts

Change-Id: I2a2baa1ed1cf845d6dd10c19a4cf644939c72a75
diff --git a/service/src/com/android/car/ICarImpl.java b/service/src/com/android/car/ICarImpl.java
index f67ead0..3d183db 100644
--- a/service/src/com/android/car/ICarImpl.java
+++ b/service/src/com/android/car/ICarImpl.java
@@ -17,6 +17,7 @@
 package com.android.car;
 
 import android.annotation.MainThread;
+import android.app.ActivityManager;
 import android.app.UiModeManager;
 import android.car.Car;
 import android.car.ICar;
@@ -24,6 +25,7 @@
 import android.car.userlib.CarUserManagerHelper;
 import android.content.Context;
 import android.content.pm.PackageManager;
+import android.content.res.Resources;
 import android.hardware.automotive.vehicle.V2_0.IVehicle;
 import android.hardware.automotive.vehicle.V2_0.VehicleArea;
 import android.os.Binder;
@@ -118,7 +120,11 @@
         mHal = new VehicleHal(vehicle);
         mVehicleInterfaceName = vehicleInterfaceName;
         mUserManagerHelper = new CarUserManagerHelper(serviceContext);
-        mCarUserService = new CarUserService(serviceContext, mUserManagerHelper);
+        final Resources res = mContext.getResources();
+        final int maxRunningUsers = res.getInteger(
+                com.android.internal.R.integer.config_multiuserMaxRunningUsers);
+        mCarUserService = new CarUserService(serviceContext, mUserManagerHelper,
+                ActivityManager.getService(), maxRunningUsers);
         mSystemActivityMonitoringService = new SystemActivityMonitoringService(serviceContext);
         mCarPowerManagementService = new CarPowerManagementService(mContext, mHal.getPowerHal(),
                 systemInterface, mUserManagerHelper);