From 6c3e689d1c95be219ed05a34386e407e2a3f0f9f Mon Sep 17 00:00:00 2001
From: Kamal Negi <kamaln@codeaurora.org>
Date: Wed, 15 Mar 2017 14:05:32 +0530
Subject: [PATCH] fm: wait for enable event to complete

After sending enable request, wait for enable event callback to
complete.
If we configure audio path before enable receiver done, audio path
won't configure properly, hence no audio will be heard.

Change-Id: Ia5030308c35f41297ff19079f38e735e962770b2
---
 .../src/com/caf/fmradio/FMRadioService.java   | 69 +++++++++++--------
 1 file changed, 40 insertions(+), 29 deletions(-)

diff --git a/fmapp2/src/com/caf/fmradio/FMRadioService.java b/fmapp2/src/com/caf/fmradio/FMRadioService.java
index 7c89709..2d157c6 100644
--- a/fmapp2/src/com/caf/fmradio/FMRadioService.java
+++ b/fmapp2/src/com/caf/fmradio/FMRadioService.java
@@ -224,6 +224,7 @@ public class FMRadioService extends Service
    private static Object mNotchFilterLock = new Object();
 
    private boolean mFmA2dpDisabled;
+   private boolean mEventReceived = false;
 
    public FMRadioService() {
    }
@@ -2124,6 +2125,27 @@ public class FMRadioService extends Service
         misAnalogPathEnabled = analogMode;
         return true;
    }
+   private boolean waitForEvent() {
+       boolean status = false;
+
+       synchronized (mEventWaitLock) {
+           Log.d(LOGTAG, "waiting for event");
+           try {
+               if (mEventReceived == false)
+                   mEventWaitLock.wait(RADIO_TIMEOUT);
+               if (mEventReceived == true)
+                   status = true;
+           } 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();
+           }
+       }
+       return status;
+   }
+
   /*
    * Turn ON FM: Powers up FM hardware, and initializes the FM module
    *                                                                                 .
@@ -2165,7 +2187,12 @@ public class FMRadioService extends Service
             Log.d(LOGTAG, "fmOn: RdsStd      :"+ config.getRdsStd());
             Log.d(LOGTAG, "fmOn: LowerLimit  :"+ config.getLowerLimit());
             Log.d(LOGTAG, "fmOn: UpperLimit  :"+ config.getUpperLimit());
+            mEventReceived = false;
             bStatus = mReceiver.enable(FmSharedPreferences.getFMConfiguration(), this);
+
+            if (mReceiver.isCherokeeChip()) {
+                bStatus = waitForEvent();
+            }
             if (isSpeakerEnabled()) {
                 setAudioPath(false);
             } else {
@@ -2325,21 +2352,6 @@ public class FMRadioService extends Service
       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();
@@ -2362,21 +2374,11 @@ public class FMRadioService extends Service
       // This will disable the FM radio device
       if (mReceiver != null)
       {
+         mEventReceived = false;
          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();
-                 }
-             }
+             bStatus = waitForEvent();
          }
          mReceiver = null;
       }
@@ -3125,14 +3127,23 @@ public class FMRadioService extends Service
       public void FmRxEvEnableReceiver() {
          Log.d(LOGTAG, "FmRxEvEnableReceiver");
          mReceiver.setRawRdsGrpMask();
+         if (mReceiver != null && mReceiver.isCherokeeChip()) {
+             synchronized(mEventWaitLock) {
+                 mEventReceived = true;
+                 mEventWaitLock.notify();
+             }
+         }
       }
       public void FmRxEvDisableReceiver()
       {
          Log.d(LOGTAG, "FmRxEvDisableReceiver");
          mFMOn = false;
          FmSharedPreferences.clearTags();
-         synchronized (mEventWaitLock) {
-             mEventWaitLock.notify();
+         if (mReceiver != null && mReceiver.isCherokeeChip()) {
+             synchronized (mEventWaitLock) {
+                 mEventReceived = true;
+                 mEventWaitLock.notify();
+             }
          }
       }
       public void FmRxEvRadioReset()
-- 
GitLab