Changing the order of notification of connection state change, as it was causing a race condition, with the UI not being updated with the correct connection state.

Change-Id: I6f4c75e4eac4ae798cb7918a4f1b7458e1eaa4b0
diff --git a/src/com/android/bluetooth/a2dp/A2dpStateMachine.java b/src/com/android/bluetooth/a2dp/A2dpStateMachine.java
index c839d29..2e8d902 100755
--- a/src/com/android/bluetooth/a2dp/A2dpStateMachine.java
+++ b/src/com/android/bluetooth/a2dp/A2dpStateMachine.java
@@ -613,13 +613,17 @@
 
     // This method does not check for error conditon (newState == prevState)
     private void broadcastConnectionState(BluetoothDevice device, int newState, int prevState) {
+        /* Notifying the connection state change of the profile before sending the intent for
+           connection state change, as it was causing a race condition, with the UI not being
+           updated with the correct connection state. */
+        if (DBG) log("Connection state " + device + ": " + prevState + "->" + newState);
+        mService.notifyProfileConnectionStateChanged(device, BluetoothProfile.A2DP,
+                                                     newState, prevState);
         Intent intent = new Intent(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
         intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, prevState);
         intent.putExtra(BluetoothProfile.EXTRA_STATE, newState);
         intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
         mContext.sendBroadcast(intent, ProfileService.BLUETOOTH_PERM);
-        if (DBG) log("Connection state " + device + ": " + prevState + "->" + newState);
-        mService.notifyProfileConnectionStateChanged(device, BluetoothProfile.A2DP, newState, prevState);
     }
 
     private void broadcastAudioState(BluetoothDevice device, int state, int prevState) {
diff --git a/src/com/android/bluetooth/hfp/HeadsetStateMachine.java b/src/com/android/bluetooth/hfp/HeadsetStateMachine.java
index e6c1107..7e1f834 100755
--- a/src/com/android/bluetooth/hfp/HeadsetStateMachine.java
+++ b/src/com/android/bluetooth/hfp/HeadsetStateMachine.java
@@ -1177,13 +1177,17 @@
 
     // This method does not check for error conditon (newState == prevState)
     private void broadcastConnectionState(BluetoothDevice device, int newState, int prevState) {
+        if (DBG) log("Connection state " + device + ": " + prevState + "->" + newState);
+        /* Notifying the connection state change of the profile before sending the intent for
+           connection state change, as it was causing a race condition, with the UI not being
+           updated with the correct connection state. */
+        mService.notifyProfileConnectionStateChanged(device, BluetoothProfile.HEADSET,
+                                                     newState, prevState);
         Intent intent = new Intent(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
         intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, prevState);
         intent.putExtra(BluetoothProfile.EXTRA_STATE, newState);
         intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
         mService.sendBroadcast(intent, HeadsetService.BLUETOOTH_PERM);
-        if (DBG) log("Connection state " + device + ": " + prevState + "->" + newState);
-        mService.notifyProfileConnectionStateChanged(device, BluetoothProfile.HEADSET, newState, prevState);
     }
 
     private void broadcastAudioState(BluetoothDevice device, int newState, int prevState) {
diff --git a/src/com/android/bluetooth/hid/HidService.java b/src/com/android/bluetooth/hid/HidService.java
index c522c66..e0d7be7 100755
--- a/src/com/android/bluetooth/hid/HidService.java
+++ b/src/com/android/bluetooth/hid/HidService.java
@@ -492,14 +492,18 @@
         }
         mInputDevices.put(device, newState);
 
+        /* Notifying the connection state change of the profile before sending the intent for
+           connection state change, as it was causing a race condition, with the UI not being
+           updated with the correct connection state. */
+        if (DBG) log("Connection state " + device + ": " + prevState + "->" + newState);
+        notifyProfileConnectionStateChanged(device, BluetoothProfile.INPUT_DEVICE,
+                                            newState, prevState);
         Intent intent = new Intent(BluetoothInputDevice.ACTION_CONNECTION_STATE_CHANGED);
         intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, prevState);
         intent.putExtra(BluetoothProfile.EXTRA_STATE, newState);
         intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
         intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
         sendBroadcast(intent, BLUETOOTH_PERM);
-        if (DBG) log("Connection state " + device + ": " + prevState + "->" + newState);
-        notifyProfileConnectionStateChanged(device, BluetoothProfile.INPUT_DEVICE, newState, prevState);
     }
 
     private void broadcastProtocolMode(BluetoothDevice device, int protocolMode) {
diff --git a/src/com/android/bluetooth/pan/PanService.java b/src/com/android/bluetooth/pan/PanService.java
index 6116ee8..7f1cc7c 100755
--- a/src/com/android/bluetooth/pan/PanService.java
+++ b/src/com/android/bluetooth/pan/PanService.java
@@ -411,16 +411,18 @@
             panDevice.mIface = iface;
         }
 
+        /* Notifying the connection state change of the profile before sending the intent for
+           connection state change, as it was causing a race condition, with the UI not being
+           updated with the correct connection state. */
+        if (DBG) Log.d(TAG, "Pan Device state : device: " + device + " State:" +
+                       prevState + "->" + state);
+        notifyProfileConnectionStateChanged(device, BluetoothProfile.PAN, state, prevState);
         Intent intent = new Intent(BluetoothPan.ACTION_CONNECTION_STATE_CHANGED);
         intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
         intent.putExtra(BluetoothPan.EXTRA_PREVIOUS_STATE, prevState);
         intent.putExtra(BluetoothPan.EXTRA_STATE, state);
         intent.putExtra(BluetoothPan.EXTRA_LOCAL_ROLE, local_role);
         sendBroadcast(intent, BLUETOOTH_PERM);
-
-        if (DBG) Log.d(TAG, "Pan Device state : device: " + device + " State:" +
-                       prevState + "->" + state);
-        notifyProfileConnectionStateChanged(device, BluetoothProfile.PAN, state, prevState);
     }
 
     // configured when we start tethering