Making it possible to call setFirstBoot when mInstallD is available

Along with that also catching Exception to be consistent with other
Installer methods

Bug: b/226868221
Test: Manual testing
Change-Id: Ifab04ca6539afdc3fa254e9abb45f66bda6e1b59
diff --git a/services/core/java/com/android/server/pm/Installer.java b/services/core/java/com/android/server/pm/Installer.java
index 2a66e02..676d7c2 100644
--- a/services/core/java/com/android/server/pm/Installer.java
+++ b/services/core/java/com/android/server/pm/Installer.java
@@ -119,7 +119,7 @@
             IInstalld.FLAG_CLEAR_APP_DATA_KEEP_ART_PROFILES;
 
     private final boolean mIsolated;
-
+    private volatile boolean mDeferSetFirstBoot;
     private volatile IInstalld mInstalld;
     private volatile Object mWarnIfHeld;
 
@@ -171,6 +171,7 @@
             mInstalld = IInstalld.Stub.asInterface(binder);
             try {
                 invalidateMounts();
+                executeDeferredActions();
             } catch (InstallerException ignored) {
             }
         } else {
@@ -180,6 +181,15 @@
     }
 
     /**
+     * Perform any deferred actions on mInstalld while the connection could not be made.
+     */
+    private void executeDeferredActions() throws InstallerException {
+        if (mDeferSetFirstBoot) {
+            setFirstBoot();
+        }
+    }
+
+    /**
      * Do several pre-flight checks before making a remote call.
      *
      * @return if the remote call should continue.
@@ -291,8 +301,15 @@
             return;
         }
         try {
-            mInstalld.setFirstBoot();
-        } catch (RemoteException e) {
+            // mInstalld might be null if the connection could not be established.
+            if (mInstalld != null) {
+                mInstalld.setFirstBoot();
+            } else {
+                // if it is null while trying to set the first boot, set a flag to try and set the
+                // first boot when the connection is eventually established
+                mDeferSetFirstBoot = true;
+            }
+        } catch (Exception e) {
             throw InstallerException.from(e);
         }
     }