Fix the case where an intent bounces several times between users.

An intent may bounce several times between users.
In this case, we want mContentUserHint to refer to the original
user.

BUG:19656340
Change-Id: I22a35fab0c228140dcb223899f5e38ff33ee5aed
diff --git a/core/java/android/content/Intent.java b/core/java/android/content/Intent.java
index b1d80f0..c9f9b56 100644
--- a/core/java/android/content/Intent.java
+++ b/core/java/android/content/Intent.java
@@ -6181,8 +6181,12 @@
      * who sent the intent.
      * @hide
      */
-    public void setContentUserHint(int contentUserHint) {
-        mContentUserHint = contentUserHint;
+    public void prepareToLeaveUser(int userId) {
+        // If mContentUserHint is not UserHandle.USER_CURRENT, the intent has already left a user.
+        // We want mContentUserHint to refer to the original user, so don't do anything.
+        if (mContentUserHint == UserHandle.USER_CURRENT) {
+            mContentUserHint = userId;
+        }
     }
 
     /**
diff --git a/core/java/com/android/internal/app/IntentForwarderActivity.java b/core/java/com/android/internal/app/IntentForwarderActivity.java
index f598828..233bee3 100644
--- a/core/java/com/android/internal/app/IntentForwarderActivity.java
+++ b/core/java/com/android/internal/app/IntentForwarderActivity.java
@@ -87,9 +87,9 @@
         if (canForward(newIntent, targetUserId)) {
             if (newIntent.getAction().equals(Intent.ACTION_CHOOSER)) {
                 Intent innerIntent = (Intent) newIntent.getParcelableExtra(Intent.EXTRA_INTENT);
-                innerIntent.setContentUserHint(callingUserId);
+                innerIntent.prepareToLeaveUser(callingUserId);
             } else {
-                newIntent.setContentUserHint(callingUserId);
+                newIntent.prepareToLeaveUser(callingUserId);
             }
 
             final android.content.pm.ResolveInfo ri = getPackageManager().resolveActivityAsUser(
diff --git a/services/core/java/com/android/server/am/ActivityStack.java b/services/core/java/com/android/server/am/ActivityStack.java
index 0714d36..8c3a950 100644
--- a/services/core/java/com/android/server/am/ActivityStack.java
+++ b/services/core/java/com/android/server/am/ActivityStack.java
@@ -2829,7 +2829,7 @@
                     + " res=" + resultCode + " data=" + resultData);
             if (resultTo.userId != r.userId) {
                 if (resultData != null) {
-                    resultData.setContentUserHint(r.userId);
+                    resultData.prepareToLeaveUser(r.userId);
                 }
             }
             if (r.info.applicationInfo.uid > 0) {