Modify sequence of turning OFF FM recording and FM audio

Disable FM audio playback first and then FM recording in case of
FM turnoff due to audio focus loss to avoid overlapping of Fmaudio
and Music app audio.

CRs-Fixed: 1074809
Change-Id: Id498e436dac0e6fe0c9ef823490b1c7588e98ecb
diff --git a/fmapp2/src/com/caf/fmradio/FMRadioService.java b/fmapp2/src/com/caf/fmradio/FMRadioService.java
index 2b9fb83..43e91a6 100644
--- a/fmapp2/src/com/caf/fmradio/FMRadioService.java
+++ b/fmapp2/src/com/caf/fmradio/FMRadioService.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009-2015, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2009-2016, The Linux Foundation. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
@@ -1604,8 +1604,6 @@
                   case AudioManager.AUDIOFOCUS_LOSS:
                       Log.v(LOGTAG, "AudioFocus: received AUDIOFOCUS_LOSS");
                       //intentional fall through.
-                      if (true == isFmRecordingOn())
-                          stopRecording();
                       if (mSpeakerPhoneOn) {
                          mSpeakerDisableHandler.removeCallbacks(mSpeakerDisableTask);
                          mSpeakerDisableHandler.postDelayed(mSpeakerDisableTask, 0);
@@ -1613,6 +1611,9 @@
                       if (true == mPlaybackInProgress) {
                           stopFM();
                       }
+                      if (true == isFmRecordingOn())
+                          stopRecording();
+
                       if (mSpeakerPhoneOn) {
                           if (isAnalogModeSupported())
                               setAudioPath(false);
@@ -2247,6 +2248,17 @@
    * Turn OFF FM Operations: This disables all the current FM operations             .
    */
    private void fmOperationsOff() {
+     // disable audio path
+      AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
+      if(audioManager != null)
+      {
+         Log.d(LOGTAG, "audioManager.setFmRadioOn = false \n" );
+         stopFM();
+         unMute();
+         audioManager.abandonAudioFocus(mAudioFocusListener);
+         //audioManager.setParameters("FMRadioOn=false");
+         Log.d(LOGTAG, "audioManager.setFmRadioOn false done \n" );
+      }
       // stop recording
       if (isFmRecordingOn())
       {
@@ -2258,17 +2270,6 @@
                return;
           }
       }
-      // disable audio path
-      AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
-      if(audioManager != null)
-      {
-         Log.d(LOGTAG, "audioManager.setFmRadioOn = false \n" );
-         stopFM();
-         unMute();
-         audioManager.abandonAudioFocus(mAudioFocusListener);
-         //audioManager.setParameters("FMRadioOn=false");
-         Log.d(LOGTAG, "audioManager.setFmRadioOn false done \n" );
-      }
       // reset FM audio settings
       resetAudioRoute();
 
@@ -2281,6 +2282,7 @@
       }
    }
 
+
   /*
    * Reset (OFF) FM Operations: This resets all the current FM operations             .
    */