Merge "PBAP: Update intent parameters for connection"
diff --git a/src/com/android/bluetooth/Utils.java b/src/com/android/bluetooth/Utils.java
index a16d3b9..73a5ad1 100644
--- a/src/com/android/bluetooth/Utils.java
+++ b/src/com/android/bluetooth/Utils.java
@@ -204,15 +204,19 @@
         int callingUser = UserHandle.getCallingUserId();
         int callingUid = Binder.getCallingUid();
         long ident = Binder.clearCallingIdentity();
+        Log.d(TAG,"callingUid =" + callingUid);
 
         try {
             // With calling identity cleared the current user is the foreground user.
             int foregroundUser = ActivityManager.getCurrentUser();
             ok = (foregroundUser == callingUser);
+            Log.e(TAG, "foregroundUser =" + foregroundUser);
+            Log.e(TAG, "callingUser =" + callingUser);
             if (!ok) {
                 // Always allow SystemUI/System access.
                 int systemUiUid = ActivityThread.getPackageManager().getPackageUid(
                         "com.android.systemui", UserHandle.USER_OWNER);
+                Log.d(TAG," systemUiUid :" + systemUiUid);
                 ok = (systemUiUid == callingUid) || (Process.SYSTEM_UID == callingUid);
             }
         } catch (Exception ex) {
diff --git a/src/com/android/bluetooth/map/BluetoothMapService.java b/src/com/android/bluetooth/map/BluetoothMapService.java
index e130599..8cf69f6 100644
--- a/src/com/android/bluetooth/map/BluetoothMapService.java
+++ b/src/com/android/bluetooth/map/BluetoothMapService.java
@@ -573,6 +573,12 @@
         if (DEBUG) Log.d(TAG, "start()");
         if(!VERBOSE)
         VERBOSE = Log.isLoggable(LOG_TAG, Log.VERBOSE);
+
+        if (!Utils.checkCaller()) {
+            Log.w(TAG, "start received for non-active user, ignoring");
+            return false;
+        }
+
         if (VERBOSE) Log.v(TAG, "verbose logging is enabled");
         IntentFilter filter = new IntentFilter();
         filter.addAction(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY);
diff --git a/src/com/android/bluetooth/opp/BluetoothOppReceiver.java b/src/com/android/bluetooth/opp/BluetoothOppReceiver.java
index 6efa650..a6770ab 100644
--- a/src/com/android/bluetooth/opp/BluetoothOppReceiver.java
+++ b/src/com/android/bluetooth/opp/BluetoothOppReceiver.java
@@ -48,6 +48,7 @@
 import android.net.Uri;
 import android.util.Log;
 import android.widget.Toast;
+import com.android.bluetooth.Utils;
 
 /**
  * Receives and handles: system broadcasts; Intents from other applications;
@@ -62,6 +63,10 @@
     public void onReceive(Context context, Intent intent) {
         String action = intent.getAction();
 
+        if (!Utils.checkCaller()) {
+            Log.w(TAG, action + " received for non-active user, ignoring!!");
+            return;
+        }
 
         if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
             if (BluetoothAdapter.STATE_ON == intent.getIntExtra(
diff --git a/src/com/android/bluetooth/pbap/BluetoothPbapService.java b/src/com/android/bluetooth/pbap/BluetoothPbapService.java
index c2be509..dcfc5c2 100644
--- a/src/com/android/bluetooth/pbap/BluetoothPbapService.java
+++ b/src/com/android/bluetooth/pbap/BluetoothPbapService.java
@@ -200,6 +200,11 @@
         mInterrupted = false;
         mAdapter = BluetoothAdapter.getDefaultAdapter();
 
+        if (!Utils.checkCaller()) {
+            Log.w(TAG, "onCreate received for non-active user, ignoring");
+            return;
+        }
+
         if (!mHasStarted) {
             mHasStarted = true;
             if (VERBOSE) Log.v(TAG, "Starting PBAP service");
diff --git a/src/com/android/bluetooth/sap/SapService.java b/src/com/android/bluetooth/sap/SapService.java
index 5dc9033..b3cc74d 100644
--- a/src/com/android/bluetooth/sap/SapService.java
+++ b/src/com/android/bluetooth/sap/SapService.java
@@ -486,10 +486,20 @@
     };
 
     private void setState(int state) {
-        setState(state, BluetoothSap.RESULT_SUCCESS);
+        setState(state, BluetoothSap.RESULT_SUCCESS, false);
     }
 
-    private synchronized void setState(int state, int result) {
+    private void setState(int state, int result, boolean force) {
+        if (!force) {
+            synchronized (this) {
+                setState(state, result);
+            }
+        } else {
+            setState(state, result);
+        }
+    }
+
+    private void setState(int state, int result) {
         if (state != mState) {
             if (DEBUG) Log.d(TAG, "Sap state " + mState + " -> " + state + ", result = "
                     + result);
@@ -527,7 +537,8 @@
                 switch (mState) {
                     case BluetoothSap.STATE_CONNECTED:
                         closeConnectionSocket();
-                        setState(BluetoothSap.STATE_DISCONNECTED, BluetoothSap.RESULT_CANCELED);
+                        setState(BluetoothSap.STATE_DISCONNECTED, BluetoothSap.RESULT_CANCELED,
+                            false);
                         result = true;
                         break;
                     default:
@@ -637,7 +648,7 @@
         } catch (Exception e) {
             Log.w(TAG,"Unable to unregister sap receiver",e);
         }
-        setState(BluetoothSap.STATE_DISCONNECTED, BluetoothSap.RESULT_CANCELED);
+        setState(BluetoothSap.STATE_DISCONNECTED, BluetoothSap.RESULT_CANCELED, true);
         CountDownLatch latch = new CountDownLatch(1);
         sendShutdownMessage(latch);
         // We need to wait for shutdown to complete to avoid being garbage collected before
@@ -661,7 +672,7 @@
     }
 
     public boolean cleanup()  {
-        setState(BluetoothSap.STATE_DISCONNECTED, BluetoothSap.RESULT_CANCELED);
+        setState(BluetoothSap.STATE_DISCONNECTED, BluetoothSap.RESULT_CANCELED, true);
         closeService(null); // No latch needed as the call is blocking
         if(mSessionStatusHandler != null) {
             mSessionStatusHandler.removeCallbacksAndMessages(null);