FM: synchronize audioTarck play and enable slimbus

Change-Id: Ifa7242262fc94fd2212318469af4194d0355d973
diff --git a/fmapp2/src/com/caf/fmradio/FMRadioService.java b/fmapp2/src/com/caf/fmradio/FMRadioService.java
index ae85350..49fe731 100644
--- a/fmapp2/src/com/caf/fmradio/FMRadioService.java
+++ b/fmapp2/src/com/caf/fmradio/FMRadioService.java
@@ -228,6 +228,7 @@
    private boolean mIsRecordSink = false;
    private static final int AUDIO_FRAMES_COUNT_TO_IGNORE = 3;
    private Object mEventWaitLock = new Object();
+   private Object mRecordSinkLock = new Object();
    private boolean mIsFMDeviceLoopbackActive = false;
    private File mStoragePath = null;
    private static final int FM_OFF_FROM_APPLICATION = 1;
@@ -418,6 +419,17 @@
             mRecordSinkThread = new RecordSinkThread();
             mRecordSinkThread.start();
             Log.d(LOGTAG, "mRecordSinkThread started");
+            try {
+                synchronized (mRecordSinkLock) {
+                    Log.d(LOGTAG, "waiting for play to complete");
+                    mRecordSinkLock.wait();
+                }
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+            if (mReceiver.isCherokeeChip() && (mPref.getBoolean("SLIMBUS_SEQ", true))) {
+                enableSlimbus(ENABLE_SLIMBUS_DATA_PORT);
+            }
         }
     }
 
@@ -470,6 +482,9 @@
                             Log.d(LOGTAG, "RecordSinkThread: mAudioTrack.play executed");
                             mAudioTrack.play();
                             Log.d(LOGTAG, "RecordSinkThread: mAudioTrack.play completed");
+                            synchronized (mRecordSinkLock) {
+                                mRecordSinkLock.notify();
+                            }
                         }
                         int size = mAudioRecord.read(buffer, 0, FM_RECORD_BUF_SIZE);
                         // check whether need to ignore first 3 frames audio data from AudioRecord
@@ -567,6 +582,9 @@
             }
             if ((!mIsFMDeviceLoopbackActive) && (!mA2dpConnected) && (!mSpeakerPhoneOn)) {
                 // not on BT and device loop is also not active
+                if (mReceiver.isCherokeeChip() && (mPref.getBoolean("SLIMBUS_SEQ", true))) {
+                    enableSlimbus(ENABLE_SLIMBUS_DATA_PORT);
+                }
                 exitRecordSinkThread();
                 configureFMDeviceLoopback(true);
             }
@@ -978,8 +996,6 @@
       if (isFmOn()) {
           setLowPowerMode(false);
           startFM();
-          if (mReceiver.isCherokeeChip() && (mPref.getBoolean("SLIMBUS_SEQ", true)))
-              enableSlimbus(ENABLE_SLIMBUS_DATA_PORT);
       }
    }
 
@@ -1179,6 +1195,7 @@
        mAudioManager.registerMediaButtonEventReceiver(fmRadio);
 
        mStoppedOnFocusLoss = false;
+       mPlaybackInProgress = true;
 
        if (mStoppedOnFactoryReset) {
            mStoppedOnFactoryReset = false;
@@ -1192,9 +1209,10 @@
                } else {
                    startApplicationLoopBack(AudioDeviceInfo.TYPE_BUILTIN_SPEAKER);
                }
+       } else {
+               Log.d(LOGTAG,"Start Hardware loop back for audio");
+               configureAudioDataPath(true);
        }
-       mPlaybackInProgress = true;
-       configureAudioDataPath(true);
        try {
            if ((mServiceInUse) && (mCallbacks != null))
                mCallbacks.onFmAudioPathStarted();
@@ -1752,8 +1770,6 @@
 
                       if(false == mPlaybackInProgress)
                           startFM();
-                      if (mReceiver.isCherokeeChip() && (mPref.getBoolean("SLIMBUS_SEQ", true)))
-                          enableSlimbus(ENABLE_SLIMBUS_DATA_PORT);
                       mSession.setActive(true);
                       break;
                   default:
@@ -2526,22 +2542,14 @@
          else
          {
            if (mReceiver.isCherokeeChip()) {
-               if ((mPref.getBoolean("SLIMBUS_SEQ", true)) ) {
-                       bStatus = enableSlimbus(ENABLE_SLIMBUS_DATA_PORT);
-                   if (bStatus) {
-                       bStatus = fmTurnOnSequenceCherokee();
-                   } else {
-                       Log.d(LOGTAG, "set SLIMBUS_SEQ flag to false");
-                       mEditor.putBoolean("SLIMBUS_SEQ", false);
-                       mEditor.commit();
-                       bStatus = fmTurnOnSequence();
-                   }
-               } else {
-                   bStatus = fmTurnOnSequence();
+               if ((mReceiver != null) && mReceiver.isCherokeeChip() &&
+                     (mPref.getBoolean("SLIMBUS_SEQ", true))) {
+                   enableSlimbus(DISABLE_SLIMBUS_DATA_PORT);
                }
-         } else {
+               bStatus = fmTurnOnSequenceCherokee();
+           } else {
                bStatus = fmTurnOnSequence();
-         }
+           }
            /* reset SSR flag */
            mIsSSRInProgressFromActivity = false;
          }
@@ -2559,8 +2567,8 @@
       {
          Log.d(LOGTAG, "audioManager.setFmRadioOn = false \n" );
          if ((mReceiver != null) && mReceiver.isCherokeeChip() &&
-                            (mPref.getBoolean("SLIMBUS_SEQ", true))) {
-             enableSlimbus(DISABLE_SLIMBUS_DATA_PORT);
+                     (mPref.getBoolean("SLIMBUS_SEQ", true))) {
+              enableSlimbus(DISABLE_SLIMBUS_DATA_PORT);
          }
          stopFM();
          unMute();
@@ -2766,7 +2774,6 @@
            Log.d(LOGTAG, "enabling speaker");
                startApplicationLoopBack(AudioDeviceInfo.TYPE_BUILTIN_SPEAKER);
        }
-       enableSlimbus(ENABLE_SLIMBUS_DATA_PORT);
        Log.d(LOGTAG, "speakerOn completed:" + speakerOn);
    }
   /*
@@ -4166,7 +4173,6 @@
            audioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC,
                   AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
            startFM();
-           enableSlimbus(ENABLE_SLIMBUS_DATA_PORT);
            mStoppedOnFocusLoss = false;
        }
    }