Make work profile apps easier to pick in ResolverActivity

Pull the "Work" profile item up out of the main list and into the
header of the resolver drawer, making it easier to discover and access
without disrupting the sort order of the list.

Bug 17935301

Change-Id: Id2d081b61828352c998e517127132f883a20b7ef
diff --git a/core/java/com/android/internal/app/ResolverActivity.java b/core/java/com/android/internal/app/ResolverActivity.java
index 2db466a..634d03d 100644
--- a/core/java/com/android/internal/app/ResolverActivity.java
+++ b/core/java/com/android/internal/app/ResolverActivity.java
@@ -95,12 +95,14 @@
     private ListView mListView;
     private Button mAlwaysButton;
     private Button mOnceButton;
+    private View mProfileView;
     private int mIconDpi;
     private int mIconSize;
     private int mMaxColumns;
     private int mLastSelected = ListView.INVALID_POSITION;
     private boolean mResolvingHome = false;
     private int mProfileSwitchMessageId = -1;
+    private Intent mIntent;
 
     private UsageStatsManager mUsm;
     private Map<String, UsageStats> mStats;
@@ -110,6 +112,9 @@
     private final PackageMonitor mPackageMonitor = new PackageMonitor() {
         @Override public void onSomePackagesChanged() {
             mAdapter.handlePackagesChanged();
+            if (mProfileView != null) {
+                bindProfileView();
+            }
         }
     };
 
@@ -217,7 +222,6 @@
 
         final long sinceTime = System.currentTimeMillis() - USAGE_STATS_PERIOD;
         mStats = mUsm.queryAndAggregateUsageStats(sinceTime, System.currentTimeMillis());
-        Log.d(TAG, "sinceTime=" + sinceTime);
 
         mMaxColumns = getResources().getInteger(R.integer.config_maxResolverActivityColumns);
 
@@ -228,7 +232,8 @@
         mIconDpi = am.getLauncherLargeIconDensity();
         mIconSize = am.getLauncherLargeIconSize();
 
-        mAdapter = new ResolveListAdapter(this, intent, initialIntents, rList,
+        mIntent = new Intent(intent);
+        mAdapter = new ResolveListAdapter(this, initialIntents, rList,
                 mLaunchedFromUid, alwaysUseOption);
 
         final int layoutId;
@@ -324,6 +329,40 @@
             setAlwaysButtonEnabled(true, mAdapter.getFilteredPosition(), false);
             mOnceButton.setEnabled(true);
         }
+
+        mProfileView = findViewById(R.id.profile_button);
+        if (mProfileView != null) {
+            mProfileView.setOnClickListener(new View.OnClickListener() {
+                @Override
+                public void onClick(View v) {
+                    final DisplayResolveInfo dri = mAdapter.getOtherProfile();
+                    if (dri == null) {
+                        return;
+                    }
+
+                    final Intent intent = intentForDisplayResolveInfo(dri);
+                    onIntentSelected(dri.ri, intent, mAlwaysUseOption);
+                    finish();
+                }
+            });
+            bindProfileView();
+        }
+    }
+
+    void bindProfileView() {
+        final DisplayResolveInfo dri = mAdapter.getOtherProfile();
+        if (dri != null) {
+            mProfileView.setVisibility(View.VISIBLE);
+            final ImageView icon = (ImageView) mProfileView.findViewById(R.id.icon);
+            final TextView text = (TextView) mProfileView.findViewById(R.id.text1);
+            if (dri.displayIcon == null) {
+                new LoadIconTask().execute(dri);
+            }
+            icon.setImageDrawable(dri.displayIcon);
+            text.setText(dri.displayLabel);
+        } else {
+            mProfileView.setVisibility(View.GONE);
+        }
     }
 
     private void setProfileSwitchMessageId(int contentUserHint) {
@@ -416,6 +455,9 @@
             mRegistered = true;
         }
         mAdapter.handlePackagesChanged();
+        if (mProfileView != null) {
+            bindProfileView();
+        }
     }
 
     @Override
@@ -702,6 +744,17 @@
         startActivity(in);
     }
 
+    Intent intentForDisplayResolveInfo(DisplayResolveInfo dri) {
+        Intent intent = new Intent(dri.origIntent != null ? dri.origIntent :
+                getReplacementIntent(dri.ri.activityInfo, mIntent));
+        intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
+                |Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
+        ActivityInfo ai = dri.ri.activityInfo;
+        intent.setComponent(new ComponentName(
+                ai.applicationInfo.packageName, ai.name));
+        return intent;
+    }
+
     private final class DisplayResolveInfo {
         ResolveInfo ri;
         CharSequence displayLabel;
@@ -722,7 +775,7 @@
         private final Intent[] mInitialIntents;
         private final List<ResolveInfo> mBaseResolveList;
         private ResolveInfo mLastChosen;
-        private final Intent mIntent;
+        private DisplayResolveInfo mOtherProfile;
         private final int mLaunchedFromUid;
         private final LayoutInflater mInflater;
 
@@ -732,10 +785,8 @@
         private int mLastChosenPosition = -1;
         private boolean mFilterLastUsed;
 
-        public ResolveListAdapter(Context context, Intent intent,
-                Intent[] initialIntents, List<ResolveInfo> rList, int launchedFromUid,
-                boolean filterLastUsed) {
-            mIntent = new Intent(intent);
+        public ResolveListAdapter(Context context, Intent[] initialIntents,
+                List<ResolveInfo> rList, int launchedFromUid, boolean filterLastUsed) {
             mInitialIntents = initialIntents;
             mBaseResolveList = rList;
             mLaunchedFromUid = launchedFromUid;
@@ -764,6 +815,10 @@
             return null;
         }
 
+        public DisplayResolveInfo getOtherProfile() {
+            return mOtherProfile;
+        }
+
         public int getFilteredPosition() {
             if (mFilterLastUsed && mLastChosenPosition >= 0) {
                 return mLastChosenPosition;
@@ -870,7 +925,7 @@
                             ri.nonLocalizedLabel = li.getNonLocalizedLabel();
                             ri.icon = li.getIconResource();
                         }
-                        mList.add(new DisplayResolveInfo(ri,
+                        addResolveInfo(new DisplayResolveInfo(ri,
                                 ri.loadLabel(getPackageManager()), null, ii));
                     }
                 }
@@ -915,7 +970,7 @@
                     mLastChosenPosition = mList.size();
                 }
                 // No duplicate labels. Use label for entry at start
-                mList.add(new DisplayResolveInfo(ro, roLabel, null, null));
+                addResolveInfo(new DisplayResolveInfo(ro, roLabel, null, null));
             } else {
                 mShowExtended = true;
                 boolean usePkg = false;
@@ -951,32 +1006,34 @@
                     }
                     if (usePkg) {
                         // Use application name for all entries from start to end-1
-                        mList.add(new DisplayResolveInfo(add, roLabel,
+                        addResolveInfo(new DisplayResolveInfo(add, roLabel,
                                 add.activityInfo.packageName, null));
                     } else {
                         // Use package name for all entries from start to end-1
-                        mList.add(new DisplayResolveInfo(add, roLabel,
+                        addResolveInfo(new DisplayResolveInfo(add, roLabel,
                                 add.activityInfo.applicationInfo.loadLabel(mPm), null));
                     }
                 }
             }
         }
 
+        private void addResolveInfo(DisplayResolveInfo dri) {
+            if (dri.ri.targetUserId != UserHandle.USER_CURRENT && mOtherProfile == null) {
+                // So far we only support a single other profile at a time.
+                // The first one we see gets special treatment.
+                mOtherProfile = dri;
+            } else {
+                mList.add(dri);
+            }
+        }
+
         public ResolveInfo resolveInfoForPosition(int position, boolean filtered) {
             return (filtered ? getItem(position) : mList.get(position)).ri;
         }
 
         public Intent intentForPosition(int position, boolean filtered) {
             DisplayResolveInfo dri = filtered ? getItem(position) : mList.get(position);
-
-            Intent intent = new Intent(dri.origIntent != null ? dri.origIntent :
-                    getReplacementIntent(dri.ri.activityInfo, mIntent));
-            intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
-                    |Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
-            ActivityInfo ai = dri.ri.activityInfo;
-            intent.setComponent(new ComponentName(
-                    ai.applicationInfo.packageName, ai.name));
-            return intent;
+            return intentForDisplayResolveInfo(dri);
         }
 
         public int getCount() {
@@ -1067,6 +1124,9 @@
 
         @Override
         protected void onPostExecute(DisplayResolveInfo info) {
+            if (mProfileView != null && mAdapter.getOtherProfile() == info) {
+                bindProfileView();
+            }
             mAdapter.notifyDataSetChanged();
         }
     }