Move processtrigger call off of calling thread

Triggers are sent at performance sensitive times, make sure the work occurs off the calling thread.

Test: confirm that cloning no longer blocks the thread that passed the trigger in, run profiling mcts
Bug: 373461116
Flag: android.os.profiling.system_triggered_profiling_new
Change-Id: Id67caee1bf80b5aa25f3b2bcec3d2a585777b230
diff --git a/service/java/com/android/os/profiling/ProfilingService.java b/service/java/com/android/os/profiling/ProfilingService.java
index 5b50ef8..e760e6a 100644
--- a/service/java/com/android/os/profiling/ProfilingService.java
+++ b/service/java/com/android/os/profiling/ProfilingService.java
@@ -1446,13 +1446,26 @@
      * Cloning will fork the running trace, stop the new forked trace, and output the result to a
      * separate file. This leaves the original trace running.
      */
-    @VisibleForTesting
     public void processTrigger(int uid, @NonNull String packageName, int triggerType) {
         if (!Flags.systemTriggeredProfilingNew()) {
             // Flag disabled.
             return;
         }
 
+        // Don't block the calling thread.
+        getHandler().post(new Runnable() {
+            @Override
+            public void run() {
+                processTriggerInternal(uid, packageName, triggerType);
+            }
+        });
+    }
+
+    /**
+     * Internal call to process trigger, not to be called on the thread that passed the trigger in.
+     */
+    @VisibleForTesting
+    public void processTriggerInternal(int uid, @NonNull String packageName, int triggerType) {
         if (mSystemTriggeredTraceUniqueSessionName == null) {
             // If we don't have the session name then we don't know how to clone the trace so stop
             // it if it's still running and then return.
diff --git a/tests/cts/src/android/profiling/cts/ProfilingServiceTests.java b/tests/cts/src/android/profiling/cts/ProfilingServiceTests.java
index ecd91b5..77bb9f3 100644
--- a/tests/cts/src/android/profiling/cts/ProfilingServiceTests.java
+++ b/tests/cts/src/android/profiling/cts/ProfilingServiceTests.java
@@ -1756,7 +1756,7 @@
                 .setLastTriggeredTimeMs(fakeLastTriggerTimeMs);
 
         // Now process the trigger.
-        mProfilingService.processTrigger(FAKE_UID, APP_PACKAGE_NAME, fakeTrigger);
+        mProfilingService.processTriggerInternal(FAKE_UID, APP_PACKAGE_NAME, fakeTrigger);
 
         // Get the new trigger time and make sure it's later than the fake one, indicating it ran.
         long newTriggerTime = mProfilingService.mAppTriggers.get(APP_PACKAGE_NAME, FAKE_UID)
@@ -1798,7 +1798,7 @@
                 .setLastTriggeredTimeMs(fakeLastTriggerTimeMs);
 
         // Now process the trigger.
-        mProfilingService.processTrigger(FAKE_UID, APP_PACKAGE_NAME, fakeTrigger);
+        mProfilingService.processTriggerInternal(FAKE_UID, APP_PACKAGE_NAME, fakeTrigger);
 
         // Get the new trigger time and make sure it's equal to the fake one, indicating it did not
         // run.
@@ -1832,7 +1832,7 @@
                 rateLimitingPeriodHours);
 
         // Now process the trigger.
-        mProfilingService.processTrigger(FAKE_UID, APP_PACKAGE_NAME, fakeTrigger);
+        mProfilingService.processTriggerInternal(FAKE_UID, APP_PACKAGE_NAME, fakeTrigger);
 
         // Get the new trigger time and make sure it's later than 0, indicating it ran.
         long newTriggerTime = mProfilingService.mAppTriggers.get(APP_PACKAGE_NAME, FAKE_UID)
@@ -1871,7 +1871,7 @@
                 rateLimitingPeriodHours);
 
         // Now process the trigger.
-        mProfilingService.processTrigger(FAKE_UID, APP_PACKAGE_NAME, fakeTrigger);
+        mProfilingService.processTriggerInternal(FAKE_UID, APP_PACKAGE_NAME, fakeTrigger);
 
         // Get the new trigger time and make sure it's equal to 0, indicating it did not run.
         long newTriggerTime = mProfilingService.mAppTriggers.get(APP_PACKAGE_NAME, FAKE_UID)