TWS_A2DP: Avoid active device switch during unpairing the earbuds

Do not switch active device between earbuds during unbond.

CRs-fixed: 2615029
Change-Id: I4b80347f6e264406bf72dd67e8bdeef2d62f0ea7
diff --git a/src/com/android/bluetooth/btservice/ActiveDeviceManager.java b/src/com/android/bluetooth/btservice/ActiveDeviceManager.java
index 1a356a8..da9cb11 100644
--- a/src/com/android/bluetooth/btservice/ActiveDeviceManager.java
+++ b/src/com/android/bluetooth/btservice/ActiveDeviceManager.java
@@ -124,7 +124,7 @@
     private BluetoothDevice mA2dpActiveDevice = null;
     private BluetoothDevice mHfpActiveDevice = null;
     private BluetoothDevice mHearingAidActiveDevice = null;
-
+    private boolean mTwsPlusSwitch = false;
     // Broadcast receiver for all changes
     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
         @Override
@@ -231,7 +231,7 @@
                         if (Objects.equals(mA2dpActiveDevice, device)) {
                             final A2dpService mA2dpService = mFactory.getA2dpService();
                             BluetoothDevice mDevice = null;
-                            if (mAdapterService.isTwsPlusDevice(device) &&
+                            if (mAdapterService.isTwsPlusDevice(device) && !mTwsPlusSwitch &&
                                 !mA2dpConnectedDevices.isEmpty()) {
                                 for (BluetoothDevice connected_device: mA2dpConnectedDevices) {
                                     if (mAdapterService.isTwsPlusDevice(connected_device) &&
@@ -241,6 +241,9 @@
                                         break;
                                     }
                                 }
+                            } else if (device.isTwsPlusDevice() && mTwsPlusSwitch) {
+                                Log.d(TAG, "Resetting mTwsPlusSwitch");
+                                mTwsPlusSwitch = false;
                             }
                             if (!setA2dpActiveDevice(mDevice) && (mDevice != null) &&
                                 mAdapterService.isTwsPlusDevice(mDevice)) {
@@ -454,7 +457,12 @@
         }
         resetState();
     }
-
+    public void notify_active_device_unbonding(BluetoothDevice device) {
+        if (device.isTwsPlusDevice() && Objects.equals(mA2dpActiveDevice, device)) {
+            Log.d(TAG,"TWS+ active device is getting unpaired, avoid switch to pair");
+            mTwsPlusSwitch = true;
+        }
+    }
     /**
      * Get the {@link Looper} for the handler thread. This is used in testing and helper
      * objects
diff --git a/src/com/android/bluetooth/btservice/AdapterService.java b/src/com/android/bluetooth/btservice/AdapterService.java
index 5bec2d0..abe9d66 100644
--- a/src/com/android/bluetooth/btservice/AdapterService.java
+++ b/src/com/android/bluetooth/btservice/AdapterService.java
@@ -2460,7 +2460,9 @@
             return false;
         }
         deviceProp.setBondingInitiatedLocally(false);
-
+        if (device.isTwsPlusDevice()) {
+            mActiveDeviceManager.notify_active_device_unbonding(device);
+        }
         Message msg = mBondStateMachine.obtainMessage(BondStateMachine.REMOVE_BOND);
         msg.obj = device;
         mBondStateMachine.sendMessage(msg);