Merge "FM: Fix default band is incorrect when language is chinese"
diff --git a/fmapp/src/com/codeaurora/fmradio/FMRadioService.java b/fmapp/src/com/codeaurora/fmradio/FMRadioService.java
index d0270ca..4505e9f 100644
--- a/fmapp/src/com/codeaurora/fmradio/FMRadioService.java
+++ b/fmapp/src/com/codeaurora/fmradio/FMRadioService.java
@@ -473,7 +473,7 @@
                                  }
                                  break;
                              case KeyEvent.KEYCODE_MEDIA_PLAY:
-                                 if (mServiceInUse ) {
+                                 if (isAntennaAvailable() && mServiceInUse ) {
                                      fmOn();
                                      if (isOrderedBroadcast()) {
                                          abortBroadcast();
@@ -656,8 +656,6 @@
       mServiceInUse = true;
       /* Application/UI is attached, so get out of lower power mode */
       setLowPowerMode(false);
-      if((mPlaybackInProgress == false) && isWiredHeadsetAvailable())
-         startFM();
       Log.d(LOGTAG, "onRebind");
    }
 
@@ -1046,13 +1044,7 @@
                          mSpeakerDisableHandler.postDelayed(mSpeakerDisableTask, 0);
                       }
                       if (true == mPlaybackInProgress) {
-                          if (mMuted)
-                              unMute();
-                          stopFM();
-                      }
-                      if (mSpeakerPhoneOn) {
-                          if (isAnalogModeSupported())
-                              setAudioPath(false);
+                          fmOff();
                       }
                       mStoppedOnFocusLoss = true;
                       break;
diff --git a/fmapp2/src/com/caf/fmradio/FMRadio.java b/fmapp2/src/com/caf/fmradio/FMRadio.java
index 335ce4f..9b130ff 100644
--- a/fmapp2/src/com/caf/fmradio/FMRadio.java
+++ b/fmapp2/src/com/caf/fmradio/FMRadio.java
@@ -188,8 +188,8 @@
    public static final String STATION_NAME = "name_of_station";
    public static final String STATION_FREQUENCY = "frequency_of_station";
 
-   private static IFMRadioService mService = null;
-   private static FmSharedPreferences mPrefs;
+   private IFMRadioService mService = null;
+   private FmSharedPreferences mPrefs;
 
    /* Button Resources */
    private ImageView mOnOffButton;
@@ -242,11 +242,11 @@
    private ScrollerText mRadioTextScroller = null;
    private ScrollerText mERadioTextScroller = null;
 
-   private static PresetStation mTunedStation = new PresetStation("", 102100);
+   private PresetStation mTunedStation = new PresetStation("", 102100);
    private PresetStation mPresetButtonStation = null;
 
    /* Radio Vars */
-   final Handler mHandler = new Handler();
+   private Handler mHandler = new Handler();
    /* Search Progress Dialog */
    private ProgressDialog mProgressDialog = null;
 
@@ -273,6 +273,8 @@
    /** fm stats property string */
    public static final String FM_STATS_PROP = "persist.fm.stats";
 
+   private BroadcastReceiver mFmSettingReceiver = null;
+
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
@@ -397,6 +399,7 @@
          } else {
              Log.d(LOGTAG, "onCreate: Start Service completed successfully");
          }
+         registerFMSettingListner();
       }
    }
 
@@ -507,6 +510,16 @@
               initiateRecordThread();
           }
       }
+      mPrefs.Load();
+      if (mPicker != null) {
+          setDisplayvalue();
+      }
+      PresetStation station = new PresetStation("",
+                                   FmSharedPreferences.getTunedFrequency());
+      if (station != null) {
+          mTunedStation.Copy(station);
+      }
+
    }
 
    @Override
@@ -523,15 +536,6 @@
    public void onResume() {
       super.onResume();
       Log.d(LOGTAG, "FMRadio: onResume");
-      mPrefs.Load();
-      if (mPicker != null) {
-          setDisplayvalue();
-      }
-      PresetStation station = new PresetStation("",
-                                   FmSharedPreferences.getTunedFrequency());
-      if (station != null) {
-          mTunedStation.Copy(station);
-      }
       mStereo = FmSharedPreferences.getLastAudioMode();
       mHandler.post(mUpdateProgramService);
       mHandler.post(mUpdateRadioText);
@@ -540,13 +544,13 @@
       updateStationInfoToUI();
       enableRadioOnOffUI();
    }
-   private class LoadedDataAndState {
+   private static class LoadedDataAndState {
       public LoadedDataAndState(){};
       public boolean onOrOff;
    }
    @Override
    public Object onRetainNonConfigurationInstance() {
-      final LoadedDataAndState data = new LoadedDataAndState();
+      LoadedDataAndState data = new LoadedDataAndState();
       if (mService != null) {
          try {
               data.onOrOff = mService.isFmOn();
@@ -562,8 +566,18 @@
 
    @Override
    public void onDestroy() {
+      super.onDestroy();
       Log.d(LOGTAG, "FMRadio: onDestroy");
+      mHandler.removeCallbacksAndMessages(null);
       cleanupTimeoutHandler();
+      if(mProgressDialog != null) {
+         mProgressDialog.dismiss();
+      }
+      if(mSearchProgressHandler != null) {
+         mSearchProgressHandler.removeCallbacksAndMessages(null);
+      }
+      removeDialog(DIALOG_PRESET_OPTIONS);
+      unRegisterReceiver(mFmSettingReceiver);
       if (mService != null) {
           try {
                if(!mService.isFmOn()) {
@@ -576,7 +590,6 @@
       unbindFromService(this);
       mService = null;
       Log.d(LOGTAG, "onDestroy: unbindFromService completed");
-      super.onDestroy();
    }
 
    @Override
@@ -790,7 +803,7 @@
                mUIUpdateHandlerHandler.sendMessage(statusUpdate);
             } catch (InterruptedException e)
             {
-               Thread.currentThread().interrupt();
+               break;
             }
             if(!isRecording()) {
                Message finished = new Message();
@@ -1469,7 +1482,7 @@
       }
    };
 
-   final FrequencyPickerDialog.OnFrequencySetListener mFrequencyChangeListener
+   FrequencyPickerDialog.OnFrequencySetListener mFrequencyChangeListener
       = new FrequencyPickerDialog.OnFrequencySetListener() {
         public void onFrequencySet(FrequencyPicker view, int frequency) {
            Log.d(LOGTAG, "mFrequencyChangeListener: onFrequencyChanged to: " +
@@ -1687,7 +1700,7 @@
       editor.clear();
       editor.commit();
    }
-   public static boolean fmConfigure() {
+   public boolean fmConfigure() {
       boolean bStatus = true;
       if(mService != null) {
          try {
@@ -1703,7 +1716,7 @@
       }
       return bStatus;
    }
-   public static void fmAutoAFSwitch() {
+   public void fmAutoAFSwitch() {
       boolean bStatus = false;
       if (mService != null) {
          try {
@@ -1718,7 +1731,7 @@
       }
    }
 
-   public static void fmAudioOutputMode() {
+   public void fmAudioOutputMode() {
       boolean bStatus = false;
       if (mService != null) {
          try {
@@ -2109,7 +2122,7 @@
       return(mIsSearching);
    }
 
-   public static PresetStation getCurrentTunedStation() {
+   public PresetStation getCurrentTunedStation() {
       return mTunedStation;
    }
 
@@ -2398,8 +2411,8 @@
                 sleepTimerExpired = hasSleepTimerExpired();
             }catch (Exception ex) {
                 Log.d( LOGTAG, "RunningThread InterruptedException");
-                Thread.currentThread().interrupt();
-            }//try
+                break;
+            }
          }
          if(true == sleepTimerExpired) {
              Message finished = new Message();
@@ -2527,7 +2540,7 @@
       updateStationInfoToUI();
    }
 
-   final Runnable mRadioEnabled = new Runnable() {
+   Runnable mRadioEnabled = new Runnable() {
       public void run() {
          /* Update UI to FM On State */
          enableRadioOnOffUI(true);
@@ -2537,7 +2550,7 @@
       }
    };
 
-   final Runnable mRadioDisabled = new Runnable() {
+   Runnable mRadioDisabled = new Runnable() {
       public void run() {
          /* Update UI to FM Off State */
          cleanupTimeoutHandler();
@@ -2547,14 +2560,15 @@
          enableRadioOnOffUI(false);
       }
    };
-   final Runnable mRadioReset = new Runnable() {
+
+    Runnable mRadioReset = new Runnable() {
       public void run() {
          /* Update UI to FM Reset (Off) State */
          resetRadio();
       }
    };
 
-   final Runnable mUpdateStationInfo = new Runnable() {
+   Runnable mUpdateStationInfo = new Runnable() {
       public void run() {
          cleanupTimeoutHandler();
          PresetStation station = new PresetStation("", FmSharedPreferences.getTunedFrequency());
@@ -2566,7 +2580,7 @@
       }
    };
 
-   final Runnable mSearchComplete = new Runnable() {
+   Runnable mSearchComplete = new Runnable() {
       public void run() {
          Log.d(LOGTAG, "mSearchComplete: ");
          mScanPty=0;
@@ -2579,13 +2593,13 @@
       }
    };
 
-   final Runnable mOnMute = new Runnable() {
+   Runnable mOnMute = new Runnable() {
       public void run() {
          setMuteModeButtonImage(true);
       }
    };
 
-   final Runnable mOnStereo = new Runnable() {
+   Runnable mOnStereo = new Runnable() {
       public void run() {
          if (FMRADIO_UI_STATION_AUDIO_STEREO == mStereo) {
              mStereoTV.setText(R.string.audio_type_stereo);
@@ -2598,7 +2612,7 @@
       }
    };
 
-   final Runnable mUpdateRadioText = new Runnable() {
+   Runnable mUpdateRadioText = new Runnable() {
       public void run() {
          String str = "";
          if ((mService != null) && isFmOn()) {
@@ -2639,14 +2653,14 @@
       }
    };
 
-   final Runnable mRadioChangeFrequency = new Runnable(){
+   Runnable mRadioChangeFrequency = new Runnable(){
        public void run() {
            mUpdatePickerValue = false;
            tuneRadio(mFrequency);
        }
    };
 
-   final Runnable mUpdateExtenRadioText = new Runnable() {
+   Runnable mUpdateExtenRadioText = new Runnable() {
       public void run() {
          String str = "";
          if ((mService != null) && isFmOn()) {
@@ -2669,7 +2683,7 @@
    };
 
    /* Create runnable for posting */
-   final Runnable mUpdateProgramService = new Runnable() {
+   Runnable mUpdateProgramService = new Runnable() {
       public void run() {
          String str = "";
          if (mService != null) {
@@ -2720,7 +2734,7 @@
     * When the entire text is scrolled, the scrolling will restart
     * after SCROLLER_RESTART_DELAY_MS
     */
-   private static final class ScrollerText extends Handler {
+   private final class ScrollerText extends Handler {
       private static final byte SCROLLER_STOPPED = 0x51;
       private static final byte SCROLLER_STARTING = 0x52;
       private static final byte SCROLLER_RUNNING = 0x53;
@@ -2776,7 +2790,7 @@
             return;
          }
          removeMessages(SCROLLER_MSG_TICK);
-         final TextView textView = mView.get();
+         TextView textView = mView.get();
          if (textView != null)  {
             mStringlength = mOriginalString.length();
             String szStr2 = "";
@@ -2812,7 +2826,7 @@
        */
       private void resetScroll() {
          mIteration = 0;
-         final TextView textView = mView.get();
+         TextView textView = mView.get();
          if (textView != null) {
             textView.setText(mOriginalString);
          }
@@ -2823,7 +2837,7 @@
        * Starts only if Length > 0
        */
       void startScroll() {
-         final TextView textView = mView.get();
+         TextView textView = mView.get();
          if (textView != null) {
             mOriginalString = (String)textView.getText();
             mStringlength = mOriginalString.length();
@@ -2836,15 +2850,15 @@
    }
 
 
-   public static IFMRadioService sService = null;
-   private static HashMap<Context, ServiceBinder> sConnectionMap = new HashMap<Context, ServiceBinder>();
+   public  IFMRadioService sService = null;
+   private  HashMap<Context, ServiceBinder> sConnectionMap = new HashMap<Context, ServiceBinder>();
 
-   public static boolean bindToService(Context context) {
+   public  boolean bindToService(Context context) {
       Log.e(LOGTAG, "bindToService: Context");
       return bindToService(context, null);
    }
 
-   public static boolean bindToService(Context context, ServiceConnection callback) {
+   public  boolean bindToService(Context context, ServiceConnection callback) {
       Log.e(LOGTAG, "bindToService: Context with serviceconnection callback");
       context.startService(new Intent(context, FMRadioService.class));
       ServiceBinder sb = new ServiceBinder(callback);
@@ -2853,7 +2867,7 @@
                                                          FMRadioService.class), sb, 0);
    }
 
-   public static void unbindFromService(Context context) {
+   public  void unbindFromService(Context context) {
       ServiceBinder sb = (ServiceBinder) sConnectionMap.remove(context);
       Log.e(LOGTAG, "unbindFromService: Context");
       if (sb == null) {
@@ -2868,7 +2882,7 @@
       }
    }
 
-   private static class ServiceBinder implements ServiceConnection {
+   private  class ServiceBinder implements ServiceConnection {
       ServiceConnection mCallback;
       ServiceBinder(ServiceConnection callback) {
          mCallback = callback;
@@ -2978,6 +2992,10 @@
       }
       public void onSearchComplete() {
          Log.d(LOGTAG, "mServiceCallbacks.onSearchComplete :");
+         mScanPty = 0;
+         mIsScaning = false;
+         mIsSeeking = false;
+         mIsSearching = false;
          mHandler.post(mSearchComplete);
       }
       public void onSearchListComplete() {
@@ -3017,4 +3035,45 @@
          startRecordingTimer();
       }
    };
+
+    private void registerFMSettingListner() {
+        if (mFmSettingReceiver == null) {
+            mFmSettingReceiver = new BroadcastReceiver() {
+                @Override
+                public void onReceive(Context context, Intent intent) {
+                     Log.d(LOGTAG, "Received intent " + intent);
+                     String action = intent.getAction();
+                     Log.d(LOGTAG, " action = " + action);
+                     if (action.equals(Settings.ACTION_FM_SETTING)) {
+                         int state = intent.getIntExtra("state", 0);
+                         Log.d(LOGTAG, "ACTION_FM_SETTING Intent received" + state);
+                         switch(state) {
+                         case Settings.FM_BAND_CHANGED:
+                              fmConfigure();
+                              break;
+                         case Settings.FM_CHAN_SPACING_CHANGED:
+                              fmConfigure();
+                              break;
+                         case Settings.FM_AF_OPTION_CHANGED:
+                              fmAudioOutputMode();
+                              break;
+                         case Settings.FM_AUDIO_MODE_CHANGED:
+                              fmAudioOutputMode();
+                              break;
+                         }
+                     }
+                 }
+            };
+            IntentFilter iFilter = new IntentFilter();
+            iFilter.addAction(Settings.ACTION_FM_SETTING);
+            registerReceiver(mFmSettingReceiver, iFilter);
+        }
+    }
+
+    private void unRegisterReceiver(BroadcastReceiver myReceiver) {
+        if(myReceiver != null) {
+           unregisterReceiver(myReceiver);
+           myReceiver = null;
+        }
+    }
 }
diff --git a/fmapp2/src/com/caf/fmradio/FMRadioService.java b/fmapp2/src/com/caf/fmradio/FMRadioService.java
index fb36a9c..a73047a 100644
--- a/fmapp2/src/com/caf/fmradio/FMRadioService.java
+++ b/fmapp2/src/com/caf/fmradio/FMRadioService.java
@@ -470,7 +470,7 @@
                                  }
                                  break;
                              case KeyEvent.KEYCODE_MEDIA_PLAY:
-                                 if (mServiceInUse ) {
+                                 if (isAntennaAvailable() && mServiceInUse) {
                                      fmOn();
                                      if (isOrderedBroadcast()) {
                                          abortBroadcast();
@@ -653,8 +653,6 @@
       mServiceInUse = true;
       /* Application/UI is attached, so get out of lower power mode */
       setLowPowerMode(false);
-      if((mPlaybackInProgress == false) && isWiredHeadsetAvailable())
-         startFM();
       Log.d(LOGTAG, "onRebind");
    }
 
@@ -1043,13 +1041,7 @@
                          mSpeakerDisableHandler.postDelayed(mSpeakerDisableTask, 0);
                       }
                       if (true == mPlaybackInProgress) {
-                          if(mMuted)
-                             unMute();
-                          stopFM();
-                      }
-                      if (mSpeakerPhoneOn) {
-                          if (isAnalogModeSupported())
-                              setAudioPath(false);
+                          fmOff();
                       }
                       mStoppedOnFocusLoss = true;
                       break;
diff --git a/fmapp2/src/com/caf/fmradio/FMStats.java b/fmapp2/src/com/caf/fmradio/FMStats.java
index 18ab71c..8defff5 100644
--- a/fmapp2/src/com/caf/fmradio/FMStats.java
+++ b/fmapp2/src/com/caf/fmradio/FMStats.java
@@ -178,7 +178,7 @@
     int  mTestSelected = 0;
     boolean mIsSearching = false;
     private static String LOGTAG = "FMStats";
-    private static IFMRadioService mService = null;
+    private IFMRadioService mService = null;
     private Thread mMultiUpdateThread = null;
     private static final int STATUS_UPDATE = 1;
     private static final int STATUS_DONE = 2;
@@ -355,6 +355,7 @@
 
     @Override
     public void onStop() {
+       Log.d(LOGTAG, "onStop");
        super.onStop();
        if(isRecording()) {
           try {
@@ -369,12 +370,16 @@
 
     @Override
     public void onDestroy() {
-
+        Log.d(LOGTAG, "onDestroy");
         stopCurTest();
-
+        if(mUIUpdateHandlerHandler != null) {
+           mUIUpdateHandlerHandler.removeCallbacksAndMessages(null);
+        }
+        if(mHandler != null) {
+           mHandler.removeCallbacksAndMessages(null);
+        }
         unRegisterBroadcastReceiver(mBandSweepDelayExprdListener);
         unRegisterBroadcastReceiver(mBandSweepDwellExprdListener);
-
         if(null != mFileCursor ) {
             try {
                 mFileCursor.close();
@@ -1707,7 +1712,7 @@
            20 times*/
           int freq = FmSharedPreferences.getTunedFrequency();
 
-          for(int i = 0; i < 20; i++) {
+          for(int i = 0; i < 20 && !Thread.currentThread().isInterrupted(); i++) {
               try {
                    Thread.sleep(500);
                    Message updateUI = new Message();
@@ -1749,19 +1754,27 @@
     /* Thread processing */
     private Runnable getManualSweepResults = new Runnable() {
        public void run() {
-           mWakeLock.acquire(10 * 1000);
-           if(mBand.cur_freq <= mBand.hFreq) {
-              if(!tuneAndUpdateSweepResult(mBand.cur_freq)) {
-                 sendStatusDoneMsg();
-              }
-              mBand.cur_freq += mBand.Spacing;
-              if(mBand.cur_freq > mBand.hFreq) {
-                 sendStatusDoneMsg();
-              }else {
-                 setAlarm(prevDwellTime * 1000, BAND_SWEEP_DWELL_DELAY_TIMEOUT);
-              }
-           }else {
-              sendStatusDoneMsg();
+           try {
+               if(mBand == null) {
+                  return;
+               }
+               mWakeLock.acquire(10 * 1000);
+               if(mBand.cur_freq <= mBand.hFreq) {
+                  if(!tuneAndUpdateSweepResult(mBand.cur_freq)) {
+                     sendStatusDoneMsg();
+                     return;
+                  }
+                  mBand.cur_freq += mBand.Spacing;
+                  if(mBand.cur_freq > mBand.hFreq) {
+                     sendStatusDoneMsg();
+                  }else {
+                     setAlarm(prevDwellTime * 1000, BAND_SWEEP_DWELL_DELAY_TIMEOUT);
+                  }
+               }else {
+                  sendStatusDoneMsg();
+               }
+           }catch(Exception e) {
+               e.printStackTrace();
            }
        }
     };
@@ -1787,31 +1800,37 @@
          boolean status = true;
          int freq;
 
-         mWakeLock.acquire(10 * 1000);
-         freq = mNextFreqInterface.getNextFreq();
-
-         for(; (status = (!mNextFreqInterface.errorOccured()) & (!Thread.currentThread().isInterrupted()));
-                  freq = mNextFreqInterface.getNextFreq()) {
-            if(validFreq(freq)) {
-               if(!tuneAndUpdateSweepResult(freq)) {
-                  status = false;
-                  break;
-               }else {
-                  setAlarm(prevDwellTime * 1000, BAND_SWEEP_DWELL_DELAY_TIMEOUT);
-                  break;
-               }
-            }
-         }
-         if(!status) {
-            sendStatusDoneMsg();
-            mNextFreqInterface.Stop();
-            mNextFreqInterface = null;
+         try {
+              mWakeLock.acquire(10 * 1000);
+              freq = mNextFreqInterface.getNextFreq();
+              for(; (status = (!mNextFreqInterface.errorOccured()) &
+                     (!Thread.currentThread().isInterrupted()));
+                          freq = mNextFreqInterface.getNextFreq()) {
+                  if(validFreq(freq)) {
+                     if(!tuneAndUpdateSweepResult(freq)) {
+                        status = false;
+                        break;
+                     }else {
+                        setAlarm(prevDwellTime * 1000,
+                                  BAND_SWEEP_DWELL_DELAY_TIMEOUT);
+                        break;
+                     }
+                  }
+              }
+              if(!status) {
+                 sendStatusDoneMsg();
+                 mNextFreqInterface.Stop();
+                 mNextFreqInterface = null;
+              }
+         }catch (Exception e) {
+              e.printStackTrace();
          }
        }
     };
 
     private boolean validFreq(int freq) {
-       if((freq >= mBand.lFreq) &&
+       if((freq >= mBand.lFreq) && (freq <= mBand.hFreq)
+           &&
           (((freq - mBand.lFreq) / mBand.Spacing) >= 0)) {
            return true;
        }else {
@@ -1870,6 +1889,8 @@
                }
            } catch (RemoteException e) {
                e.printStackTrace();
+           } catch(Exception e) {
+               e.printStackTrace();
            }
 
            try {
@@ -1880,6 +1901,8 @@
                    return null;
            } catch (RemoteException e) {
                e.printStackTrace();
+           } catch(Exception e) {
+               e.printStackTrace();
            }
 
            if(isTransportLayerSMD()) {
@@ -1893,6 +1916,8 @@
                   }
               } catch (RemoteException e) {
                   e.printStackTrace();
+              } catch(Exception e) {
+                  e.printStackTrace();
               }
            } else {
               try {
@@ -1903,6 +1928,8 @@
                       return null;
               } catch (RemoteException e) {
                   e.printStackTrace();
+              }catch(Exception e) {
+                  e.printStackTrace();
               }
            }
 
@@ -1914,6 +1941,8 @@
                    return null;
            } catch (RemoteException e) {
                e.printStackTrace();
+           }catch (Exception e) {
+               e.printStackTrace();
            }
         } else {
            return null;
@@ -1949,15 +1978,15 @@
             }
     };
 
-    public static IFMRadioService sService = null;
-    private static HashMap<Context, ServiceBinder> sConnectionMap = new HashMap<Context, ServiceBinder>();
+    public IFMRadioService sService = null;
+    private HashMap<Context, ServiceBinder> sConnectionMap = new HashMap<Context, ServiceBinder>();
 
-    public static boolean bindToService(Context context) {
+    public boolean bindToService(Context context) {
        Log.e(LOGTAG, "bindToService: Context");
        return bindToService(context, null);
     }
 
-    public static boolean bindToService(Context context, ServiceConnection callback) {
+    public boolean bindToService(Context context, ServiceConnection callback) {
        Log.e(LOGTAG, "bindToService: Context with serviceconnection callback");
        context.startService(new Intent(context, FMRadioService.class));
        ServiceBinder sb = new ServiceBinder(callback);
@@ -1966,7 +1995,7 @@
                                                           FMRadioService.class), sb, 0);
     }
 
-    public static void unbindFromService(Context context) {
+    public void unbindFromService(Context context) {
        ServiceBinder sb = (ServiceBinder) sConnectionMap.remove(context);
        Log.e(LOGTAG, "unbindFromService: Context");
        if (sb == null)
@@ -1983,7 +2012,7 @@
        }
     }
 
-    private static class ServiceBinder implements ServiceConnection
+    private class ServiceBinder implements ServiceConnection
     {
        ServiceConnection mCallback;
        ServiceBinder(ServiceConnection callback) {
@@ -2136,9 +2165,9 @@
           }
       };
       /* Radio Vars */
-     final Handler mHandler = new Handler();
+     private Handler mHandler = new Handler();
 
-     final Runnable mTuneComplete = new Runnable(){
+     private Runnable mTuneComplete = new Runnable(){
          public void run(){
              if((null != mMultiUpdateThread) &&(null != mSync))
              {
@@ -2297,8 +2326,10 @@
               @Override
                public void onReceive(Context context, Intent intent) {
                    Log.d(LOGTAG, "received Band sweep Dwell expired");
-                   mWakeLock.acquire(5 * 1000);
-                   ResumeBandSweep();
+                   if(mTestRunning) {
+                      mWakeLock.acquire(5 * 1000);
+                      ResumeBandSweep();
+                   }
                }
            };
            IntentFilter intentFilter = new IntentFilter(BAND_SWEEP_DWELL_DELAY_TIMEOUT);
diff --git a/fmapp2/src/com/caf/fmradio/FMTransmitterActivity.java b/fmapp2/src/com/caf/fmradio/FMTransmitterActivity.java
index e83f61f..9798e3e 100644
--- a/fmapp2/src/com/caf/fmradio/FMTransmitterActivity.java
+++ b/fmapp2/src/com/caf/fmradio/FMTransmitterActivity.java
@@ -98,8 +98,8 @@
    private static final int ACTIVITY_RESULT_SETTINGS = 1;
 
    private static final int MAX_PRESETS = 7;
-   private static IFMTransmitterService mService = null;
-   private static FmSharedPreferences mPrefs;
+   private IFMTransmitterService mService = null;
+   private FmSharedPreferences mPrefs;
 
    /* Button Resources */
    private ImageView mOnOffButton;
@@ -134,9 +134,9 @@
    private String mPSData = null;
 
    /* Radio Vars */
-   final Handler mHandler = new Handler();
-   private final Handler enableRadioHandler = new Handler();
-   private final Handler disableRadioHandler = new Handler();
+   private Handler mHandler = new Handler();
+   private Handler enableRadioHandler = new Handler();
+   private Handler disableRadioHandler = new Handler();
 
    /* Search Progress Dialog */
    private ProgressDialog mProgressDialog = null;
@@ -150,6 +150,7 @@
    private static final int FREQUENCY_STEP_LARGE = 200;
    public static boolean mUpdatePickerValue = false;
 
+   private BroadcastReceiver mFmSettingReceiver = null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
@@ -243,6 +244,7 @@
           }else {
              Log.d(LOGTAG, "onCreate: Start Service completed successfully");
           }
+          registerFMSettingListner();
        }
    }
 
@@ -341,6 +343,7 @@
    @Override
    public void onResume() {
       super.onResume();
+      Log.d(LOGTAG, "Resuming");
       LoadPreferences();
       if(mPicker != null) {
          setDisplayvalue();
@@ -353,20 +356,30 @@
 
    @Override
    public void onDestroy() {
+      super.onDestroy();
+      mHandler.removeCallbacksAndMessages(null);
+      if(mProgressDialog != null) {
+         mProgressDialog.dismiss();
+         mProgressDialog = null;
+      }
+      if(mSearchProgressHandler != null) {
+         mSearchProgressHandler.removeCallbacksAndMessages(null);
+      }
+      removeDialog(DIALOG_PRESET_LIST_AUTO_SET);
+      unRegisterReceiver(mFmSettingReceiver);
       unbindFromService(this);
       mService = null;
       Log.d(LOGTAG, "onDestroy: unbindFromService completed");
-      super.onDestroy();
    }
 
-   private class LoadedDataAndState {
+   private static class LoadedDataAndState {
       public LoadedDataAndState(){};
       public boolean onOrOff;
    }
 
    @Override
    public Object onRetainNonConfigurationInstance() {
-      final LoadedDataAndState data = new LoadedDataAndState();
+      LoadedDataAndState data = new LoadedDataAndState();
       if(mService != null) {
          try {
               data.onOrOff = mService.isFmOn();
@@ -759,7 +772,7 @@
       }
    };
 
-   final FrequencyPickerDialog.OnFrequencySetListener
+   private FrequencyPickerDialog.OnFrequencySetListener
       mFrequencyChangeListener =
          new FrequencyPickerDialog.OnFrequencySetListener() {
             public void onFrequencySet
@@ -867,7 +880,7 @@
       }
    }
 
-   public static void fmConfigure() {
+   public void fmConfigure() {
       if(mService != null) {
          try {
               mService.fmReconfigure();
@@ -1051,7 +1064,7 @@
       return (mIsSearching);
    }
 
-   public static int getCurrentTunedFrequency() {
+   public int getCurrentTunedFrequency() {
       return mTunedFrequency;
    }
 
@@ -1148,14 +1161,14 @@
   }
 
 
-  final Runnable mUpdateStationInfo = new Runnable() {
+  private Runnable mUpdateStationInfo = new Runnable() {
      public void run() {
         updateSearchProgress();
         resetFMStationInfoUI();
      }
   };
 
-  final Runnable mSearchListComplete = new Runnable() {
+  private Runnable mSearchListComplete = new Runnable() {
      public void run() {
         Log.d(LOGTAG, "mSearchListComplete: ");
         mIsSearching = false;
@@ -1196,7 +1209,7 @@
 
 
 
-  final Runnable mUpdateRadioText = new Runnable() {
+  private Runnable mUpdateRadioText = new Runnable() {
      public void run() {
         String str = "";
         if((mService != null) && isFmOn()) {
@@ -1237,7 +1250,7 @@
      }
   };
 
-  final Runnable mRadioChangeFrequency = new Runnable(){
+  private Runnable mRadioChangeFrequency = new Runnable(){
      public void run() {
         mUpdatePickerValue = false;
         tuneRadio(mFrequency);
@@ -1260,7 +1273,7 @@
     * character after every SCROLLER_UPDATE_DELAY_MS When the entire text is
     * scrolled, the scrolling will restart after SCROLLER_RESTART_DELAY_MS
     */
-  private static final class ScrollerText extends Handler {
+  private final class ScrollerText extends Handler {
 
      private static final byte SCROLLER_STOPPED = 0x51;
      private static final byte SCROLLER_STARTING = 0x52;
@@ -1379,15 +1392,15 @@
      }
   }
 
-  public static IFMTransmitterService sService = null;
-  private static HashMap<Context, ServiceBinder> sConnectionMap = new HashMap<Context, ServiceBinder>();
+  public IFMTransmitterService sService = null;
+  private HashMap<Context, ServiceBinder> sConnectionMap = new HashMap<Context, ServiceBinder>();
 
-  public static boolean bindToService(Context context) {
+  public boolean bindToService(Context context) {
      Log.e(LOGTAG, "bindToService: Context");
      return bindToService(context, null);
   }
 
-  public static boolean bindToService(Context context,
+  public boolean bindToService(Context context,
                         ServiceConnection callback) {
      Log.e(LOGTAG, "bindToService: Context with serviceconnection callback");
      context.startService(new Intent(context, FMTransmitterService.class));
@@ -1397,7 +1410,7 @@
                                 FMTransmitterService.class), sb, 0);
   }
 
-  public static void unbindFromService(Context context) {
+  public void unbindFromService(Context context) {
      ServiceBinder sb = (ServiceBinder) sConnectionMap.remove(context);
      Log.e(LOGTAG, "unbindFromService: Context");
      if(sb == null) {
@@ -1413,7 +1426,7 @@
      }
   }
 
-  private static class ServiceBinder implements ServiceConnection {
+  private class ServiceBinder implements ServiceConnection {
      ServiceConnection mCallback;
 
      ServiceBinder(ServiceConnection callback) {
@@ -1530,7 +1543,7 @@
      }
   };
 
-  final Runnable mRadioStateUpdated = new Runnable() {
+  private Runnable mRadioStateUpdated = new Runnable() {
      public void run() {
         enableRadioOnOffButton();
         /* Update UI to FM On State */
@@ -1551,7 +1564,7 @@
      }
   };
 
-  final Runnable mRadioReset = new Runnable() {
+  private Runnable mRadioReset = new Runnable() {
      public void run() {
         /* Save the existing frequency */
         resetSearchProgress();
@@ -1560,4 +1573,40 @@
         enableRadioOnOffUI(false);
      }
   };
+
+  private void registerFMSettingListner() {
+       if(mFmSettingReceiver == null) {
+           mFmSettingReceiver = new BroadcastReceiver() {
+               @Override
+               public void onReceive(Context context, Intent intent) {
+                    Log.d(LOGTAG, "Received intent " + intent);
+                    String action = intent.getAction();
+                    Log.d(LOGTAG, " action = " + action);
+                    if (action.equals(Settings.ACTION_FM_SETTING)) {
+                        int state = intent.getIntExtra("state", 0);
+                        Log.d(LOGTAG, "ACTION_FM_SETTING Intent received" + state);
+                        switch(state) {
+                        case Settings.FM_BAND_CHANGED:
+                             fmConfigure();
+                             break;
+                        case Settings.FM_CHAN_SPACING_CHANGED:
+                             fmConfigure();
+                             break;
+                        }
+                    }
+                }
+           };
+           IntentFilter iFilter = new IntentFilter();
+           iFilter.addAction(Settings.ACTION_FM_SETTING);
+           registerReceiver(mFmSettingReceiver, iFilter);
+       }
+  }
+
+  private void unRegisterReceiver(BroadcastReceiver myReceiver) {
+      if(myReceiver != null) {
+         unregisterReceiver(myReceiver);
+         myReceiver = null;
+      }
+  }
+
 }
diff --git a/fmapp2/src/com/caf/fmradio/Settings.java b/fmapp2/src/com/caf/fmradio/Settings.java
index 328126a..d64621b 100644
--- a/fmapp2/src/com/caf/fmradio/Settings.java
+++ b/fmapp2/src/com/caf/fmradio/Settings.java
@@ -62,7 +62,11 @@
         public static final String USER_DEFINED_BAND_MAX_KEY = "user_defined_band_max";
         public static final String CHAN_SPACING_KEY = "chanl_spacing";
         public static final String RESTORE_FACTORY_DEFAULT_ACTION = "com.caf.fmradio.settings.revert_to_defaults";
-
+        public static final String ACTION_FM_SETTING = "com.caf.fmradio.settings.changed";
+        public static final int FM_BAND_CHANGED = 1;
+        public static final int FM_CHAN_SPACING_CHANGED = 2;
+        public static final int FM_AF_OPTION_CHANGED = 3;
+        public static final int FM_AUDIO_MODE_CHANGED = 4;
         private static final String LOGTAG = FMRadio.LOGTAG;
         private static final String USR_BAND_MSG = "Enter Freq from range 76.0 - 108.0";
 
@@ -272,8 +276,7 @@
                                                         + index);
               FmSharedPreferences.setCountry(index);
               setBandSummary(index);
-              bStatus = FMRadio.fmConfigure();
-              FMTransmitterActivity.fmConfigure();
+              sendSettingsChangedIntent(FM_BAND_CHANGED);
               if (curList != null) {
                   curList.clear();
               }
@@ -286,14 +289,13 @@
                if(valStr != null) {
                   index  = mChannelSpacingPref.findIndexOfValue(valStr);
                }
-               if ((index < 0) || (index >= summaryRecordItems.length)) {
+               if ((index < 0) || (index >= chSpacingItems.length)) {
                    index = 0;
                    mChannelSpacingPref.setValueIndex(0);
                }
                mChannelSpacingPref.setSummary(chSpacingItems[index]);
                FmSharedPreferences.setChSpacing(2 - index);
-               FMRadio.fmConfigure();
-               FMTransmitterActivity.fmConfigure();
+               sendSettingsChangedIntent(FM_CHAN_SPACING_CHANGED);
                if(curList != null) {
                   curList.clear();
                }
@@ -311,8 +313,7 @@
                min_freq = FmSharedPreferences.getLowerLimit();
                if((freq > 0) && (freq < max_freq) && (freq >= 76000)) {
                   FmSharedPreferences.setLowerLimit((int)freq);
-                  FMRadio.fmConfigure();
-                  FMTransmitterActivity.fmConfigure();
+                  sendSettingsChangedIntent(FM_BAND_CHANGED);
                   setBandSummary(summaryBandItems.length - 1);
                   clearStationList();
                }else {
@@ -331,8 +332,7 @@
                max_freq = FmSharedPreferences.getUpperLimit();
                if((freq > 0) && (freq > min_freq) && (freq <= 108000)) {
                   FmSharedPreferences.setUpperLimit((int)freq);
-                  FMRadio.fmConfigure();
-                  FMTransmitterActivity.fmConfigure();
+                  sendSettingsChangedIntent(FM_BAND_CHANGED);
                   setBandSummary(summaryBandItems.length - 1);
                   clearStationList();
                }else {
@@ -345,8 +345,8 @@
                      Log.d(LOGTAG, "onSharedPreferenceChanged: Auto AF Enable: "
                                                + bAFAutoSwitch);
                      FmSharedPreferences.setAutoAFSwitch(bAFAutoSwitch);
-                     FMRadio.fmAutoAFSwitch();
                      mPrefs.Save();
+                     sendSettingsChangedIntent(FM_AF_OPTION_CHANGED);
                  }else if(key.equals(RECORD_DURATION_KEY)) {
                      if(FMRadio.RECORDING_ENABLE) {
                         String valueStr = mRecordDurPreference.getValue();
@@ -387,7 +387,7 @@
                          FmSharedPreferences.setAudioOutputMode(false);
                      }
                      mPrefs.Save();
-                     FMRadio.fmAudioOutputMode();
+                     sendSettingsChangedIntent(FM_AUDIO_MODE_CHANGED);
                  }
               }
           }
@@ -526,4 +526,11 @@
         private void displayToast(String msg) {
            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
         }
+
+        private void sendSettingsChangedIntent(int action) {
+           Intent intent = new Intent(ACTION_FM_SETTING);
+           intent.putExtra("state", action);
+           Log.d(LOGTAG, "Sending  FM SETTING Change intent for = " + action);
+           getApplicationContext().sendBroadcast(intent);
+        }
 }