Merge cherrypicks of ['googleplex-android-review.googlesource.com/32897336', 'googleplex-android-review.googlesource.com/32927430'] into 25Q2-release.

Change-Id: I26aa46234f8f00dc448cf152941cc33ce2ea7394
diff --git a/src/com/android/server/telecom/AudioRoute.java b/src/com/android/server/telecom/AudioRoute.java
index 2b03182..661f1db 100644
--- a/src/com/android/server/telecom/AudioRoute.java
+++ b/src/com/android/server/telecom/AudioRoute.java
@@ -443,22 +443,22 @@
         }
 
         int result = BluetoothStatusCodes.SUCCESS;
-        if (pendingAudioRoute.getCommunicationDeviceType() == TYPE_BLUETOOTH_SCO
-                && !mIsScoManagedByAudio) {
+        boolean shouldDisconnectSco = !mIsScoManagedByAudio
+                && pendingAudioRoute.getCommunicationDeviceType() == TYPE_BLUETOOTH_SCO;
+        if (shouldDisconnectSco) {
             Log.i(this, "Disconnecting SCO device via BluetoothHeadset.");
             result = bluetoothRouteManager.getDeviceManager().disconnectSco();
-        } else {
-            // Only clear communication device if the destination route will be inactive; route to
-            // route transitions do not require clearing the communication device.
-            boolean onlyClearCommunicationDeviceOnInactive =
-                    pendingAudioRoute.getFeatureFlags().onlyClearCommunicationDeviceOnInactive();
-            if (!onlyClearCommunicationDeviceOnInactive
-                    || (onlyClearCommunicationDeviceOnInactive && !pendingAudioRoute.isActive())) {
-                Log.i(this,
-                        "clearCommunicationDevice: AudioManager#clearCommunicationDevice, type=%s",
-                        DEVICE_TYPE_STRINGS.get(pendingAudioRoute.getCommunicationDeviceType()));
-                audioManager.clearCommunicationDevice();
-            }
+        }
+        // Only clear communication device if the destination route will be inactive; route to
+        // route transitions do not require clearing the communication device.
+        boolean onlyClearCommunicationDeviceOnInactive =
+                pendingAudioRoute.getFeatureFlags().onlyClearCommunicationDeviceOnInactive();
+        if ((!onlyClearCommunicationDeviceOnInactive && !shouldDisconnectSco)
+                || !pendingAudioRoute.isActive()) {
+            Log.i(this,
+                    "clearCommunicationDevice: AudioManager#clearCommunicationDevice, type=%s",
+                    DEVICE_TYPE_STRINGS.get(pendingAudioRoute.getCommunicationDeviceType()));
+            audioManager.clearCommunicationDevice();
         }
 
         if (result == BluetoothStatusCodes.SUCCESS) {
diff --git a/src/com/android/server/telecom/CallAudioRouteController.java b/src/com/android/server/telecom/CallAudioRouteController.java
index c9e0ab3..aa0976f 100644
--- a/src/com/android/server/telecom/CallAudioRouteController.java
+++ b/src/com/android/server/telecom/CallAudioRouteController.java
@@ -587,7 +587,8 @@
         // It's possible that there are multiple HFP devices connected and if we receive SCO audio
         // connected for the destination route's BT device, then we shouldn't disconnect SCO when
         // clearing the communication device for the original route if it was also a HFP device.
-        boolean isScoDeviceAlreadyConnected = mScoAudioConnectedDevice != null
+        // This does not apply to the route deactivation scenario.
+        boolean isScoDeviceAlreadyConnected = mScoAudioConnectedDevice != null && isDestRouteActive
                 && Objects.equals(mScoAudioConnectedDevice, mBluetoothRoutes.get(destRoute));
         if (mIsPending) {
             if (destRoute.equals(mPendingAudioRoute.getDestRoute())