Merge "fm: Sleep timer does not stop after force stop"
diff --git a/FMRecord/src/com/codeaurora/fmrecording/FMRecordingService.java b/FMRecord/src/com/codeaurora/fmrecording/FMRecordingService.java
index 39db48a..e542512 100644
--- a/FMRecord/src/com/codeaurora/fmrecording/FMRecordingService.java
+++ b/FMRecord/src/com/codeaurora/fmrecording/FMRecordingService.java
@@ -64,6 +64,9 @@
 import android.R.drawable;
 import android.content.ComponentName;
 import android.content.res.Resources;
+import android.os.Process;
+import android.app.ActivityManager;
+import android.app.ActivityManager.RunningAppProcessInfo;
 
 public class FMRecordingService extends Service {
     private static final String TAG     = "FMRecordingService";
@@ -86,6 +89,10 @@
     static final int START = 1;
     static final int STOP = 0;
     private int mRecordDuration = -1;
+    private Thread mStatusCheckThread = null;
+    private int clientPid = -1;
+    private String clientProcessName = "";
+
     public void onCreate() {
 
         super.onCreate();
@@ -319,6 +326,7 @@
         sendRecordingStatusIntent(STOP);
         saveFile();
         stopForeground(true);
+        stopClientStatusCheck();
     }
 
     private void saveFile() {
@@ -464,11 +472,15 @@
                      Log.d(TAG, " action = " +action);
                      if (action.equals(ACTION_FM_RECORDING)) {
                          int state = intent.getIntExtra("state", STOP);
-                         Log.d(TAG, "ACTION_FM_RECORDING Intent received" +state);
+                         Log.d(TAG, "ACTION_FM_RECORDING Intent received" + state);
                          if (state == START) {
                              Log.d(TAG, "Recording start");
                              mRecordDuration = intent.getIntExtra("record_duration", mRecordDuration);
-                             startRecord();
+                             if(startRecord()) {
+                                clientProcessName = intent.getStringExtra("process_name");
+                                clientPid = intent.getIntExtra("process_id", -1);
+                                startClientStatusCheck();
+                             }
                          } else if (state == STOP) {
                              Log.d(TAG, "Stop recording");
                              stopRecord();
@@ -482,4 +494,62 @@
         }
     }
 
+    private boolean getClientStatus(int pid, String processName) {
+      boolean status = false;
+      ActivityManager actvityManager =
+                    (ActivityManager)this.getSystemService(
+                                                           this.ACTIVITY_SERVICE);
+
+      List<RunningAppProcessInfo> procInfos =
+                                     actvityManager.getRunningAppProcesses();
+
+      for(RunningAppProcessInfo procInfo : procInfos) {
+         if ((pid == procInfo.pid)
+               &&
+              (procInfo.processName.equals(processName))) {
+              status = true;
+              break;
+         }
+      }
+      procInfos.clear();
+      return status;
+    }
+
+    private Runnable clientStatusCheckThread = new Runnable() {
+       @Override
+       public void run() {
+          while(!Thread.currentThread().isInterrupted()) {
+                try {
+                     if(!getClientStatus(clientPid, clientProcessName)) {
+                        stopRecord();
+                        break;
+                     };
+                     Thread.sleep(500);
+                }catch(Exception e) {
+                     Log.d(TAG, "Client status check thread interrupted");
+                     break;
+                }
+          }
+       }
+    };
+
+   private void startClientStatusCheck() {
+       if((mStatusCheckThread == null) ||
+          (mStatusCheckThread.getState() == Thread.State.TERMINATED)) {
+           mStatusCheckThread = new Thread(null,
+                                           clientStatusCheckThread,
+                                           "GetClientStatus");
+       }
+       if((mStatusCheckThread != null) &&
+          (mStatusCheckThread.getState() == Thread.State.NEW)) {
+           mStatusCheckThread.start();
+       }
+   }
+
+   private void stopClientStatusCheck() {
+       if(mStatusCheckThread != null) {
+          mStatusCheckThread.interrupt();
+       }
+   }
+
 }
diff --git a/fmapp/src/com/codeaurora/fmradio/FMRadioService.java b/fmapp/src/com/codeaurora/fmradio/FMRadioService.java
index 4505e9f..db64782 100644
--- a/fmapp/src/com/codeaurora/fmradio/FMRadioService.java
+++ b/fmapp/src/com/codeaurora/fmradio/FMRadioService.java
@@ -82,6 +82,9 @@
 import android.content.IntentFilter;
 import android.app.IntentService;
 import android.os.UserHandle;
+import android.os.Process;
+import android.app.ActivityManager;
+import android.app.ActivityManager.RunningAppProcessInfo;
 
 /**
  * Provides "background" FM Radio (that uses the hardware) capabilities,
@@ -688,6 +691,24 @@
       return true;
    }
 
+   private String getProcessName() {
+      int id = Process.myPid();
+      String myProcessName = this.getPackageName();
+
+      ActivityManager actvityManager =
+             (ActivityManager)this.getSystemService(this.ACTIVITY_SERVICE);
+      List<RunningAppProcessInfo> procInfos =
+             actvityManager.getRunningAppProcesses();
+
+      for(RunningAppProcessInfo procInfo : procInfos) {
+         if (id == procInfo.pid) {
+              myProcessName = procInfo.processName;
+         }
+      }
+      procInfos.clear();
+      return myProcessName;
+   }
+
    private void sendRecordIntent(int action) {
        Intent intent = new Intent(ACTION_FM_RECORDING);
        intent.putExtra("state", action);
@@ -698,6 +719,8 @@
              mRecordDuration = (FmSharedPreferences.getRecordDuration() * 60 * 1000);
           }
           intent.putExtra("record_duration", mRecordDuration);
+          intent.putExtra("process_name", getProcessName());
+          intent.putExtra("process_id", Process.myPid());
         }
        Log.d(LOGTAG, "Sending Recording intent for = " +action);
        getApplicationContext().sendBroadcast(intent);
diff --git a/fmapp2/src/com/caf/fmradio/FMRadioService.java b/fmapp2/src/com/caf/fmradio/FMRadioService.java
index b162627..c7abf00 100644
--- a/fmapp2/src/com/caf/fmradio/FMRadioService.java
+++ b/fmapp2/src/com/caf/fmradio/FMRadioService.java
@@ -80,6 +80,9 @@
 import android.content.ComponentName;
 import android.os.StatFs;
 import android.os.SystemClock;
+import android.os.Process;
+import android.app.ActivityManager;
+import android.app.ActivityManager.RunningAppProcessInfo;
 
 /**
  * Provides "background" FM Radio (that uses the hardware) capabilities,
@@ -686,6 +689,24 @@
       return true;
    }
 
+   private String getProcessName() {
+      int id = Process.myPid();
+      String myProcessName = this.getPackageName();
+
+      ActivityManager actvityManager =
+              (ActivityManager)this.getSystemService(this.ACTIVITY_SERVICE);
+      List<RunningAppProcessInfo> procInfos =
+              actvityManager.getRunningAppProcesses();
+
+      for(RunningAppProcessInfo procInfo : procInfos) {
+         if (id == procInfo.pid) {
+              myProcessName = procInfo.processName;
+         }
+      }
+      procInfos.clear();
+      return myProcessName;
+   }
+
    private void sendRecordIntent(int action) {
        Intent intent = new Intent(ACTION_FM_RECORDING);
        intent.putExtra("state", action);
@@ -696,6 +717,8 @@
              mRecordDuration = (FmSharedPreferences.getRecordDuration() * 60 * 1000);
           }
           intent.putExtra("record_duration", mRecordDuration);
+          intent.putExtra("process_name", getProcessName());
+          intent.putExtra("process_id", Process.myPid());
         }
        Log.d(LOGTAG, "Sending Recording intent for = " +action);
        getApplicationContext().sendBroadcast(intent);