Reschedule the pre-reboot verification after boot completed.

Bug: 143528612
Test: atest CtsStagedInstallHostTestCases
Change-Id: I34d7ee3b84bc351d58ebf024a7ed10dd7db1f55b
Merged-In: I34d7ee3b84bc351d58ebf024a7ed10dd7db1f55b
(cherry picked from commit b793c2174ec133b6f7d3b1d439d30423fffcf59f)
diff --git a/services/core/java/com/android/server/pm/PackageInstallerService.java b/services/core/java/com/android/server/pm/PackageInstallerService.java
index 47dc509..6a47c4c 100644
--- a/services/core/java/com/android/server/pm/PackageInstallerService.java
+++ b/services/core/java/com/android/server/pm/PackageInstallerService.java
@@ -218,6 +218,7 @@
 
     public void systemReady() {
         mAppOps = mContext.getSystemService(AppOpsManager.class);
+        mStagingManager.systemReady();
 
         synchronized (mSessions) {
             readSessionsLocked();
diff --git a/services/core/java/com/android/server/pm/StagingManager.java b/services/core/java/com/android/server/pm/StagingManager.java
index ff578a6..2210ff8 100644
--- a/services/core/java/com/android/server/pm/StagingManager.java
+++ b/services/core/java/com/android/server/pm/StagingManager.java
@@ -21,10 +21,12 @@
 import android.apex.ApexInfo;
 import android.apex.ApexInfoList;
 import android.apex.ApexSessionInfo;
+import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.IIntentReceiver;
 import android.content.IIntentSender;
 import android.content.Intent;
+import android.content.IntentFilter;
 import android.content.IntentSender;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageInstaller;
@@ -73,12 +75,16 @@
     private final PackageInstallerService mPi;
     private final ApexManager mApexManager;
     private final PowerManager mPowerManager;
+    private final Context mContext;
     private final Handler mBgHandler;
+    private PackageInstallerSession mPendingSession;
+    private boolean mIsReady;
 
     @GuardedBy("mStagedSessions")
     private final SparseArray<PackageInstallerSession> mStagedSessions = new SparseArray<>();
 
     StagingManager(PackageInstallerService pi, ApexManager am, Context context) {
+        mContext = context;
         mPi = pi;
         mApexManager = am;
         mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
@@ -204,6 +210,11 @@
 
     private void preRebootVerification(@NonNull PackageInstallerSession session) {
         try {
+            if (!mIsReady) {
+                mPendingSession = session;
+                return;
+            }
+
             boolean success = true;
 
             final ApexInfoList apexInfoList = new ApexInfoList();
@@ -691,6 +702,28 @@
         }
     }
 
+    void systemReady() {
+        // Register the receiver of boot completed intent for staging manager.
+        mContext.registerReceiver(new BroadcastReceiver() {
+            @Override
+            public void onReceive(Context ctx, Intent intent) {
+                readyToStart();
+                ctx.unregisterReceiver(this);
+            }
+        }, new IntentFilter(Intent.ACTION_BOOT_COMPLETED));
+    }
+
+    // Notify the handler that system is ready, and reschedule the pre-reboot verifications.
+    private synchronized void readyToStart() {
+        mIsReady = true;
+        if (mPendingSession != null) {
+            mBgHandler.post(() -> {
+                preRebootVerification(mPendingSession);
+                mPendingSession = null;
+            });
+        }
+    }
+
     private static class LocalIntentReceiver {
         private final LinkedBlockingQueue<Intent> mResult = new LinkedBlockingQueue<>();