Merge "FM: Query and apply STREAM_MUSIC volume     when output device routing changes."
diff --git a/fmapp2/src/com/caf/fmradio/FMRadioService.java b/fmapp2/src/com/caf/fmradio/FMRadioService.java
index 7826d5c..5f591fe 100644
--- a/fmapp2/src/com/caf/fmradio/FMRadioService.java
+++ b/fmapp2/src/com/caf/fmradio/FMRadioService.java
@@ -143,7 +143,7 @@
    private boolean mServiceInUse = false;
    private static boolean mMuted = false;
    private static boolean mResumeAfterCall = false;
-   private static int mAudioDevice = 0;
+   private static int mAudioDevice = AudioDeviceInfo.TYPE_WIRED_HEADPHONES;
    MediaRecorder mRecorder = null;
    MediaRecorder mA2dp = null;
    private boolean mFMOn = false;
@@ -402,6 +402,20 @@
             if (mReceiver.isCherokeeChip() && (mPref.getBoolean("SLIMBUS_SEQ", true))) {
                 enableSlimbus(ENABLE_SLIMBUS_DATA_PORT);
             }
+            String status = audioManager.getParameters("fm_status");
+            Log.d(LOGTAG," FM hardwareLoopback Status = " + status);
+            if (status.contains("1")) {
+               /* This case usually happens, when FM is force killed through settings app
+                * and we don't get chance to disable Hardware LoopBack.
+                * Hardware LoopBack will be running,disable it first and enable again
+                * using routing set param to audio */
+               Log.d(LOGTAG," FM HardwareLoopBack Active, disable it first and enable again");
+               mAudioDeviceType =
+                  AudioDeviceInfo.TYPE_WIRED_HEADPHONES | AudioSystem.DEVICE_OUT_FM;
+               String keyValPairs = new String("fm_routing="+mAudioDeviceType);
+               Log.d(LOGTAG, "keyValPairs = "+keyValPairs);
+               audioManager.setParameters(keyValPairs);
+            }
             mIsFMDeviceLoopbackActive = true;
             /*or with DEVICE_OUT_FM to support backward compatiblity*/
             mAudioDeviceType = mAudioDevice | AudioSystem.DEVICE_OUT_FM;
@@ -594,6 +608,10 @@
                             stopRecording();
                         }
                     } else if( action.equals(AudioManager.VOLUME_CHANGED_ACTION)) {
+                        if(!isFmOn()) {
+                            Log.d(LOGTAG, "FM is Turned off ,not applying the changed volume");
+                            return;
+                        }
                         int streamType =
                               intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1);
                         if (streamType == AudioManager.STREAM_MUSIC) {