Corp badging for Recents

Badge apps that belong to the corp profile when shown in the recents list.

Add a userId to TaskKey to help with caching.

Bug: 11676348

Change-Id: I92c8262dce763de89497977fcc6a53899afda136
diff --git a/packages/SystemUI/src/com/android/systemui/recent/RecentTasksLoader.java b/packages/SystemUI/src/com/android/systemui/recent/RecentTasksLoader.java
index eb09335..aa4e69a 100644
--- a/packages/SystemUI/src/com/android/systemui/recent/RecentTasksLoader.java
+++ b/packages/SystemUI/src/com/android/systemui/recent/RecentTasksLoader.java
@@ -17,10 +17,12 @@
 package com.android.systemui.recent;
 
 import android.app.ActivityManager;
+import android.app.AppGlobals;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.ActivityInfo;
+import android.content.pm.IPackageManager;
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.content.res.Resources;
@@ -30,7 +32,9 @@
 import android.os.AsyncTask;
 import android.os.Handler;
 import android.os.Process;
+import android.os.RemoteException;
 import android.os.UserHandle;
+import android.os.UserManager;
 import android.util.Log;
 import android.view.MotionEvent;
 import android.view.View;
@@ -162,9 +166,14 @@
             intent.setComponent(origActivity);
         }
         final PackageManager pm = mContext.getPackageManager();
+        final IPackageManager ipm = AppGlobals.getPackageManager();
         intent.setFlags((intent.getFlags()&~Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED)
                 | Intent.FLAG_ACTIVITY_NEW_TASK);
-        final ResolveInfo resolveInfo = pm.resolveActivity(intent, 0);
+        ResolveInfo resolveInfo = null;
+        try {
+            resolveInfo = ipm.resolveIntent(intent, null, 0, userId);
+        } catch (RemoteException re) {
+        }
         if (resolveInfo != null) {
             final ActivityInfo info = resolveInfo.activityInfo;
             final String title = info.loadLabel(pm).toString();
@@ -192,7 +201,11 @@
         final PackageManager pm = mContext.getPackageManager();
         Bitmap thumbnail = am.getTaskTopThumbnail(td.persistentTaskId);
         Drawable icon = getFullResIcon(td.resolveInfo, pm);
-
+        if (td.userId != UserHandle.myUserId()) {
+            // Need to badge the icon
+            final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
+            icon = um.getBadgedDrawableForUser(icon, new UserHandle(td.userId));
+        }
         if (DEBUG) Log.v(TAG, "Loaded bitmap for task "
                 + td + ": " + thumbnail);
         synchronized (td) {
diff --git a/packages/SystemUI/src/com/android/systemui/recents/RecentsTaskLoader.java b/packages/SystemUI/src/com/android/systemui/recents/RecentsTaskLoader.java
index 754d956..5aa99bb 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/RecentsTaskLoader.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/RecentsTaskLoader.java
@@ -192,8 +192,9 @@
                                     " forceLoad: " + forceLoadTask);
                     // Load the application icon
                     if (loadIcon == null || forceLoadTask) {
-                        ActivityInfo info = ssp.getActivityInfo(t.key.baseIntent.getComponent());
-                        Drawable icon = ssp.getActivityIcon(info);
+                        ActivityInfo info = ssp.getActivityInfo(t.key.baseIntent.getComponent(),
+                                t.userId);
+                        Drawable icon = ssp.getActivityIcon(info, t.userId);
                         if (!mCancelled) {
                             if (icon != null) {
                                 Console.log(Constants.DebugFlags.App.TaskDataLoader,
@@ -402,7 +403,7 @@
         int taskCount = tasks.size();
         for (int i = 0; i < taskCount; i++) {
             ActivityManager.RecentTaskInfo t = tasks.get(i);
-            ActivityInfo info = ssp.getActivityInfo(t.baseIntent.getComponent());
+            ActivityInfo info = ssp.getActivityInfo(t.baseIntent.getComponent(), t.userId);
             String activityLabel = (t.activityLabel == null ? ssp.getActivityLabel(info) :
                     t.activityLabel.toString());
             Bitmap activityIcon = t.activityIcon;
@@ -428,7 +429,7 @@
                     }
                 }
                 if (task.applicationIcon == null) {
-                    task.applicationIcon = ssp.getActivityIcon(info);
+                    task.applicationIcon = ssp.getActivityIcon(info, task.userId);
                     if (task.applicationIcon != null) {
                         mApplicationIconCache.put(task.key, task.applicationIcon);
                     } else {
diff --git a/packages/SystemUI/src/com/android/systemui/recents/SystemServicesProxy.java b/packages/SystemUI/src/com/android/systemui/recents/SystemServicesProxy.java
index 505238d..7f0d9ee 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/SystemServicesProxy.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/SystemServicesProxy.java
@@ -18,14 +18,19 @@
 
 import android.app.ActivityManager;
 import android.app.ActivityOptions;
+import android.app.AppGlobals;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.ActivityInfo;
+import android.content.pm.IPackageManager;
 import android.content.pm.PackageManager;
 import android.graphics.Bitmap;
 import android.graphics.drawable.ColorDrawable;
 import android.graphics.drawable.Drawable;
+import android.os.RemoteException;
+import android.os.UserHandle;
+import android.os.UserManager;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -38,6 +43,8 @@
 public class SystemServicesProxy {
     ActivityManager mAm;
     PackageManager mPm;
+    IPackageManager mIpm;
+    UserManager mUm;
     String mPackage;
 
     Bitmap mDummyIcon;
@@ -46,6 +53,8 @@
     public SystemServicesProxy(Context context) {
         mAm = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
         mPm = context.getPackageManager();
+        mUm = (UserManager) context.getSystemService(Context.USER_SERVICE);
+        mIpm = AppGlobals.getPackageManager();
         mPackage = context.getPackageName();
 
         if (Constants.DebugFlags.App.EnableSystemServicesProxy) {
@@ -142,14 +151,19 @@
         mAm.removeTask(taskId, ActivityManager.REMOVE_TASK_KILL_PROCESS);
     }
 
-    /** Returns the activity info for a given component name */
-    public ActivityInfo getActivityInfo(ComponentName cn) {
-        if (mPm == null) return null;
+    /**
+     * Returns the activity info for a given component name.
+     * 
+     * @param ComponentName The component name of the activity.
+     * @param userId The userId of the user that this is for.
+     */
+    public ActivityInfo getActivityInfo(ComponentName cn, int userId) {
+        if (mIpm == null) return null;
         if (Constants.DebugFlags.App.EnableSystemServicesProxy) return null;
 
         try {
-            return mPm.getActivityInfo(cn, PackageManager.GET_META_DATA);
-        } catch (PackageManager.NameNotFoundException e) {
+            return mIpm.getActivityInfo(cn, PackageManager.GET_META_DATA, userId);
+        } catch (RemoteException e) {
             e.printStackTrace();
             return null;
         }
@@ -167,15 +181,22 @@
         return info.loadLabel(mPm).toString();
     }
 
-    /** Returns the activity icon */
-    public Drawable getActivityIcon(ActivityInfo info) {
-        if (mPm == null) return null;
+    /**
+     * Returns the activity icon for the ActivityInfo for a user, badging if
+     * necessary.
+     */
+    public Drawable getActivityIcon(ActivityInfo info, int userId) {
+        if (mPm == null || mUm == null) return null;
 
         // If we are mocking, then return a mock label
         if (Constants.DebugFlags.App.EnableSystemServicesProxy) {
             return new ColorDrawable(0xFF666666);
         }
 
-        return info.loadIcon(mPm);
+        Drawable icon = info.loadIcon(mPm);
+        if (userId != UserHandle.myUserId()) {
+            icon = mUm.getBadgedDrawableForUser(icon, new UserHandle(userId));
+        }
+        return icon;
     }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/recents/model/Task.java b/packages/SystemUI/src/com/android/systemui/recents/model/Task.java
index a0ff3b7..9da1b47 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/model/Task.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/model/Task.java
@@ -37,25 +37,33 @@
     public static class TaskKey {
         public final int id;
         public final Intent baseIntent;
+        public final int userId;
 
-        public TaskKey(int id, Intent intent) {
+        public TaskKey(int id, Intent intent, int userId) {
             this.id = id;
             this.baseIntent = intent;
+            this.userId = userId;
         }
 
         @Override
         public boolean equals(Object o) {
-            return hashCode() == o.hashCode();
+            if (!(o instanceof TaskKey)) {
+                return false;
+            }
+            return id == ((TaskKey) o).id
+                    && userId == ((TaskKey) o).userId;
         }
 
         @Override
         public int hashCode() {
-            return id;
+            return (id << 5) + userId;
         }
 
         @Override
         public String toString() {
-            return "Task.Key: " + id + ", " + baseIntent.getComponent().getPackageName();
+            return "Task.Key: " + id + ", "
+                    + "u" + userId + ", "
+                    + baseIntent.getComponent().getPackageName();
         }
     }
 
@@ -71,7 +79,7 @@
 
     public Task(int id, boolean isActive, Intent intent, String activityTitle,
                 Bitmap activityIcon, int userId) {
-        this.key = new TaskKey(id, intent);
+        this.key = new TaskKey(id, intent, userId);
         this.activityLabel = activityTitle;
         this.activityIcon = activityIcon;
         this.isActive = isActive;