FM: Query and apply STREAM_MUSIC volume
    when output device routing changes.

   -when ever Bluetooth device connect/disconnect
   query for STREAM_MUSIC volume and apply to fm volume.

Change-Id: I0a1cb47f7469439b94ac4b87f3c7cf58a0bbd3b1
diff --git a/fmapp2/src/com/caf/fmradio/FMRadioService.java b/fmapp2/src/com/caf/fmradio/FMRadioService.java
index 8faa313..7826d5c 100644
--- a/fmapp2/src/com/caf/fmradio/FMRadioService.java
+++ b/fmapp2/src/com/caf/fmradio/FMRadioService.java
@@ -391,7 +391,7 @@
                " DeviceLoopbackActive = " + mIsFMDeviceLoopbackActive +
                " mStoppedOnFocusLoss = "+mStoppedOnFocusLoss);
         int mAudioDeviceType;
-  
+
         AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
         if(enable) {
             if(mIsFMDeviceLoopbackActive || mStoppedOnFocusLoss) {
@@ -422,6 +422,16 @@
         return true;
     }
 
+    private void setCurrentFMVolume() {
+        if(isFmOn()) {
+            AudioManager maudioManager =
+                    (AudioManager) getSystemService(Context.AUDIO_SERVICE);
+            int mCurrentVolumeIndex =
+                    maudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
+            setFMVolume(mCurrentVolumeIndex);
+        }
+    }
+
     /**
       * Registers an intent to listen for ACTION_MEDIA_UNMOUNTED notifications.
       * The intent will call closeExternalStorageFiles() if the external media
@@ -591,8 +601,19 @@
                               intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, -1);
                             setFMVolume(mCurrentVolumeIndex);
                         }
-                    }
+                    } else if (action.equals(BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED)) {
+                            mHandler.removeCallbacks(mFmVolumeHandler);
+                            mHandler.post(mFmVolumeHandler);
 
+                    } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
+                        int state =
+                           intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
+                        Log.d(LOGTAG, "ACTION_STATE_CHANGED state :"+ state);
+                        if (state == BluetoothAdapter.STATE_OFF) {
+                            mHandler.removeCallbacks(mFmVolumeHandler);
+                            mHandler.post(mFmVolumeHandler);
+                        }
+                    }
                 }
             };
             AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
@@ -602,6 +623,8 @@
             iFilter.addAction("HDMI_CONNECTED");
             iFilter.addAction(Intent.ACTION_SHUTDOWN);
             iFilter.addAction(AudioManager.VOLUME_CHANGED_ACTION);
+            iFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
+            iFilter.addAction(BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED);
             iFilter.addCategory(Intent.CATEGORY_DEFAULT);
             registerReceiver(mHeadsetReceiver, iFilter);
         }
@@ -705,7 +728,17 @@
         }
     }
 
-
+    final Runnable    mFmVolumeHandler = new Runnable() {
+        public void run() {
+            try {
+                Thread.sleep(1000);
+            } catch (Exception ex) {
+                Log.d( LOGTAG, "RunningThread InterruptedException");
+                return;
+            }
+            setCurrentFMVolume();
+        }
+    };
 
     final Runnable    mHeadsetPluginHandler = new Runnable() {
         public void run() {