Merge "FM: Close the service and end the process on user switch"
diff --git a/fmapp2/src/com/caf/fmradio/FMRadioService.java b/fmapp2/src/com/caf/fmradio/FMRadioService.java
index 71dc72c..f605749 100644
--- a/fmapp2/src/com/caf/fmradio/FMRadioService.java
+++ b/fmapp2/src/com/caf/fmradio/FMRadioService.java
@@ -101,6 +101,7 @@
 import android.bluetooth.BluetoothDevice;
 import android.os.IBinder.DeathRecipient;
 
+
 /**
  * Provides "background" FM Radio (that uses the hardware) capabilities,
  * allowing the user to switch between activities without stopping playback.
@@ -128,6 +129,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;
@@ -262,6 +264,7 @@
       registerDelayedServiceStop();
       registerExternalStorageListener();
       registerAirplaneModeStatusChanged();
+      registerUserSwitch();
 
       mSession = new MediaSession(getApplicationContext(), this.getClass().getName());
       mSession.setCallback(mSessionCallback);
@@ -348,6 +351,10 @@
           unregisterReceiver(mSdcardUnmountReceiver);
           mSdcardUnmountReceiver = null;
       }
+      if (mRegisterUserSwitched != null) {
+          unregisterReceiver(mRegisterUserSwitched);
+          mRegisterUserSwitched = null;
+      }
       /* Since the service is closing, disable the receiver */
       if (isFmOn())
           fmOff();
@@ -616,6 +623,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