FM: Close the service and end the process on user switch

Close the FM activity and end the process on user switch. Now, FM
application should be launched by user whenever we jumps back
to owner from guest role.

Change-Id: I40750e8b47073df3c62f2e3012162a313b3996ce
CRs-fixed: 2072913
diff --git a/fmapp2/src/com/caf/fmradio/FMRadioService.java b/fmapp2/src/com/caf/fmradio/FMRadioService.java
index a40700d..4c0ef18 100644
--- a/fmapp2/src/com/caf/fmradio/FMRadioService.java
+++ b/fmapp2/src/com/caf/fmradio/FMRadioService.java
@@ -100,6 +100,7 @@
 import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothDevice;
 
+
 /**
  * Provides "background" FM Radio (that uses the hardware) capabilities,
  * allowing the user to switch between activities without stopping playback.
@@ -127,6 +128,7 @@
    private boolean mOverA2DP = false;
    private BroadcastReceiver mFmMediaButtonListener;
    private BroadcastReceiver mAirplaneModeChanged;
+   private BroadcastReceiver mRegisterUserSwitched;
    private IFMRadioServiceCallbacks mCallbacks;
    private static FmSharedPreferences mPrefs;
    private boolean mHeadsetPlugged = false;
@@ -254,6 +256,7 @@
       registerDelayedServiceStop();
       registerExternalStorageListener();
       registerAirplaneModeStatusChanged();
+      registerUserSwitch();
 
       mSession = new MediaSession(getApplicationContext(), this.getClass().getName());
       mSession.setCallback(mSessionCallback);
@@ -340,6 +343,10 @@
           unregisterReceiver(mSdcardUnmountReceiver);
           mSdcardUnmountReceiver = null;
       }
+      if (mRegisterUserSwitched != null) {
+          unregisterReceiver(mRegisterUserSwitched);
+          mRegisterUserSwitched = null;
+      }
       /* Since the service is closing, disable the receiver */
       if (isFmOn())
           fmOff();
@@ -608,6 +615,41 @@
        }
    }
 
+    public void registerUserSwitch(){
+        if (mRegisterUserSwitched == null) {
+            mRegisterUserSwitched = new BroadcastReceiver() {
+                @Override
+                public void onReceive(Context context, Intent intent) {
+                    String action = intent.getAction();
+                    Log.d(LOGTAG, "on receive UserSwitched " + action);
+                    if (action.equals(Intent.ACTION_USER_SWITCHED)) {
+                        Log.d(LOGTAG, "ACTION_USER_SWITCHED Intent received");
+                        int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0);
+                        Log.d(LOGTAG, "ACTION_USER_SWITCHED, user ID:" + userId);
+                        if (userId == 0) {
+                            if (isFmOn()){
+                                fmOff();
+                                try {
+                                    if ((mServiceInUse) && (mCallbacks != null) ) {
+                                        mCallbacks.onDisabled();
+                                    }
+                                } catch (RemoteException e) {
+                                     e.printStackTrace();
+                                }
+                            }
+                            stop();
+                            android.os.Process.killProcess(android.os.Process.myPid());
+                            System.exit(0);
+                        }
+                    }
+                }
+            };
+            IntentFilter iFilter = new IntentFilter();
+            iFilter.addAction(Intent.ACTION_USER_SWITCHED);
+            registerReceiver(mRegisterUserSwitched, iFilter);
+       }
+   }
+
      /**
      * Registers an intent to listen for ACTION_HEADSET_PLUG
      * notifications. This intent is called to know if the headset