Merge "Fix call audio switch issue for BT LE to speaker and back to BT switch" into tm-qpr-dev
diff --git a/src/com/android/server/telecom/bluetooth/BluetoothDeviceManager.java b/src/com/android/server/telecom/bluetooth/BluetoothDeviceManager.java
index 387c39c..759c356 100644
--- a/src/com/android/server/telecom/bluetooth/BluetoothDeviceManager.java
+++ b/src/com/android/server/telecom/bluetooth/BluetoothDeviceManager.java
@@ -187,6 +187,7 @@
     private boolean mLeAudioCallbackRegistered = false;
     private BluetoothLeAudio mBluetoothLeAudioService;
     private boolean mLeAudioSetAsCommunicationDevice = false;
+    private String mLeAudioDevice;
     private boolean mHearingAidSetAsCommunicationDevice = false;
     private BluetoothDevice mBluetoothHearingAidActiveDeviceCache;
     private BluetoothAdapter mBluetoothAdapter;
@@ -418,23 +419,26 @@
     }
 
     public void clearLeAudioCommunicationDevice() {
+        Log.i(this, "clearLeAudioCommunicationDevice: mLeAudioSetAsCommunicationDevice = " +
+                mLeAudioSetAsCommunicationDevice + " device = " + mLeAudioDevice);
         if (!mLeAudioSetAsCommunicationDevice) {
             return;
         }
         mLeAudioSetAsCommunicationDevice = false;
+        if (mLeAudioDevice != null) {
+            mBluetoothRouteManager.onAudioLost(mLeAudioDevice);
+            mLeAudioDevice = null;
+        }
+
         if (mAudioManager == null) {
             Log.i(this, "clearLeAudioCommunicationDevice: mAudioManager is null");
             return;
         }
-
-        AudioDeviceInfo audioDeviceInfo = mAudioManager.getCommunicationDevice();
-        if (audioDeviceInfo != null && audioDeviceInfo.getType()
+        if (mAudioManager.getCommunicationDevice() != null
+                && mAudioManager.getCommunicationDevice().getType()
                 == AudioDeviceInfo.TYPE_BLE_HEADSET) {
-            mBluetoothRouteManager.onAudioLost(audioDeviceInfo.getAddress());
             mAudioManager.clearCommunicationDevice();
-            mLeAudioSetAsCommunicationDevice = false;
         }
-        mLeAudioSetAsCommunicationDevice = false;
     }
 
     public void clearHearingAidCommunicationDevice() {
@@ -500,6 +504,7 @@
             Log.i(this, " bleHeadset device set");
             mBluetoothRouteManager.onAudioOn(bleHeadset.getAddress());
             mLeAudioSetAsCommunicationDevice = true;
+            mLeAudioDevice = bleHeadset.getAddress();
         }
         return result;
     }