Merge "FP2-1061:"Unfortuately,Settings has stopped" pops up after change to Owner" into fp2-dev-v2
diff --git a/src/org/codeaurora/bluetooth/dun/BluetoothDunReceiver.java b/src/org/codeaurora/bluetooth/dun/BluetoothDunReceiver.java
index 60f3dc7..65f8ce1 100644
--- a/src/org/codeaurora/bluetooth/dun/BluetoothDunReceiver.java
+++ b/src/org/codeaurora/bluetooth/dun/BluetoothDunReceiver.java
@@ -34,6 +34,13 @@
 import android.content.Intent;
 import android.util.Log;
 import android.os.SystemProperties;
+import android.os.UserHandle;
+import android.os.UserManager;
+import android.app.ActivityManager;
+import android.app.ActivityThread;
+import android.content.pm.UserInfo;
+import android.os.Binder;
+import android.os.Process;
 
 public class BluetoothDunReceiver extends BroadcastReceiver {
 
@@ -41,6 +48,33 @@
 
     private static final boolean V = BluetoothDunService.VERBOSE;
 
+    public static boolean checkCaller() {
+        boolean ok;
+        // Get the caller's user id then clear the calling identity
+        // which will be restored in the finally clause.
+        int callingUser = UserHandle.getCallingUserId();
+        int callingUid = Binder.getCallingUid();
+        long ident = Binder.clearCallingIdentity();
+
+        try {
+            // With calling identity cleared the current user is the foreground user.
+            int foregroundUser = ActivityManager.getCurrentUser();
+            ok = (foregroundUser == callingUser);
+            if (!ok) {
+                // Always allow SystemUI/System access.
+                int systemUiUid = ActivityThread.getPackageManager().getPackageUid(
+                        "com.android.systemui", UserHandle.USER_OWNER);
+                ok = (systemUiUid == callingUid) || (Process.SYSTEM_UID == callingUid);
+            }
+        } catch (Exception ex) {
+            Log.e(TAG, "checkIfCallerIsSelfOrForegroundUser: Exception ex=" + ex);
+            ok = false;
+        } finally {
+            Binder.restoreCallingIdentity(ident);
+        }
+        return ok;
+    }
+
     @Override
     public void onReceive(Context context, Intent intent) {
 
@@ -50,6 +84,11 @@
 
         if (V) Log.v(TAG, "DunReceiver onReceive ");
 
+        if (!checkCaller()) {
+            Log.w(TAG, "onReceive received for non-active user, ignoring");
+            return;
+        }
+
         Intent in = new Intent();
         in.putExtras(intent);
         in.setClass(context, BluetoothDunService.class);
diff --git a/src/org/codeaurora/bluetooth/ftp/BluetoothFtpReceiver.java b/src/org/codeaurora/bluetooth/ftp/BluetoothFtpReceiver.java
index 6f443e6..3690ca5 100644
--- a/src/org/codeaurora/bluetooth/ftp/BluetoothFtpReceiver.java
+++ b/src/org/codeaurora/bluetooth/ftp/BluetoothFtpReceiver.java
@@ -33,6 +33,13 @@
 import android.content.Context;
 import android.util.Log;
 import android.os.SystemProperties;
+import android.os.UserHandle;
+import android.os.UserManager;
+import android.app.ActivityManager;
+import android.app.ActivityThread;
+import android.content.pm.UserInfo;
+import android.os.Binder;
+import android.os.Process;
 
 public class BluetoothFtpReceiver extends BroadcastReceiver {
 
@@ -40,11 +47,43 @@
 
         private static final boolean V = Log.isLoggable(BluetoothFtpService.LOG_TAG, Log.VERBOSE) ? true : false;
 
+        public static boolean checkCaller() {
+            boolean ok;
+            // Get the caller's user id then clear the calling identity
+            // which will be restored in the finally clause.
+            int callingUser = UserHandle.getCallingUserId();
+            int callingUid = Binder.getCallingUid();
+            long ident = Binder.clearCallingIdentity();
+
+            try {
+                // With calling identity cleared the current user is the foreground user.
+                int foregroundUser = ActivityManager.getCurrentUser();
+                ok = (foregroundUser == callingUser);
+                if (!ok) {
+                    // Always allow SystemUI/System access.
+                    int systemUiUid = ActivityThread.getPackageManager().getPackageUid(
+                            "com.android.systemui", UserHandle.USER_OWNER);
+                    ok = (systemUiUid == callingUid) || (Process.SYSTEM_UID == callingUid);
+                }
+            } catch (Exception ex) {
+                Log.e(TAG, "checkIfCallerIsSelfOrForegroundUser: Exception ex=" + ex);
+                ok = false;
+            } finally {
+                Binder.restoreCallingIdentity(ident);
+            }
+            return ok;
+        }
+
         @Override
         public void  onReceive  (Context context, Intent intent) {
 
             if(V) Log.v(TAG,"BluetoothFtpReceiver onReceive :" + intent.getAction());
 
+            if (!checkCaller()) {
+                Log.w(TAG, "onReceive received for non-active user, ignoring");
+                return;
+            }
+
             Intent in = new Intent();
             in.putExtras(intent);
             in.setClass(context, BluetoothFtpService.class);
diff --git a/src/org/codeaurora/bluetooth/sap/BluetoothSapReceiver.java b/src/org/codeaurora/bluetooth/sap/BluetoothSapReceiver.java
index a9b23ea..2c036be 100644
--- a/src/org/codeaurora/bluetooth/sap/BluetoothSapReceiver.java
+++ b/src/org/codeaurora/bluetooth/sap/BluetoothSapReceiver.java
@@ -34,6 +34,13 @@
 import android.content.Intent;
 import android.util.Log;
 import android.os.SystemProperties;
+import android.os.UserHandle;
+import android.os.UserManager;
+import android.app.ActivityManager;
+import android.app.ActivityThread;
+import android.content.pm.UserInfo;
+import android.os.Binder;
+import android.os.Process;
 
 public class BluetoothSapReceiver extends BroadcastReceiver {
 
@@ -41,6 +48,33 @@
 
     private static final boolean V = BluetoothSapService.VERBOSE;
 
+    public static boolean checkCaller() {
+        boolean ok;
+        // Get the caller's user id then clear the calling identity
+        // which will be restored in the finally clause.
+        int callingUser = UserHandle.getCallingUserId();
+        int callingUid = Binder.getCallingUid();
+        long ident = Binder.clearCallingIdentity();
+
+        try {
+            // With calling identity cleared the current user is the foreground user.
+            int foregroundUser = ActivityManager.getCurrentUser();
+            ok = (foregroundUser == callingUser);
+            if (!ok) {
+                // Always allow SystemUI/System access.
+                int systemUiUid = ActivityThread.getPackageManager().getPackageUid(
+                        "com.android.systemui", UserHandle.USER_OWNER);
+                ok = (systemUiUid == callingUid) || (Process.SYSTEM_UID == callingUid);
+            }
+        } catch (Exception ex) {
+            Log.e(TAG, "checkIfCallerIsSelfOrForegroundUser: Exception ex=" + ex);
+            ok = false;
+        } finally {
+            Binder.restoreCallingIdentity(ident);
+        }
+        return ok;
+    }
+
     @Override
     public void onReceive(Context context, Intent intent) {
 
@@ -48,6 +82,11 @@
             return;
         }
 
+        if (!checkCaller()) {
+            Log.w(TAG, "onReceive received for non-active user, ignoring");
+            return;
+        }
+
         if (V) Log.v(TAG, "SapReceiver onReceive ");
 
         Intent in = new Intent();