Remove pre create user from android start up

Bug: 182288420
Test: atest CarServiceUnitTest:com.android.car.user.CarUserServiceTest CarServiceUnitTest:com.android.car.user.CarUserManagerUnitTest
    atest CarSecurityPermissionTest:com.android.car.user.CarUserManagerPermissionTest
Change-Id: I6e4c3fec23591c7cfdd14a4a8c6e359adadea0e7
diff --git a/car-internal-lib/src/com/android/car/internal/ICarSystemServerClient.aidl b/car-internal-lib/src/com/android/car/internal/ICarSystemServerClient.aidl
index 405a637..3020646 100644
--- a/car-internal-lib/src/com/android/car/internal/ICarSystemServerClient.aidl
+++ b/car-internal-lib/src/com/android/car/internal/ICarSystemServerClient.aidl
@@ -48,11 +48,6 @@
     void initBootUser();
 
     /**
-     * Notify to pre-create users.
-     */
-    void preCreateUsers();
-
-    /**
       * Notify that the device must be factory reset, so CarService can ask user to confirm.
       *
       * @param callback used to trigger the factory reset.
diff --git a/car-lib/src/android/car/ICarUserService.aidl b/car-lib/src/android/car/ICarUserService.aidl
index abd5d28..267044d 100644
--- a/car-lib/src/android/car/ICarUserService.aidl
+++ b/car-lib/src/android/car/ICarUserService.aidl
@@ -21,6 +21,7 @@
 import android.car.user.UserRemovalResult;
 import android.car.user.UserIdentificationAssociationResponse;
 import android.car.user.UserSwitchResult;
+
 import com.android.internal.infra.AndroidFuture;
 import com.android.internal.os.IResultReceiver;
 
@@ -33,6 +34,7 @@
     void setUserSwitchUiCallback(in IResultReceiver callback);
     void createUser(@nullable String name, String userType, int flags, int timeoutMs,
       in AndroidFuture<UserCreationResult> receiver);
+    void updatePreCreatedUsers();
     void removeUser(int userId, in AndroidFuture<UserRemovalResult> receiver);
     List<UserInfo> getAllDrivers();
     List<UserInfo> getPassengers(int driverId);
diff --git a/car-lib/src/android/car/user/CarUserManager.java b/car-lib/src/android/car/user/CarUserManager.java
index 0e7fdd2..7dc0e7e 100644
--- a/car-lib/src/android/car/user/CarUserManager.java
+++ b/car-lib/src/android/car/user/CarUserManager.java
@@ -446,6 +446,29 @@
         return createUser(name, UserManager.USER_TYPE_FULL_SECONDARY, flags);
     }
 
+    /**
+     * Updates pre-created users.
+     * <p>
+     * Updates pre-created users based on the car properties defined
+     * {@code CarProperties.number_pre_created_guests} and (@code
+     * CarProperties.number_pre_created_users}.
+     *
+     * @hide
+     */
+    @RequiresPermission(anyOf = {android.Manifest.permission.MANAGE_USERS,
+            android.Manifest.permission.CREATE_USERS})
+    public void updatePreCreatedUsers() {
+        int uid = myUid();
+        EventLog.writeEvent(EventLogTags.CAR_USER_MGR_PRE_CREATE_USER_REQ, uid);
+        try {
+            mService.updatePreCreatedUsers();
+        } catch (SecurityException e) {
+            throw e;
+        } catch (RemoteException | RuntimeException e) {
+            handleExceptionFromCarService(e, null);
+        }
+    }
+
     // TODO(b/159283854): move to UserManager
     private void onGuestCreated(UserInfo user) {
         Settings.Secure.putStringForUser(getContext().getContentResolver(),
diff --git a/car-lib/src/com/android/car/internal/common/EventLogTags.logtags b/car-lib/src/com/android/car/internal/common/EventLogTags.logtags
index 41412c6..31ed2b1 100644
--- a/car-lib/src/com/android/car/internal/common/EventLogTags.logtags
+++ b/car-lib/src/com/android/car/internal/common/EventLogTags.logtags
@@ -48,11 +48,13 @@
 150051 car_service_vhal_reconnected (number_services|1)
 150052 car_service_set_car_service_helper (pid|1)
 150053 car_service_on_user_lifecycle (type|1),(from_user_id|1),(to_user_id|1)
-150054 car_service_set_initial_user (user_id|1)
+#150054 (Not_used_anymore) car_service_set_initial_user (user_id|1)
 150055 car_service_create (has_vhal|1)
 150056 car_service_connected (interface|3)
 150057 car_service_destroy (has_vhal|1)
 150058 car_service_vhal_died (cookie|2)
+150059 car_service_init_boot_user
+150060 car_service_on_user_removed (user_id|1)
 
 ####
 #### Tags used by CarService subsystems, like user and power management.
@@ -118,6 +120,7 @@
 150182 car_user_mgr_remove_user_req (uid|1),(user_id|1)
 150183 car_user_mgr_remove_user_resp (uid|1),(status|1)
 150184 car_user_mgr_notify_lifecycle_listener (number_listeners|1),(event_type|1),(from_user_id|1),(to_user_id|1)
+150185 car_user_mgr_pre_create_user_req (uid|1)
 
 #### Device policy related tags (range 150200 - 150299)
 150200 car_dp_mgr_remove_user_req (uid|1),(user_id|1)
diff --git a/service/res/values/config.xml b/service/res/values/config.xml
index afbe13e..4c879ef 100644
--- a/service/res/values/config.xml
+++ b/service/res/values/config.xml
@@ -389,6 +389,4 @@
     <!-- Number of milliseconds to wait before the system goes into Suspend-to-RAM.
          The valid range is 0 to 3 * 60 * 1000. The default duration is 3 min (= 3 * 60 * 1000). -->
     <integer name="config_maxSuspendWaitDuration">180000</integer>
-    <!-- Number of milliseconds to wait before creating pre-created users after system start. -->
-    <integer name="config_WaitDurationForUserPreCreation">100000</integer>
 </resources>
diff --git a/service/src/com/android/car/ICarImpl.java b/service/src/com/android/car/ICarImpl.java
index 651fe87..a7ca6a2 100644
--- a/service/src/com/android/car/ICarImpl.java
+++ b/service/src/com/android/car/ICarImpl.java
@@ -57,9 +57,9 @@
 import com.android.car.cluster.ClusterHomeService;
 import com.android.car.cluster.ClusterNavigationService;
 import com.android.car.cluster.InstrumentClusterService;
+import com.android.car.evs.CarEvsService;
 import com.android.car.garagemode.GarageModeService;
 import com.android.car.hal.VehicleHal;
-import com.android.car.evs.CarEvsService;
 import com.android.car.internal.ICarServiceHelper;
 import com.android.car.internal.ICarSystemServerClient;
 import com.android.car.internal.common.EventLogTags;
@@ -924,23 +924,15 @@
         @Override
         public void initBootUser() throws RemoteException {
             assertCallingFromSystemProcess();
-            // TODO(b/160819016): add events log
+            EventLog.writeEvent(EventLogTags.CAR_SERVICE_INIT_BOOT_USER);
             if (DBG) Slog.d(TAG, "initBootUser(): ");
             mCarUserService.initBootUser();
         }
 
         @Override
-        public void preCreateUsers() throws RemoteException {
-            assertCallingFromSystemProcess();
-            // TODO(b/160819016): add events log
-            if (DBG) Slog.d(TAG, "preCreateUsers(): ");
-            mCarUserService.preCreateUsers();
-        }
-
-        @Override
         public void onUserRemoved(UserInfo user) throws RemoteException {
             assertCallingFromSystemProcess();
-            // TODO(b/160819016): add events log
+            EventLog.writeEvent(EventLogTags.CAR_SERVICE_ON_USER_REMOVED, user.id);
             if (DBG) Slog.d(TAG, "onUserRemoved(): " + user.toFullString());
             mCarUserService.onUserRemoved(user);
         }
diff --git a/service/src/com/android/car/user/CarUserService.java b/service/src/com/android/car/user/CarUserService.java
index 8fc0da5..b80be81 100644
--- a/service/src/com/android/car/user/CarUserService.java
+++ b/service/src/com/android/car/user/CarUserService.java
@@ -218,8 +218,6 @@
     // TODO(b/163566866): Use mSwitchGuestUserBeforeSleep for new create guest request
     private final boolean mSwitchGuestUserBeforeSleep;
 
-    private final int mUserPreCreationDelayMs;
-
     @Nullable
     @GuardedBy("mLockUser")
     private UserInfo mInitialUser;
@@ -317,8 +315,6 @@
         mEnablePassengerSupport = resources.getBoolean(R.bool.enablePassengerSupport);
         mSwitchGuestUserBeforeSleep = resources.getBoolean(
                 R.bool.config_switchGuestUserBeforeGoingSleep);
-        mUserPreCreationDelayMs = resources.getInteger(
-                R.integer.config_WaitDurationForUserPreCreation);
         mCarUxRestrictionService = uxRestrictionService;
     }
 
@@ -361,7 +357,6 @@
         }
 
         writer.println("SwitchGuestUserBeforeSleep: " + mSwitchGuestUserBeforeSleep);
-        writer.println("UserPreCreationDelayMs: " + mUserPreCreationDelayMs);
 
         writer.println("MaxRunningUsers: " + mMaxRunningUsers);
         List<UserInfo> allDrivers = getAllDrivers();
@@ -759,8 +754,7 @@
             initResumeReplaceGuest();
         }
 
-        // No delay required for pre-creating users on suspend.
-        preCreateUsersInternal(/*delay=*/ 0);
+        preCreateUsersInternal();
     }
 
     /**
@@ -2293,12 +2287,14 @@
     /**
      * Manages the required number of pre-created users.
      */
-    public void preCreateUsers() {
-        preCreateUsersInternal(mUserPreCreationDelayMs);
+    @Override
+    public void updatePreCreatedUsers() {
+        checkManageOrCreateUsersPermission("preCreateUsers");
+        preCreateUsersInternal();
     }
 
-    private void preCreateUsersInternal(int delay) {
-        mHandler.postDelayed(() -> mUserPreCreator.managePreCreatedUsers(), delay);
+    private void preCreateUsersInternal() {
+        mHandler.post(() -> mUserPreCreator.managePreCreatedUsers());
     }
 
     // TODO(b/167698977): members below were copied from UserManagerService; it would be better to
diff --git a/tests/CarSecurityPermissionTest/src/com/android/car/user/CarUserManagerPermissionTest.java b/tests/CarSecurityPermissionTest/src/com/android/car/user/CarUserManagerPermissionTest.java
index d984802..5d20577 100644
--- a/tests/CarSecurityPermissionTest/src/com/android/car/user/CarUserManagerPermissionTest.java
+++ b/tests/CarSecurityPermissionTest/src/com/android/car/user/CarUserManagerPermissionTest.java
@@ -76,6 +76,14 @@
     }
 
     @Test
+    public void testUpdatePreCreatedUserPermission() throws Exception {
+        Exception e = expectThrows(SecurityException.class,
+                () -> mCarUserManager.updatePreCreatedUsers());
+        assertThat(e.getMessage()).contains(CREATE_USERS);
+        assertThat(e.getMessage()).contains(MANAGE_USERS);
+    }
+
+    @Test
     public void testCreateUserPermission() throws Exception {
         Exception e = expectThrows(SecurityException.class,
                 () -> mCarUserManager.createUser(null, UserManager.USER_TYPE_FULL_SECONDARY, 0));
diff --git a/tests/carservice_unit_test/src/com/android/car/user/CarUserManagerUnitTest.java b/tests/carservice_unit_test/src/com/android/car/user/CarUserManagerUnitTest.java
index 7662c7b..557d5c3 100644
--- a/tests/carservice_unit_test/src/com/android/car/user/CarUserManagerUnitTest.java
+++ b/tests/carservice_unit_test/src/com/android/car/user/CarUserManagerUnitTest.java
@@ -426,6 +426,13 @@
     }
 
     @Test
+    public void testUpdatedPreCreatedUser_success() throws Exception {
+        mMgr.updatePreCreatedUsers();
+
+        verify(mService).updatePreCreatedUsers();
+    }
+
+    @Test
     public void testGetUserIdentificationAssociation_nullTypes() throws Exception {
         assertThrows(IllegalArgumentException.class,
                 () -> mMgr.getUserIdentificationAssociation(null));
diff --git a/tests/carservice_unit_test/src/com/android/car/user/CarUserServiceTest.java b/tests/carservice_unit_test/src/com/android/car/user/CarUserServiceTest.java
index 0385861..e54437d 100644
--- a/tests/carservice_unit_test/src/com/android/car/user/CarUserServiceTest.java
+++ b/tests/carservice_unit_test/src/com/android/car/user/CarUserServiceTest.java
@@ -2032,6 +2032,14 @@
     }
 
     @Test
+    public void testUpdatePreCreatedUser_success() throws Exception {
+        mCarUserService.updatePreCreatedUsers();
+        waitForHandlerThreadToFinish();
+
+        verify(mUserPreCreator).managePreCreatedUsers();
+    }
+
+    @Test
     public void testOnSuspend_replace() throws Exception {
         mockExistingUsersAndCurrentUser(mGuestUser);
         when(mInitialUserSetter.canReplaceGuestUser(any())).thenReturn(true);