FM: Scanned frequencies are saved in ascending ordering

Change-Id: Ie426ec3fa4aa3e8f4136938eb35f419ecc0aa71a
diff --git a/fmapp2/src/com/caf/fmradio/FMRadio.java b/fmapp2/src/com/caf/fmradio/FMRadio.java
index c79352f..609c4d6 100644
--- a/fmapp2/src/com/caf/fmradio/FMRadio.java
+++ b/fmapp2/src/com/caf/fmradio/FMRadio.java
@@ -245,6 +245,9 @@
    private ScrollerText mRadioTextScroller = null;
    private ScrollerText mERadioTextScroller = null;
 
+   /* Scanning frequencies */
+   ArrayList<Integer> mScannedFrequencies;
+
    private PresetStation mTunedStation = new PresetStation("", 102100);
    private PresetStation mPresetButtonStation = null;
 
@@ -2033,6 +2036,23 @@
       }
    }
 
+   private void saveStations() {
+       if (mScannedFrequencies != null && mScannedFrequencies.size() > 0) {
+           Collections.sort(mScannedFrequencies);
+           SharedPreferences sp = getSharedPreferences(SCAN_STATION_PREFS_NAME, 0);
+           SharedPreferences.Editor editor = sp.edit();
+
+           int index = 0;
+           for (Integer freq : mScannedFrequencies) {
+               index++;
+               editor.putString(STATION_NAME + index, index + "");
+               editor.putInt(STATION_FREQUENCY + index, freq);
+           }
+           editor.putInt(NUM_OF_STATIONS, index);
+           editor.commit();
+       }
+   }
+
    private void setupPresetLayout() {
       int numStations = FmSharedPreferences.getListStationCount();
       int addedStations = 0;
@@ -2640,6 +2660,7 @@
          updateSearchProgress();
          resetFMStationInfoUI();
          invalidateOptionsMenu();
+         saveStations();
       }
    };
 
@@ -3048,15 +3069,11 @@
          Log.d(LOGTAG, "mServiceCallbacks.onTuneStatusChanged: ");
          if (mIsScaning) {
              Log.d(LOGTAG, "isScanning....................");
-             SharedPreferences sp = getSharedPreferences(SCAN_STATION_PREFS_NAME, 0);
-             SharedPreferences.Editor editor = sp.edit();
-             int station_number = sp.getInt(NUM_OF_STATIONS, 0);
-             station_number++;
-             editor.putInt(NUM_OF_STATIONS, station_number);
-             editor.putString(STATION_NAME + station_number, station_number + "");
-             editor.putInt(STATION_FREQUENCY + station_number,
-                                   FmSharedPreferences.getTunedFrequency());
-             editor.commit();
+             if (mScannedFrequencies == null) {
+                 mScannedFrequencies = new ArrayList<Integer>();
+             }
+
+             mScannedFrequencies.add(FmSharedPreferences.getTunedFrequency());
          }
          cleanupTimeoutHandler();
          mHandler.post(mUpdateStationInfo);