Clean up generatePackageInfoFromApex() API

This changes the API for generatePackageInfoFromApex in PackageParser
in response to review suggestions made in ag/6701090.

The method is renamed to generatePackageInfo with a PackageParser.Package
parameter (as well as an ApexInfo and flags). The implementation is combined
with the main generatePackageInfo method. Callers are changed to perform
the parsing themselves, including the collection of certificates if
necessary.

Note: the method signature and implementation had already changed since
ag/6701090, so the suggestions from that CL may not apply directly.

Bug: 129261524
Test: atest PackageParserTest
Test: atest CtsStagedInstallHostTestCases
Change-Id: I495539679812110f89d5e3d93f2622ba2dab36c0
diff --git a/services/core/java/com/android/server/pm/ApexManager.java b/services/core/java/com/android/server/pm/ApexManager.java
index dd099b1..a1b6d49 100644
--- a/services/core/java/com/android/server/pm/ApexManager.java
+++ b/services/core/java/com/android/server/pm/ApexManager.java
@@ -282,30 +282,39 @@
                         if ((new File(ai.modulePath)).isDirectory()) {
                             break;
                         }
+                        int flags = PackageManager.GET_META_DATA
+                                | PackageManager.GET_SIGNING_CERTIFICATES
+                                | PackageManager.GET_SIGNATURES;
+                        PackageParser.Package pkg;
                         try {
-                            final PackageInfo pkg = PackageParser.generatePackageInfoFromApex(
-                                    ai, PackageManager.GET_META_DATA
-                                            | PackageManager.GET_SIGNING_CERTIFICATES);
-                            mAllPackagesCache.add(pkg);
-                            if (ai.isActive) {
-                                if (activePackagesSet.contains(pkg.packageName)) {
-                                    throw new IllegalStateException(
-                                            "Two active packages have the same name: "
-                                                    + pkg.packageName);
-                                }
-                                activePackagesSet.add(pkg.packageName);
-                            }
-                            if (ai.isFactory) {
-                                if (factoryPackagesSet.contains(pkg.packageName)) {
-                                    throw new IllegalStateException(
-                                            "Two factory packages have the same name: "
-                                                    + pkg.packageName);
-                                }
-                                factoryPackagesSet.add(pkg.packageName);
-                            }
+                            File apexFile = new File(ai.modulePath);
+                            PackageParser pp = new PackageParser();
+                            pkg = pp.parsePackage(apexFile, flags, false);
+                            PackageParser.collectCertificates(pkg, false);
                         } catch (PackageParser.PackageParserException pe) {
                             throw new IllegalStateException("Unable to parse: " + ai, pe);
                         }
+
+                        final PackageInfo packageInfo =
+                                PackageParser.generatePackageInfo(pkg, ai, flags);
+                        mAllPackagesCache.add(packageInfo);
+                        if (ai.isActive) {
+                            if (activePackagesSet.contains(packageInfo.packageName)) {
+                                throw new IllegalStateException(
+                                        "Two active packages have the same name: "
+                                                + packageInfo.packageName);
+                            }
+                            activePackagesSet.add(packageInfo.packageName);
+                        }
+                        if (ai.isFactory) {
+                            if (factoryPackagesSet.contains(packageInfo.packageName)) {
+                                throw new IllegalStateException(
+                                        "Two factory packages have the same name: "
+                                                + packageInfo.packageName);
+                            }
+                            factoryPackagesSet.add(packageInfo.packageName);
+                        }
+
                     }
                 } catch (RemoteException re) {
                     Slog.e(TAG, "Unable to retrieve packages from apexservice: " + re.toString());