Merge "wait for disable event callback to complete"
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/FmReceiver.java b/qcom/fmradio/FmReceiver.java
index 7d6ab0d..5ba27c8 100644
--- a/qcom/fmradio/FmReceiver.java
+++ b/qcom/fmradio/FmReceiver.java
@@ -41,7 +41,6 @@
 import android.net.wifi.WifiManager;
 import android.content.IntentFilter;
 import android.bluetooth.BluetoothAdapter;
-import java.lang.Object;
 
 /**
  * This class contains all interfaces and types needed to
@@ -440,8 +439,6 @@
            }
        }
    };
-   private volatile int mState;
-   public static Object mLockObject = new Object();
 
    /**
     * Constructor for the receiver Object
@@ -449,7 +446,7 @@
    public FmReceiver(){
       mControl = new FmRxControls();
       mRdsData = new FmRxRdsData (sFd);
-      mRxEvents = new FmRxEventListner(this);
+      mRxEvents = new FmRxEventListner();
    }
 
    /**
@@ -464,7 +461,7 @@
    public FmReceiver(String devicePath,
                      FmRxEvCallbacksAdaptor callback) throws InstantiationException {
       mControl = new FmRxControls();
-      mRxEvents = new FmRxEventListner(this);
+      mRxEvents = new FmRxEventListner();
 
       Log.e(TAG, "FmReceiver constructor");
       //registerClient(callback);
@@ -579,32 +576,22 @@
        * Check for FM State.
        * If FMRx already on, then return.
       */
-
-      int mState = getFMState();
+      int state = getFMState();
 
       mIntentFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
       mBtIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
 
-      if (mState == FMState_Rx_Turned_On || mState == FMState_Srch_InProg) {
-
+      if (state == FMState_Rx_Turned_On || state == FMState_Srch_InProg) {
          Log.d(TAG, "enable: FM already turned On and running");
          return status;
-      }else if (mState == subPwrLevel_FMTurning_Off) {
+      }else if (state == subPwrLevel_FMTurning_Off) {
          Log.v(TAG, "FM is in the process of turning off.Pls wait for sometime.");
-         synchronized(mLockObject){
-                try {
-                    if (getFMState() != FMState_Turned_Off) {
-                        mLockObject.wait();
-                    }
-                } catch (InterruptedException e) {
-                    Log.e(TAG, "Error in waiting on mLockObject" + e);
-                }
-         }
-      }else if (mState == subPwrLevel_FMRx_Starting) {
+         return status;
+      }else if (state == subPwrLevel_FMRx_Starting) {
          Log.v(TAG, "FM is in the process of turning On.Pls wait for sometime.");
          return status;
-      }else if ((mState == FMState_Tx_Turned_On)
-                || (mState == subPwrLevel_FMTx_Starting)) {
+      }else if ((state == FMState_Tx_Turned_On)
+                || (state == subPwrLevel_FMTx_Starting)) {
          Log.v(TAG, "FM Tx is turned on or in the process of turning on.");
          return status;
       }
diff --git a/qcom/fmradio/FmRxEventListner.java b/qcom/fmradio/FmRxEventListner.java
index 9c9bba2..c178f17 100644
--- a/qcom/fmradio/FmRxEventListner.java
+++ b/qcom/fmradio/FmRxEventListner.java
@@ -59,13 +59,8 @@
     }
 
     private Thread mThread;
-    private FmReceiver mReceiver;
     private static final String TAG = "FMRadio";
 
-    public FmRxEventListner(FmReceiver receiver) {
-        mReceiver = receiver;
-    }
-
     public void startListner (final int fd, final FmRxEvCallbacks cb) {
         /* start a thread and listen for messages */
         mThread = new Thread(){
@@ -218,14 +213,11 @@
                             case 18:
                                 Log.d(TAG, "Got RADIO_DISABLED");
                                 if (FmTransceiver.getFMPowerState() == FmTransceiver.subPwrLevel_FMTurning_Off) {
-                                    synchronized(FmReceiver.mLockObject) {
-                                        FmTransceiver.release("/dev/radio0");
-                                        cb.FmRxEvDisableReceiver();
-                                        /*Set the state as FMOff */
-                                        FmTransceiver.setFMPowerState(FmTransceiver.FMState_Turned_Off);
-                                        Log.v(TAG, "RxEvtList: CURRENT-STATE : FMTurningOff ---> NEW-STATE : FMOff");
-                                        FmReceiver.mLockObject.notify();
-                                    }
+                                    FmTransceiver.release("/dev/radio0");
+                                    /*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 {
                                     Log.d(TAG, "Unexpected RADIO_DISABLED recvd");