Merge "Revert "GAP: Connection state change check for multiple devices"" into q-keystone-qcom-dev
diff --git a/src/com/android/bluetooth/btservice/AdapterProperties.java b/src/com/android/bluetooth/btservice/AdapterProperties.java
index c7eb53c..beff7c8 100644
--- a/src/com/android/bluetooth/btservice/AdapterProperties.java
+++ b/src/com/android/bluetooth/btservice/AdapterProperties.java
@@ -86,18 +86,7 @@
     private CopyOnWriteArrayList<BluetoothDevice> mBondedDevices =
             new CopyOnWriteArrayList<BluetoothDevice>();
 
-    private final class ProfilesConnectionState {
-        public int mProfilesConnecting, mProfilesConnected, mProfilesDisconnecting;
-
-        ProfilesConnectionState(int connecting, int connected, int disconnecting) {
-          mProfilesConnecting    = connecting;
-          mProfilesConnected     = connected;
-          mProfilesDisconnecting = disconnecting;
-        }
-    }
-    private final HashMap<BluetoothDevice, ProfilesConnectionState> mDevicesConnectionState =
-            new HashMap<>();
-
+    private int mProfilesConnecting, mProfilesConnected, mProfilesDisconnecting;
     private final HashMap<Integer, Pair<Integer, Integer>> mProfileConnectionState =
             new HashMap<>();
 
@@ -229,7 +218,6 @@
 
     public void init(RemoteDevices remoteDevices) {
         mProfileConnectionState.clear();
-        mDevicesConnectionState.clear();
         mRemoteDevices = remoteDevices;
 
         // Get default max connected audio devices from config.xml in frameworks/base/core
@@ -286,7 +274,6 @@
         }
         mService = null;
         mBondedDevices.clear();
-        mDevicesConnectionState.clear();
     }
 
     @Override
@@ -800,10 +787,6 @@
                     debugLog("Adding bonded device:" + device);
                     mBondedDevices.add(device);
                 }
-                if (!mDevicesConnectionState.containsKey(device)) {
-                    debugLog("Adding connection state:" + device);
-                    mDevicesConnectionState.put(device, new ProfilesConnectionState(0, 0, 0));
-                }
             } else if (state == BluetoothDevice.BOND_NONE) {
                 // remove device from list
                 if (mBondedDevices.remove(device)) {
@@ -811,10 +794,6 @@
                 } else {
                     debugLog("Failed to remove device: " + device);
                 }
-                if (mDevicesConnectionState.containsKey(device)) {
-                    debugLog("Removing connection state:" + device);
-                    mDevicesConnectionState.remove(device);
-                }
             }
         } catch (Exception ee) {
             Log.w(TAG, "onBondStateChanged: Exception ", ee);
@@ -889,7 +868,7 @@
 
             try {
                 validateConnectionState =
-                   updateCountersAndCheckForConnectionStateChange(device, state, prevState);
+                   updateCountersAndCheckForConnectionStateChange(state, prevState);
             } catch (IllegalStateException ee) {
                 Log.w(TAG, "ADAPTER_CONNECTION_STATE_CHANGE: unexpected transition for profile="
                         + profile + ", " + prevState + " -> " + state);
@@ -953,45 +932,33 @@
         }
     }
 
-    private boolean updateCountersAndCheckForConnectionStateChange(BluetoothDevice device,
-            int state, int prevState) {
-        if(!mDevicesConnectionState.containsKey(device)) {
-            Log.e(TAG, "Can't find device connection record, adding new one: " + device);
-            mDevicesConnectionState.put(device, new ProfilesConnectionState(0, 0, 0));
-        }
-        ProfilesConnectionState connstate = mDevicesConnectionState.get(device);
-
-        Log.e(TAG, "prevState=" + prevState + " -> State=" + state +
-            " mProfilesConnecting=" + connstate.mProfilesConnecting +
-            " mProfilesConnected=" + connstate.mProfilesConnected +
-            " mProfilesDisconnecting=" + connstate.mProfilesDisconnecting);
-
+    private boolean updateCountersAndCheckForConnectionStateChange(int state, int prevState) {
         switch (prevState) {
             case BluetoothProfile.STATE_CONNECTING:
-                if (connstate.mProfilesConnecting > 0) {
-                    connstate.mProfilesConnecting--;
+                if (mProfilesConnecting > 0) {
+                    mProfilesConnecting--;
                 } else {
-                    Log.e(TAG, "mProfilesConnecting " + connstate.mProfilesConnecting);
+                    Log.e(TAG, "mProfilesConnecting " + mProfilesConnecting);
                     throw new IllegalStateException(
                             "Invalid state transition, " + prevState + " -> " + state);
                 }
                 break;
 
             case BluetoothProfile.STATE_CONNECTED:
-                if (connstate.mProfilesConnected > 0) {
-                    connstate.mProfilesConnected--;
+                if (mProfilesConnected > 0) {
+                    mProfilesConnected--;
                 } else {
-                    Log.e(TAG, "mProfilesConnected " + connstate.mProfilesConnected);
+                    Log.e(TAG, "mProfilesConnected " + mProfilesConnected);
                     throw new IllegalStateException(
                             "Invalid state transition, " + prevState + " -> " + state);
                 }
                 break;
 
             case BluetoothProfile.STATE_DISCONNECTING:
-                if (connstate.mProfilesDisconnecting > 0) {
-                    connstate.mProfilesDisconnecting--;
+                if (mProfilesDisconnecting > 0) {
+                    mProfilesDisconnecting--;
                 } else {
-                    Log.e(TAG, "mProfilesDisconnecting " + connstate.mProfilesDisconnecting);
+                    Log.e(TAG, "mProfilesDisconnecting " + mProfilesDisconnecting);
                     throw new IllegalStateException(
                             "Invalid state transition, " + prevState + " -> " + state);
                 }
@@ -1000,19 +967,19 @@
 
         switch (state) {
             case BluetoothProfile.STATE_CONNECTING:
-                connstate.mProfilesConnecting++;
-                return (connstate.mProfilesConnected == 0 && connstate.mProfilesConnecting == 1);
+                mProfilesConnecting++;
+                return (mProfilesConnected == 0 && mProfilesConnecting == 1);
 
             case BluetoothProfile.STATE_CONNECTED:
-                connstate.mProfilesConnected++;
-                return (connstate.mProfilesConnected == 1);
+                mProfilesConnected++;
+                return (mProfilesConnected == 1);
 
             case BluetoothProfile.STATE_DISCONNECTING:
-                connstate.mProfilesDisconnecting++;
-                return (connstate.mProfilesConnected == 0 && connstate.mProfilesDisconnecting == 1);
+                mProfilesDisconnecting++;
+                return (mProfilesConnected == 0 && mProfilesDisconnecting == 1);
 
             case BluetoothProfile.STATE_DISCONNECTED:
-                return (connstate.mProfilesConnected == 0 && connstate.mProfilesConnecting == 0);
+                return (mProfilesConnected == 0 && mProfilesConnecting == 0);
 
             default:
                 return true;
@@ -1273,8 +1240,9 @@
             // Reset adapter and profile connection states
             setConnectionState(BluetoothAdapter.STATE_DISCONNECTED);
             mProfileConnectionState.clear();
-            mDevicesConnectionState.clear();
-
+            mProfilesConnected = 0;
+            mProfilesConnecting = 0;
+            mProfilesDisconnecting = 0;
             // adapterPropertyChangedCallback has already been received.  Set the scan mode.
             setScanMode(AbstractionLayer.BT_SCAN_MODE_CONNECTABLE);
             // This keeps NV up-to date on first-boot after flash.