Merge "FM: Take care of audio routing when BT is connected and disconnected."
diff --git a/fmapp2/src/com/caf/fmradio/FMRadioService.java b/fmapp2/src/com/caf/fmradio/FMRadioService.java
index aaf38cb..b35b3f4 100644
--- a/fmapp2/src/com/caf/fmradio/FMRadioService.java
+++ b/fmapp2/src/com/caf/fmradio/FMRadioService.java
@@ -553,7 +553,9 @@
                        // audio policy manager switch audio to speaker.
                        mHandler.removeCallbacks(mHeadsetPluginHandler);
                        mHandler.post(mHeadsetPluginHandler);
-                    } else if(mA2dpDeviceState.isA2dpStateChange(action) ) {
+                    } else if(mA2dpDeviceState.isA2dpStateChange(action) &&
+                             (mA2dpDeviceState.isConnected(intent) ||
+                              mA2dpDeviceState.isDisconnected(intent))) {
                         boolean  bA2dpConnected =
                         mA2dpDeviceState.isConnected(intent);
                         Log.d(LOGTAG, "bA2dpConnected:" +bA2dpConnected);
diff --git a/fmapp2/src/com/caf/hc_utils/A2dpDeviceStatus.java b/fmapp2/src/com/caf/hc_utils/A2dpDeviceStatus.java
index 45b647e..6b58f9e 100644
--- a/fmapp2/src/com/caf/hc_utils/A2dpDeviceStatus.java
+++ b/fmapp2/src/com/caf/hc_utils/A2dpDeviceStatus.java
@@ -65,6 +65,15 @@
         }
         return isConnected;
     }
+    public boolean isDisconnected(Intent intent) {
+        boolean isDisconnected = false;
+        int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE,
+                                BluetoothA2dp.STATE_CONNECTED);
+        if((state == BluetoothA2dp.STATE_DISCONNECTED)) {
+            isDisconnected = true;
+        }
+        return isDisconnected;
+    }
     public boolean isPlaying(Intent intent) {
         boolean isPlaying = false;
         int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE,