Remove LOCKED_BOOT_COMPLETED from CarPackageManager and CarUserService

- LOCKED_BOOT_COMPLETED for user 0 can fail for car service if user 0
  starting happens before car service's register.
- When car service is started, user 0 is already in locked state, so
  the check does not do anything useful.
- CarUserService: move the user 0 restriction setting to user 0 unlock instead.
- CarPackageManager: move package parsing into user switching. App install
  can be different per user and it makes sense to re-run it per each
  user switching.

Bug: 129945695
Test: build, unit tests, check adb shell dumpsys car_service

Change-Id: Iedd765f5b449aa49173946b612cf8092ce94e79c
diff --git a/service/src/com/android/car/user/CarUserService.java b/service/src/com/android/car/user/CarUserService.java
index f0192ed..a5d64dc 100644
--- a/service/src/com/android/car/user/CarUserService.java
+++ b/service/src/com/android/car/user/CarUserService.java
@@ -87,13 +87,10 @@
         if (Log.isLoggable(TAG, Log.DEBUG)) {
             Log.d(TAG, "init");
         }
-        if (mCarUserManagerHelper.isHeadlessSystemUser()) {
-            IntentFilter filter = new IntentFilter();
-            filter.addAction(Intent.ACTION_LOCKED_BOOT_COMPLETED);
-            filter.addAction(Intent.ACTION_USER_SWITCHED);
+        IntentFilter filter = new IntentFilter();
+        filter.addAction(Intent.ACTION_USER_SWITCHED);
 
-            mContext.registerReceiver(this, filter);
-        }
+        mContext.registerReceiver(this, filter);
     }
 
     @Override
@@ -101,9 +98,7 @@
         if (Log.isLoggable(TAG, Log.DEBUG)) {
             Log.d(TAG, "release");
         }
-        if (mCarUserManagerHelper.isHeadlessSystemUser()) {
-            mContext.unregisterReceiver(this);
-        }
+        mContext.unregisterReceiver(this);
     }
 
     @Override
@@ -121,24 +116,25 @@
         writer.println("LastUnlockedUsers:" + lastUnlockedUsers);
     }
 
+    private void updateDefaultUserRestriction() {
+        // We want to set restrictions on system and guest users only once. These are persisted
+        // onto disk, so it's sufficient to do it once + we minimize the number of disk writes.
+        if (Settings.Global.getInt(mContext.getContentResolver(),
+                CarSettings.Global.DEFAULT_USER_RESTRICTIONS_SET, /* default= */ 0) == 0) {
+            setSystemUserRestrictions();
+            mCarUserManagerHelper.initDefaultGuestRestrictions();
+            Settings.Global.putInt(mContext.getContentResolver(),
+                    CarSettings.Global.DEFAULT_USER_RESTRICTIONS_SET, 1);
+        }
+    }
+
     @Override
     public void onReceive(Context context, Intent intent) {
         if (Log.isLoggable(TAG, Log.DEBUG)) {
             Log.d(TAG, "onReceive " + intent);
         }
 
-        if (Intent.ACTION_LOCKED_BOOT_COMPLETED.equals(intent.getAction())) {
-            // We want to set restrictions on system and guest users only once. These are persisted
-            // onto disk, so it's sufficient to do it once + we minimize the number of disk writes.
-            if (Settings.Global.getInt(mContext.getContentResolver(),
-                    CarSettings.Global.DEFAULT_USER_RESTRICTIONS_SET, /* default= */ 0) == 0) {
-                setSystemUserRestrictions();
-                mCarUserManagerHelper.initDefaultGuestRestrictions();
-                Settings.Global.putInt(mContext.getContentResolver(),
-                        CarSettings.Global.DEFAULT_USER_RESTRICTIONS_SET, 1);
-            }
-
-        } else if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) {
+        if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) {
             // Update last active user if the switched-to user is a persistent, non-system user.
             final int currentUser = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
             if (currentUser > UserHandle.USER_SYSTEM
@@ -173,6 +169,7 @@
             }
             // Assumes that car service need to do it only once during boot-up
             if (unlocked && !mUser0Unlocked) {
+                updateDefaultUserRestriction();
                 tasks = new ArrayList<>(mUser0UnlockTasks);
                 mUser0UnlockTasks.clear();
                 mUser0Unlocked = unlocked;