NoMan: Optimize profile badge retrieval

Instead of stuffing the profile badge into extras, store the
originating user and read the badge via UserManager.

Bug: 16735645
Change-Id: Ia5f9b6f113dcbc88581af5308585edcf9b92b88d
diff --git a/core/java/android/app/Notification.java b/core/java/android/app/Notification.java
index 07b3633..21525bc 100644
--- a/core/java/android/app/Notification.java
+++ b/core/java/android/app/Notification.java
@@ -813,14 +813,12 @@
      */
     public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
 
-
     /**
-     * {@link #extras} key: Bitmap representing the profile badge to be shown with the
-     * notification.
+     * {@link #extras} key: the user that built the notification.
      *
      * @hide
      */
-    public static final String EXTRA_PROFILE_BADGE = "android.profileBadge";
+    public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
 
     /**
      * Value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification should not be
@@ -1870,7 +1868,11 @@
         private final NotificationColorUtil mColorUtil;
         private ArrayList<String> mPeople;
         private int mColor = COLOR_DEFAULT;
-        private Bitmap mProfileBadge;
+
+        /**
+         * The user that built the notification originally.
+         */
+        private int mOriginatingUserId;
 
         /**
          * Contains extras related to rebuilding during the build phase.
@@ -2579,8 +2581,10 @@
         }
 
         private Bitmap getProfileBadge() {
+            // Note: This assumes that the current user can read the profile badge of the
+            // originating user.
             UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
-            Drawable badge = userManager.getBadgeForUser(android.os.Process.myUserHandle());
+            Drawable badge = userManager.getBadgeForUser(new UserHandle(mOriginatingUserId));
             if (badge == null) {
                 return null;
             }
@@ -2594,6 +2598,7 @@
         }
 
         private RemoteViews applyStandardTemplate(int resId, boolean fitIn1U) {
+            Bitmap profileIcon = getProfileBadge();
             RemoteViews contentView = new BuilderRemoteViews(mContext.getPackageName(), resId);
             boolean showLine3 = false;
             boolean showLine2 = false;
@@ -2601,8 +2606,8 @@
             if (mPriority < PRIORITY_LOW) {
                 // TODO: Low priority presentation
             }
-            if (mProfileBadge != null) {
-                contentView.setImageViewBitmap(R.id.profile_icon, mProfileBadge);
+            if (profileIcon != null) {
+                contentView.setImageViewBitmap(R.id.profile_icon, profileIcon);
                 contentView.setViewVisibility(R.id.profile_icon, View.VISIBLE);
             } else {
                 contentView.setViewVisibility(R.id.profile_icon, View.GONE);
@@ -2927,6 +2932,7 @@
          */
         public void populateExtras(Bundle extras) {
             // Store original information used in the construction of this object
+            extras.putInt(EXTRA_ORIGINATING_USERID, mOriginatingUserId);
             extras.putString(EXTRA_REBUILD_CONTEXT_PACKAGE, mContext.getPackageName());
             extras.putCharSequence(EXTRA_TITLE, mContentTitle);
             extras.putCharSequence(EXTRA_TEXT, mContentText);
@@ -2944,9 +2950,6 @@
             if (!mPeople.isEmpty()) {
                 extras.putStringArray(EXTRA_PEOPLE, mPeople.toArray(new String[mPeople.size()]));
             }
-            if (mProfileBadge != null) {
-                extras.putParcelable(EXTRA_PROFILE_BADGE, mProfileBadge);
-            }
             // NOTE: If you're adding new extras also update restoreFromNotification().
         }
 
@@ -3157,6 +3160,7 @@
 
             // Extras.
             Bundle extras = n.extras;
+            mOriginatingUserId = extras.getInt(EXTRA_ORIGINATING_USERID);
             mContentTitle = extras.getCharSequence(EXTRA_TITLE);
             mContentText = extras.getCharSequence(EXTRA_TEXT);
             mSubText = extras.getCharSequence(EXTRA_SUB_TEXT);
@@ -3174,9 +3178,6 @@
                 mPeople.clear();
                 Collections.addAll(mPeople, extras.getStringArray(EXTRA_PEOPLE));
             }
-            if (extras.containsKey(EXTRA_PROFILE_BADGE)) {
-                mProfileBadge = extras.getParcelable(EXTRA_PROFILE_BADGE);
-            }
         }
 
         /**
@@ -3192,7 +3193,7 @@
          * object.
          */
         public Notification build() {
-            mProfileBadge = getProfileBadge();
+            mOriginatingUserId = mContext.getUserId();
 
             Notification n = buildUnstyled();