Use ART Service to clear app profiles when it is enabled.

Changed system server initialization order: ArtManagerLocal needs to be
initialized before mPackageManagerService.systemReady() is called,
because that calls CarrierAppUtils.disableCarrierAppsUntilPrivileged,
which may call ApplicationPackageManager.setSystemAppState to set
SYSTEM_APP_STATE_UNINSTALLED, which ends up calling ArtManagerLocal to
clean up any app profiles.

Test: Boot with dalvik.vm.useartservice=true and verify that calls to
      clearAppProfilesLIF and destroyAppProfilesLeafLIF during boot
      are handled by ART Service.
Bug: 251903639
Change-Id: I8a3a16ce85f315a28fa8459325470cfe5f32ca8c
diff --git a/services/core/java/com/android/server/pm/AppDataHelper.java b/services/core/java/com/android/server/pm/AppDataHelper.java
index d252d40..b1c6f8c 100644
--- a/services/core/java/com/android/server/pm/AppDataHelper.java
+++ b/services/core/java/com/android/server/pm/AppDataHelper.java
@@ -20,6 +20,7 @@
 
 import static com.android.server.pm.DexOptHelper.useArtService;
 import static com.android.server.pm.PackageManagerService.TAG;
+import static com.android.server.pm.PackageManagerServiceUtils.getPackageManagerLocal;
 import static com.android.server.pm.PackageManagerServiceUtils.logCriticalInfo;
 
 import android.annotation.NonNull;
@@ -586,11 +587,14 @@
             Slog.wtf(TAG, "Package was null!", new Throwable());
             return;
         }
-        // TODO(b/251903639): Call into ART Service.
-        try {
-            mArtManagerService.clearAppProfiles(pkg);
-        } catch (LegacyDexoptDisabledException e) {
-            throw new RuntimeException(e);
+        if (useArtService()) {
+            destroyAppProfilesWithArtService(pkg);
+        } else {
+            try {
+                mArtManagerService.clearAppProfiles(pkg);
+            } catch (LegacyDexoptDisabledException e) {
+                throw new RuntimeException(e);
+            }
         }
     }
 
@@ -629,13 +633,28 @@
     }
 
     private void destroyAppProfilesLeafLIF(AndroidPackage pkg) {
-        // TODO(b/251903639): Call into ART Service.
-        try {
-            mInstaller.destroyAppProfiles(pkg.getPackageName());
-        } catch (LegacyDexoptDisabledException e) {
-            throw new RuntimeException(e);
-        } catch (Installer.InstallerException e) {
-            Slog.w(TAG, String.valueOf(e));
+        if (useArtService()) {
+            destroyAppProfilesWithArtService(pkg);
+        } else {
+            try {
+                mInstaller.destroyAppProfiles(pkg.getPackageName());
+            } catch (LegacyDexoptDisabledException e) {
+                throw new RuntimeException(e);
+            } catch (Installer.InstallerException e) {
+                Slog.w(TAG, String.valueOf(e));
+            }
+        }
+    }
+
+    private void destroyAppProfilesWithArtService(AndroidPackage pkg) {
+        try (PackageManagerLocal.FilteredSnapshot snapshot =
+                        getPackageManagerLocal().withFilteredSnapshot()) {
+            try {
+                DexOptHelper.getArtManagerLocal().clearAppProfiles(snapshot, pkg.getPackageName());
+            } catch (IllegalArgumentException e) {
+                // Package isn't found, but that should only happen due to race.
+                Slog.w(TAG, e);
+            }
         }
     }
 
diff --git a/services/java/com/android/server/SystemServer.java b/services/java/com/android/server/SystemServer.java
index 5c5442d..3725756 100644
--- a/services/java/com/android/server/SystemServer.java
+++ b/services/java/com/android/server/SystemServer.java
@@ -2736,6 +2736,10 @@
         mSystemServiceManager.startService(PermissionPolicyService.class);
         t.traceEnd();
 
+        t.traceBegin("ArtManagerLocal");
+        DexOptHelper.initializeArtManagerLocal(context, mPackageManagerService);
+        t.traceEnd();
+
         t.traceBegin("MakePackageManagerServiceReady");
         mPackageManagerService.systemReady();
         t.traceEnd();
@@ -2770,10 +2774,6 @@
         mSystemServiceManager.startService(GAME_MANAGER_SERVICE_CLASS);
         t.traceEnd();
 
-        t.traceBegin("ArtManagerLocal");
-        DexOptHelper.initializeArtManagerLocal(context, mPackageManagerService);
-        t.traceEnd();
-
         if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_UWB)) {
             t.traceBegin("UwbService");
             mSystemServiceManager.startServiceFromJar(UWB_SERVICE_CLASS, UWB_APEX_SERVICE_JAR_PATH);