Merge "FM: Remove FM transmitter functionality"
diff --git a/fmapp2/res/values/customize.xml b/fmapp2/res/values/customize.xml
index f24c67c..f4dcdbc 100644
--- a/fmapp2/res/values/customize.xml
+++ b/fmapp2/res/values/customize.xml
@@ -50,4 +50,19 @@
     -->
     <bool name="def_only_stereo_enabled">false</bool>
 
+    <!-- enabled the name format of recordings files or not, default value is false -->
+    <bool name="def_save_name_format_enabled">false</bool>
+
+    <!-- the prefix of recordings files name, file name like this "FM-yyyy-MM-dd-HH-mm-ss.3gpp" -->
+    <string name="def_save_name_prefix" translatable="false">FM</string>
+
+    <!-- the suffix of recordings files, default value is ".3gpp" -->
+    <string name="def_save_name_suffix" translatable="false">.3gpp</string>
+
+    <!-- the name format of recordings files -->
+    <string name="def_save_name_format" translatable="false">yyyy-MM-dd-HH-mm-ss</string>
+
+    <!-- the save path of recordings files -->
+    <string name="def_fmRecord_savePath" translatable="false"></string>
+
 </resources>
diff --git a/fmapp2/src/com/caf/fmradio/FMRadio.java b/fmapp2/src/com/caf/fmradio/FMRadio.java
index 20b1133..909a14c 100644
--- a/fmapp2/src/com/caf/fmradio/FMRadio.java
+++ b/fmapp2/src/com/caf/fmradio/FMRadio.java
@@ -1679,7 +1679,6 @@
 
    private void disableRadio() {
       boolean bStatus = false;
-      boolean bSpeakerPhoneOn = isSpeakerEnabled();
 
       cancelSearch();
       endSleepTimer();
@@ -1698,11 +1697,6 @@
             }
 
             enableRadioOnOffUI();
-            // restore default wired headset on FM power off
-            if (bSpeakerPhoneOn) {
-               mService.enableSpeaker(false);
-               mSpeakerButton.setImageResource(R.drawable.btn_earphone);
-            }
          }catch (RemoteException e) {
             e.printStackTrace();
          }
@@ -3197,6 +3191,16 @@
           Log.d(LOGTAG, "mServiceCallbacks.onA2DPConnectionstateChanged :");
           A2DPConnectionState(state);
       }
+      public void onFmAudioPathStarted() {
+          Log.d(LOGTAG, "mServiceCallbacks.onFmAudioPathStarted:");
+          mSpeakerButton.setClickable(true);
+          mMuteButton.setClickable(true);
+      }
+      public void onFmAudioPathStopped() {
+          Log.d(LOGTAG, "mServiceCallbacks.onFmAudioPathStopped:");
+          mSpeakerButton.setClickable(false);
+          mMuteButton.setClickable(false);
+      }
    };
 
     private void registerFMSettingListner() {
diff --git a/fmapp2/src/com/caf/fmradio/FMRadioService.java b/fmapp2/src/com/caf/fmradio/FMRadioService.java
index 188a068..a4fce5d 100644
--- a/fmapp2/src/com/caf/fmradio/FMRadioService.java
+++ b/fmapp2/src/com/caf/fmradio/FMRadioService.java
@@ -967,7 +967,8 @@
                     if (isFmOn() && getResources()
                             .getBoolean(R.bool.def_headset_next_enabled)) {
                         try {
-                            mCallbacks.onSeekNextStation();
+                            if ((mServiceInUse) && (mCallbacks != null))
+                                mCallbacks.onSeekNextStation();
                         }catch (RemoteException e) {
                         }
                     }
@@ -1012,7 +1013,7 @@
         }
    };
 
-   private void startFM(){
+   private void startFM() {
        Log.d(LOGTAG, "In startFM");
        if(true == mAppShutdown) { // not to send intent to AudioManager in Shutdown
            return;
@@ -1065,12 +1066,24 @@
        }
        mPlaybackInProgress = true;
        configureAudioDataPath(true);
+       try {
+           if ((mServiceInUse) && (mCallbacks != null))
+               mCallbacks.onFmAudioPathStarted();
+       } catch(RemoteException e) {
+           e.printStackTrace();
+       }
    }
 
-   private void stopFM(){
+   private void stopFM() {
        Log.d(LOGTAG, "In stopFM");
        configureAudioDataPath(false);
        mPlaybackInProgress = false;
+       try {
+           if ((mServiceInUse) && (mCallbacks != null))
+               mCallbacks.onFmAudioPathStopped();
+       } catch(RemoteException e) {
+           e.printStackTrace();
+       }
    }
 
    private void resetFM(){
@@ -1094,6 +1107,32 @@
        return status;
    }
 
+    private File createTempFile(String prefix, String suffix, File directory)
+            throws IOException {
+        // Force a prefix null check first
+        if (prefix.length() < 3) {
+            throw new IllegalArgumentException("prefix must be at least 3 characters");
+        }
+        if (suffix == null) {
+            suffix = ".tmp";
+        }
+        File tmpDirFile = directory;
+        if (tmpDirFile == null) {
+            String tmpDir = System.getProperty("java.io.tmpdir", ".");
+            tmpDirFile = new File(tmpDir);
+        }
+
+        String nameFormat = getResources().getString(R.string.def_save_name_format);
+        SimpleDateFormat df = new SimpleDateFormat(nameFormat);
+        String currentTime = df.format(System.currentTimeMillis());
+
+        File result;
+        do {
+            result = new File(tmpDirFile, prefix + currentTime + suffix);
+        } while (!result.createNewFile());
+        return result;
+   }
+
    public boolean startRecording() {
       int mRecordDuration = -1;
 
@@ -1137,12 +1176,27 @@
         }
 
         mSampleFile = null;
-        File sampleDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/FMRecording");
+        File sampleDir = null;
+        if (!"".equals(getResources().getString(R.string.def_fmRecord_savePath))) {
+            String fmRecordSavePath = getResources().getString(R.string.def_fmRecord_savePath);
+            sampleDir = new File(Environment.getExternalStorageDirectory().toString()
+                    + fmRecordSavePath);
+        } else {
+            sampleDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
+                    + "/FMRecording");
+        }
+
         if(!(sampleDir.mkdirs() || sampleDir.isDirectory()))
             return false;
         try {
-            mSampleFile = File
-                    .createTempFile("FMRecording", ".3gpp", sampleDir);
+            if (getResources().getBoolean(R.bool.def_save_name_format_enabled)) {
+                String suffix = getResources().getString(R.string.def_save_name_suffix);
+                suffix = "".equals(suffix) ? ".3gpp" : suffix;
+                String prefix = getResources().getString(R.string.def_save_name_prefix) + '-';
+                mSampleFile = createTempFile(prefix, suffix, sampleDir);
+            } else {
+                mSampleFile = File.createTempFile("FMRecording", ".3gpp", sampleDir);
+            }
         } catch (IOException e) {
             Log.e(LOGTAG, "Not able to access SD Card");
             Toast.makeText(this, "Not able to access SD Card", Toast.LENGTH_SHORT).show();
@@ -2127,6 +2181,7 @@
    * Turn OFF FM Operations: This disables all the current FM operations             .
    */
    private void fmOperationsOff() {
+      // stop recording
       if (isFmRecordingOn())
       {
           stopRecording();
@@ -2137,14 +2192,19 @@
                return;
           }
       }
+      // disable audio path
       AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
       if(audioManager != null)
       {
          Log.d(LOGTAG, "audioManager.setFmRadioOn = false \n" );
          stopFM();
-         //audioManager.setParameters("FMRadioOn=false");
          Log.d(LOGTAG, "audioManager.setFmRadioOn false done \n" );
       }
+      // reset FM audio settings
+      if (isSpeakerEnabled() == true)
+          enableSpeaker(false);
+      if (isMuted() == true)
+          unMute();
 
       if (isAnalogModeEnabled()) {
               SystemProperties.set("hw.fm.isAnalog","false");
diff --git a/fmapp2/src/com/caf/fmradio/FMStats.java b/fmapp2/src/com/caf/fmradio/FMStats.java
index 961f04b..c035475 100644
--- a/fmapp2/src/com/caf/fmradio/FMStats.java
+++ b/fmapp2/src/com/caf/fmradio/FMStats.java
@@ -2719,6 +2719,12 @@
           public void onA2DPConnectionstateChanged(boolean state){
              Log.d(LOGTAG, "mServiceCallbacks.onA2DPConnectionstateChanged :");
           }
+          public void onFmAudioPathStarted() {
+             Log.d(LOGTAG, "mServiceCallbacks.onFmAudioPathStarted:");
+          }
+          public void onFmAudioPathStopped() {
+             Log.d(LOGTAG, "mServiceCallbacks.onFmAudioPathStopped:");
+          }
       };
       /* Radio Vars */
      private Handler mHandler = new Handler();
diff --git a/fmapp2/src/com/caf/fmradio/IFMRadioServiceCallbacks.aidl b/fmapp2/src/com/caf/fmradio/IFMRadioServiceCallbacks.aidl
index e6fded7..24aaa70 100644
--- a/fmapp2/src/com/caf/fmradio/IFMRadioServiceCallbacks.aidl
+++ b/fmapp2/src/com/caf/fmradio/IFMRadioServiceCallbacks.aidl
@@ -48,4 +48,6 @@
   void onRecordingStarted();
   void onSeekNextStation();
   void onA2DPConnectionstateChanged(boolean state);
+  void onFmAudioPathStarted();
+  void onFmAudioPathStopped();
 }
diff --git a/libfm_jni/LibfmJni.cpp b/libfm_jni/LibfmJni.cpp
index d1a0942..52ad029 100644
--- a/libfm_jni/LibfmJni.cpp
+++ b/libfm_jni/LibfmJni.cpp
@@ -114,32 +114,18 @@
 
 jfloat Seek(JNIEnv *env, jobject thiz, jfloat freq, jboolean isUp)
 {
-    int ret = 0;
-    int tmp_freq;
-    int ret_freq;
-    float val;
+    int ret = JNI_FALSE;
+    float val = freq;
 
-    tmp_freq = (int)(freq * FREQ_MULT); //Eg, 87.55 * 100 --> 87550
-    if (pFMRadio)
+    if (pFMRadio) {
         ret = pFMRadio->Set_mute(true);
-    else
-        ret = JNI_FALSE;
-    if (ret) {
-        ALOGE("%s, error, [ret=%d]\n", __func__, ret);
-    }
-    ALOGD("%s, [mute] [ret=%d]\n", __func__, ret);
-    if (pFMRadio)
+        ALOGD("%s, [mute] [ret=%d]\n", __func__, ret);
         ret = pFMRadio->Seek((int)isUp);
-    else
-        ret = JNI_FALSE;
-    if (ret < 0) {
-        ret_freq = tmp_freq; //seek error, so use original freq
+        ALOGD("%s, [freq=%f] [ret=%d]\n", __func__, freq, ret);
+        if (ret > 0)
+            val = (float)ret/FREQ_MULT;   //Eg, 8755 / 100 --> 87.55
     }
 
-    ALOGD("%s, [freq=%d] [ret=%d]\n", __func__, ret_freq, ret);
-
-    val = (float)ret/FREQ_MULT;   //Eg, 8755 / 100 --> 87.55
-
     return val;
 }