Merge "FM:Setup default audio encoder parameters."
diff --git a/fmapp2/AndroidManifest.xml b/fmapp2/AndroidManifest.xml
index c1e3217..92af7e3 100644
--- a/fmapp2/AndroidManifest.xml
+++ b/fmapp2/AndroidManifest.xml
@@ -87,36 +87,6 @@
 
         </activity>
 
-        <activity android:icon="@drawable/ic_launcher_fm_tx"
-            android:name=".FMTransmitterActivity"
-            android:label="@string/tx_app_name"
-            android:clearTaskOnLaunch="true"
-            android:taskAffinity=""
-            android:excludeFromRecents="false">
-
-            <intent-filter>
-                <action android:name="android.intent.action.MAIN" />
-                <category android:name="android.intent.category.LAUNCHER" />
-            </intent-filter>
-
-            <intent-filter>
-                <action android:name="com.caf.fmradio.FMTRANSMITTER_ACTIVITY" />
-                <category android:name="android.intent.category.DEFAULT" />
-            </intent-filter>
-
-        </activity>
-
-        <receiver android:name="FMTransmitterConfigReceiver">
-
-            <intent-filter>
-                <action android:name="android.intent.action.BOOT_COMPLETED" />
-            </intent-filter>
-
-        </receiver>
-
-        <service android:name=".FMTransmitterService"
-            android:exported="true" />
-
         <activity android:name=".FMStats"
             android:label="@string/test_menu">
 
diff --git a/fmapp2/src/com/caf/fmradio/FMRadioService.java b/fmapp2/src/com/caf/fmradio/FMRadioService.java
index 171c86b..c8a5970 100644
--- a/fmapp2/src/com/caf/fmradio/FMRadioService.java
+++ b/fmapp2/src/com/caf/fmradio/FMRadioService.java
@@ -188,11 +188,7 @@
    public static final String ACTION_FM_RECORDING_STATUS =
            "codeaurora.intent.action.FM.Recording.Status";
    private BroadcastReceiver mFmRecordingStatus  = null;
-   static final int RECORD_START = 1;
-   static final int RECORD_STOP = 0;
    private Thread mRecordServiceCheckThread = null;
-   private boolean mUnMuteOnFocusLoss = false;
-   private boolean mSpeakerOnFocusLoss = false;
    private MediaSession mSession;
    private boolean mIsSSRInProgress = false;
    private boolean mIsSSRInProgressFromActivity = false;
@@ -553,7 +549,9 @@
                        // audio policy manager switch audio to speaker.
                        mHandler.removeCallbacks(mHeadsetPluginHandler);
                        mHandler.post(mHeadsetPluginHandler);
-                    } else if(mA2dpDeviceState.isA2dpStateChange(action) ) {
+                    } else if(mA2dpDeviceState.isA2dpStateChange(action) &&
+                             (mA2dpDeviceState.isConnected(intent) ||
+                              mA2dpDeviceState.isDisconnected(intent))) {
                         boolean  bA2dpConnected =
                         mA2dpDeviceState.isConnected(intent);
                         Log.d(LOGTAG, "bA2dpConnected:" +bA2dpConnected);
@@ -909,23 +907,6 @@
       return myProcessName;
    }
 
-   private void sendRecordIntent(int action) {
-       Intent intent = new Intent(ACTION_FM_RECORDING);
-       intent.putExtra("state", action);
-       if(action == RECORD_START) {
-          int mRecordDuration = -1;
-          if(FmSharedPreferences.getRecordDuration() !=
-             FmSharedPreferences.RECORD_DUR_INDEX_3_VAL) {
-             mRecordDuration = (FmSharedPreferences.getRecordDuration() * 60 * 1000);
-          }
-          intent.putExtra("record_duration", mRecordDuration);
-          intent.putExtra("process_name", getProcessName());
-          intent.putExtra("process_id", Process.myPid());
-        }
-       Log.d(LOGTAG, "Sending Recording intent for = " +action);
-       getApplicationContext().sendBroadcast(intent);
-   }
-
    private void sendRecordServiceIntent(int action) {
        Intent intent = new Intent(ACTION_FM);
        intent.putExtra("state", action);
@@ -1073,8 +1054,6 @@
        }
        startRecordSink();
        mPlaybackInProgress = true;
-       mUnMuteOnFocusLoss = false;
-       mSpeakerOnFocusLoss = false;
    }
 
    private void stopFM(){
@@ -1106,6 +1085,8 @@
    }
 
    public boolean startRecording() {
+      int mRecordDuration = -1;
+
       Log.d(LOGTAG, "In startRecording of Recorder");
       if((true == mSingleRecordingInstanceSupported) &&
          (true == mOverA2DP )) {
@@ -1124,9 +1105,16 @@
         if (!updateAndShowStorageHint())
             return false;
         long maxFileSize = mStorageSpace - LOW_STORAGE_THRESHOLD;
+        if(FmSharedPreferences.getRecordDuration() !=
+            FmSharedPreferences.RECORD_DUR_INDEX_3_VAL) {
+            mRecordDuration = (FmSharedPreferences.getRecordDuration() * 60 * 1000);
+         }
+
         mRecorder = new MediaRecorder();
         try {
               mRecorder.setMaxFileSize(maxFileSize);
+              if (mRecordDuration >= 0)
+                  mRecorder.setMaxDuration(mRecordDuration);
         } catch (RuntimeException exception) {
 
         }
@@ -1187,9 +1175,10 @@
                          Log.d(LOGTAG, "Maximum file size/duration reached, stop the recording");
                          stopRecording();
                      }
-                     // Show the toast.
-                     Toast.makeText(FMRadioService.this, R.string.FMRecording_reach_size_limit,
-                               Toast.LENGTH_LONG).show();
+                     if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED)
+                         // Show the toast.
+                         Toast.makeText(FMRadioService.this, R.string.FMRecording_reach_size_limit,
+                                        Toast.LENGTH_LONG).show();
                  }
              }
              // from MediaRecorder.OnErrorListener
@@ -1371,14 +1360,7 @@
 
        if((TelephonyManager.CALL_STATE_OFFHOOK == state)||
           (TelephonyManager.CALL_STATE_RINGING == state)) {
-           boolean bTempSpeaker = (mSpeakerPhoneOn | mSpeakerOnFocusLoss) ; //need to restore SpeakerPhone
-           if (mUnMuteOnFocusLoss) {
-               if (audioManager != null) {
-                   Log.d(LOGTAG, "Mute");
-                   mMuted = true;
-                   audioManager.setStreamMute(AudioManager.STREAM_MUSIC,true);
-               }
-           }
+           boolean bTempSpeaker = mSpeakerPhoneOn ; //need to restore SpeakerPhone
            boolean bTempMute = mMuted;// need to restore Mute status
            int bTempCall = mCallStatus;//need to restore call status
            if (isFmOn() && fmOff()) {
@@ -1511,23 +1493,10 @@
                       Log.v(LOGTAG, "AudioFocus: received AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
                   case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                       Log.v(LOGTAG, "AudioFocus: received AUDIOFOCUS_LOSS_TRANSIENT");
-                      if (mSpeakerPhoneOn) {
-                         mSpeakerDisableHandler.removeCallbacks(mSpeakerDisableTask);
-                         mSpeakerDisableHandler.postDelayed(mSpeakerDisableTask, 0);
-                         mSpeakerOnFocusLoss = true;
-                      }
                       if (true == mPlaybackInProgress) {
-                          if(mMuted) {
-                             unMute();
-                             mUnMuteOnFocusLoss = true;
-                          }
                           stopFM();
+                          mStoppedOnFocusLoss = true;
                       }
-                      if (mSpeakerPhoneOn) {
-                          if (isAnalogModeSupported())
-                              setAudioPath(false);
-                      }
-                      mStoppedOnFocusLoss = true;
                       break;
                   case AudioManager.AUDIOFOCUS_LOSS:
                       Log.v(LOGTAG, "AudioFocus: received AUDIOFOCUS_LOSS");
@@ -2158,7 +2127,6 @@
          Log.d(LOGTAG, "audioManager.setFmRadioOn false done \n" );
       }
 
-      sendRecordServiceIntent(RECORD_STOP);
       if (isAnalogModeEnabled()) {
               SystemProperties.set("hw.fm.isAnalog","false");
               misAnalogPathEnabled = false;
diff --git a/fmapp2/src/com/caf/fmradio/FMTransmitterConfigReceiver.java b/fmapp2/src/com/caf/fmradio/FMTransmitterConfigReceiver.java
deleted file mode 100644
index b20a57d..0000000
--- a/fmapp2/src/com/caf/fmradio/FMTransmitterConfigReceiver.java
+++ /dev/null
@@ -1,87 +0,0 @@
-/*
- * Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *        * Redistributions of source code must retain the above copyright
- *            notice, this list of conditions and the following disclaimer.
- *        * Redistributions in binary form must reproduce the above copyright
- *            notice, this list of conditions and the following disclaimer in the
- *            documentation and/or other materials provided with the distribution.
- *        * Neither the name of The Linux Foundation nor
- *            the names of its contributors may be used to endorse or promote
- *            products derived from this software without specific prior written
- *            permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NON-INFRINGEMENT ARE DISCLAIMED.    IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package com.caf.fmradio;
-
-import android.content.Intent;
-import android.content.BroadcastReceiver;
-import android.content.pm.PackageManager;
-import android.content.Context;
-import android.content.ComponentName;
-import android.util.Log;
-import android.os.SystemProperties;
-import java.io.FileReader;
-import java.io.File;
-import java.lang.String;
-
-
-public class FMTransmitterConfigReceiver extends BroadcastReceiver {
-
-    private static FileReader socinfo_fd;
-    private static char[] socinfo = new char[20];
-    private static String build_id = "1";
-
-    private static final String TAG = "FMFolderConfigReceiver";
-    @Override
-    public void onReceive(Context context, Intent intent) {
-        String action = intent.getAction();
-        Log.d(TAG, "Received intent: " + action);
-        if((action != null) && action.equals("android.intent.action.BOOT_COMPLETED")) {
-            Log.d(TAG, "boot complete intent received");
-            boolean isFmTransmitterSupported = SystemProperties.getBoolean("ro.fm.transmitter",true);
-
-            if ("msm7630_surf".equals(SystemProperties.get("ro.board.platform"))) {
-                Log.d(TAG,"this is msm7630_surf");
-                try {
-                    File f = new File("/sys/devices/soc0/build_id");
-                    if (f.exists()) {
-                        socinfo_fd = new FileReader("/sys/devices/soc0/build_id");
-                    } else {
-                        socinfo_fd = new FileReader("/sys/devices/system/soc/soc0/build_id");
-                    }
-                    socinfo_fd.read(socinfo,0,20);
-                    socinfo_fd.close();
-                } catch(Exception e) {
-                    Log.e(TAG,"Exception in FileReader");
-                }
-                Log.d(TAG, "socinfo=" +socinfo);
-                build_id = new String(socinfo,17,1);
-                Log.d(TAG, "build_id=" +build_id);
-            }
-            if ((!isFmTransmitterSupported) || (build_id.equals("0"))) {
-            PackageManager pManager = context.getPackageManager();
-               if (pManager != null) {
-                   Log.d(TAG, "disableing the FM Transmitter");
-                   ComponentName fmTransmitter = new ComponentName("com.caf.fmradio", "com.caf.fmradio.FMTransmitterActivity");
-                   pManager.setComponentEnabledSetting(fmTransmitter, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
-                                                    PackageManager.DONT_KILL_APP);
-               }
-           }
-        }
-   }
-}
diff --git a/fmapp2/src/com/caf/hc_utils/A2dpDeviceStatus.java b/fmapp2/src/com/caf/hc_utils/A2dpDeviceStatus.java
index 45b647e..6b58f9e 100644
--- a/fmapp2/src/com/caf/hc_utils/A2dpDeviceStatus.java
+++ b/fmapp2/src/com/caf/hc_utils/A2dpDeviceStatus.java
@@ -65,6 +65,15 @@
         }
         return isConnected;
     }
+    public boolean isDisconnected(Intent intent) {
+        boolean isDisconnected = false;
+        int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE,
+                                BluetoothA2dp.STATE_CONNECTED);
+        if((state == BluetoothA2dp.STATE_DISCONNECTED)) {
+            isDisconnected = true;
+        }
+        return isDisconnected;
+    }
     public boolean isPlaying(Intent intent) {
         boolean isPlaying = false;
         int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE,