Merge cherrypicks of ['googleplex-android-review.googlesource.com/24027039'] into udc-release.

Change-Id: Iec7aeabc1f7b9e20f6dfbb19974c6298b5e904b9
diff --git a/src/com/android/server/telecom/CallAudioRouteStateMachine.java b/src/com/android/server/telecom/CallAudioRouteStateMachine.java
index c5423c2..4a03726 100644
--- a/src/com/android/server/telecom/CallAudioRouteStateMachine.java
+++ b/src/com/android/server/telecom/CallAudioRouteStateMachine.java
@@ -1741,28 +1741,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);
         });
     }