Render preview layout in full widgets sheet and bottom widgets sheet

Video: https://drive.google.com/file/d/1gRE0axaq6tm3R_gjSon0RR5lC8HCmwee/view?usp=sharing

Test: Install a sample app which provides a preview layout.
      Go to full widgets sheet to observe widget previews are correctly
      displayed for bitmap previews, layout previews and placeholder
      previews. Repeat the same for bottom widgets picker.
      Pin a widget from a sample app which provide a custom RemoteViews
      preview, observe the custom RemoteViews is correctly rendered.

Bug: 179797520
Change-Id: I2ccd1f77b54a2430d842ee25349fa6e54b9159dc
diff --git a/res/layout/live_preview_widget_cell.xml b/res/layout/live_preview_widget_cell.xml
new file mode 100644
index 0000000..7a42d19
--- /dev/null
+++ b/res/layout/live_preview_widget_cell.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2021 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<com.android.launcher3.dragndrop.LivePreviewWidgetCell
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="wrap_content"
+    android:layout_height="wrap_content"
+    android:layout_weight="1"
+    android:orientation="vertical"
+    android:focusable="true"
+    android:background="?android:attr/colorPrimaryDark"
+    android:gravity="center_horizontal">
+
+    <include layout="@layout/widget_cell_content"  />
+
+</com.android.launcher3.dragndrop.LivePreviewWidgetCell>
\ No newline at end of file
diff --git a/src/com/android/launcher3/dragndrop/LivePreviewWidgetCell.java b/src/com/android/launcher3/dragndrop/LivePreviewWidgetCell.java
index a9389bc..71e10a8 100644
--- a/src/com/android/launcher3/dragndrop/LivePreviewWidgetCell.java
+++ b/src/com/android/launcher3/dragndrop/LivePreviewWidgetCell.java
@@ -10,7 +10,9 @@
 import com.android.launcher3.BaseActivity;
 import com.android.launcher3.DeviceProfile;
 import com.android.launcher3.LauncherAppWidgetProviderInfo;
+import com.android.launcher3.WidgetPreviewLoader;
 import com.android.launcher3.icons.BitmapRenderer;
+import com.android.launcher3.model.WidgetItem;
 import com.android.launcher3.widget.WidgetCell;
 
 /**
@@ -36,6 +38,15 @@
         mPreview = view;
     }
 
+    public RemoteViews getPreview() {
+        return mPreview;
+    }
+
+    /** Resets any resource. This should be called before recycling this view. */
+    public void reset() {
+        mPreview = null;
+    }
+
     @Override
     public void ensurePreview() {
         if (mPreview != null && mActiveRequest == null) {
@@ -49,6 +60,18 @@
         super.ensurePreview();
     }
 
+    @Override
+    public void applyFromCellItem(WidgetItem item, WidgetPreviewLoader loader) {
+        if (mPreview == null
+                && item.widgetInfo != null
+                && item.widgetInfo.previewLayout != View.NO_ID) {
+            mPreview = new RemoteViews(item.widgetInfo.provider.getPackageName(),
+                    item.widgetInfo.previewLayout);
+        }
+
+        super.applyFromCellItem(item, loader);
+    }
+
     /**
      * Generates a bitmap by inflating {@param views}.
      * @see com.android.launcher3.WidgetPreviewLoader#generateWidgetPreview
diff --git a/src/com/android/launcher3/widget/BaseWidgetSheet.java b/src/com/android/launcher3/widget/BaseWidgetSheet.java
index a38e90d..15566a4 100644
--- a/src/com/android/launcher3/widget/BaseWidgetSheet.java
+++ b/src/com/android/launcher3/widget/BaseWidgetSheet.java
@@ -30,6 +30,7 @@
 import com.android.launcher3.R;
 import com.android.launcher3.Utilities;
 import com.android.launcher3.dragndrop.DragOptions;
+import com.android.launcher3.dragndrop.LivePreviewWidgetCell;
 import com.android.launcher3.popup.PopupDataProvider;
 import com.android.launcher3.testing.TestLogging;
 import com.android.launcher3.testing.TestProtocol;
@@ -99,12 +100,16 @@
             return false;
         }
 
+        PendingItemDragHelper dragHelper = new PendingItemDragHelper(v);
+        if (v instanceof LivePreviewWidgetCell) {
+            dragHelper.setPreview(((LivePreviewWidgetCell) v).getPreview());
+        }
+
         int[] loc = new int[2];
         getPopupContainer().getLocationInDragLayer(image, loc);
 
-        new PendingItemDragHelper(v).startDrag(
-                image.getBitmapBounds(), image.getBitmap().getWidth(), image.getWidth(),
-                new Point(loc[0], loc[1]), this, new DragOptions());
+        dragHelper.startDrag(image.getBitmapBounds(), image.getBitmap().getWidth(),
+                image.getWidth(), new Point(loc[0], loc[1]), this, new DragOptions());
         close(true);
         return true;
     }
diff --git a/src/com/android/launcher3/widget/PendingItemDragHelper.java b/src/com/android/launcher3/widget/PendingItemDragHelper.java
index 3c11274..8fe42f4 100644
--- a/src/com/android/launcher3/widget/PendingItemDragHelper.java
+++ b/src/com/android/launcher3/widget/PendingItemDragHelper.java
@@ -25,6 +25,8 @@
 import android.view.View;
 import android.widget.RemoteViews;
 
+import androidx.annotation.Nullable;
+
 import com.android.launcher3.DeviceProfile;
 import com.android.launcher3.DragSource;
 import com.android.launcher3.Launcher;
@@ -48,14 +50,14 @@
     private final PendingAddItemInfo mAddInfo;
     private int[] mEstimatedCellSize;
 
-    private RemoteViews mPreview;
+    @Nullable private RemoteViews mPreview;
 
     public PendingItemDragHelper(View view) {
         super(view);
         mAddInfo = (PendingAddItemInfo) view.getTag();
     }
 
-    public void setPreview(RemoteViews preview) {
+    public void setPreview(@Nullable RemoteViews preview) {
         mPreview = preview;
     }
 
diff --git a/src/com/android/launcher3/widget/WidgetsBottomSheet.java b/src/com/android/launcher3/widget/WidgetsBottomSheet.java
index 3585a18..223cda2 100644
--- a/src/com/android/launcher3/widget/WidgetsBottomSheet.java
+++ b/src/com/android/launcher3/widget/WidgetsBottomSheet.java
@@ -36,6 +36,7 @@
 import com.android.launcher3.R;
 import com.android.launcher3.ResourceUtils;
 import com.android.launcher3.anim.PendingAnimation;
+import com.android.launcher3.dragndrop.LivePreviewWidgetCell;
 import com.android.launcher3.model.WidgetItem;
 import com.android.launcher3.model.data.ItemInfo;
 import com.android.launcher3.util.PackageUserKey;
@@ -136,8 +137,8 @@
     }
 
     protected WidgetCell addItemCell(ViewGroup parent) {
-        WidgetCell widget = (WidgetCell) LayoutInflater.from(getContext()).inflate(
-                R.layout.widget_cell, parent, false);
+        LivePreviewWidgetCell widget = (LivePreviewWidgetCell) LayoutInflater.from(
+                getContext()).inflate(R.layout.live_preview_widget_cell, parent, false);
 
         widget.setOnClickListener(this);
         widget.setOnLongClickListener(this);
diff --git a/src/com/android/launcher3/widget/picker/WidgetsListRowViewHolderBinder.java b/src/com/android/launcher3/widget/picker/WidgetsListRowViewHolderBinder.java
index cec6b80..bd78777 100644
--- a/src/com/android/launcher3/widget/picker/WidgetsListRowViewHolderBinder.java
+++ b/src/com/android/launcher3/widget/picker/WidgetsListRowViewHolderBinder.java
@@ -25,6 +25,7 @@
 
 import com.android.launcher3.R;
 import com.android.launcher3.WidgetPreviewLoader;
+import com.android.launcher3.dragndrop.LivePreviewWidgetCell;
 import com.android.launcher3.model.WidgetItem;
 import com.android.launcher3.recyclerview.ViewHolderBinder;
 import com.android.launcher3.widget.WidgetCell;
@@ -107,8 +108,8 @@
                     mLayoutInflater.inflate(R.layout.widget_list_divider, row);
                 } else {
                     // Add cell for even index
-                    WidgetCell widget = (WidgetCell) mLayoutInflater.inflate(
-                            R.layout.widget_cell, row, false);
+                    LivePreviewWidgetCell widget = (LivePreviewWidgetCell) mLayoutInflater.inflate(
+                            R.layout.live_preview_widget_cell, row, false);
 
                     // set up touch.
                     widget.setOnClickListener(mIconClickListener);
@@ -124,7 +125,8 @@
 
         // Bind the view in the widget horizontal tray region.
         for (int i = 0; i < infoList.size(); i++) {
-            WidgetCell widget = (WidgetCell) row.getChildAt(2 * i);
+            LivePreviewWidgetCell widget = (LivePreviewWidgetCell) row.getChildAt(2 * i);
+            widget.reset();
             widget.applyFromCellItem(infoList.get(i), mWidgetPreviewLoader);
             widget.setApplyBitmapDeferred(mApplyBitmapDeferred);
             widget.ensurePreview();