wait for disable event callback to complete

After sending the disable request, wait for disable event callback
to complete.
If we enable fm before disable callback has been completed,
state of FM is TURNING OFF, hence enable FM gets failed.

Change-Id: I49fa255d2c8dcec2d5f60df350a40e69fa3cee9c
diff --git a/fmapp2/src/com/caf/fmradio/FMRadioService.java b/fmapp2/src/com/caf/fmradio/FMRadioService.java
index 0c5713b..002224d 100644
--- a/fmapp2/src/com/caf/fmradio/FMRadioService.java
+++ b/fmapp2/src/com/caf/fmradio/FMRadioService.java
@@ -212,11 +212,13 @@
    private boolean mIsRecordSink = false;
    private static final int AUDIO_FRAMES_COUNT_TO_IGNORE = 3;
    private Object mRecordSinkLock = new Object();
+   private Object mEventWaitLock = new Object();
    private boolean mIsFMDeviceLoopbackActive = false;
    private File mStoragePath = null;
 
    private static final int FM_OFF_FROM_APPLICATION = 1;
    private static final int FM_OFF_FROM_ANTENNA = 2;
+   private static final int RADIO_TIMEOUT = 1500;
 
    private static Object mNotchFilterLock = new Object();
 
@@ -2284,6 +2286,21 @@
       if (mReceiver != null)
       {
          bStatus = mReceiver.disable(this);
+         if (bStatus &&
+                 (mReceiver.getFMState() == mReceiver.subPwrLevel_FMTurning_Off)) {
+             synchronized (mEventWaitLock) {
+                 Log.d(LOGTAG, "waiting for disable event");
+                 try {
+                     mEventWaitLock.wait(RADIO_TIMEOUT);
+                 } catch (IllegalMonitorStateException e) {
+                     Log.e(LOGTAG, "Exception caught while waiting for event");
+                     e.printStackTrace();
+                 } catch (InterruptedException ex) {
+                     Log.e(LOGTAG, "Exception caught while waiting for event");
+                     ex.printStackTrace();
+                 }
+             }
+         }
          mReceiver = null;
       }
       fmOperationsOff();
@@ -2291,7 +2308,6 @@
       return(bStatus);
    }
 
-
    private boolean fmOff(int off_from) {
        if (off_from == FM_OFF_FROM_APPLICATION || off_from == FM_OFF_FROM_ANTENNA) {
            Log.d(LOGTAG, "FM application close button pressed or antenna removed");
@@ -3025,6 +3041,9 @@
          Log.d(LOGTAG, "FmRxEvDisableReceiver");
          mFMOn = false;
          FmSharedPreferences.clearTags();
+         synchronized (mEventWaitLock) {
+             mEventWaitLock.notify();
+         }
       }
       public void FmRxEvRadioReset()
       {
diff --git a/qcom/fmradio/FmRxEventListner.java b/qcom/fmradio/FmRxEventListner.java
index 0ad7c0c..c178f17 100644
--- a/qcom/fmradio/FmRxEventListner.java
+++ b/qcom/fmradio/FmRxEventListner.java
@@ -214,9 +214,9 @@
                                 Log.d(TAG, "Got RADIO_DISABLED");
                                 if (FmTransceiver.getFMPowerState() == FmTransceiver.subPwrLevel_FMTurning_Off) {
                                     FmTransceiver.release("/dev/radio0");
-                                    cb.FmRxEvDisableReceiver();
                                     /*Set the state as FMOff */
                                     FmTransceiver.setFMPowerState(FmTransceiver.FMState_Turned_Off);
+                                    cb.FmRxEvDisableReceiver();
                                     Log.v(TAG, "RxEvtList: CURRENT-STATE : FMTurningOff ---> NEW-STATE : FMOff");
                                     Thread.currentThread().interrupt();
                                 } else {