Allow multiple simultaneous connections for BT input devices.

Move responsibility for disconnecting devices before connecting a new
profile from CachedBluetoothDevice to LocalBluetoothProfileManager.

The ProfileManager subclasses of LocalBluetoothProfileManager will
handle disconnecting the previously connected device, if necessary,
as part of connect(). The HID profile allows multiple simultaneous
connected devices, while the other supported profiles do not.

Bug: 3333975
Change-Id: Id51b26e64f7c3ee7d54af3a03ca82a669f305b52
diff --git a/src/com/android/settings/bluetooth/CachedBluetoothDevice.java b/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
index 46391d3..88964e0 100644
--- a/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
+++ b/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
@@ -279,7 +279,7 @@
     }
 
     private void connectWithoutResettingTimer(boolean connectAllProfiles) {
-        // Try to initialize the profiles if there were not.
+        // Try to initialize the profiles if they were not.
         if (mProfiles.size() == 0) {
             if (!updateProfiles()) {
                 // If UUIDs are not available yet, connect will be happen
@@ -299,7 +299,6 @@
                         .getProfileManager(mLocalManager, profile);
                 if (profileManager.isPreferred(mDevice)) {
                     ++preferredProfiles;
-                    disconnectConnected(this, profile);
                     connectInt(this, profile);
                 }
             }
@@ -322,7 +321,6 @@
                 LocalBluetoothProfileManager profileManager = LocalBluetoothProfileManager
                         .getProfileManager(mLocalManager, profile);
                 profileManager.setPreferred(mDevice, true);
-                disconnectConnected(this, profile);
                 connectInt(this, profile);
             }
         }
@@ -332,25 +330,9 @@
         mConnectAttempted = SystemClock.elapsedRealtime();
         // Reset the only-show-one-error-dialog tracking variable
         mIsConnectingErrorPossible = true;
-        disconnectConnected(this, profile);
         connectInt(this, profile);
     }
 
-    private void disconnectConnected(CachedBluetoothDevice device, Profile profile) {
-        LocalBluetoothProfileManager profileManager =
-            LocalBluetoothProfileManager.getProfileManager(mLocalManager, profile);
-        CachedBluetoothDeviceManager cachedDeviceManager = mLocalManager.getCachedDeviceManager();
-        List<BluetoothDevice> devices = profileManager.getConnectedDevices();
-        if (devices == null) return;
-        for (BluetoothDevice btDevice : devices) {
-            CachedBluetoothDevice cachedDevice = cachedDeviceManager.findDevice(btDevice);
-
-            if (cachedDevice != null && !cachedDevice.equals(device)) {
-                cachedDevice.disconnect(profile);
-            }
-        }
-    }
-
     private boolean connectInt(CachedBluetoothDevice cachedDevice, Profile profile) {
         if (!cachedDevice.ensurePaired()) return false;
 
diff --git a/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java b/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java
index 5c8d868..a2ac743 100644
--- a/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java
+++ b/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java
@@ -471,6 +471,12 @@
 
         @Override
         public boolean connect(BluetoothDevice device) {
+            List<BluetoothDevice> sinks = getConnectedDevices();
+            if (sinks != null) {
+                for (BluetoothDevice sink : sinks) {
+                    mService.disconnect(sink);
+                }
+            }
             return mService.connect(device);
         }
 
@@ -727,6 +733,12 @@
 
         @Override
         public boolean connect(BluetoothDevice device) {
+            List<BluetoothDevice> sinks = getConnectedDevices();
+            if (sinks != null) {
+                for (BluetoothDevice sink : sinks) {
+                    mService.disconnect(sink);
+                }
+            }
             return mService.connect(device);
         }