Do not crash AudioRecorderServices in onStartCommand()

Instead of crashing in BaseAudioRecorderService.onStartCommand() when
receiving ACTION_THROW, post a Runnable so that the crash happens out of
onStartCommand(). Otherwise, system will try to restart a service.

Bug: 154292570
Test: atest TvMicrophoneCaptureIndicatorTest
Change-Id: I0763a3a4b76759aa921c17e87035067e0cb5b6c5
Exempt-From-Owner-Approval: only affecting TV
diff --git a/hostsidetests/systemui/audiorecorder_base/src/android/systemui/cts/audiorecorder/base/BaseAudioRecorderService.java b/hostsidetests/systemui/audiorecorder_base/src/android/systemui/cts/audiorecorder/base/BaseAudioRecorderService.java
index dd7cc17..9a839fb 100644
--- a/hostsidetests/systemui/audiorecorder_base/src/android/systemui/cts/audiorecorder/base/BaseAudioRecorderService.java
+++ b/hostsidetests/systemui/audiorecorder_base/src/android/systemui/cts/audiorecorder/base/BaseAudioRecorderService.java
@@ -23,11 +23,9 @@
 import android.app.Service;
 import android.content.Context;
 import android.content.Intent;
-import android.media.AudioRecord;
-import android.media.MediaRecorder;
-import android.media.MediaRecorder.AudioSource;
+import android.os.Handler;
 import android.os.IBinder;
-import android.util.Log;
+import android.os.Looper;
 
 public abstract class BaseAudioRecorderService extends Service {
     private static final String ACTION_START =
@@ -43,6 +41,17 @@
     private static final String NOTIFICATION_TITLE = "Audio Record Service";
     private static final String NOTIFICATION_TEXT = "Recording...";
 
+    private Handler mHandler;
+    private final Runnable mCrashRunnable = () -> {
+        throw new RuntimeException("Commanded to throw!");
+    };
+
+    @Override
+    public void onCreate() {
+        super.onCreate();
+        mHandler = new Handler(Looper.getMainLooper());
+    }
+
     @Override
     public int onStartCommand(Intent intent, int flags, int startId) {
         startForeground(NOTIFICATION_ID, buildNotification());
@@ -53,7 +62,9 @@
         } else if (ACTION_STOP.equals(action) && isRecording()) {
             stopRecording();
         } else if (ACTION_THROW.equals(action)) {
-            throw new RuntimeException("Commanded to throw!");
+            // If the service crashes in onStartCommand() the system will try to re-start it. We do
+            // not want that. So we post "crash" commands.
+            mHandler.post(mCrashRunnable);
         }
 
         if (!isRecording()) {