Write staged sessions to /data/staging.

If a session is marked as isStaged, write the package files directly on
a directory named /data/staging/session_${SESSION_ID}, instead of the
/data/app temporary directory. This allows us to set different SELinux
policy (e.g. allowing apexd to read that directory), and to persist the
data across reboots without altering the existing code.

To ensure we cover existing workflows, in this CL we try as much as
possible to re-use existing code to create staging directories and wire
data into them.

Bug: 118865310
Test: Verified that adb install --staged file.apk successfully creates a
/data/staging/session_SESSION_ID/base.apk file. If --apex is passed, the
file is named base.apex.
Change-Id: Iacfd7cfb90b738eeb752fec3e6a4e38ea307259a
diff --git a/services/core/java/com/android/server/pm/PackageManagerShellCommand.java b/services/core/java/com/android/server/pm/PackageManagerShellCommand.java
index ac9c6ef..2e9d26a 100644
--- a/services/core/java/com/android/server/pm/PackageManagerShellCommand.java
+++ b/services/core/java/com/android/server/pm/PackageManagerShellCommand.java
@@ -2293,6 +2293,9 @@
                     break;
                 case "--apex":
                     sessionParams.installFlags |= PackageManager.INSTALL_APEX;
+                    // TODO(b/118865310): APEX packages should always imply
+                    //                    sessionParams.isStaged(). Enforce this when the staged
+                    //                    install workflow is complete.
                     break;
                 case "--multi-package":
                     sessionParams.setMultiPackage();
@@ -2588,7 +2591,7 @@
         try {
             session = new PackageInstaller.Session(
                     mInterface.getPackageInstaller().openSession(sessionId));
-            if (!session.isMultiPackage()) {
+            if (!session.isMultiPackage() && !session.isStaged()) {
                 // Sanity check that all .dm files match an apk.
                 // (The installer does not support standalone .dm files and will not process them.)
                 try {