Loading widget description on the background thread

Bug: 281074741
Test: Varified on device
Flag: N/A
Change-Id: I6b9b13e712534bdc582b98dbfb7bdfcc40d2f19d
diff --git a/src/com/android/launcher3/dragndrop/AddItemActivity.java b/src/com/android/launcher3/dragndrop/AddItemActivity.java
index 4906c1d..00f4285 100644
--- a/src/com/android/launcher3/dragndrop/AddItemActivity.java
+++ b/src/com/android/launcher3/dragndrop/AddItemActivity.java
@@ -304,7 +304,8 @@
         mWidgetOptions = pendingInfo.getDefaultSizeOptions(this);
         mWidgetCell.getWidgetView().setTag(pendingInfo);
 
-        applyWidgetItemAsync(() -> new WidgetItem(widgetInfo, mIdp, mApp.getIconCache()));
+        applyWidgetItemAsync(() -> new WidgetItem(
+                widgetInfo, mIdp, mApp.getIconCache(), mApp.getContext()));
         return WidgetsModel.newPendingItemInfo(this, widgetInfo.getComponent(),
                 widgetInfo.getUser());
     }
diff --git a/src/com/android/launcher3/model/WidgetItem.java b/src/com/android/launcher3/model/WidgetItem.java
index 7198d54..c99b889 100644
--- a/src/com/android/launcher3/model/WidgetItem.java
+++ b/src/com/android/launcher3/model/WidgetItem.java
@@ -3,6 +3,7 @@
 import static com.android.launcher3.Utilities.ATLEAST_S;
 
 import android.annotation.SuppressLint;
+import android.content.Context;
 import android.content.pm.ActivityInfo;
 import android.content.pm.PackageManager;
 import android.content.res.Resources;
@@ -25,13 +26,15 @@
     public final ShortcutConfigActivityInfo activityInfo;
 
     public final String label;
+    public final CharSequence description;
     public final int spanX, spanY;
 
     public WidgetItem(LauncherAppWidgetProviderInfo info,
-            InvariantDeviceProfile idp, IconCache iconCache) {
+            InvariantDeviceProfile idp, IconCache iconCache, Context context) {
         super(info.provider, info.getProfile());
 
         label = iconCache.getTitleNoCache(info);
+        description = ATLEAST_S ? info.loadDescription(context) : null;
         widgetInfo = info;
         activityInfo = null;
 
@@ -43,6 +46,7 @@
         super(info.getComponent(), info.getUser());
         label = info.isPersistable() ? iconCache.getTitleNoCache(info) :
                 Utilities.trim(info.getLabel(pm));
+        description = null;
         widgetInfo = null;
         activityInfo = info;
         spanX = spanY = 1;
diff --git a/src/com/android/launcher3/widget/WidgetCell.java b/src/com/android/launcher3/widget/WidgetCell.java
index 43218a1..c30342a 100644
--- a/src/com/android/launcher3/widget/WidgetCell.java
+++ b/src/com/android/launcher3/widget/WidgetCell.java
@@ -17,7 +17,6 @@
 package com.android.launcher3.widget;
 
 import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_WIDGETS_TRAY;
-import static com.android.launcher3.Utilities.ATLEAST_S;
 import static com.android.launcher3.widget.LauncherAppWidgetProviderInfo.fromProviderInfo;
 import static com.android.launcher3.widget.util.WidgetSizes.getWidgetItemSizePx;
 
@@ -25,6 +24,7 @@
 import android.graphics.Bitmap;
 import android.graphics.drawable.Drawable;
 import android.os.Process;
+import android.text.TextUtils;
 import android.util.AttributeSet;
 import android.util.Log;
 import android.util.Size;
@@ -219,14 +219,11 @@
                 mItem.spanX, mItem.spanY));
         mWidgetDims.setContentDescription(context.getString(
                 R.string.widget_accessible_dims_format, mItem.spanX, mItem.spanY));
-        if (ATLEAST_S && mItem.widgetInfo != null) {
-            CharSequence description = mItem.widgetInfo.loadDescription(context);
-            if (description != null && description.length() > 0) {
-                mWidgetDescription.setText(description);
-                mWidgetDescription.setVisibility(VISIBLE);
-            } else {
-                mWidgetDescription.setVisibility(GONE);
-            }
+        if (!TextUtils.isEmpty(mItem.description)) {
+            mWidgetDescription.setText(mItem.description);
+            mWidgetDescription.setVisibility(VISIBLE);
+        } else {
+            mWidgetDescription.setVisibility(GONE);
         }
 
         if (item.activityInfo != null) {
diff --git a/src_shortcuts_overrides/com/android/launcher3/model/WidgetsModel.java b/src_shortcuts_overrides/com/android/launcher3/model/WidgetsModel.java
index 1b743e8..2f16065 100644
--- a/src_shortcuts_overrides/com/android/launcher3/model/WidgetsModel.java
+++ b/src_shortcuts_overrides/com/android/launcher3/model/WidgetsModel.java
@@ -129,7 +129,7 @@
                         LauncherAppWidgetProviderInfo.fromProviderInfo(context, widgetInfo);
 
                 widgetsAndShortcuts.add(new WidgetItem(
-                        launcherWidgetInfo, idp, app.getIconCache()));
+                        launcherWidgetInfo, idp, app.getIconCache(), app.getContext()));
                 updatedItems.add(launcherWidgetInfo);
             }
 
@@ -200,7 +200,8 @@
                                     app.getContext().getPackageManager()));
                         } else {
                             items.set(i, new WidgetItem(item.widgetInfo,
-                                    app.getInvariantDeviceProfile(), app.getIconCache()));
+                                    app.getInvariantDeviceProfile(), app.getIconCache(),
+                                    app.getContext()));
                         }
                     }
                 }
diff --git a/tests/src/com/android/launcher3/widget/picker/WidgetsListHeaderViewHolderBinderTest.java b/tests/src/com/android/launcher3/widget/picker/WidgetsListHeaderViewHolderBinderTest.java
index 76492ba..8fc4481 100644
--- a/tests/src/com/android/launcher3/widget/picker/WidgetsListHeaderViewHolderBinderTest.java
+++ b/tests/src/com/android/launcher3/widget/picker/WidgetsListHeaderViewHolderBinderTest.java
@@ -147,7 +147,7 @@
 
             widgetItems.add(new WidgetItem(
                     LauncherAppWidgetProviderInfo.fromProviderInfo(mContext, widgetInfo),
-                    mTestProfile, mIconCache));
+                    mTestProfile, mIconCache, mContext));
         }
         return widgetItems;
     }
diff --git a/tests/src/com/android/launcher3/widget/picker/WidgetsListTableViewHolderBinderTest.java b/tests/src/com/android/launcher3/widget/picker/WidgetsListTableViewHolderBinderTest.java
index e0101f5..60590e7 100644
--- a/tests/src/com/android/launcher3/widget/picker/WidgetsListTableViewHolderBinderTest.java
+++ b/tests/src/com/android/launcher3/widget/picker/WidgetsListTableViewHolderBinderTest.java
@@ -144,7 +144,7 @@
 
             widgetItems.add(new WidgetItem(
                     LauncherAppWidgetProviderInfo.fromProviderInfo(mContext, widgetInfo),
-                    mTestProfile, mIconCache));
+                    mTestProfile, mIconCache, mContext));
         }
         return widgetItems;
     }
diff --git a/tests/src/com/android/launcher3/widget/picker/model/WidgetsListContentEntryTest.java b/tests/src/com/android/launcher3/widget/picker/model/WidgetsListContentEntryTest.java
index d8f1f14..7552619 100644
--- a/tests/src/com/android/launcher3/widget/picker/model/WidgetsListContentEntryTest.java
+++ b/tests/src/com/android/launcher3/widget/picker/model/WidgetsListContentEntryTest.java
@@ -26,6 +26,7 @@
 
 import android.appwidget.AppWidgetProviderInfo;
 import android.content.ComponentName;
+import android.content.Context;
 import android.os.UserHandle;
 
 import androidx.test.ext.junit.runners.AndroidJUnit4;
@@ -249,12 +250,13 @@
         String label = mWidgetsToLabels.get(componentName);
         AppWidgetProviderInfo widgetInfo = createAppWidgetProviderInfo(componentName);
 
+        Context context = getApplicationContext();
         LauncherAppWidgetProviderInfo launcherAppWidgetProviderInfo =
-                LauncherAppWidgetProviderInfo.fromProviderInfo(getApplicationContext(), widgetInfo);
+                LauncherAppWidgetProviderInfo.fromProviderInfo(context, widgetInfo);
         launcherAppWidgetProviderInfo.spanX = spanX;
         launcherAppWidgetProviderInfo.spanY = spanY;
         launcherAppWidgetProviderInfo.label = label;
 
-        return new WidgetItem(launcherAppWidgetProviderInfo, mTestProfile, mIconCache);
+        return new WidgetItem(launcherAppWidgetProviderInfo, mTestProfile, mIconCache, context);
     }
 }
diff --git a/tests/src/com/android/launcher3/widget/picker/search/SimpleWidgetsSearchAlgorithmTest.java b/tests/src/com/android/launcher3/widget/picker/search/SimpleWidgetsSearchAlgorithmTest.java
index 0124f73..9c03ccf 100644
--- a/tests/src/com/android/launcher3/widget/picker/search/SimpleWidgetsSearchAlgorithmTest.java
+++ b/tests/src/com/android/launcher3/widget/picker/search/SimpleWidgetsSearchAlgorithmTest.java
@@ -202,7 +202,7 @@
 
             WidgetItem widgetItem = new WidgetItem(
                     LauncherAppWidgetProviderInfo.fromProviderInfo(mContext, widgetInfo),
-                    mTestProfile, mIconCache);
+                    mTestProfile, mIconCache, mContext);
             widgetItems.add(widgetItem);
         }
         return widgetItems;
diff --git a/tests/src/com/android/launcher3/widget/picker/util/WidgetsTableUtilsTest.java b/tests/src/com/android/launcher3/widget/picker/util/WidgetsTableUtilsTest.java
index 834f851..2c5a396 100644
--- a/tests/src/com/android/launcher3/widget/picker/util/WidgetsTableUtilsTest.java
+++ b/tests/src/com/android/launcher3/widget/picker/util/WidgetsTableUtilsTest.java
@@ -218,19 +218,18 @@
                 new Point(2, 4), new Point(4, 4));
 
         ArrayList<WidgetItem> widgetItems = new ArrayList<>();
-        widgetSizes.stream().forEach(
-                widgetSize -> {
-                    AppWidgetProviderInfo info = createAppWidgetProviderInfo(
-                            ComponentName.createRelative(
-                                    TEST_PACKAGE,
-                                    ".WidgetProvider_" + widgetSize.x + "x" + widgetSize.y));
-                    LauncherAppWidgetProviderInfo widgetInfo =
-                            LauncherAppWidgetProviderInfo.fromProviderInfo(mContext, info);
-                    widgetInfo.spanX = widgetSize.x;
-                    widgetInfo.spanY = widgetSize.y;
-                    widgetItems.add(new WidgetItem(widgetInfo, mTestInvariantProfile, mIconCache));
-                }
-        );
+        widgetSizes.stream().forEach(widgetSize -> {
+            AppWidgetProviderInfo info = createAppWidgetProviderInfo(
+                    ComponentName.createRelative(
+                            TEST_PACKAGE,
+                            ".WidgetProvider_" + widgetSize.x + "x" + widgetSize.y));
+            LauncherAppWidgetProviderInfo widgetInfo =
+                    LauncherAppWidgetProviderInfo.fromProviderInfo(mContext, info);
+            widgetInfo.spanX = widgetSize.x;
+            widgetInfo.spanY = widgetSize.y;
+            widgetItems.add(new WidgetItem(
+                    widgetInfo, mTestInvariantProfile, mIconCache, mContext));
+        });
         mWidget1x1 = widgetItems.get(0);
         mWidget2x2 = widgetItems.get(1);
         mWidget2x3 = widgetItems.get(2);