Promotion of fm.lnx.2.1-00028.

CRs      Change ID                                   Subject
--------------------------------------------------------------------------------------------------------------
1109351   I42a15fea24ed26bfe7460a4611bca1a7620de4e1   pause cmd behaves like audiofocus loss
1112971   Ieea88675bf2fbe46fe18539033f340a2f8bb4d2f   Change rename_dialog background color to default
2005546   Ic3630f7bdd39ca812d9ae81b45a9c496049aef84   FM Radio: Wait for disable to complete
1110621   I729ac3e92512ff43368494ecde87d3d9f8dfe2c6   Don't resume after call ends until gain audio focus

Change-Id: Iafa0f2bf0a4d7df744fe0983129871c7dfd157b0
CRs-Fixed: 2005546, 1112971, 1110621, 1109351
diff --git a/fm_hci/fm_hci.c b/fm_hci/fm_hci.c
index c48edd5..378e0c9 100644
--- a/fm_hci/fm_hci.c
+++ b/fm_hci/fm_hci.c
@@ -677,6 +677,12 @@
         return;
     }
     event_notification(hci, HC_EVENT_EXIT);
+    pthread_mutex_lock(&hci->event_lock);
+again:
+    pthread_cond_wait(&hci->event_cond, &hci->event_lock);
+    if (!(ready_events & HC_EVENT_EXIT_DONE))
+        goto again;
+    pthread_mutex_unlock(&hci->event_lock);
 }
 
 int fm_hci_init(fm_hci_hal_t *hci_hal)
@@ -785,6 +791,7 @@
     vendor_close(hci);
     pthread_cond_broadcast(&hci->event_cond);
     pthread_cond_broadcast(&hci->cmd_credits_cond);
+    event_notification(hci, HC_EVENT_EXIT_DONE);
     stop_rx_thread(hci);
     stop_tx_thread(hci);
     ALOGD("Tx, Rx Threads join done");
diff --git a/fm_hci/fm_hci.h b/fm_hci/fm_hci.h
index 8818068..2cfb30e 100644
--- a/fm_hci/fm_hci.h
+++ b/fm_hci/fm_hci.h
@@ -46,6 +46,7 @@
 #define HC_EVENT_LPM_IDLE_TIMEOUT      0x0100
 #define HC_EVENT_EXIT                  0x0200
 #define HC_EVENT_EPILOG                0x0400
+#define HC_EVENT_EXIT_DONE             0x8000
 
 #define MAX_FM_CMD_CNT                 100
 #define FM_CMD                         0x11
diff --git a/fmapp2/res/layout/rename_dialog.xml b/fmapp2/res/layout/rename_dialog.xml
index 2374c9c..bef5432 100644
--- a/fmapp2/res/layout/rename_dialog.xml
+++ b/fmapp2/res/layout/rename_dialog.xml
@@ -40,7 +40,6 @@
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:inputType="text"
-        android:textColor="#FFF"
         android:singleLine="true"
         android:maxLength="16">
      <requestFocus />
diff --git a/fmapp2/src/com/caf/fmradio/FMRadioService.java b/fmapp2/src/com/caf/fmradio/FMRadioService.java
index 7f6775b..a178548 100644
--- a/fmapp2/src/com/caf/fmradio/FMRadioService.java
+++ b/fmapp2/src/com/caf/fmradio/FMRadioService.java
@@ -156,7 +156,7 @@
    private File mA2DPSampleFile = null;
    //Track FM playback for reenter App usecases
    private boolean mPlaybackInProgress = false;
-   private boolean mStoppedOnFocusLoss = false;
+   private boolean mStoppedOnFocusLoss = true;
    private boolean mStoppedOnFactoryReset = false;
    private File mSampleFile = null;
    long mSampleStart = 0;
@@ -742,8 +742,10 @@
                         Log.d(LOGTAG, "Music Service command : "+cmd+ " received");
                         if (cmd != null && cmd.equals("pause")) {
                             if (isFmOn()) {
-                                fmOperationsOff();
-                                mStoppedOnFocusLoss = true;
+                                AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
+                                audioManager.abandonAudioFocus(mAudioFocusListener);
+                                mDelayedStopHandler.obtainMessage(FOCUSCHANGE, AudioManager.AUDIOFOCUS_LOSS, 0).sendToTarget();
+
                                 if (isOrderedBroadcast()) {
                                     abortBroadcast();
                                 }
@@ -1053,12 +1055,14 @@
            return;
        }
 
-       AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
-       int granted = audioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC,
-              AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
-       if(granted != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
-          Log.d(LOGTAG, "audio focuss couldnot be granted");
-          return;
+       if (mStoppedOnFocusLoss) {
+           AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
+           int granted = audioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC,
+                   AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
+           if (granted != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
+               Log.d(LOGTAG, "audio focuss couldnot be granted");
+               return;
+           }
        }
        mSession.setActive(true);
 
@@ -1442,6 +1446,32 @@
        resolver.insert(uri, values);
    }
 
+    private void resumeAfterCall() {
+        if (getCallState() != TelephonyManager.CALL_STATE_IDLE)
+            return;
+
+        // start playing again
+        if (!mResumeAfterCall)
+            return;
+
+        // resume playback only if FM Radio was playing
+        // when the call was answered
+        if (isAntennaAvailable() && (!isFmOn()) && mServiceInUse) {
+            Log.d(LOGTAG, "Resuming after call:");
+            if(!fmOn()) {
+                return;
+            }
+            mResumeAfterCall = false;
+            if (mCallbacks != null) {
+                try {
+                    mCallbacks.onEnabled();
+                } catch (RemoteException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+    }
+
    private void fmActionOnCallState( int state ) {
    //if Call Status is non IDLE we need to Mute FM as well stop recording if
    //any. Similarly once call is ended FM should be unmuted.
@@ -1473,49 +1503,6 @@
                mMuted = bTempMute;
            }
        }
-       else if (state == TelephonyManager.CALL_STATE_IDLE) {
-          // start playing again
-          if (mResumeAfterCall)
-          {
-             // resume playback only if FM Radio was playing
-             // when the call was answered
-              if (isAntennaAvailable() && (!isFmOn()) && mServiceInUse)
-              {
-                   Log.d(LOGTAG, "Resuming after call:");
-                   do {
-                       granted = audioManager.requestAudioFocus(mAudioFocusListener,
-                               AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
-                       ++count;
-                       try {
-                           Thread.sleep(100);
-                       } catch (Exception ex) {
-                           Log.d( LOGTAG, "InterruptedException");
-                       }
-                   } while(granted != AudioManager.AUDIOFOCUS_REQUEST_GRANTED && count != 3);
-
-                   if(true != fmOn()) {
-                       return;
-                   }
-
-                   mResumeAfterCall = false;
-                   if(mCallbacks != null) {
-                      try {
-                           mCallbacks.onEnabled();
-                      } catch (RemoteException e) {
-                           e.printStackTrace();
-                      }
-                   }
-              }
-          } else {
-              if (!isFmOn() && (mServiceInUse) && (mCallbacks != null)) {
-                  try {
-                      mCallbacks.onDisabled();
-                  } catch (RemoteException e) {
-                      e.printStackTrace();
-                  }
-              }
-          }
-       }//idle
    }
 
     /* Handle Phone Call + FM Concurrency */
@@ -1590,7 +1577,7 @@
               stopRecording();
               break;
           case FOCUSCHANGE:
-              if( false == isFmOn() ) {
+              if( !isFmOn() && !mResumeAfterCall) {
                   Log.v(LOGTAG, "FM is not running, not handling change");
                   return;
               }
@@ -1604,8 +1591,8 @@
                       Log.v(LOGTAG, "AudioFocus: received AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
                       if (true == mPlaybackInProgress) {
                           stopFM();
-                          mStoppedOnFocusLoss = true;
                       }
+                      mStoppedOnFocusLoss = true;
                       break;
                   case AudioManager.AUDIOFOCUS_LOSS:
                       Log.v(LOGTAG, "AudioFocus: received AUDIOFOCUS_LOSS");
@@ -1631,9 +1618,14 @@
                       break;
                   case AudioManager.AUDIOFOCUS_GAIN:
                       Log.v(LOGTAG, "AudioFocus: received AUDIOFOCUS_GAIN");
+                      mStoppedOnFocusLoss = false;
+                      if (mResumeAfterCall) {
+                          Log.v(LOGTAG, "resumeAfterCall");
+                          resumeAfterCall();
+                          break;
+                      }
                       if(false == mPlaybackInProgress)
                           startFM();
-                      mStoppedOnFocusLoss = false;
                       mSession.setActive(true);
                       break;
                   default:
@@ -2263,7 +2255,12 @@
          Log.d(LOGTAG, "audioManager.setFmRadioOn = false \n" );
          stopFM();
          unMute();
-         audioManager.abandonAudioFocus(mAudioFocusListener);
+         // If call is active, we will use audio focus to resume fm after call ends.
+         // So don't abandon audiofocus automatically
+         if (getCallState() == TelephonyManager.CALL_STATE_IDLE) {
+             audioManager.abandonAudioFocus(mAudioFocusListener);
+             mStoppedOnFocusLoss = true;
+         }
          //audioManager.setParameters("FMRadioOn=false");
          Log.d(LOGTAG, "audioManager.setFmRadioOn false done \n" );
       }
@@ -3832,7 +3829,7 @@
    }
    private void requestFocus() {
       if( (false == mPlaybackInProgress) &&
-          (true  == mStoppedOnFocusLoss) ) {
+          (true  == mStoppedOnFocusLoss) && isFmOn()) {
            // adding code for audio focus gain.
            AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            audioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC,