FM: Add AudioroutingListener

 - AddAudioRoutingListener

Change-Id: I949236bdb4f47b445337350809d586f2a1efada9
diff --git a/fmapp2/src/com/caf/fmradio/FMRadioService.java b/fmapp2/src/com/caf/fmradio/FMRadioService.java
index 5e82784..00e097c 100644
--- a/fmapp2/src/com/caf/fmradio/FMRadioService.java
+++ b/fmapp2/src/com/caf/fmradio/FMRadioService.java
@@ -96,6 +96,7 @@
 import android.app.ActivityManager;
 import android.app.ActivityManager.RunningAppProcessInfo;
 import android.media.session.MediaSession;
+import android.media.AudioRouting;
 import android.bluetooth.BluetoothA2dp;
 import android.bluetooth.BluetoothProfile;
 import android.bluetooth.BluetoothAdapter;
@@ -246,6 +247,8 @@
    private boolean mEventReceived = false;
    private boolean isfmOffFromApplication = false;
 
+   private AudioRoutingListener mRoutingListener =  null;
+   private int mCurrentDevice = AudioDeviceInfo.TYPE_UNKNOWN; // current output device
    public FMRadioService() {
    }
 
@@ -297,6 +300,8 @@
       Log.d(LOGTAG, " is A2DP device Supported In HAL"+mA2dpDeviceSupportInHal);
 
       getA2dpStatusAtStart();
+
+      mRoutingListener = new AudioRoutingListener();
    }
 
    @Override
@@ -397,6 +402,8 @@
                           .build())
                 .setBufferSizeInBytes(FM_RECORD_BUF_SIZE)
                 .build();
+        Log.d(LOGTAG," adding RoutingChangedListener() ");
+        mAudioTrack.addOnRoutingChangedListener(mRoutingListener, null);
 
         if (mMuted)
             mAudioTrack.setVolume(0.0f);
@@ -547,11 +554,13 @@
             }
             status = AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_FM,
                                           AudioSystem.DEVICE_STATE_AVAILABLE, "", "");
+            mCurrentDevice = AudioDeviceInfo.TYPE_WIRED_HEADSET;
             if (status != AudioSystem.SUCCESS) {
                 success = false;
                 Log.e(LOGTAG, "configureFMDeviceLoopback failed! status:" + status);
                 AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_FM,
                                      AudioSystem.DEVICE_STATE_UNAVAILABLE, "", "");
+                mCurrentDevice = AudioDeviceInfo.TYPE_UNKNOWN;
             } else {
                 mIsFMDeviceLoopbackActive = true;
             }
@@ -559,6 +568,7 @@
             AudioSystem.setDeviceConnectionState(AudioSystem.DEVICE_OUT_FM,
                                  AudioSystem.DEVICE_STATE_UNAVAILABLE, "", "");
             mIsFMDeviceLoopbackActive = false;
+            mCurrentDevice = AudioDeviceInfo.TYPE_UNKNOWN;
         }
 
         return success;
@@ -1674,6 +1684,20 @@
       }
    };
 
+    private class AudioRoutingListener implements AudioRouting.OnRoutingChangedListener {
+        public void onRoutingChanged(AudioRouting audioRouting) {
+            Log.d(LOGTAG," onRoutingChanged  + currdevice " + mCurrentDevice);
+            AudioDeviceInfo routedDevice = audioRouting.getRoutedDevice();
+            // if routing is nowhere, we get routedDevice as null
+            if(routedDevice != null) {
+                Log.d(LOGTAG," Audio Routed to device id " + routedDevice.getType());
+                if(routedDevice.getType() != mCurrentDevice) {
+                    startApplicationLoopBack(mCurrentDevice);
+                }
+            }
+        }
+    }
+
    private Handler mDelayedStopHandler = new Handler() {
       @Override
       public void handleMessage(Message msg) {
@@ -4275,6 +4299,7 @@
             CreateRecordSessions();
             Log.d(LOGTAG,"creating AudioTrack session");
         }
+        mCurrentDevice = outputDevice.getType();
         mAudioTrack.setPreferredDevice(outputDevice);
         Log.d(LOGTAG,"PreferredDevice is set to "+ outputDevice.getType());
         if(!isRecordSinking()) {
diff --git a/fmapp2/src/com/caf/hc_utils/A2dpDeviceStatus.java b/fmapp2/src/com/caf/hc_utils/A2dpDeviceStatus.java
index 6b58f9e..7f44278 100644
--- a/fmapp2/src/com/caf/hc_utils/A2dpDeviceStatus.java
+++ b/fmapp2/src/com/caf/hc_utils/A2dpDeviceStatus.java
@@ -60,7 +60,8 @@
         boolean isConnected = false;
         int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE,
                                 BluetoothA2dp.STATE_DISCONNECTED);
-        if((state == BluetoothA2dp.STATE_CONNECTED) || (state == BluetoothProfile.STATE_CONNECTED)) {
+        if((state == BluetoothA2dp.STATE_CONNECTED) || (state == BluetoothProfile.STATE_CONNECTED)
+           || (state ==  BluetoothA2dp.STATE_CONNECTING)) {
             isConnected = true;
         }
         return isConnected;