Merge "fm: Sleep timer does not stop after force stop"
diff --git a/fmapp2/src/com/caf/fmradio/FMRadio.java b/fmapp2/src/com/caf/fmradio/FMRadio.java
index 9b130ff..2b45049 100644
--- a/fmapp2/src/com/caf/fmradio/FMRadio.java
+++ b/fmapp2/src/com/caf/fmradio/FMRadio.java
@@ -387,20 +387,6 @@
       if ((mERadioTextScroller == null) && (mERadioTextTV != null)) {
           mERadioTextScroller = new ScrollerText(mERadioTextTV);
       }
-
-
-      //HDMI and FM concurrecny is not supported.
-      if (isHdmiOn()) {
-          showDialog(DIALOG_CMD_FAILED_HDMI_ON);
-      }
-      else {
-         if (false == bindToService(this, osc)) {
-             Log.d(LOGTAG, "onCreate: Failed to Start Service");
-         } else {
-             Log.d(LOGTAG, "onCreate: Start Service completed successfully");
-         }
-         registerFMSettingListner();
-      }
    }
 
    protected void setDisplayvalue(){
@@ -450,18 +436,7 @@
    public void onStop() {
       Log.d(LOGTAG, "FMRadio: onStop");
       if(isSleepTimerActive()) {
-          mSleepUpdateHandlerThread.interrupt();
-          long timeNow = ((SystemClock.elapsedRealtime()));
-          if (timeNow < mSleepAtPhoneTime) {
-              try {
-                if (null != mService) {
-                    mService.delayedStop((mSleepAtPhoneTime - timeNow),
-                                               FMRadioService.STOP_SERVICE);
-                }
-              }catch (Exception e) {
-                e.printStackTrace();
-              }
-          }
+         mSleepUpdateHandlerThread.interrupt();
       }
       if(isRecording()) {
           try {
@@ -479,36 +454,16 @@
    public void onStart() {
       super.onStart();
       Log.d(LOGTAG, "FMRadio: onStart");
-      try {
-         if(mService != null) {
-            mService.registerCallbacks(mServiceCallbacks);
+      if (isHdmiOn()) {
+          showDialog(DIALOG_CMD_FAILED_HDMI_ON);
+      }
+      else {
+         if ((mService == null ) && (false == bindToService(this, osc))) {
+             Log.d(LOGTAG, "onStart: Failed to Start Service");
+         } else {
+             Log.d(LOGTAG, "onStart: Start Service completed successfully");
          }
-      }catch (RemoteException e) {
-         e.printStackTrace();
-      }
-      if(isSleepTimerActive()) {
-          Log.d(LOGTAG, "isSleepTimerActive is true");
-          try {
-            if (null != mService) {
-                mService.cancelDelayedStop(FMRadioService.STOP_SERVICE);
-            }
-          } catch (Exception e) {
-            e.printStackTrace();
-          }
-          initiateSleepThread();
-      }
-      if(isRecording()) {
-          Log.d(LOGTAG,"isRecordTimerActive is true");
-          try {
-            if (null != mService) {
-                mService.cancelDelayedStop(FMRadioService.STOP_RECORD);
-            }
-          }catch (Exception e) {
-            e.printStackTrace();
-          }
-          if(isRecording()) {
-              initiateRecordThread();
-          }
+         registerFMSettingListner();
       }
       mPrefs.Load();
       if (mPicker != null) {
@@ -535,6 +490,40 @@
    @Override
    public void onResume() {
       super.onResume();
+      try {
+         if(mService != null) {
+            mService.registerCallbacks(mServiceCallbacks);
+         }
+      }catch (RemoteException e) {
+         e.printStackTrace();
+      }
+      if(isSleepTimerActive()) {
+          Log.d(LOGTAG, "isSleepTimerActive is true");
+          try {
+               if(null != mService) {
+                  mService.cancelDelayedStop(FMRadioService.STOP_SERVICE);
+               }
+               if(null != mSleepUpdateHandlerThread) {
+                  mSleepUpdateHandlerThread.interrupt();
+               }
+          }catch (Exception e) {
+               e.printStackTrace();
+          }
+          initiateSleepThread();
+      }
+      if(isRecording()) {
+          Log.d(LOGTAG,"isRecordTimerActive is true");
+          try {
+            if (null != mService) {
+                mService.cancelDelayedStop(FMRadioService.STOP_RECORD);
+            }
+          }catch (Exception e) {
+            e.printStackTrace();
+          }
+          if(isRecording()) {
+              initiateRecordThread();
+          }
+      }
       Log.d(LOGTAG, "FMRadio: onResume");
       mStereo = FmSharedPreferences.getLastAudioMode();
       mHandler.post(mUpdateProgramService);
@@ -2305,13 +2294,33 @@
                                                 "SleepUpdateThread");
       }
       /* If the thread state is "new" then the thread has not yet started */
-      if (mSleepUpdateHandlerThread.getState() == Thread.State.NEW) {
-         mSleepUpdateHandlerThread.start();
+      if(mSleepUpdateHandlerThread.getState() == Thread.State.NEW && isFmOn()) {
+          try {
+              if((mService != null) &&
+                 !mService.isSleepTimerActive()) {
+                 long timeNow = ((SystemClock.elapsedRealtime()));
+                 if(timeNow < mSleepAtPhoneTime) {
+                    mService.delayedStop((mSleepAtPhoneTime - timeNow),
+                                            FMRadioService.STOP_SERVICE);
+                 }
+              }
+              mSleepUpdateHandlerThread.start();
+          }catch(Exception e) {
+              e.printStackTrace();
+          }
       }
    }
 
    private void endSleepTimer() {
       mSleepAtPhoneTime = 0;
+      try {
+           if(mService != null) {
+              mService.cancelDelayedStop(FMRadioService.STOP_SERVICE);
+           }
+      }catch(RemoteException e) {
+           e.printStackTrace();
+      }
+
       if(null != mSleepUpdateHandlerThread) {
          mSleepUpdateHandlerThread.interrupt();
       }
@@ -2333,8 +2342,16 @@
 
    private boolean isSleepTimerActive() {
       boolean active = false;
-      if (mSleepAtPhoneTime > 0) {
-          active = true;
+      try {
+          if((mService != null)
+             && (mService.isSleepTimerActive())
+             && (mSleepAtPhoneTime > 0)) {
+             active = true;
+             Log.d(LOGTAG, "Sleeptimer is active");
+          }else {
+          }
+      }catch(RemoteException e) {
+          e.printStackTrace();
       }
       return active;
    }
@@ -2407,6 +2424,7 @@
                 Thread.sleep(500);
                 Message statusUpdate = new Message();
                 statusUpdate.what = SLEEPTIMER_UPDATE;
+                Log.d(LOGTAG, "SLEEP TIMER UPDATE");
                 mUIUpdateHandlerHandler.sendMessage(statusUpdate);
                 sleepTimerExpired = hasSleepTimerExpired();
             }catch (Exception ex) {
@@ -2901,6 +2919,7 @@
             mCallback.onServiceDisconnected(className);
          }
          sService = null;
+         mService = null;
       }
    }
 
@@ -2924,6 +2943,9 @@
             if (isRecording()) {
                 initiateRecordThread();
             }
+            if(isSleepTimerActive()) {
+               initiateSleepThread();
+            }
             return;
          }else {
             Log.e(LOGTAG, "IFMRadioService onServiceConnected failed");
@@ -2937,6 +2959,10 @@
          finish();
       }
       public void onServiceDisconnected(ComponentName classname) {
+         Log.d(LOGTAG, "Service got disconnected");
+         unbindFromService(FMRadio.this);
+         mService = null;
+         sService = null;
       }
    };
 
diff --git a/fmapp2/src/com/caf/fmradio/FMRadioService.java b/fmapp2/src/com/caf/fmradio/FMRadioService.java
index 2954bc2..c7abf00 100644
--- a/fmapp2/src/com/caf/fmradio/FMRadioService.java
+++ b/fmapp2/src/com/caf/fmradio/FMRadioService.java
@@ -103,6 +103,7 @@
    private BroadcastReceiver mSdcardUnmountReceiver = null;
    private BroadcastReceiver mMusicCommandListener = null;
    private BroadcastReceiver mSleepExpiredListener = null;
+   private boolean mSleepActive = false;
    private BroadcastReceiver mRecordTimeoutListener = null;
    private BroadcastReceiver mDelayedServiceStopListener = null;
    private boolean mOverA2DP = false;
@@ -652,7 +653,7 @@
    @Override
    public void onRebind(Intent intent) {
       mDelayedStopHandler.removeCallbacksAndMessages(null);
-      cancelAlarms();
+      cancelAlarmDealyedServiceStop();
       mServiceInUse = true;
       /* Application/UI is attached, so get out of lower power mode */
       setLowPowerMode(false);
@@ -666,7 +667,7 @@
       // make sure the service will shut down on its own if it was
       // just started but not bound to and nothing is playing
       mDelayedStopHandler.removeCallbacksAndMessages(null);
-      cancelAlarms();
+      cancelAlarmDealyedServiceStop();
       setAlarmDelayedServiceStop();
    }
 
@@ -1498,6 +1499,10 @@
       {
            return (mService.get().getRecordingStartTime());
       }
+      public boolean isSleepTimerActive()
+      {
+           return (mService.get().isSleepTimerActive());
+      }
    }
    private final IBinder mBinder = new ServiceStub(this);
 
@@ -2915,12 +2920,14 @@
        PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
        Log.d(LOGTAG, "delayedStop called" + SystemClock.elapsedRealtime() + duration);
        am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + duration, pi);
+       mSleepActive = true;
    }
    private void cancelAlarmSleepExpired() {
        Intent i = new Intent(SLEEP_EXPIRED_ACTION);
        AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
        am.cancel(pi);
+       mSleepActive = false;
    }
    private void setAlarmRecordTimeout(long duration) {
        Intent i = new Intent(RECORD_EXPIRED_ACTION);
@@ -2962,6 +2969,10 @@
    public long getRecordingStartTime() {
       return mSampleStart;
    }
+
+   public boolean isSleepTimerActive() {
+      return mSleepActive;
+   }
    //handling the sleep and record stop when FM App not in focus
    private void delayedStop(long duration, int nType) {
        int whatId = (nType == STOP_SERVICE) ? STOPSERVICE_ONSLEEP: STOPRECORD_ONTIMEOUT;
diff --git a/fmapp2/src/com/caf/fmradio/IFMRadioService.aidl b/fmapp2/src/com/caf/fmradio/IFMRadioService.aidl
index fe54124..2f6f1e3 100644
--- a/fmapp2/src/com/caf/fmradio/IFMRadioService.aidl
+++ b/fmapp2/src/com/caf/fmradio/IFMRadioService.aidl
@@ -71,5 +71,6 @@
     int getAfJmpRmssiSamplesCnt();
     boolean setRxRepeatCount(int count);
     long getRecordingStartTime();
+    boolean isSleepTimerActive();
 }