FM: Auto turn ON FM after WCNSS SSR

After WCNSS SSR, auto turn ON FM from FM app.

CRs-fixed: 749400
Change-Id: I9dfa0d9bae621658fdaea2f4ff87f290f748f9b1
diff --git a/fmapp2/src/com/caf/fmradio/FMRadio.java b/fmapp2/src/com/caf/fmradio/FMRadio.java
index 08021cc..0acbc63 100644
--- a/fmapp2/src/com/caf/fmradio/FMRadio.java
+++ b/fmapp2/src/com/caf/fmradio/FMRadio.java
@@ -1620,6 +1620,20 @@
       boolean bStatus = false;
       if (mService != null) {
           try {
+            if(mService.isSSRInProgress()) {
+               Log.e(LOGTAG, "SSR In Progress, looping");
+               while(mService.isSSRInProgress()) {
+                  try
+                  {
+                     Thread.sleep(500);
+                  } catch (InterruptedException e)
+                  {
+                     break;
+                  }
+               }
+               Log.e(LOGTAG, "SSR done, continuing");
+            }
+
             if((false == mService.isFmOn()) && isAntennaAvailable()) {
                 bStatus = mService.fmOn();
                 if(bStatus) {
@@ -1680,11 +1694,21 @@
       }
       if (mService != null) {
          try {
-            if(bSpeakerPhoneOn) {
-               mService.enableSpeaker(false);
-            }
             mService.fmRadioReset();
             enableRadioOnOffUI(false);
+
+            Log.e(LOGTAG, "Done with reset, restarting FM");
+            /* Start Turn ON sequence again */
+            mOnOffButton.setEnabled(false);
+            mOnOffButton.setClickable(false);
+            mOnOffButton.setOnClickListener(null);
+
+            mDisableRadioHandler.removeCallbacks(mDisableRadioTask);
+            mEnableRadioHandler.removeCallbacks(mEnableRadioTask);
+            mEnableRadioHandler.postDelayed(mEnableRadioTask, 0);
+
+            cleanupTimeoutHandler();
+            Log.e(LOGTAG, "Done with restart");
          }catch (RemoteException e) {
             e.printStackTrace();
          }
diff --git a/fmapp2/src/com/caf/fmradio/FMRadioService.java b/fmapp2/src/com/caf/fmradio/FMRadioService.java
index 9727050..f643ec3 100644
--- a/fmapp2/src/com/caf/fmradio/FMRadioService.java
+++ b/fmapp2/src/com/caf/fmradio/FMRadioService.java
@@ -181,6 +181,8 @@
    private boolean mUnMuteOnFocusLoss = false;
    private boolean mSpeakerOnFocusLoss = false;
    private MediaSession mSession;
+   private boolean mIsSSRInProgress = false;
+   private boolean mIsSSRInProgressFromActivity = false;
 
    public FMRadioService() {
    }
@@ -845,6 +847,14 @@
        mResumeAfterCall = false;
        if ( true == mPlaybackInProgress ) // no need to resend event
            return;
+
+       /* If audio focus lost while SSR in progress, don't request for Audio focus */
+       if ( (true == mIsSSRInProgress || true == mIsSSRInProgressFromActivity) &&
+             true == mStoppedOnFocusLoss) {
+           Log.d(LOGTAG, "Audio focus lost while SSR in progress, returning");
+           return;
+       }
+
        AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        int granted = audioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC,
               AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
@@ -1718,6 +1728,11 @@
       {
            return (mService.get().isSleepTimerActive());
       }
+
+      public boolean isSSRInProgress()
+      {
+         return(mService.get().isSSRInProgress());
+      }
    }
    private final IBinder mBinder = new ServiceStub(this);
 
@@ -1845,6 +1860,9 @@
                               // we disable
             stop();
          }
+
+         /* reset SSR flag */
+         mIsSSRInProgressFromActivity = false;
       }
       return(bStatus);
    }
@@ -1889,12 +1907,6 @@
    * Reset (OFF) FM Operations: This resets all the current FM operations             .
    */
    private void fmOperationsReset() {
-      if ( mSpeakerPhoneOn)
-      {
-          mSpeakerPhoneOn = false;
-          AudioSystem.setForceUse(AudioSystem.FOR_MEDIA, AudioSystem.FORCE_NONE);
-      }
-
       if (isFmRecordingOn())
       {
           stopRecording();
@@ -1904,7 +1916,6 @@
       if(audioManager != null)
       {
          Log.d(LOGTAG, "audioManager.setFmRadioOn = false \n" );
-         unMute();
          resetFM();
          //audioManager.setParameters("FMRadioOn=false");
          Log.d(LOGTAG, "audioManager.setFmRadioOn false done \n" );
@@ -1958,6 +1969,10 @@
       return(bStatus);
    }
 
+   public boolean isSSRInProgress() {
+      return mIsSSRInProgress;
+   }
+
    /* Returns whether FM hardware is ON.
     *
     * @return true if FM was tuned, searching. (at the end of
@@ -2678,6 +2693,7 @@
       }
       public void FmRxEvRadioReset()
       {
+         boolean bStatus;
          Log.d(LOGTAG, "FmRxEvRadioReset");
          if(isFmOn()) {
              // Received radio reset event while FM is ON
@@ -2690,7 +2706,24 @@
                 */
                 if((mServiceInUse) && (mCallbacks != null) )
                 {
+                    mIsSSRInProgressFromActivity = true;
                     mCallbacks.onRadioReset();
+                } else {
+                    Log.d(LOGTAG, "Activity is not in foreground, turning on from service");
+                    if (isAntennaAvailable())
+                    {
+                        mIsSSRInProgress = true;
+                        bStatus = fmOn();
+                        if(bStatus)
+                        {
+                             bStatus = tune(FmSharedPreferences.getTunedFrequency());
+                             if(!bStatus)
+                               Log.e(LOGTAG, "Tuning after SSR from service failed");
+                        } else {
+                           Log.e(LOGTAG, "Turning on after SSR from service failed");
+                        }
+                        mIsSSRInProgress = false;
+                    }
                 }
              }
              catch (RemoteException e)
diff --git a/fmapp2/src/com/caf/fmradio/IFMRadioService.aidl b/fmapp2/src/com/caf/fmradio/IFMRadioService.aidl
index 2f6f1e3..7b04d71 100644
--- a/fmapp2/src/com/caf/fmradio/IFMRadioService.aidl
+++ b/fmapp2/src/com/caf/fmradio/IFMRadioService.aidl
@@ -72,5 +72,6 @@
     boolean setRxRepeatCount(int count);
     long getRecordingStartTime();
     boolean isSleepTimerActive();
+    boolean isSSRInProgress();
 }