Merge "Widget Picker - show app icon next to recommended app label." into main
diff --git a/res/layout/widget_cell_content.xml b/res/layout/widget_cell_content.xml
index 6db4fb5..0c606f6 100644
--- a/res/layout/widget_cell_content.xml
+++ b/res/layout/widget_cell_content.xml
@@ -48,14 +48,15 @@
     <!-- The name of the widget. -->
     <TextView
         android:id="@+id/widget_name"
-        android:layout_width="match_parent"
+        android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:ellipsize="end"
         android:fadingEdge="horizontal"
-        android:gravity="center_horizontal"
+        android:gravity="center_horizontal|center_vertical"
         android:singleLine="true"
         android:maxLines="1"
         android:textColor="?android:attr/textColorPrimary"
+        android:drawablePadding="@dimen/widget_cell_app_icon_padding"
         android:textSize="@dimen/widget_cell_font_size" />
 
     <!-- The original dimensions of the widget -->
diff --git a/res/values/dimens.xml b/res/values/dimens.xml
index fa0daa4..a912e2d 100644
--- a/res/values/dimens.xml
+++ b/res/values/dimens.xml
@@ -178,6 +178,8 @@
     <dimen name="widget_cell_vertical_padding">8dp</dimen>
     <dimen name="widget_cell_horizontal_padding">16dp</dimen>
     <dimen name="widget_cell_font_size">14sp</dimen>
+    <dimen name="widget_cell_app_icon_size">24dp</dimen>
+    <dimen name="widget_cell_app_icon_padding">8dp</dimen>
 
     <dimen name="widget_tabs_button_horizontal_padding">4dp</dimen>
     <dimen name="widget_tabs_horizontal_padding">16dp</dimen>
diff --git a/src/com/android/launcher3/model/WidgetItem.java b/src/com/android/launcher3/model/WidgetItem.java
index ddf4023..1dd58c3 100644
--- a/src/com/android/launcher3/model/WidgetItem.java
+++ b/src/com/android/launcher3/model/WidgetItem.java
@@ -19,6 +19,7 @@
 import com.android.launcher3.Flags;
 import com.android.launcher3.InvariantDeviceProfile;
 import com.android.launcher3.Utilities;
+import com.android.launcher3.icons.BitmapInfo;
 import com.android.launcher3.icons.IconCache;
 import com.android.launcher3.pm.ShortcutConfigActivityInfo;
 import com.android.launcher3.util.ComponentKey;
@@ -35,6 +36,7 @@
     public final LauncherAppWidgetProviderInfo widgetInfo;
     public final ShortcutConfigActivityInfo activityInfo;
 
+    public BitmapInfo bitmap = BitmapInfo.LOW_RES_INFO;
     public final String label;
     public final CharSequence description;
     public final int spanX, spanY;
diff --git a/src/com/android/launcher3/widget/WidgetCell.java b/src/com/android/launcher3/widget/WidgetCell.java
index 7345511..f2f83c8 100644
--- a/src/com/android/launcher3/widget/WidgetCell.java
+++ b/src/com/android/launcher3/widget/WidgetCell.java
@@ -48,10 +48,13 @@
 import com.android.launcher3.CheckLongPressHelper;
 import com.android.launcher3.Flags;
 import com.android.launcher3.Launcher;
+import com.android.launcher3.LauncherAppState;
 import com.android.launcher3.R;
 import com.android.launcher3.icons.FastBitmapDrawable;
 import com.android.launcher3.icons.RoundDrawableWrapper;
 import com.android.launcher3.model.WidgetItem;
+import com.android.launcher3.model.data.ItemInfoWithIcon;
+import com.android.launcher3.model.data.PackageItemInfo;
 import com.android.launcher3.util.CancellableTask;
 import com.android.launcher3.views.ActivityContext;
 
@@ -102,6 +105,8 @@
     private float mAppWidgetHostViewScale = 1f;
     private int mSourceContainer = CONTAINER_WIDGETS_TRAY;
 
+    private CancellableTask mIconLoadRequest;
+
     public WidgetCell(Context context) {
         this(context, null);
     }
@@ -185,6 +190,7 @@
         mPreviewContainerScale = 1f;
         mItem = null;
         mWidgetSize = new Size(0, 0);
+        showAppIconInWidgetTitle(false);
     }
 
     public void setSourceContainer(int sourceContainer) {
@@ -377,6 +383,31 @@
         mWidgetDescription.setVisibility(show ? VISIBLE : GONE);
     }
 
+    /**
+     * Set whether the app icon, for the app that provides the widget, should be shown next to the
+     * title text of the widget.
+     *
+     * @param show true if the app icon should be shown in the title text of the cell, false hides
+     *             it.
+     */
+    public void showAppIconInWidgetTitle(boolean show) {
+        if (show) {
+            if (mItem.widgetInfo != null) {
+                loadHighResPackageIcon();
+
+                Drawable icon = mItem.bitmap.newIcon(getContext());
+                int size = getResources().getDimensionPixelSize(R.dimen.widget_cell_app_icon_size);
+                icon.setBounds(0, 0, size, size);
+                mWidgetName.setCompoundDrawablesRelative(
+                        icon,
+                        null, null, null);
+            }
+        } else {
+            cancelIconLoadRequest();
+            mWidgetName.setCompoundDrawables(null, null, null, null);
+        }
+    }
+
     @Override
     public boolean onTouchEvent(MotionEvent ev) {
         super.onTouchEvent(ev);
@@ -430,4 +461,38 @@
 
         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
     }
+
+    /**
+     * Loads a high resolution package icon to show next to the widget title.
+     */
+    public void loadHighResPackageIcon() {
+        cancelIconLoadRequest();
+        if (mItem.bitmap.isLowRes()) {
+            // We use the package icon instead of the receiver one so that the overall package that
+            // the widget came from can be identified in the recommended widgets. This matches with
+            // the package icon headings in the all widgets list.
+            PackageItemInfo tmpPackageItem = new PackageItemInfo(
+                    mItem.componentName.getPackageName(),
+                    mItem.user);
+            mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
+                    .updateIconInBackground(this::reapplyIconInfo, tmpPackageItem);
+        }
+    }
+
+    /** Can be called to update the package icon shown in the label of recommended widgets. */
+    private void reapplyIconInfo(ItemInfoWithIcon info) {
+        if (mItem == null || info.bitmap.isNullOrLowRes()) {
+            showAppIconInWidgetTitle(false);
+            return;
+        }
+        mItem.bitmap = info.bitmap;
+        showAppIconInWidgetTitle(true);
+    }
+
+    private void cancelIconLoadRequest() {
+        if (mIconLoadRequest != null) {
+            mIconLoadRequest.cancel();
+            mIconLoadRequest = null;
+        }
+    }
 }
diff --git a/src/com/android/launcher3/widget/picker/WidgetsRecommendationTableLayout.java b/src/com/android/launcher3/widget/picker/WidgetsRecommendationTableLayout.java
index 47750a5..12564f4 100644
--- a/src/com/android/launcher3/widget/picker/WidgetsRecommendationTableLayout.java
+++ b/src/com/android/launcher3/widget/picker/WidgetsRecommendationTableLayout.java
@@ -119,6 +119,7 @@
             for (WidgetItem widgetItem : widgetItems) {
                 WidgetCell widgetCell = addItemCell(tableRow);
                 widgetCell.applyFromCellItem(widgetItem, data.mPreviewScale);
+                widgetCell.showAppIconInWidgetTitle(true);
                 widgetCell.showBadge();
                 if (enableCategorizedWidgetSuggestions()) {
                     widgetCell.showDescription(false);