FM: Keep track of scanned frequencies in service

By keeping track of scanned frequencies in the service, this allows
us to fetch it when the activity is resumed and not rely on having
active callbacks.

Change-Id: Idea9cab3e5fd2a789333c3f126416e0102613aa8
diff --git a/fmapp2/src/com/caf/fmradio/FMRadio.java b/fmapp2/src/com/caf/fmradio/FMRadio.java
index a44c43f..97b4c8a 100644
--- a/fmapp2/src/com/caf/fmradio/FMRadio.java
+++ b/fmapp2/src/com/caf/fmradio/FMRadio.java
@@ -248,9 +248,6 @@
    private ScrollerText mRadioTextScroller = null;
    private ScrollerText mERadioTextScroller = null;
 
-   /* Scanning frequencies */
-   ArrayList<Integer> mScannedFrequencies;
-
    private PresetStation mTunedStation = new PresetStation("", 102100);
    private PresetStation mPresetButtonStation = null;
 
@@ -576,7 +573,7 @@
         }
         try {
             if (!mService.isSearchInProgress()) {
-                resetSearch();
+                mServiceCallbacks.onSearchComplete();
             }
         }catch (RemoteException e) {
             e.printStackTrace();
@@ -1703,9 +1700,6 @@
       SharedPreferences.Editor editor = sp.edit();
       editor.clear();
       editor.commit();
-      if (mScannedFrequencies != null) {
-         mScannedFrequencies.clear();
-      }
    }
    public boolean fmConfigure() {
       boolean bStatus = true;
@@ -2066,13 +2060,19 @@
    }
 
    private void saveStations() {
-       if (mScannedFrequencies != null && mScannedFrequencies.size() > 0) {
-           Collections.sort(mScannedFrequencies);
+       List<Integer> scannedFrequencies = null;
+       try {
+           scannedFrequencies = mService.getScannedFrequencies();
+       } catch (RemoteException e) {
+           e.printStackTrace();
+       }
+       if (scannedFrequencies != null && scannedFrequencies.size() > 0) {
+           Collections.sort(scannedFrequencies);
            SharedPreferences sp = getSharedPreferences(SCAN_STATION_PREFS_NAME, 0);
            SharedPreferences.Editor editor = sp.edit();
 
            int index = 0;
-           for (Integer freq : mScannedFrequencies) {
+           for (Integer freq : scannedFrequencies) {
                index++;
                editor.putString(STATION_NAME + index, index + "");
                editor.putInt(STATION_FREQUENCY + index, freq);
@@ -3104,11 +3104,6 @@
          Log.d(LOGTAG, "mServiceCallbacks.onTuneStatusChanged: ");
          if (mIsScaning) {
              Log.d(LOGTAG, "isScanning....................");
-             if (mScannedFrequencies == null) {
-                 mScannedFrequencies = new ArrayList<Integer>();
-             }
-
-             mScannedFrequencies.add(FmSharedPreferences.getTunedFrequency());
          }
          cleanupTimeoutHandler();
          mHandler.post(mUpdateStationInfo);
@@ -3138,7 +3133,13 @@
       public void onSearchComplete() {
          Log.d(LOGTAG, "mServiceCallbacks.onSearchComplete :");
          if (mIsScaning) {
-             if (mScannedFrequencies != null && mScannedFrequencies.size() > 0) {
+             List<Integer> scannedFrequencies = null;
+             try {
+                 scannedFrequencies = mService.getScannedFrequencies();
+             } catch (RemoteException e) {
+                 e.printStackTrace();
+             }
+             if (scannedFrequencies != null && !scannedFrequencies.isEmpty()) {
                  mShowStationList = true;
              } else {
                  mHandler.post(new Runnable() {
diff --git a/fmapp2/src/com/caf/fmradio/FMRadioService.java b/fmapp2/src/com/caf/fmradio/FMRadioService.java
index 83165fb..abf7453 100644
--- a/fmapp2/src/com/caf/fmradio/FMRadioService.java
+++ b/fmapp2/src/com/caf/fmradio/FMRadioService.java
@@ -165,6 +165,7 @@
    private FMDeathRecipient mFMdr;
 
    //PhoneStateListener instances corresponding to each
+   private ArrayList<Integer> mScannedFrequencies = new ArrayList<Integer>();
 
    private FmRxRdsData mFMRxRDSData=null;
    // interval after which we stop the service when idle
@@ -2139,6 +2140,11 @@
          return(mService.get().isSearchInProgress());
       }
 
+      public List<Integer> getScannedFrequencies()
+      {
+         return(mService.get().getScannedFrequencies());
+      }
+
       public int getExtenCountryCode()
       {
          return(mService.get().getExtenCountryCode());
@@ -2576,6 +2582,10 @@
       return(bStatus);
    }
 
+   public List<Integer> getScannedFrequencies() {
+       return mScannedFrequencies;
+   }
+
    public boolean isSearchInProgress() {
       int state = mReceiver.getFMState();
       return state == qcom.fmradio.FmTransceiver.FMState_Srch_InProg;
@@ -2816,6 +2826,8 @@
     */
    public boolean scan(int pty)
    {
+      // Clear previously scanned frequencies
+      mScannedFrequencies.clear();
       boolean bCommandSent=false;
       if (mReceiver != null)
       {
@@ -3352,6 +3364,9 @@
             if(mReceiver != null) {
                clearStationInfo();
             }
+            if (isSearchInProgress()) {
+                mScannedFrequencies.add(frequency);
+            }
             if(mCallbacks != null)
             {
                mCallbacks.onTuneStatusChanged();
diff --git a/fmapp2/src/com/caf/fmradio/IFMRadioService.aidl b/fmapp2/src/com/caf/fmradio/IFMRadioService.aidl
index 5eea3bc..f4900af 100644
--- a/fmapp2/src/com/caf/fmradio/IFMRadioService.aidl
+++ b/fmapp2/src/com/caf/fmradio/IFMRadioService.aidl
@@ -76,6 +76,7 @@
     boolean isRtPlusSupported();
     boolean isA2DPConnected();
     boolean isSearchInProgress();
+    List getScannedFrequencies();
     boolean getIntfDetLowTh();
     boolean getIntfDetHighTh();
     boolean getRxRepeatCount();