Convert compilation reason and filter to int-enum before logging

Test: adb logcat -b events | grep sysui_multi_action
Bug: 73102540
Change-Id: I052df3d4e65ef16b43190c14a236421945c86e21
(cherry picked from commit c7a44be7b3faef307f47db479bd6b06846201cf9)
diff --git a/cmds/statsd/src/atoms.proto b/cmds/statsd/src/atoms.proto
index 99611f4..fc2241f 100644
--- a/cmds/statsd/src/atoms.proto
+++ b/cmds/statsd/src/atoms.proto
@@ -1321,10 +1321,10 @@
     optional string launch_token = 13;
 
     // The compiler filter used when when the package was optimized.
-    optional string package_optimization_compilation_filter = 14;
+    optional int32 package_optimization_compilation_filter = 14;
 
     // The reason why the package was optimized.
-    optional string package_optimization_compilation_reason = 15;
+    optional int32 package_optimization_compilation_reason = 15;
 }
 
 message AppStartCancelChanged {
diff --git a/core/java/android/content/pm/dex/PackageOptimizationInfo.java b/core/java/android/content/pm/dex/PackageOptimizationInfo.java
index b650457..7e7d29e 100644
--- a/core/java/android/content/pm/dex/PackageOptimizationInfo.java
+++ b/core/java/android/content/pm/dex/PackageOptimizationInfo.java
@@ -22,19 +22,19 @@
  * @hide
  */
 public class PackageOptimizationInfo {
-    private final String mCompilationFilter;
-    private final String mCompilationReason;
+    private final int mCompilationFilter;
+    private final int mCompilationReason;
 
-    public PackageOptimizationInfo(String compilerFilter, String compilationReason) {
+    public PackageOptimizationInfo(int compilerFilter, int compilationReason) {
         this.mCompilationReason = compilationReason;
         this.mCompilationFilter = compilerFilter;
     }
 
-    public String getCompilationReason() {
+    public int getCompilationReason() {
         return mCompilationReason;
     }
 
-    public String getCompilationFilter() {
+    public int getCompilationFilter() {
         return mCompilationFilter;
     }
 
@@ -42,6 +42,6 @@
      * Create a default optimization info object for the case when we have no information.
      */
     public static PackageOptimizationInfo createWithNoInfo() {
-        return new PackageOptimizationInfo("no-info", "no-info");
+        return new PackageOptimizationInfo(-1, -1);
     }
 }
diff --git a/services/core/java/com/android/server/pm/PackageManagerServiceCompilerMapping.java b/services/core/java/com/android/server/pm/PackageManagerServiceCompilerMapping.java
index fce8285..5fc5bac 100644
--- a/services/core/java/com/android/server/pm/PackageManagerServiceCompilerMapping.java
+++ b/services/core/java/com/android/server/pm/PackageManagerServiceCompilerMapping.java
@@ -25,7 +25,7 @@
  */
 public class PackageManagerServiceCompilerMapping {
     // Names for compilation reasons.
-    static final String REASON_STRINGS[] = {
+    public static final String REASON_STRINGS[] = {
             "first-boot", "boot", "install", "bg-dexopt", "ab-ota", "inactive", "shared"
     };
 
diff --git a/services/core/java/com/android/server/pm/dex/ArtManagerService.java b/services/core/java/com/android/server/pm/dex/ArtManagerService.java
index 41ed6f2..cdafe57 100644
--- a/services/core/java/com/android/server/pm/dex/ArtManagerService.java
+++ b/services/core/java/com/android/server/pm/dex/ArtManagerService.java
@@ -27,19 +27,18 @@
 import android.content.pm.dex.ArtManager.ProfileType;
 import android.content.pm.dex.ArtManagerInternal;
 import android.content.pm.dex.DexMetadataHelper;
+import android.content.pm.dex.ISnapshotRuntimeProfileCallback;
 import android.content.pm.dex.PackageOptimizationInfo;
 import android.os.Binder;
 import android.os.Build;
 import android.os.Handler;
 import android.os.ParcelFileDescriptor;
 import android.os.RemoteException;
-import android.content.pm.dex.ISnapshotRuntimeProfileCallback;
 import android.os.SystemProperties;
 import android.os.UserHandle;
 import android.system.Os;
 import android.util.ArrayMap;
 import android.util.Slog;
-
 import com.android.internal.annotations.GuardedBy;
 import com.android.internal.os.BackgroundThread;
 import com.android.internal.util.ArrayUtils;
@@ -47,9 +46,8 @@
 import com.android.server.LocalServices;
 import com.android.server.pm.Installer;
 import com.android.server.pm.Installer.InstallerException;
-
+import com.android.server.pm.PackageManagerServiceCompilerMapping;
 import dalvik.system.DexFile;
-
 import dalvik.system.VMRuntime;
 import java.io.File;
 import java.io.FileNotFoundException;
@@ -88,6 +86,10 @@
 
     private final Handler mHandler;
 
+    static {
+        verifyTronLoggingConstants();
+    }
+
     public ArtManagerService(IPackageManager pm, Installer installer, Object installLock) {
         mPackageManager = pm;
         mInstaller = installer;
@@ -423,6 +425,100 @@
         return result;
     }
 
+    // Constants used for logging compilation filter to TRON.
+    // DO NOT CHANGE existing values.
+    //
+    // NOTE: '-1' value is reserved for the case where we cannot produce a valid
+    // PackageOptimizationInfo because the ArtManagerInternal is not ready to be used by the
+    // ActivityMetricsLoggers.
+    private static final int TRON_COMPILATION_FILTER_ERROR = 0;
+    private static final int TRON_COMPILATION_FILTER_UNKNOWN = 1;
+    private static final int TRON_COMPILATION_FILTER_ASSUMED_VERIFIED = 2;
+    private static final int TRON_COMPILATION_FILTER_EXTRACT = 3;
+    private static final int TRON_COMPILATION_FILTER_VERIFY = 4;
+    private static final int TRON_COMPILATION_FILTER_QUICKEN = 5;
+    private static final int TRON_COMPILATION_FILTER_SPACE_PROFILE = 6;
+    private static final int TRON_COMPILATION_FILTER_SPACE = 7;
+    private static final int TRON_COMPILATION_FILTER_SPEED_PROFILE = 8;
+    private static final int TRON_COMPILATION_FILTER_SPEED = 9;
+    private static final int TRON_COMPILATION_FILTER_EVERYTHING_PROFILE = 10;
+    private static final int TRON_COMPILATION_FILTER_EVERYTHING = 11;
+    private static final int TRON_COMPILATION_FILTER_FAKE_RUN_FROM_APK = 12;
+    private static final int TRON_COMPILATION_FILTER_FAKE_RUN_FROM_APK_FALLBACK = 13;
+    private static final int TRON_COMPILATION_FILTER_FAKE_RUN_FROM_VDEX_FALLBACK = 14;
+
+    // Constants used for logging compilation reason to TRON.
+    // DO NOT CHANGE existing values.
+    //
+    // NOTE: '-1' value is reserved for the case where we cannot produce a valid
+    // PackageOptimizationInfo because the ArtManagerInternal is not ready to be used by the
+    // ActivityMetricsLoggers.
+    private static final int TRON_COMPILATION_REASON_ERROR = 0;
+    private static final int TRON_COMPILATION_REASON_UNKNOWN = 1;
+    private static final int TRON_COMPILATION_REASON_FIRST_BOOT = 2;
+    private static final int TRON_COMPILATION_REASON_BOOT = 3;
+    private static final int TRON_COMPILATION_REASON_INSTALL = 4;
+    private static final int TRON_COMPILATION_REASON_BG_DEXOPT = 5;
+    private static final int TRON_COMPILATION_REASON_AB_OTA = 6;
+    private static final int TRON_COMPILATION_REASON_INACTIVE = 7;
+    private static final int TRON_COMPILATION_REASON_SHARED = 8;
+
+    /**
+     * Convert the compilation reason to an int suitable to be logged to TRON.
+     */
+    private static int getCompilationReasonTronValue(String compilationReason) {
+        switch (compilationReason) {
+            case "unknown" : return TRON_COMPILATION_REASON_UNKNOWN;
+            case "error" : return TRON_COMPILATION_REASON_ERROR;
+            case "first-boot" : return TRON_COMPILATION_REASON_FIRST_BOOT;
+            case "boot" : return TRON_COMPILATION_REASON_BOOT;
+            case "install" : return TRON_COMPILATION_REASON_INSTALL;
+            case "bg-dexopt" : return TRON_COMPILATION_REASON_BG_DEXOPT;
+            case "ab-ota" : return TRON_COMPILATION_REASON_AB_OTA;
+            case "inactive" : return TRON_COMPILATION_REASON_INACTIVE;
+            case "shared" : return TRON_COMPILATION_REASON_SHARED;
+            default: return TRON_COMPILATION_REASON_UNKNOWN;
+        }
+    }
+
+    /**
+     * Convert the compilation filter to an int suitable to be logged to TRON.
+     */
+    private static int getCompilationFilterTronValue(String compilationFilter) {
+        switch (compilationFilter) {
+            case "error" : return TRON_COMPILATION_FILTER_ERROR;
+            case "unknown" : return TRON_COMPILATION_FILTER_UNKNOWN;
+            case "assume-verified" : return TRON_COMPILATION_FILTER_ASSUMED_VERIFIED;
+            case "extract" : return TRON_COMPILATION_FILTER_EXTRACT;
+            case "verify" : return TRON_COMPILATION_FILTER_VERIFY;
+            case "quicken" : return TRON_COMPILATION_FILTER_QUICKEN;
+            case "space-profile" : return TRON_COMPILATION_FILTER_SPACE_PROFILE;
+            case "space" : return TRON_COMPILATION_FILTER_SPACE;
+            case "speed-profile" : return TRON_COMPILATION_FILTER_SPEED_PROFILE;
+            case "speed" : return TRON_COMPILATION_FILTER_SPEED;
+            case "everything-profile" : return TRON_COMPILATION_FILTER_EVERYTHING_PROFILE;
+            case "everything" : return TRON_COMPILATION_FILTER_EVERYTHING;
+            case "run-from-apk" : return TRON_COMPILATION_FILTER_FAKE_RUN_FROM_APK;
+            case "run-from-apk-fallback" :
+                return TRON_COMPILATION_FILTER_FAKE_RUN_FROM_APK_FALLBACK;
+            case "run-from-vdex-fallback" :
+                return TRON_COMPILATION_FILTER_FAKE_RUN_FROM_VDEX_FALLBACK;
+            default: return TRON_COMPILATION_FILTER_UNKNOWN;
+        }
+    }
+
+    private static void verifyTronLoggingConstants() {
+        for (int i = 0; i < PackageManagerServiceCompilerMapping.REASON_STRINGS.length; i++) {
+            String reason = PackageManagerServiceCompilerMapping.REASON_STRINGS[i];
+            int value = getCompilationReasonTronValue(reason);
+            if (value == TRON_COMPILATION_REASON_ERROR
+                    || value == TRON_COMPILATION_REASON_UNKNOWN) {
+                throw new IllegalArgumentException("Compilation reason not configured for TRON "
+                        + "logging: " + reason);
+            }
+        }
+    }
+
     private class ArtManagerInternalImpl extends ArtManagerInternal {
         @Override
         public PackageOptimizationInfo getPackageOptimizationInfo(
@@ -445,7 +541,11 @@
                 compilationReason = "error";
             }
 
-            return new PackageOptimizationInfo(compilationFilter, compilationReason);
+            int compilationFilterTronValue = getCompilationFilterTronValue(compilationFilter);
+            int compilationReasonTronValue = getCompilationReasonTronValue(compilationReason);
+
+            return new PackageOptimizationInfo(
+                    compilationFilterTronValue, compilationReasonTronValue);
         }
     }
 }