Move set speakerphone out of async task executor.

This turned out to cause a regression related to switching to hearing
aids.  There are other set/clearcommunicationDevice operations elsewhere
in Telecom which are not part of the async task executor so we were seeing
race conditions where the clear hear doesn't happen before the set
in the Telecom BluetoothDeviceManager code.

For now we'll move this back out of the task executor since that was
only done to try and satisfy issues with Cuttlefish tests.

Test: Run CTS/Unit test suite.
Bug: 283198536
Change-Id: I9cc11eb11b59eb04b706a4bd6f61c1b7240a50c2
Merged-In: I9cc11eb11b59eb04b706a4bd6f61c1b7240a50c2
diff --git a/src/com/android/server/telecom/CallAudioRouteStateMachine.java b/src/com/android/server/telecom/CallAudioRouteStateMachine.java
index 531f42e..55a5df8 100644
--- a/src/com/android/server/telecom/CallAudioRouteStateMachine.java
+++ b/src/com/android/server/telecom/CallAudioRouteStateMachine.java
@@ -1734,28 +1734,30 @@
         final boolean hasAnyCalls = mCallsManager.hasAnyCalls();
         // These APIs are all via two-way binder calls so can potentially block Telecom.  Since none
         // of this has to happen in the Telecom lock we'll offload it to the async executor.
+
+        AudioDeviceInfo speakerDevice = null;
+        for (AudioDeviceInfo info : mAudioManager.getAvailableCommunicationDevices()) {
+            if (info.getType() == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) {
+                speakerDevice = info;
+                break;
+            }
+        }
+        boolean speakerOn = false;
+        if (speakerDevice != null && on) {
+            boolean result = mAudioManager.setCommunicationDevice(speakerDevice);
+            if (result) {
+                speakerOn = true;
+            }
+        } else {
+            AudioDeviceInfo curDevice = mAudioManager.getCommunicationDevice();
+            if (curDevice != null
+                    && curDevice.getType() == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) {
+                mAudioManager.clearCommunicationDevice();
+            }
+        }
+        final boolean isSpeakerOn = speakerOn;
         mAsyncTaskExecutor.execute(() -> {
-            AudioDeviceInfo speakerDevice = null;
-            for (AudioDeviceInfo info : mAudioManager.getAvailableCommunicationDevices()) {
-                if (info.getType() == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) {
-                    speakerDevice = info;
-                    break;
-                }
-            }
-            boolean speakerOn = false;
-            if (speakerDevice != null && on) {
-                boolean result = mAudioManager.setCommunicationDevice(speakerDevice);
-                if (result) {
-                    speakerOn = true;
-                }
-            } else {
-                AudioDeviceInfo curDevice = mAudioManager.getCommunicationDevice();
-                if (curDevice != null
-                        && curDevice.getType() == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) {
-                    mAudioManager.clearCommunicationDevice();
-                }
-            }
-            mStatusBarNotifier.notifySpeakerphone(hasAnyCalls && speakerOn);
+            mStatusBarNotifier.notifySpeakerphone(hasAnyCalls && isSpeakerOn);
         });
     }