FM: Fix the audio noise issue

- When wired headset removed while FM is playing,
  audio will route to speaker and it will be noise.
- Listen the ACTION_AUDIO_BECOMING_NOISY and disable the FM.

Change-Id: I3c6f53993cfaecf0459c655268a37cf8c3e91b43
CRs-Fixed: 643575
diff --git a/fmapp2/AndroidManifest.xml b/fmapp2/AndroidManifest.xml
index aeb3a1c..a0ed3ca 100644
--- a/fmapp2/AndroidManifest.xml
+++ b/fmapp2/AndroidManifest.xml
@@ -74,6 +74,7 @@
         <receiver android:name="com.caf.fmradio.FMMediaButtonIntentReceiver">
             <intent-filter>
                  <action android:name="android.intent.action.MEDIA_BUTTON" />
+                 <action android:name="android.media.AUDIO_BECOMING_NOISY" />
             </intent-filter>
         </receiver>
 
diff --git a/fmapp2/src/com/caf/fmradio/FMMediaButtonIntentReceiver.java b/fmapp2/src/com/caf/fmradio/FMMediaButtonIntentReceiver.java
index 9bc4d77..949453f 100644
--- a/fmapp2/src/com/caf/fmradio/FMMediaButtonIntentReceiver.java
+++ b/fmapp2/src/com/caf/fmradio/FMMediaButtonIntentReceiver.java
@@ -36,6 +36,7 @@
 import android.content.pm.PackageManager;
 import android.content.Context;
 import android.content.ComponentName;
+import android.media.AudioManager;
 import android.util.Log;
 import android.view.KeyEvent;
 import android.os.Bundle;
@@ -45,9 +46,14 @@
 
 private static final String TAG = "FMMediaButtonIntentReceiver";
 public static final String FM_MEDIA_BUTTON = "com.caf.fmradio.action.MEDIA_BUTTON";
+public static final String AUDIO_BECOMING_NOISY = "com.caf.fmradio.action.AUDIO_BECOMING_NOISY";
 public void onReceive(Context context, Intent intent) {
-        String action = intent.getAction();
-       if ((action != null) && action.equals("android.intent.action.MEDIA_BUTTON")) {
+       String action = intent.getAction();
+       if ((action != null) && action.equals(AudioManager.ACTION_AUDIO_BECOMING_NOISY)) {
+           Log.d(TAG, "ACTION_AUDIO_BECOMING_NOISY intent received for ACTION_HEADSET_PLUG");
+           Intent i = new Intent(AUDIO_BECOMING_NOISY);
+           context.sendBroadcast(i);
+       } else if ((action != null) && action.equals("android.intent.action.MEDIA_BUTTON")) {
            KeyEvent event = (KeyEvent)
                     intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
 
diff --git a/fmapp2/src/com/caf/fmradio/FMRadio.java b/fmapp2/src/com/caf/fmradio/FMRadio.java
index 9d48888..5f4b6dd 100644
--- a/fmapp2/src/com/caf/fmradio/FMRadio.java
+++ b/fmapp2/src/com/caf/fmradio/FMRadio.java
@@ -2046,11 +2046,9 @@
             String display = "";
             if (station != null) {
                display = station.getName();
-                Log.e(LOGTAG, "Venkat:before" + display );
                 if (display.length() > 6)
-		 display = display.substring(0,6)+"...";
-	       Log.e(LOGTAG, "Venkat: after" + display );
-		mPresetButtons[buttonIndex].setEllipsize(TextUtils.TruncateAt.END);
+                    display = display.substring(0,6)+"...";
+	       mPresetButtons[buttonIndex].setEllipsize(TextUtils.TruncateAt.END);
                mPresetButtons[buttonIndex].setText(display);
                mPresetButtons[buttonIndex].setTag(station);
                mPresetButtons[buttonIndex].setHeight(-1);
diff --git a/fmapp2/src/com/caf/fmradio/FMRadioService.java b/fmapp2/src/com/caf/fmradio/FMRadioService.java
index a218893..9e9cb74 100644
--- a/fmapp2/src/com/caf/fmradio/FMRadioService.java
+++ b/fmapp2/src/com/caf/fmradio/FMRadioService.java
@@ -106,6 +106,7 @@
    private boolean mSleepActive = false;
    private BroadcastReceiver mRecordTimeoutListener = null;
    private BroadcastReceiver mDelayedServiceStopListener = null;
+   private BroadcastReceiver mAudioBecomeNoisyListener = null;
    private boolean mOverA2DP = false;
    private BroadcastReceiver mFmMediaButtonListener;
    private IFMRadioServiceCallbacks mCallbacks;
@@ -204,6 +205,7 @@
       // registering media button receiver seperately as we need to set
       // different priority for receiving media events
       registerFmMediaButtonReceiver();
+      registerAudioBecomeNoisy();
       if ( false == SystemProperties.getBoolean("ro.fm.mulinst.recording.support",true)) {
            mSingleRecordingInstanceSupported = true;
       }
@@ -257,6 +259,10 @@
           unregisterReceiver(mFmMediaButtonListener);
           mFmMediaButtonListener = null;
       }
+      if (mAudioBecomeNoisyListener != null) {
+          unregisterReceiver(mAudioBecomeNoisyListener);
+          mAudioBecomeNoisyListener = null;
+      }
       if (mSleepExpiredListener != null ) {
           unregisterReceiver(mSleepExpiredListener);
           mSleepExpiredListener = null;
@@ -505,6 +511,40 @@
          }
      }
 
+    public void registerAudioBecomeNoisy() {
+        if (mAudioBecomeNoisyListener == null) {
+            mAudioBecomeNoisyListener = new BroadcastReceiver() {
+                @Override
+                public void onReceive(Context context, Intent intent) {
+                    Log.d(LOGTAG, "FMMediaButtonIntentReceiver.AUDIO_BECOMING_NOISY");
+                    String intentAction = intent.getAction();
+                    if (FMMediaButtonIntentReceiver.AUDIO_BECOMING_NOISY.equals(intentAction)) {
+                       if (isFmOn())
+                       {
+                           /* Disable FM and let the UI know */
+                           fmOff();
+                           try
+                           {
+                              /* Notify the UI/Activity, only if the service is "bound"
+                              by an activity and if Callbacks are registered
+                              */
+                              if((mServiceInUse) && (mCallbacks != null) )
+                              {
+                                  mCallbacks.onDisabled();
+                              }
+                           } catch (RemoteException e)
+                           {
+                               e.printStackTrace();
+                           }
+                       }
+                    }
+                }
+            };
+            IntentFilter intentFilter = new IntentFilter(FMMediaButtonIntentReceiver.AUDIO_BECOMING_NOISY);
+            registerReceiver(mAudioBecomeNoisyListener, intentFilter);
+        }
+    }
+
     public void registerMusicServiceCommandReceiver() {
         if (mMusicCommandListener == null) {
             mMusicCommandListener = new BroadcastReceiver() {
@@ -599,6 +639,8 @@
             /* Update the UI based on the state change of the headset/antenna*/
             if(!isAntennaAvailable())
             {
+                if (!isFmOn())
+                    return;
                 /* Disable FM and let the UI know */
                 fmOff();
                 try