Merge "Partial cleanup after b/143488140" into sc-dev
diff --git a/quickstep/src/com/android/launcher3/QuickstepTransitionManager.java b/quickstep/src/com/android/launcher3/QuickstepTransitionManager.java
index b72cf39..009ca27 100644
--- a/quickstep/src/com/android/launcher3/QuickstepTransitionManager.java
+++ b/quickstep/src/com/android/launcher3/QuickstepTransitionManager.java
@@ -110,7 +110,7 @@
     private static final String TAG = "QuickstepTransition";
 
     private static final boolean ENABLE_SHELL_STARTING_SURFACE =
-            SystemProperties.getBoolean("persist.debug.shell_starting_surface", true);
+            SystemProperties.getBoolean("persist.debug.shell_starting_surface", false);
 
     /** Duration of status bar animations. */
     public static final int STATUS_BAR_TRANSITION_DURATION = 120;
diff --git a/quickstep/src/com/android/quickstep/views/RecentsView.java b/quickstep/src/com/android/quickstep/views/RecentsView.java
index 0eb2392..b4d0658 100644
--- a/quickstep/src/com/android/quickstep/views/RecentsView.java
+++ b/quickstep/src/com/android/quickstep/views/RecentsView.java
@@ -3155,7 +3155,8 @@
         // Align ClearAllButton to the left (RTL) or right (non-RTL), which is different from other
         // TaskViews. This must be called after laying out ClearAllButton.
         if (layoutChildren) {
-            int clearAllWidthDiff = mTaskWidth - mClearAllButton.getWidth();
+            int clearAllWidthDiff = mOrientationHandler.getPrimaryValue(mTaskWidth, mTaskHeight)
+                    - mOrientationHandler.getPrimarySize(mClearAllButton);
             mClearAllButton.setScrollOffsetPrimary(mIsRtl ? clearAllWidthDiff : -clearAllWidthDiff);
         }
 
diff --git a/res/values/dimens.xml b/res/values/dimens.xml
index a06b42c..8a160bd 100644
--- a/res/values/dimens.xml
+++ b/res/values/dimens.xml
@@ -84,7 +84,7 @@
     <dimen name="fastscroll_end_margin">-26dp</dimen>
 
     <!-- All Apps -->
-    <dimen name="all_apps_open_vertical_translate">96dp</dimen>
+    <dimen name="all_apps_open_vertical_translate">300dp</dimen>
     <dimen name="all_apps_search_bar_field_height">48dp</dimen>
     <dimen name="all_apps_search_bar_bottom_padding">30dp</dimen>
     <dimen name="all_apps_empty_search_message_top_offset">40dp</dimen>
diff --git a/src/com/android/launcher3/ButtonDropTarget.java b/src/com/android/launcher3/ButtonDropTarget.java
index 4740079..7926862 100644
--- a/src/com/android/launcher3/ButtonDropTarget.java
+++ b/src/com/android/launcher3/ButtonDropTarget.java
@@ -183,7 +183,6 @@
         hideTooltip();
 
         if (!d.dragComplete) {
-            d.dragView.setColor(0);
             d.dragView.setAlpha(1f);
         } else {
             d.dragView.setAlpha(DRAG_VIEW_HOVER_OVER_OPACITY);
diff --git a/src/com/android/launcher3/CellLayout.java b/src/com/android/launcher3/CellLayout.java
index fc09295..c3816cc 100644
--- a/src/com/android/launcher3/CellLayout.java
+++ b/src/com/android/launcher3/CellLayout.java
@@ -61,7 +61,6 @@
 import com.android.launcher3.accessibility.DragAndDropAccessibilityDelegate;
 import com.android.launcher3.anim.Interpolators;
 import com.android.launcher3.config.FeatureFlags;
-import com.android.launcher3.dragndrop.AppWidgetHostViewDrawable;
 import com.android.launcher3.folder.PreviewBackground;
 import com.android.launcher3.model.data.ItemInfo;
 import com.android.launcher3.util.CellAndSpan;
@@ -70,6 +69,7 @@
 import com.android.launcher3.util.Themes;
 import com.android.launcher3.util.Thunk;
 import com.android.launcher3.views.ActivityContext;
+import com.android.launcher3.widget.LauncherAppWidgetHostView;
 
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
@@ -1038,6 +1038,9 @@
             mDragCellSpan[0] = spanX;
             mDragCellSpan[1] = spanY;
 
+            // Apply color extraction on a widget when dragging.
+            applyColorExtractionOnWidget(dragObject, mDragCell, spanX, spanY);
+
             final int oldIndex = mDragOutlineCurrent;
             mDragOutlineAnims[oldIndex].animateOut();
             mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
@@ -1059,18 +1062,17 @@
     }
 
     /** Applies the local color extraction to a dragging widget object. */
-    private void applyColorExtraction(DropTarget.DragObject dragObject, int[] targetCell, int spanX,
-            int spanY) {
+    private void applyColorExtractionOnWidget(DropTarget.DragObject dragObject, int[] targetCell,
+            int spanX, int spanY) {
         // Apply local extracted color if the DragView is an AppWidgetHostViewDrawable.
-        Drawable drawable = dragObject.dragView.getDrawable();
-        if (drawable instanceof AppWidgetHostViewDrawable) {
+        View view = dragObject.dragView.getContentView();
+        if (view instanceof LauncherAppWidgetHostView) {
             Workspace workspace =
                     Launcher.getLauncher(dragObject.dragView.getContext()).getWorkspace();
             int screenId = workspace.getIdForScreen(this);
             int pageId = workspace.getPageIndexForScreenId(screenId);
-            AppWidgetHostViewDrawable hostViewDrawable = ((AppWidgetHostViewDrawable) drawable);
             cellToRect(targetCell[0], targetCell[1], spanX, spanY, mTempRect);
-            hostViewDrawable.getAppWidgetHostView().handleDrag(mTempRect, pageId);
+            ((LauncherAppWidgetHostView) view).handleDrag(mTempRect, pageId);
         }
     }
 
diff --git a/src/com/android/launcher3/Utilities.java b/src/com/android/launcher3/Utilities.java
index 1776777..6f12ec7 100644
--- a/src/com/android/launcher3/Utilities.java
+++ b/src/com/android/launcher3/Utilities.java
@@ -600,7 +600,7 @@
             LauncherActivityInfo activityInfo = launcher.getSystemService(LauncherApps.class)
                     .resolveActivity(info.getIntent(), info.user);
             outObj[0] = activityInfo;
-            return activityInfo == null ? null : new IconProvider(launcher).getIconForUI(
+            return activityInfo == null ? null : new IconProvider(launcher).getIcon(
                     activityInfo, launcher.getDeviceProfile().inv.fillResIconDpi);
         } else if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
             if (info instanceof PendingAddShortcutInfo) {
diff --git a/src/com/android/launcher3/Workspace.java b/src/com/android/launcher3/Workspace.java
index c9e575d..10091a1 100644
--- a/src/com/android/launcher3/Workspace.java
+++ b/src/com/android/launcher3/Workspace.java
@@ -69,7 +69,6 @@
 import com.android.launcher3.anim.PendingAnimation;
 import com.android.launcher3.config.FeatureFlags;
 import com.android.launcher3.dot.FolderDotInfo;
-import com.android.launcher3.dragndrop.AppWidgetHostViewDrawable;
 import com.android.launcher3.dragndrop.DragController;
 import com.android.launcher3.dragndrop.DragLayer;
 import com.android.launcher3.dragndrop.DragOptions;
@@ -414,7 +413,9 @@
         }
 
         if (mDragInfo != null && mDragInfo.cell != null) {
-            CellLayout layout = (CellLayout) mDragInfo.cell.getParent().getParent();
+            CellLayout layout = (CellLayout) (mDragInfo.cell instanceof LauncherAppWidgetHostView
+                    ? dragObject.dragView.getContentViewParent().getParent()
+                    : mDragInfo.cell.getParent().getParent());
             layout.markCellsAsUnoccupiedForView(mDragInfo.cell);
         }
 
@@ -1527,10 +1528,19 @@
             draggableView = (DraggableView) child;
         }
 
+        final View contentView = previewProvider.getContentView();
+        final float scale;
         // The draggable drawable follows the touch point around on the screen
-        final Drawable drawable = previewProvider.createDrawable();
+        final Drawable drawable;
+        if (contentView == null) {
+            drawable = previewProvider.createDrawable();
+            scale = previewProvider.getScaleAndPosition(drawable, mTempXY);
+        } else {
+            drawable = null;
+            scale = previewProvider.getScaleAndPosition(contentView, mTempXY);
+        }
+
         int halfPadding = previewProvider.previewPadding / 2;
-        float scale = previewProvider.getScaleAndPosition(drawable, mTempXY);
         int dragLayerX = mTempXY[0];
         int dragLayerY = mTempXY[1];
 
@@ -1556,22 +1566,37 @@
             }
         }
 
-        if (drawable instanceof AppWidgetHostViewDrawable) {
-            mDragController.addDragListener(new AppWidgetHostViewDragListener(mLauncher));
+        final DragView dv;
+        if (contentView instanceof View) {
+            if (contentView instanceof LauncherAppWidgetHostView) {
+                mDragController.addDragListener(new AppWidgetHostViewDragListener(mLauncher));
+            }
+            dv = mDragController.startDrag(
+                    contentView,
+                    draggableView,
+                    dragLayerX,
+                    dragLayerY,
+                    source,
+                    dragObject,
+                    dragVisualizeOffset,
+                    dragRect,
+                    scale * iconScale,
+                    scale,
+                    dragOptions);
+        } else {
+            dv = mDragController.startDrag(
+                    drawable,
+                    draggableView,
+                    dragLayerX,
+                    dragLayerY,
+                    source,
+                    dragObject,
+                    dragVisualizeOffset,
+                    dragRect,
+                    scale * iconScale,
+                    scale,
+                    dragOptions);
         }
-        DragView dv = mDragController.startDrag(
-                drawable,
-                draggableView,
-                dragLayerX,
-                dragLayerY,
-                source,
-                dragObject,
-                dragVisualizeOffset,
-                dragRect,
-                scale * iconScale,
-                scale,
-                dragOptions);
-        dv.setIntrinsicIconScaleFactor(dragOptions.intrinsicIconScaleFactor);
         return dv;
     }
 
@@ -1901,6 +1926,8 @@
                         CellLayout parentCell = getParentCellLayoutForView(cell);
                         if (parentCell != null) {
                             parentCell.removeView(cell);
+                        } else if (mDragInfo.cell instanceof LauncherAppWidgetHostView) {
+                            d.dragView.detachContentView(/* reattachToPreviousParent= */ false);
                         } else if (FeatureFlags.IS_STUDIO_BUILD) {
                             throw new NullPointerException("mDragInfo.cell has null parent");
                         }
@@ -1939,6 +1966,9 @@
                     if (!returnToOriginalCellToPreventShuffling) {
                         onNoCellFound(dropTargetLayout);
                     }
+                    if (mDragInfo.cell instanceof LauncherAppWidgetHostView) {
+                        d.dragView.detachContentView(/* reattachToPreviousParent= */ true);
+                    }
 
                     // If we can't find a drop location, we return the item to its original position
                     CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams();
@@ -2637,10 +2667,6 @@
     }
 
     private Drawable createWidgetDrawable(ItemInfo widgetInfo, View layout) {
-        if (layout instanceof LauncherAppWidgetHostView) {
-            return new AppWidgetHostViewDrawable((LauncherAppWidgetHostView) layout);
-        }
-
         int[] unScaledSize = estimateItemSize(widgetInfo);
         int visibility = layout.getVisibility();
         layout.setVisibility(VISIBLE);
@@ -2721,10 +2747,11 @@
 
         boolean isWidget = info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET ||
                 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
-        if ((animationType == ANIMATE_INTO_POSITION_AND_RESIZE || external) && finalView != null) {
+        if ((animationType == ANIMATE_INTO_POSITION_AND_RESIZE || external)
+                && finalView != null
+                && dragView.getContentView() != finalView) {
             Drawable crossFadeDrawable = createWidgetDrawable(info, finalView);
-            dragView.setCrossFadeDrawable(crossFadeDrawable);
-            dragView.crossFade((int) (duration * 0.8f));
+            dragView.crossFadeContent(crossFadeDrawable, (int) (duration * 0.8f));
         } else if (isWidget && external) {
             scaleXY[0] = scaleXY[1] = Math.min(scaleXY[0],  scaleXY[1]);
         }
diff --git a/src/com/android/launcher3/allapps/AllAppsTransitionController.java b/src/com/android/launcher3/allapps/AllAppsTransitionController.java
index 0060b83..c61c0d6 100644
--- a/src/com/android/launcher3/allapps/AllAppsTransitionController.java
+++ b/src/com/android/launcher3/allapps/AllAppsTransitionController.java
@@ -197,7 +197,7 @@
     /**
      * Updates the total scroll range but does not update the UI.
      */
-    void setScrollRangeDelta(float delta) {
+    public void setScrollRangeDelta(float delta) {
         mScrollRangeDelta = delta;
         mShiftRange = mLauncher.getDeviceProfile().heightPx - mScrollRangeDelta;
     }
diff --git a/src/com/android/launcher3/dragndrop/AppWidgetHostViewDrawable.java b/src/com/android/launcher3/dragndrop/AppWidgetHostViewDrawable.java
deleted file mode 100644
index 2135f5d..0000000
--- a/src/com/android/launcher3/dragndrop/AppWidgetHostViewDrawable.java
+++ /dev/null
@@ -1,88 +0,0 @@
-/*
- * 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.
- */
-package com.android.launcher3.dragndrop;
-
-import android.graphics.Canvas;
-import android.graphics.ColorFilter;
-import android.graphics.Paint;
-import android.graphics.PixelFormat;
-import android.graphics.drawable.Drawable;
-
-import com.android.launcher3.widget.LauncherAppWidgetHostView;
-
-/**
- * A drawable which renders {@link LauncherAppWidgetHostView} to a canvas.
- *
- * TODO(b/183609936) Stop using that class and remove it.
- */
-public final class AppWidgetHostViewDrawable extends Drawable {
-
-    private final LauncherAppWidgetHostView mAppWidgetHostView;
-    private Paint mPaint = new Paint();
-
-    public AppWidgetHostViewDrawable(LauncherAppWidgetHostView appWidgetHostView) {
-        mAppWidgetHostView = appWidgetHostView;
-    }
-
-    @Override
-    public void draw(Canvas canvas) {
-        int saveCount = canvas.saveLayer(0, 0, getIntrinsicWidth(), getIntrinsicHeight(), mPaint);
-        mAppWidgetHostView.draw(canvas);
-        canvas.restoreToCount(saveCount);
-    }
-
-    @Override
-    public int getIntrinsicWidth() {
-        return mAppWidgetHostView.getMeasuredWidth();
-    }
-
-    @Override
-    public int getIntrinsicHeight() {
-        return mAppWidgetHostView.getMeasuredHeight();
-    }
-
-    @Override
-    public int getOpacity() {
-        // This is up to app widget provider. We don't know if the host view will cover anything
-        // behind the drawable.
-        return PixelFormat.UNKNOWN;
-    }
-
-    @Override
-    public void setAlpha(int alpha) {
-        mPaint.setAlpha(alpha);
-    }
-
-    @Override
-    public int getAlpha() {
-        return mPaint.getAlpha();
-    }
-
-    @Override
-    public void setColorFilter(ColorFilter colorFilter) {
-        mPaint.setColorFilter(colorFilter);
-    }
-
-    @Override
-    public ColorFilter getColorFilter() {
-        return mPaint.getColorFilter();
-    }
-
-    /** Returns the {@link LauncherAppWidgetHostView}. */
-    public LauncherAppWidgetHostView getAppWidgetHostView() {
-        return mAppWidgetHostView;
-    }
-}
diff --git a/src/com/android/launcher3/dragndrop/BaseItemDragListener.java b/src/com/android/launcher3/dragndrop/BaseItemDragListener.java
index 707fd06..981e3a6 100644
--- a/src/com/android/launcher3/dragndrop/BaseItemDragListener.java
+++ b/src/com/android/launcher3/dragndrop/BaseItemDragListener.java
@@ -34,7 +34,6 @@
 import com.android.launcher3.DragSource;
 import com.android.launcher3.DropTarget.DragObject;
 import com.android.launcher3.Launcher;
-import com.android.launcher3.R;
 import com.android.launcher3.util.ActivityTracker.SchedulerCallback;
 import com.android.launcher3.widget.PendingItemDragHelper;
 
@@ -143,15 +142,13 @@
         // the dragLayer alpha to 0 to have a nice fade-in animation. But that will prevent the
         // dragView from being visible. Instead just skip the fade-in animation here.
         mLauncher.getDragLayer().setAlpha(1);
-
-        dragObject.dragView.setColor(
-                mLauncher.getResources().getColor(R.color.delete_target_hover_tint));
+        dragObject.dragView.setAlpha(.5f);
     }
 
     @Override
     public void onPreDragEnd(DragObject dragObject, boolean dragStarted) {
         if (dragStarted) {
-            dragObject.dragView.setColor(0);
+            dragObject.dragView.setAlpha(1f);
         }
     }
 
diff --git a/src/com/android/launcher3/dragndrop/DragController.java b/src/com/android/launcher3/dragndrop/DragController.java
index b7a70cb..d7f6cdb 100644
--- a/src/com/android/launcher3/dragndrop/DragController.java
+++ b/src/com/android/launcher3/dragndrop/DragController.java
@@ -34,6 +34,8 @@
 import android.view.MotionEvent;
 import android.view.View;
 
+import androidx.annotation.Nullable;
+
 import com.android.launcher3.AbstractFloatingView;
 import com.android.launcher3.DragSource;
 import com.android.launcher3.DropTarget;
@@ -125,20 +127,23 @@
 
     /**
      * Starts a drag.
-     * When the drag is started, the UI automatically goes into spring loaded mode. On a successful
-     * drop, it is the responsibility of the {@link DropTarget} to exit out of the spring loaded
-     * mode. If the drop was cancelled for some reason, the UI will automatically exit out of this mode.
+     *
+     * <p>When the drag is started, the UI automatically goes into spring loaded mode. On a
+     * successful drop, it is the responsibility of the {@link DropTarget} to exit out of the spring
+     * loaded mode. If the drop was cancelled for some reason, the UI will automatically exit out of
+     * this mode.
      *
      * @param drawable The drawable to be displayed in the drag view.  It will be re-scaled to the
-     *          enlarged size.
-     * @param originalView The source view (ie. icon, widget etc.) that is being dragged
-     *          and which the DragView represents
+     *                 enlarged size.
+     * @param originalView The source view (ie. icon, widget etc.) that is being dragged and which
+     *                     the DragView represents
      * @param dragLayerX The x position in the DragLayer of the left-top of the bitmap.
      * @param dragLayerY The y position in the DragLayer of the left-top of the bitmap.
      * @param source An object representing where the drag originated
      * @param dragInfo The data associated with the object that is being dragged
      * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
-     *          Makes dragging feel more precise, e.g. you can clip out a transparent border
+     *                   Makes dragging feel more precise, e.g. you can clip out a transparent
+     *                   border
      */
     public DragView startDrag(
             Drawable drawable,
@@ -152,6 +157,61 @@
             float initialDragViewScale,
             float dragViewScaleOnDrop,
             DragOptions options) {
+        return startDrag(drawable, /* view= */ null, originalView, dragLayerX, dragLayerY,
+                source, dragInfo, dragOffset, dragRegion, initialDragViewScale, dragViewScaleOnDrop,
+                options);
+    }
+
+    /**
+     * Starts a drag.
+     *
+     * <p>When the drag is started, the UI automatically goes into spring loaded mode. On a
+     * successful drop, it is the responsibility of the {@link DropTarget} to exit out of the spring
+     * loaded mode. If the drop was cancelled for some reason, the UI will automatically exit out of
+     * this mode.
+     *
+     * @param view The view to be displayed in the drag view.  It will be re-scaled to the
+     *             enlarged size.
+     * @param originalView The source view (ie. icon, widget etc.) that is being dragged and which
+     *                     the DragView represents
+     * @param dragLayerX The x position in the DragLayer of the left-top of the bitmap.
+     * @param dragLayerY The y position in the DragLayer of the left-top of the bitmap.
+     * @param source An object representing where the drag originated
+     * @param dragInfo The data associated with the object that is being dragged
+     * @param dragRegion Coordinates within the bitmap b for the position of item being dragged.
+     *                   Makes dragging feel more precise, e.g. you can clip out a transparent
+     *                   border
+     */
+    public DragView startDrag(
+            View view,
+            DraggableView originalView,
+            int dragLayerX,
+            int dragLayerY,
+            DragSource source,
+            ItemInfo dragInfo,
+            Point dragOffset,
+            Rect dragRegion,
+            float initialDragViewScale,
+            float dragViewScaleOnDrop,
+            DragOptions options) {
+        return startDrag(/* drawable= */ null, view, originalView, dragLayerX, dragLayerY,
+                source, dragInfo, dragOffset, dragRegion, initialDragViewScale, dragViewScaleOnDrop,
+                options);
+    }
+
+    private DragView startDrag(
+            @Nullable Drawable drawable,
+            @Nullable View view,
+            DraggableView originalView,
+            int dragLayerX,
+            int dragLayerY,
+            DragSource source,
+            ItemInfo dragInfo,
+            Point dragOffset,
+            Rect dragRegion,
+            float initialDragViewScale,
+            float dragViewScaleOnDrop,
+            DragOptions options) {
         if (PROFILE_DRAWING_DURING_DRAG) {
             android.os.Debug.startMethodTracing("Launcher");
         }
@@ -182,14 +242,25 @@
         final Resources res = mLauncher.getResources();
         final float scaleDps = mIsInPreDrag
                 ? res.getDimensionPixelSize(R.dimen.pre_drag_view_scale) : 0f;
-        final DragView dragView = mDragObject.dragView = new DragView(
-                mLauncher,
-                drawable,
-                registrationX,
-                registrationY,
-                initialDragViewScale,
-                dragViewScaleOnDrop,
-                scaleDps);
+        final DragView dragView = mDragObject.dragView = drawable != null
+                ? new DragView(
+                    mLauncher,
+                    drawable,
+                    registrationX,
+                    registrationY,
+                    initialDragViewScale,
+                    dragViewScaleOnDrop,
+                    scaleDps)
+                : new DragView(
+                    mLauncher,
+                    view,
+                    view.getMeasuredWidth(),
+                    view.getMeasuredHeight(),
+                    registrationX,
+                    registrationY,
+                    initialDragViewScale,
+                    dragViewScaleOnDrop,
+                    scaleDps);
         dragView.setItemInfo(dragInfo);
         mDragObject.dragComplete = false;
 
diff --git a/src/com/android/launcher3/dragndrop/DragView.java b/src/com/android/launcher3/dragndrop/DragView.java
index 0f26ff4..68a8af2 100644
--- a/src/com/android/launcher3/dragndrop/DragView.java
+++ b/src/com/android/launcher3/dragndrop/DragView.java
@@ -16,19 +16,22 @@
 
 package com.android.launcher3.dragndrop;
 
+import static android.view.View.MeasureSpec.EXACTLY;
+import static android.view.View.MeasureSpec.makeMeasureSpec;
+
+import static com.android.launcher3.LauncherAnimUtils.VIEW_ALPHA;
 import static com.android.launcher3.Utilities.getBadge;
 import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
 
-import android.animation.FloatArrayEvaluator;
+import android.animation.AnimatorSet;
+import android.animation.ObjectAnimator;
 import android.animation.ValueAnimator;
 import android.animation.ValueAnimator.AnimatorUpdateListener;
 import android.annotation.TargetApi;
+import android.content.Context;
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
 import android.graphics.Color;
-import android.graphics.ColorMatrix;
-import android.graphics.ColorMatrixColorFilter;
-import android.graphics.Paint;
 import android.graphics.Path;
 import android.graphics.Point;
 import android.graphics.Rect;
@@ -39,7 +42,11 @@
 import android.os.Handler;
 import android.os.Looper;
 import android.view.View;
+import android.view.ViewGroup;
+import android.widget.FrameLayout;
+import android.widget.ImageView;
 
+import androidx.annotation.Nullable;
 import androidx.dynamicanimation.animation.FloatPropertyCompat;
 import androidx.dynamicanimation.animation.SpringAnimation;
 import androidx.dynamicanimation.animation.SpringForce;
@@ -55,22 +62,22 @@
 import com.android.launcher3.icons.LauncherIcons;
 import com.android.launcher3.model.data.ItemInfo;
 import com.android.launcher3.statemanager.StateManager.StateListener;
-import com.android.launcher3.util.Themes;
 import com.android.launcher3.util.Thunk;
+import com.android.launcher3.views.BaseDragLayer;
 
-import java.util.Arrays;
+/** A custom view for rendering an icon, folder, shortcut or widget during drag-n-drop. */
+public class DragView extends FrameLayout implements StateListener<LauncherState> {
 
-public class DragView extends View implements StateListener<LauncherState> {
-    private static final ColorMatrix sTempMatrix1 = new ColorMatrix();
-    private static final ColorMatrix sTempMatrix2 = new ColorMatrix();
-
-    public static final int COLOR_CHANGE_DURATION = 120;
     public static final int VIEW_ZOOM_DURATION = 150;
 
-    private boolean mShouldDraw = true;
-    private Drawable mDrawable;
-    private Drawable mCrossFadeDrawable;
-    @Thunk Paint mPaint;
+    private final View mContent;
+    // The following are only used for rendering mContent directly during drag-n-drop.
+    @Nullable private ViewGroup.LayoutParams mContentViewLayoutParams;
+    @Nullable private ViewGroup mContentViewParent;
+    private int mContentViewInParentViewIndex = -1;
+    private final int mWidth;
+    private final int mHeight;
+
     private final int mBlurSizeOutline;
     private final int mRegistrationX;
     private final int mRegistrationY;
@@ -85,16 +92,8 @@
     @Thunk final DragController mDragController;
     final FirstFrameAnimatorHelper mFirstFrameAnimatorHelper;
     private boolean mHasDrawn = false;
-    @Thunk float mCrossFadeProgress = 0f;
-    private boolean mAnimationCancelled = false;
 
-    ValueAnimator mAnim;
-    // The intrinsic icon scale factor is the scale factor for a drag icon over the workspace
-    // size.  This is ignored for non-icons.
-    private float mIntrinsicIconScale = 1f;
-
-    @Thunk float[] mCurrentFilter;
-    private ValueAnimator mFilterAnimator;
+    final ValueAnimator mAnim;
 
     private int mLastTouchX;
     private int mLastTouchY;
@@ -106,7 +105,14 @@
     private SpringFloatValue mTranslateX, mTranslateY;
     private Path mScaledMaskPath;
     private Drawable mBadge;
-    private ColorMatrixColorFilter mBaseFilter;
+
+    public DragView(Launcher launcher, Drawable drawable, int registrationX,
+            int registrationY, final float initialScale, final float scaleOnDrop,
+            final float finalScaleDps) {
+        this(launcher, getViewFromDrawable(launcher, drawable),
+                drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
+                registrationX, registrationY, initialScale, scaleOnDrop, finalScaleDps);
+    }
 
     /**
      * Construct the drag view.
@@ -114,11 +120,16 @@
      * The registration point is the point inside our view that the touch events should
      * be centered upon.
      * @param launcher The Launcher instance
-     * @param drawable The view that we're dragging around.  We scale it up when we draw it.
+     * @param content the view content that is attached to the drag view.
+     * @param width the width of the dragView
+     * @param height the height of the dragView
+     * @param initialScale The view that we're dragging around.  We scale it up when we draw it.
      * @param registrationX The x coordinate of the registration point.
      * @param registrationY The y coordinate of the registration point.
+     * @param scaleOnDrop the scale used in the drop animation.
+     * @param finalScaleDps the scale used in the zoom out animation when the drag view is shown.
      */
-    public DragView(Launcher launcher, Drawable drawable, int registrationX,
+    public DragView(Launcher launcher, View content, int width, int height, int registrationX,
             int registrationY, final float initialScale, final float scaleOnDrop,
             final float finalScaleDps) {
         super(launcher);
@@ -127,8 +138,19 @@
         mDragController = launcher.getDragController();
         mFirstFrameAnimatorHelper = new FirstFrameAnimatorHelper(this);
 
-        final float scale = (drawable.getIntrinsicWidth() + finalScaleDps)
-                / drawable.getIntrinsicWidth();
+        mContent = content;
+        mWidth = width;
+        mHeight = height;
+        mContentViewLayoutParams = mContent.getLayoutParams();
+        if (mContent.getParent() instanceof ViewGroup) {
+            mContentViewParent = (ViewGroup) mContent.getParent();
+            mContentViewInParentViewIndex = mContentViewParent.indexOfChild(mContent);
+            mContentViewParent.removeView(mContent);
+        }
+
+        addView(content, new LayoutParams(width, height));
+
+        final float scale = (width + finalScaleDps) / width;
 
         // Set the initial scale to avoid any jumps
         setScaleX(initialScale);
@@ -146,9 +168,7 @@
             }
         });
 
-        mDrawable = drawable;
-        setDragRegion(new Rect(0, 0, drawable.getIntrinsicWidth(),
-                drawable.getIntrinsicHeight()));
+        setDragRegion(new Rect(0, 0, width, height));
 
         // The point in our scaled bitmap that the touch events are located
         mRegistrationX = registrationX;
@@ -158,12 +178,11 @@
         mScaleOnDrop = scaleOnDrop;
 
         // Force a measure, because Workspace uses getMeasuredHeight() before the layout pass
-        int ms = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
-        measure(ms, ms);
-        mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
+        measure(makeMeasureSpec(width, EXACTLY), makeMeasureSpec(height, EXACTLY));
 
         mBlurSizeOutline = getResources().getDimensionPixelSize(R.dimen.blur_size_medium_outline);
         setElevation(getResources().getDimension(R.dimen.drag_elevation));
+        setWillNotDraw(false);
     }
 
     @Override
@@ -190,145 +209,105 @@
      */
     @TargetApi(Build.VERSION_CODES.O)
     public void setItemInfo(final ItemInfo info) {
-        if (info.itemType != LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
-                info.itemType != LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT &&
-                info.itemType != LauncherSettings.Favorites.ITEM_TYPE_FOLDER) {
+        if (info.itemType != LauncherSettings.Favorites.ITEM_TYPE_APPLICATION
+                && info.itemType != LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT
+                && info.itemType != LauncherSettings.Favorites.ITEM_TYPE_FOLDER) {
             return;
         }
         // Load the adaptive icon on a background thread and add the view in ui thread.
-        MODEL_EXECUTOR.getHandler().postAtFrontOfQueue(new Runnable() {
-            @Override
-            public void run() {
-                Object[] outObj = new Object[1];
-                int w = mDrawable.getIntrinsicWidth();
-                int h = mDrawable.getIntrinsicHeight();
-                Drawable dr = Utilities.getFullDrawable(mLauncher, info, w, h, outObj);
+        MODEL_EXECUTOR.getHandler().postAtFrontOfQueue(() -> {
+            Object[] outObj = new Object[1];
+            int w = mWidth;
+            int h = mHeight;
+            Drawable dr = Utilities.getFullDrawable(mLauncher, info, w, h, outObj);
 
-                if (dr instanceof AdaptiveIconDrawable) {
-                    int blurMargin = (int) mLauncher.getResources()
-                            .getDimension(R.dimen.blur_size_medium_outline) / 2;
+            if (dr instanceof AdaptiveIconDrawable) {
+                int blurMargin = (int) mLauncher.getResources()
+                        .getDimension(R.dimen.blur_size_medium_outline) / 2;
 
-                    Rect bounds = new Rect(0, 0, w, h);
-                    bounds.inset(blurMargin, blurMargin);
-                    // Badge is applied after icon normalization so the bounds for badge should not
-                    // be scaled down due to icon normalization.
-                    Rect badgeBounds = new Rect(bounds);
-                    mBadge = getBadge(mLauncher, info, outObj[0]);
-                    mBadge.setBounds(badgeBounds);
+                Rect bounds = new Rect(0, 0, w, h);
+                bounds.inset(blurMargin, blurMargin);
+                // Badge is applied after icon normalization so the bounds for badge should not
+                // be scaled down due to icon normalization.
+                Rect badgeBounds = new Rect(bounds);
+                mBadge = getBadge(mLauncher, info, outObj[0]);
+                mBadge.setBounds(badgeBounds);
 
-                    // Do not draw the background in case of folder as its translucent
-                    mShouldDraw = !(dr instanceof FolderAdaptiveIcon);
+                // Do not draw the background in case of folder as its translucent
+                final boolean shouldDrawBackground = !(dr instanceof FolderAdaptiveIcon);
 
-                    try (LauncherIcons li = LauncherIcons.obtain(mLauncher)) {
-                        Drawable nDr; // drawable to be normalized
-                        if (mShouldDraw) {
-                            nDr = dr;
-                        } else {
-                            // Since we just want the scale, avoid heavy drawing operations
-                            nDr = new AdaptiveIconDrawable(new ColorDrawable(Color.BLACK), null);
-                        }
-                        Utilities.scaleRectAboutCenter(bounds,
-                                li.getNormalizer().getScale(nDr, null, null, null));
+                try (LauncherIcons li = LauncherIcons.obtain(mLauncher)) {
+                    Drawable nDr; // drawable to be normalized
+                    if (shouldDrawBackground) {
+                        nDr = dr;
+                    } else {
+                        // Since we just want the scale, avoid heavy drawing operations
+                        nDr = new AdaptiveIconDrawable(new ColorDrawable(Color.BLACK), null);
                     }
-                    AdaptiveIconDrawable adaptiveIcon = (AdaptiveIconDrawable) dr;
-
-                    // Shrink very tiny bit so that the clip path is smaller than the original bitmap
-                    // that has anti aliased edges and shadows.
-                    Rect shrunkBounds = new Rect(bounds);
-                    Utilities.scaleRectAboutCenter(shrunkBounds, 0.98f);
-                    adaptiveIcon.setBounds(shrunkBounds);
-                    final Path mask = adaptiveIcon.getIconMask();
-
-                    mTranslateX = new SpringFloatValue(DragView.this,
-                            w * AdaptiveIconDrawable.getExtraInsetFraction());
-                    mTranslateY = new SpringFloatValue(DragView.this,
-                            h * AdaptiveIconDrawable.getExtraInsetFraction());
-
-                    bounds.inset(
-                            (int) (-bounds.width() * AdaptiveIconDrawable.getExtraInsetFraction()),
-                            (int) (-bounds.height() * AdaptiveIconDrawable.getExtraInsetFraction())
-                    );
-                    mBgSpringDrawable = adaptiveIcon.getBackground();
-                    if (mBgSpringDrawable == null) {
-                        mBgSpringDrawable = new ColorDrawable(Color.TRANSPARENT);
-                    }
-                    mBgSpringDrawable.setBounds(bounds);
-                    mFgSpringDrawable = adaptiveIcon.getForeground();
-                    if (mFgSpringDrawable == null) {
-                        mFgSpringDrawable = new ColorDrawable(Color.TRANSPARENT);
-                    }
-                    mFgSpringDrawable.setBounds(bounds);
-
-                    new Handler(Looper.getMainLooper()).post(new Runnable() {
-                        @Override
-                        public void run() {
-                            // Assign the variable on the UI thread to avoid race conditions.
-                            mScaledMaskPath = mask;
-
-                            if (info.isDisabled()) {
-                                FastBitmapDrawable d = new FastBitmapDrawable((Bitmap) null);
-                                d.setIsDisabled(true);
-                                mBaseFilter = (ColorMatrixColorFilter) d.getColorFilter();
-                            }
-                            updateColorFilter();
-                        }
-                    });
+                    Utilities.scaleRectAboutCenter(bounds,
+                            li.getNormalizer().getScale(nDr, null, null, null));
                 }
-            }});
+                AdaptiveIconDrawable adaptiveIcon = (AdaptiveIconDrawable) dr;
+
+                // Shrink very tiny bit so that the clip path is smaller than the original bitmap
+                // that has anti aliased edges and shadows.
+                Rect shrunkBounds = new Rect(bounds);
+                Utilities.scaleRectAboutCenter(shrunkBounds, 0.98f);
+                adaptiveIcon.setBounds(shrunkBounds);
+                final Path mask = adaptiveIcon.getIconMask();
+
+                mTranslateX = new SpringFloatValue(DragView.this,
+                        w * AdaptiveIconDrawable.getExtraInsetFraction());
+                mTranslateY = new SpringFloatValue(DragView.this,
+                        h * AdaptiveIconDrawable.getExtraInsetFraction());
+
+                bounds.inset(
+                        (int) (-bounds.width() * AdaptiveIconDrawable.getExtraInsetFraction()),
+                        (int) (-bounds.height() * AdaptiveIconDrawable.getExtraInsetFraction())
+                );
+                mBgSpringDrawable = adaptiveIcon.getBackground();
+                if (mBgSpringDrawable == null) {
+                    mBgSpringDrawable = new ColorDrawable(Color.TRANSPARENT);
+                }
+                mBgSpringDrawable.setBounds(bounds);
+                mFgSpringDrawable = adaptiveIcon.getForeground();
+                if (mFgSpringDrawable == null) {
+                    mFgSpringDrawable = new ColorDrawable(Color.TRANSPARENT);
+                }
+                mFgSpringDrawable.setBounds(bounds);
+
+                new Handler(Looper.getMainLooper()).post(() -> {
+                    // Assign the variable on the UI thread to avoid race conditions.
+                    mScaledMaskPath = mask;
+                    // Avoid relayout as we do not care about children affecting layout
+                    removeAllViewsInLayout();
+
+                    if (info.isDisabled()) {
+                        FastBitmapDrawable d = new FastBitmapDrawable((Bitmap) null);
+                        d.setIsDisabled(true);
+                        mBgSpringDrawable.setColorFilter(d.getColorFilter());
+                        mFgSpringDrawable.setColorFilter(d.getColorFilter());
+                        mBadge.setColorFilter(d.getColorFilter());
+                    }
+                    invalidate();
+                });
+            }
+        });
     }
 
-    @TargetApi(Build.VERSION_CODES.O)
-    private void updateColorFilter() {
-        if (mCurrentFilter == null) {
-            mPaint.setColorFilter(null);
-
-            if (mScaledMaskPath != null) {
-                mBgSpringDrawable.setColorFilter(mBaseFilter);
-                mFgSpringDrawable.setColorFilter(mBaseFilter);
-                mBadge.setColorFilter(mBaseFilter);
-            }
-        } else {
-            ColorMatrixColorFilter currentFilter = new ColorMatrixColorFilter(mCurrentFilter);
-            mPaint.setColorFilter(currentFilter);
-
-            if (mScaledMaskPath != null) {
-                if (mBaseFilter != null) {
-                    mBaseFilter.getColorMatrix(sTempMatrix1);
-                    sTempMatrix2.set(mCurrentFilter);
-                    sTempMatrix1.postConcat(sTempMatrix2);
-
-                    currentFilter = new ColorMatrixColorFilter(sTempMatrix1);
-                }
-
-                mBgSpringDrawable.setColorFilter(currentFilter);
-                mFgSpringDrawable.setColorFilter(currentFilter);
-                mBadge.setColorFilter(currentFilter);
-            }
+    // TODO(b/183609936): This is only for LauncherAppWidgetHostView that is rendered in a drawable.
+    // Once LauncherAppWidgetHostView is directly rendered in this view, removes this method.
+    @Override
+    public void invalidate() {
+        super.invalidate();
+        if (mContent instanceof ImageView) {
+            mContent.invalidate();
         }
-
-        invalidate();
     }
 
     @Override
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
-        setMeasuredDimension(mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());
-    }
-
-    /** Sets the scale of the view over the normal workspace icon size. */
-    public void setIntrinsicIconScaleFactor(float scale) {
-        mIntrinsicIconScale = scale;
-    }
-
-    public float getIntrinsicIconScaleFactor() {
-        return mIntrinsicIconScale;
-    }
-
-    public int getDragRegionLeft() {
-        return mDragRegion.left;
-    }
-
-    public int getDragRegionTop() {
-        return mDragRegion.top;
+        super.onMeasure(makeMeasureSpec(mWidth, EXACTLY), makeMeasureSpec(mHeight, EXACTLY));
     }
 
     public int getDragRegionWidth() {
@@ -356,40 +335,11 @@
     }
 
     @Override
-    protected void onDraw(Canvas canvas) {
+    public void draw(Canvas canvas) {
+        super.draw(canvas);
+
+        // Draw after the content
         mHasDrawn = true;
-
-        if (mShouldDraw) {
-            // Always draw the bitmap to mask anti aliasing due to clipPath
-            boolean crossFade = mCrossFadeProgress > 0 && mCrossFadeDrawable != null;
-            if (crossFade) {
-                int alpha = crossFade ? (int) (255 * (1 - mCrossFadeProgress)) : 255;
-                mPaint.setAlpha(alpha);
-            }
-            mDrawable.setColorFilter(mPaint.getColorFilter());
-            mDrawable.setAlpha(mPaint.getAlpha());
-            mDrawable.setBounds(
-                    new Rect(0, 0, mDrawable.getIntrinsicWidth(),
-                            mDrawable.getIntrinsicHeight()));
-            mDrawable.draw(canvas);
-            if (crossFade) {
-                mPaint.setAlpha((int) (255 * mCrossFadeProgress));
-                final int saveCount = canvas.save();
-                float sX = ((float) mDrawable.getIntrinsicWidth())
-                        / mCrossFadeDrawable.getIntrinsicWidth();
-                float sY = ((float) mDrawable.getIntrinsicHeight())
-                        / mCrossFadeDrawable.getIntrinsicHeight();
-                canvas.scale(sX, sY);
-                mCrossFadeDrawable.setColorFilter(mPaint.getColorFilter());
-                mCrossFadeDrawable.setAlpha(mPaint.getAlpha());
-                mDrawable.setBounds(
-                        new Rect(0, 0, mDrawable.getIntrinsicWidth(),
-                                mDrawable.getIntrinsicHeight()));
-                mCrossFadeDrawable.draw(canvas);
-                canvas.restoreToCount(saveCount);
-            }
-        }
-
         if (mScaledMaskPath != null) {
             int cnt = canvas.save();
             canvas.clipPath(mScaledMaskPath);
@@ -401,74 +351,27 @@
         }
     }
 
-    public void setCrossFadeDrawable(Drawable crossFadeDrawable) {
-        mCrossFadeDrawable = crossFadeDrawable;
-    }
-
-    public void crossFade(int duration) {
-        ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
-        va.setDuration(duration);
-        va.setInterpolator(Interpolators.DEACCEL_1_5);
-        va.addUpdateListener(a -> {
-            mCrossFadeProgress = a.getAnimatedFraction();
-            invalidate();
-        });
-        va.start();
-    }
-
-    public void setColor(int color) {
-        if (mPaint == null) {
-            mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
+    public void crossFadeContent(Drawable crossFadeDrawable, int duration) {
+        if (mContent.getParent() == null) {
+            // If the content is already removed, ignore
+            return;
         }
-        if (color != 0) {
-            ColorMatrix m1 = new ColorMatrix();
-            m1.setSaturation(0);
+        View newContent = getViewFromDrawable(getContext(), crossFadeDrawable);
+        newContent.measure(makeMeasureSpec(mWidth, EXACTLY), makeMeasureSpec(mHeight, EXACTLY));
+        newContent.layout(0, 0, mWidth, mHeight);
+        addViewInLayout(newContent, 0, new LayoutParams(mWidth, mHeight));
 
-            ColorMatrix m2 = new ColorMatrix();
-            Themes.setColorScaleOnMatrix(color, m2);
-            m1.postConcat(m2);
-
-            animateFilterTo(m1.getArray());
-        } else {
-            if (mCurrentFilter == null) {
-                updateColorFilter();
-            } else {
-                animateFilterTo(new ColorMatrix().getArray());
-            }
-        }
-    }
-
-    private void animateFilterTo(float[] targetFilter) {
-        float[] oldFilter = mCurrentFilter == null ? new ColorMatrix().getArray() : mCurrentFilter;
-        mCurrentFilter = Arrays.copyOf(oldFilter, oldFilter.length);
-
-        if (mFilterAnimator != null) {
-            mFilterAnimator.cancel();
-        }
-        mFilterAnimator = ValueAnimator.ofObject(new FloatArrayEvaluator(mCurrentFilter),
-                oldFilter, targetFilter);
-        mFilterAnimator.setDuration(COLOR_CHANGE_DURATION);
-        mFilterAnimator.addUpdateListener(new AnimatorUpdateListener() {
-
-            @Override
-            public void onAnimationUpdate(ValueAnimator animation) {
-                updateColorFilter();
-            }
-        });
-        mFilterAnimator.start();
+        AnimatorSet anim = new AnimatorSet();
+        anim.play(ObjectAnimator.ofFloat(newContent, VIEW_ALPHA, 0, 1));
+        anim.play(ObjectAnimator.ofFloat(mContent, VIEW_ALPHA, 0));
+        anim.setDuration(duration).setInterpolator(Interpolators.DEACCEL_1_5);
+        anim.start();
     }
 
     public boolean hasDrawn() {
         return mHasDrawn;
     }
 
-    @Override
-    public void setAlpha(float alpha) {
-        super.setAlpha(alpha);
-        mPaint.setAlpha((int) (255 * alpha));
-        invalidate();
-    }
-
     /**
      * Create a window containing this view and show it.
      *
@@ -479,22 +382,21 @@
         mDragLayer.addView(this);
 
         // Start the pick-up animation
-        DragLayer.LayoutParams lp = new DragLayer.LayoutParams(0, 0);
-        lp.width = mDrawable.getIntrinsicWidth();
-        lp.height = mDrawable.getIntrinsicHeight();
+        BaseDragLayer.LayoutParams lp = new BaseDragLayer.LayoutParams(mWidth, mHeight);
         lp.customPosition = true;
         setLayoutParams(lp);
+
+        if (mContent != null) {
+            // At the drag start, the source view visibility is set to invisible.
+            mContent.setVisibility(VISIBLE);
+        }
+
         move(touchX, touchY);
         // Post the animation to skip other expensive work happening on the first frame
-        post(new Runnable() {
-            public void run() {
-                mAnim.start();
-            }
-        });
+        post(mAnim::start);
     }
 
     public void cancelAnimation() {
-        mAnimationCancelled = true;
         if (mAnim != null && mAnim.isRunning()) {
             mAnim.cancel();
         }
@@ -547,6 +449,33 @@
         setTranslationY(mLastTouchY - mRegistrationY + mAnimatedShiftY);
     }
 
+
+    /**
+     * Detaches {@link #mContent}, if previously attached, from this view.
+     *
+     * <p>In the case of no change in the drop position, sets {@code reattachToPreviousParent} to
+     * {@code true} to attach the {@link #mContent} back to its previous parent.
+     */
+    public void detachContentView(boolean reattachToPreviousParent) {
+        if (mContent != null && mContentViewParent != null && mContentViewInParentViewIndex >= 0) {
+            removeView(mContent);
+            mContent.setLayoutParams(mContentViewLayoutParams);
+            if (reattachToPreviousParent) {
+                mContentViewParent.addView(mContent, mContentViewInParentViewIndex);
+            }
+            mContentViewParent = null;
+            mContentViewInParentViewIndex = -1;
+        }
+    }
+
+    /**
+     * Removes this view from the {@link DragLayer}.
+     *
+     * <p>If the drag content is a {@link #mContent}, this call doesn't reattach the
+     * {@link #mContent} back to its previous parent. To reattach to previous parent, the caller
+     * should call {@link #detachContentView} with {@code reattachToPreviousParent} sets to true
+     * before this call.
+     */
     public void remove() {
         if (getParent() != null) {
             mDragLayer.removeView(DragView.this);
@@ -561,9 +490,23 @@
         return mInitialScale;
     }
 
-    /** Returns the current {@link Drawable} that is rendered in this view. */
-    public Drawable getDrawable() {
-        return mDrawable;
+    @Override
+    public boolean hasOverlappingRendering() {
+        return false;
+    }
+
+    /** Returns the current content view that is rendered in the drag view. */
+    public View getContentView() {
+        return mContent;
+    }
+
+    /**
+     * Returns the previous {@link ViewGroup} parent of the {@link #mContent} before the drag
+     * content is attached to this view.
+     */
+    @Nullable
+    public ViewGroup getContentViewParent() {
+        return mContentViewParent;
     }
 
     private static class SpringFloatValue {
@@ -583,9 +526,9 @@
                 };
 
         // Following three values are fine tuned with motion ux designer
-        private final static int STIFFNESS = 4000;
-        private final static float DAMPENING_RATIO = 1f;
-        private final static int PARALLAX_MAX_IN_DP = 8;
+        private static final int STIFFNESS = 4000;
+        private static final float DAMPENING_RATIO = 1f;
+        private static final int PARALLAX_MAX_IN_DP = 8;
 
         private final View mView;
         private final SpringAnimation mSpring;
@@ -607,4 +550,10 @@
             mSpring.animateToFinalPosition(Utilities.boundToRange(value, -mDelta, mDelta));
         }
     }
+
+    private static View getViewFromDrawable(Context context, Drawable drawable) {
+        ImageView iv = new ImageView(context);
+        iv.setImageDrawable(drawable);
+        return iv;
+    }
 }
diff --git a/src/com/android/launcher3/graphics/DragPreviewProvider.java b/src/com/android/launcher3/graphics/DragPreviewProvider.java
index 0e61b98..a549750 100644
--- a/src/com/android/launcher3/graphics/DragPreviewProvider.java
+++ b/src/com/android/launcher3/graphics/DragPreviewProvider.java
@@ -29,11 +29,12 @@
 import android.graphics.drawable.Drawable;
 import android.view.View;
 
+import androidx.annotation.Nullable;
+
 import com.android.launcher3.BubbleTextView;
 import com.android.launcher3.Launcher;
 import com.android.launcher3.R;
 import com.android.launcher3.config.FeatureFlags;
-import com.android.launcher3.dragndrop.AppWidgetHostViewDrawable;
 import com.android.launcher3.dragndrop.DraggableView;
 import com.android.launcher3.icons.BitmapRenderer;
 import com.android.launcher3.icons.FastBitmapDrawable;
@@ -95,7 +96,7 @@
      */
     public Drawable createDrawable() {
         if (mView instanceof LauncherAppWidgetHostView) {
-            return new AppWidgetHostViewDrawable((LauncherAppWidgetHostView) mView);
+            return null;
         }
 
         int width = 0;
@@ -116,6 +117,18 @@
                         height + blurSizeOutline, (c) -> drawDragView(c, scale)));
     }
 
+    /**
+     * Returns the content view if the content should be rendered directly in
+     * {@link com.android.launcher3.dragndrop.DragView}. Otherwise, returns null.
+     */
+    @Nullable
+    public View getContentView() {
+        if (mView instanceof LauncherAppWidgetHostView) {
+            return mView;
+        }
+        return null;
+    }
+
     public final void generateDragOutline(Bitmap preview) {
         if (FeatureFlags.IS_STUDIO_BUILD && mOutlineGeneratorCallback != null) {
             throw new RuntimeException("Drag outline generated twice");
@@ -152,6 +165,22 @@
         return scale;
     }
 
+    /** Returns the scale and position of a given view for drag-n-drop. */
+    public float getScaleAndPosition(View view, int[] outPos) {
+        float scale = Launcher.getLauncher(mView.getContext())
+                .getDragLayer().getLocationInDragLayer(mView, outPos);
+        if (mView instanceof LauncherAppWidgetHostView) {
+            // App widgets are technically scaled, but are drawn at their expected size -- so the
+            // app widget scale should not affect the scale of the preview.
+            scale /= ((LauncherAppWidgetHostView) mView).getScaleToFit();
+        }
+
+        outPos[0] = Math.round(outPos[0]
+                - (view.getWidth() - scale * mView.getWidth() * mView.getScaleX()) / 2);
+        outPos[1] = Math.round(outPos[1] - (1 - scale) * view.getHeight() / 2 - previewPadding / 2);
+        return scale;
+    }
+
     protected Bitmap convertPreviewToAlphaBitmap(Bitmap preview) {
         return preview.copy(Bitmap.Config.ALPHA_8, true);
     }
diff --git a/src/com/android/launcher3/model/LoaderTask.java b/src/com/android/launcher3/model/LoaderTask.java
index f6c7c06..79396b1 100644
--- a/src/com/android/launcher3/model/LoaderTask.java
+++ b/src/com/android/launcher3/model/LoaderTask.java
@@ -109,6 +109,8 @@
 public class LoaderTask implements Runnable {
     private static final String TAG = "LoaderTask";
 
+    private static final boolean DEBUG = true;
+
     protected final LauncherAppState mApp;
     private final AllAppsList mBgAllAppsList;
     protected final BgDataModel mBgDataModel;
@@ -190,7 +192,7 @@
         try (LauncherModel.LoaderTransaction transaction = mApp.getModel().beginLoader(this)) {
             List<ShortcutInfo> allShortcuts = new ArrayList<>();
             loadWorkspace(allShortcuts);
-            logger.addSplit("loadWorkspace");
+            logASplit(logger, "loadWorkspace");
 
             // Sanitize data re-syncs widgets/shortcuts based on the workspace loaded from db.
             // sanitizeData should not be invoked if the workspace is loaded from a db different
@@ -199,30 +201,30 @@
             if (mApp.getInvariantDeviceProfile().dbFile.equals(mDbName)) {
                 verifyNotStopped();
                 sanitizeData();
-                logger.addSplit("sanitizeData");
+                logASplit(logger, "sanitizeData");
             }
 
             verifyNotStopped();
             mResults.bindWorkspace();
-            logger.addSplit("bindWorkspace");
+            logASplit(logger, "bindWorkspace");
 
             mModelDelegate.workspaceLoadComplete();
             // Notify the installer packages of packages with active installs on the first screen.
             sendFirstScreenActiveInstallsBroadcast();
-            logger.addSplit("sendFirstScreenActiveInstallsBroadcast");
+            logASplit(logger, "sendFirstScreenActiveInstallsBroadcast");
 
             // Take a break
             waitForIdle();
-            logger.addSplit("step 1 complete");
+            logASplit(logger, "step 1 complete");
             verifyNotStopped();
 
             // second step
             List<LauncherActivityInfo> allActivityList = loadAllApps();
-            logger.addSplit("loadAllApps");
+            logASplit(logger, "loadAllApps");
 
             verifyNotStopped();
             mResults.bindAllApps();
-            logger.addSplit("bindAllApps");
+            logASplit(logger, "bindAllApps");
 
             verifyNotStopped();
             IconCacheUpdateHandler updateHandler = mIconCache.getUpdateHandler();
@@ -230,54 +232,54 @@
             updateHandler.updateIcons(allActivityList,
                     LauncherActivityCachingLogic.newInstance(mApp.getContext()),
                     mApp.getModel()::onPackageIconsUpdated);
-            logger.addSplit("update icon cache");
+            logASplit(logger, "update icon cache");
 
             if (FeatureFlags.ENABLE_DEEP_SHORTCUT_ICON_CACHE.get()) {
                 verifyNotStopped();
-                logger.addSplit("save shortcuts in icon cache");
+                logASplit(logger, "save shortcuts in icon cache");
                 updateHandler.updateIcons(allShortcuts, new ShortcutCachingLogic(),
                         mApp.getModel()::onPackageIconsUpdated);
             }
 
             // Take a break
             waitForIdle();
-            logger.addSplit("step 2 complete");
+            logASplit(logger, "step 2 complete");
             verifyNotStopped();
 
             // third step
             List<ShortcutInfo> allDeepShortcuts = loadDeepShortcuts();
-            logger.addSplit("loadDeepShortcuts");
+            logASplit(logger, "loadDeepShortcuts");
 
             verifyNotStopped();
             mResults.bindDeepShortcuts();
-            logger.addSplit("bindDeepShortcuts");
+            logASplit(logger, "bindDeepShortcuts");
 
             if (FeatureFlags.ENABLE_DEEP_SHORTCUT_ICON_CACHE.get()) {
                 verifyNotStopped();
-                logger.addSplit("save deep shortcuts in icon cache");
+                logASplit(logger, "save deep shortcuts in icon cache");
                 updateHandler.updateIcons(allDeepShortcuts,
                         new ShortcutCachingLogic(), (pkgs, user) -> { });
             }
 
             // Take a break
             waitForIdle();
-            logger.addSplit("step 3 complete");
+            logASplit(logger, "step 3 complete");
             verifyNotStopped();
 
             // fourth step
             List<ComponentWithLabelAndIcon> allWidgetsList =
                     mBgDataModel.widgetsModel.update(mApp, null);
-            logger.addSplit("load widgets");
+            logASplit(logger, "load widgets");
 
             verifyNotStopped();
             mResults.bindWidgets();
-            logger.addSplit("bindWidgets");
+            logASplit(logger, "bindWidgets");
             verifyNotStopped();
 
             updateHandler.updateIcons(allWidgetsList,
                     new ComponentWithIconCachingLogic(mApp.getContext(), true),
                     mApp.getModel()::onWidgetLabelsUpdated);
-            logger.addSplit("save widgets in icon cache");
+            logASplit(logger, "save widgets in icon cache");
 
             // fifth step
             if (FeatureFlags.FOLDER_NAME_SUGGEST.get()) {
@@ -286,13 +288,13 @@
 
             verifyNotStopped();
             updateHandler.finish();
-            logger.addSplit("finish icon update");
+            logASplit(logger, "finish icon update");
 
             mModelDelegate.modelLoadComplete();
             transaction.commit();
         } catch (CancellationException e) {
             // Loader stopped, ignore
-            logger.addSplit("Cancelled");
+            logASplit(logger, "Cancelled");
         } finally {
             logger.dumpToLog();
         }
@@ -977,4 +979,11 @@
         return (provider != null) && (provider.provider != null)
                 && (provider.provider.getPackageName() != null);
     }
+
+    private static void logASplit(final TimingLogger logger, final String label) {
+        logger.addSplit(label);
+        if (DEBUG) {
+            Log.d(TAG, label);
+        }
+    }
 }
diff --git a/src/com/android/launcher3/testing/TestProtocol.java b/src/com/android/launcher3/testing/TestProtocol.java
index 60dd0f3..9a02b66 100644
--- a/src/com/android/launcher3/testing/TestProtocol.java
+++ b/src/com/android/launcher3/testing/TestProtocol.java
@@ -109,7 +109,6 @@
     public static final String REQUEST_MOCK_SENSOR_ROTATION = "mock-sensor-rotation";
 
     public static final String PERMANENT_DIAG_TAG = "TaplTarget";
-    public static final String RECEIVER_LEAK = "b/185385047";
     public static final String NO_SWIPE_TO_HOME = "b/158017601";
     public static final String WORK_PROFILE_REMOVED = "b/159671700";
     public static final String TIS_NO_EVENTS = "b/180915942";
diff --git a/src/com/android/launcher3/util/FlingAnimation.java b/src/com/android/launcher3/util/FlingAnimation.java
index 9d0ad22..c9aa51c 100644
--- a/src/com/android/launcher3/util/FlingAnimation.java
+++ b/src/com/android/launcher3/util/FlingAnimation.java
@@ -70,9 +70,6 @@
 
         mAnimationTimeFraction = ((float) mDuration) / (mDuration + DRAG_END_DELAY);
 
-        // Don't highlight the icon as it's animating
-        mDragObject.dragView.setColor(0);
-
         final int duration = mDuration + DRAG_END_DELAY;
         final long startTime = AnimationUtils.currentAnimationTimeMillis();
 
diff --git a/src/com/android/launcher3/widget/LauncherAppWidgetHostView.java b/src/com/android/launcher3/widget/LauncherAppWidgetHostView.java
index 0d53f20..f77c740 100644
--- a/src/com/android/launcher3/widget/LauncherAppWidgetHostView.java
+++ b/src/com/android/launcher3/widget/LauncherAppWidgetHostView.java
@@ -339,7 +339,7 @@
         // The layout depends on the orientation.
         if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
             int parentViewWidth = parentView == null ? 0 : parentView.getWidth();
-            xOffset = screenHeight - mWorkspace.getPaddingRight() - parentViewWidth;
+            xOffset = screenWidth - mWorkspace.getPaddingRight() - parentViewWidth;
         } else {
             int parentViewPaddingLeft = parentView == null ? 0 : parentView.getPaddingLeft();
             xOffset = mWorkspace.getPaddingLeft() + parentViewPaddingLeft;
diff --git a/src/com/android/launcher3/widget/PendingItemDragHelper.java b/src/com/android/launcher3/widget/PendingItemDragHelper.java
index e78d517..df368cd 100644
--- a/src/com/android/launcher3/widget/PendingItemDragHelper.java
+++ b/src/com/android/launcher3/widget/PendingItemDragHelper.java
@@ -33,7 +33,6 @@
 import com.android.launcher3.LauncherAppState;
 import com.android.launcher3.PendingAddItemInfo;
 import com.android.launcher3.R;
-import com.android.launcher3.dragndrop.AppWidgetHostViewDrawable;
 import com.android.launcher3.dragndrop.DragOptions;
 import com.android.launcher3.dragndrop.DraggableView;
 import com.android.launcher3.graphics.DragPreviewProvider;
@@ -93,6 +92,8 @@
         LauncherAppState app = LauncherAppState.getInstance(launcher);
 
         Drawable preview = null;
+        final int previewWidth;
+        final int previewHeight;
         final float scale;
         final Point dragOffset;
         final Rect dragRegion;
@@ -114,12 +115,11 @@
                                 createWidgetInfo.info, maxWidth, previewSizeBeforeScale));
             }
             if (mAppWidgetHostViewPreview != null) {
-                preview = new AppWidgetHostViewDrawable(mAppWidgetHostViewPreview);
                 previewSizeBeforeScale[0] = mAppWidgetHostViewPreview.getMeasuredWidth();
                 launcher.getDragController()
                         .addDragListener(new AppWidgetHostViewDragListener(launcher));
             }
-            if (preview == null) {
+            if (preview == null && mAppWidgetHostViewPreview == null) {
                 Drawable p = new FastBitmapDrawable(
                         app.getWidgetCache().generateWidgetPreview(launcher,
                                 createWidgetInfo.info, maxWidth, null,
@@ -140,7 +140,14 @@
                 previewBounds.left += padding;
                 previewBounds.right -= padding;
             }
-            scale = previewBounds.width() / (float) preview.getIntrinsicWidth();
+            if (mAppWidgetHostViewPreview != null) {
+                previewWidth = mAppWidgetHostViewPreview.getMeasuredWidth();
+                previewHeight = mAppWidgetHostViewPreview.getMeasuredHeight();
+            } else {
+                previewWidth = preview.getIntrinsicWidth();
+                previewHeight = preview.getIntrinsicHeight();
+            }
+            scale = previewBounds.width() / (float) previewWidth;
             launcher.getDragController().addDragListener(new WidgetHostViewLoader(launcher, mView));
 
             dragOffset = null;
@@ -152,8 +159,10 @@
             LauncherIcons li = LauncherIcons.obtain(launcher);
             preview = new FastBitmapDrawable(
                     li.createScaledBitmapWithoutShadow(icon, 0));
+            previewWidth = preview.getIntrinsicWidth();
+            previewHeight = preview.getIntrinsicHeight();
             li.recycle();
-            scale = ((float) launcher.getDeviceProfile().iconSizePx) / preview.getIntrinsicWidth();
+            scale = ((float) launcher.getDeviceProfile().iconSizePx) / previewWidth;
 
             dragOffset = new Point(previewPadding / 2, previewPadding / 2);
 
@@ -181,13 +190,19 @@
         launcher.getWorkspace().prepareDragWithProvider(this);
 
         int dragLayerX = screenPos.x + previewBounds.left
-                + (int) ((scale * preview.getIntrinsicWidth() - preview.getIntrinsicWidth()) / 2);
+                + (int) ((scale * previewWidth - previewWidth) / 2);
         int dragLayerY = screenPos.y + previewBounds.top
-                + (int) ((scale * preview.getIntrinsicHeight() - preview.getIntrinsicHeight()) / 2);
+                + (int) ((scale * previewHeight - previewHeight) / 2);
 
         // Start the drag
-        launcher.getDragController().startDrag(preview, draggableView, dragLayerX, dragLayerY,
-                source, mAddInfo, dragOffset, dragRegion, scale, scale, options);
+        if (mAppWidgetHostViewPreview != null) {
+            launcher.getDragController().startDrag(mAppWidgetHostViewPreview, draggableView,
+                    dragLayerX, dragLayerY, source, mAddInfo, dragOffset, dragRegion, scale, scale,
+                    options);
+        } else {
+            launcher.getDragController().startDrag(preview, draggableView, dragLayerX, dragLayerY,
+                    source, mAddInfo, dragOffset, dragRegion, scale, scale, options);
+        }
     }
 
     @Override
diff --git a/src/com/android/launcher3/widget/dragndrop/AppWidgetHostViewDragListener.java b/src/com/android/launcher3/widget/dragndrop/AppWidgetHostViewDragListener.java
index 66bb363..4a60983 100644
--- a/src/com/android/launcher3/widget/dragndrop/AppWidgetHostViewDragListener.java
+++ b/src/com/android/launcher3/widget/dragndrop/AppWidgetHostViewDragListener.java
@@ -17,7 +17,6 @@
 
 import com.android.launcher3.DropTarget;
 import com.android.launcher3.Launcher;
-import com.android.launcher3.dragndrop.AppWidgetHostViewDrawable;
 import com.android.launcher3.dragndrop.DragController;
 import com.android.launcher3.dragndrop.DragOptions;
 import com.android.launcher3.widget.LauncherAppWidgetHostView;
@@ -26,7 +25,6 @@
 public final class AppWidgetHostViewDragListener implements DragController.DragListener {
     private final Launcher mLauncher;
     private DropTarget.DragObject mDragObject;
-    private AppWidgetHostViewDrawable mAppWidgetHostViewDrawable;
     private LauncherAppWidgetHostView mAppWidgetHostView;
 
     public AppWidgetHostViewDragListener(Launcher launcher) {
@@ -35,11 +33,9 @@
 
     @Override
     public void onDragStart(DropTarget.DragObject dragObject, DragOptions unused) {
-        if (dragObject.dragView.getDrawable() instanceof AppWidgetHostViewDrawable) {
+        if (dragObject.dragView.getContentView() instanceof LauncherAppWidgetHostView) {
             mDragObject = dragObject;
-            mAppWidgetHostViewDrawable =
-                    (AppWidgetHostViewDrawable) mDragObject.dragView.getDrawable();
-            mAppWidgetHostView = mAppWidgetHostViewDrawable.getAppWidgetHostView();
+            mAppWidgetHostView = (LauncherAppWidgetHostView) dragObject.dragView.getContentView();
             mAppWidgetHostView.startDrag(this);
         } else {
             mLauncher.getDragController().removeDragListener(this);
diff --git a/src/com/android/launcher3/widget/picker/WidgetsFullSheet.java b/src/com/android/launcher3/widget/picker/WidgetsFullSheet.java
index 2bba316..5543256 100644
--- a/src/com/android/launcher3/widget/picker/WidgetsFullSheet.java
+++ b/src/com/android/launcher3/widget/picker/WidgetsFullSheet.java
@@ -435,8 +435,11 @@
 
     private void setViewVisibilityBasedOnSearch(boolean isInSearchMode) {
         mIsInSearchMode = isInSearchMode;
-        mSearchAndRecommendationViewHolder.mRecommendedWidgetsTable
-                .setVisibility(isInSearchMode ? GONE : VISIBLE);
+        if (isInSearchMode) {
+            mSearchAndRecommendationViewHolder.mRecommendedWidgetsTable.setVisibility(GONE);
+        } else {
+            onRecommendedWidgetsBound();
+        }
         if (mHasWorkProfile) {
             mViewPager.setVisibility(isInSearchMode ? GONE : VISIBLE);
             mTabsView.setVisibility(isInSearchMode ? GONE : VISIBLE);
@@ -466,8 +469,8 @@
                 Log.d(TAG, "Header view height is 0 when inflating recommended widgets");
             }
             float maxTableHeight =
-                    (mLauncher.getDeviceProfile().heightPx - mTabsHeight - getHeaderViewHeight())
-                            * RECOMMENDATION_TABLE_HEIGHT_RATIO;
+                    (mLauncher.getDeviceProfile().availableHeightPx - mTabsHeight
+                            - getHeaderViewHeight()) * RECOMMENDATION_TABLE_HEIGHT_RATIO;
             List<ArrayList<WidgetItem>> recommendedWidgetsInTable =
                     WidgetsTableUtils.groupWidgetItemsIntoTable(recommendedWidgets,
                             mMaxSpansPerRow);
diff --git a/src/com/android/launcher3/widget/picker/WidgetsRecommendationTableLayout.java b/src/com/android/launcher3/widget/picker/WidgetsRecommendationTableLayout.java
index 1bfffc2..824b580 100644
--- a/src/com/android/launcher3/widget/picker/WidgetsRecommendationTableLayout.java
+++ b/src/com/android/launcher3/widget/picker/WidgetsRecommendationTableLayout.java
@@ -42,7 +42,6 @@
     private static final String TAG = "WidgetsRecommendationTableLayout";
     private static final float DOWN_SCALE_RATIO = 0.9f;
     private static final float MAX_DOWN_SCALE_RATIO = 0.5f;
-    private final DeviceProfile mDeviceProfile;
     private final float mWidgetCellTextViewsHeight;
 
     private float mRecommendationTableMaxHeight = Float.MAX_VALUE;
@@ -56,7 +55,6 @@
 
     public WidgetsRecommendationTableLayout(Context context, AttributeSet attrs) {
         super(context, attrs);
-        mDeviceProfile = Launcher.getLauncher(context).getDeviceProfile();
         // There are 1 row for title, 1 row for dimension and 2 rows for description.
         mWidgetCellTextViewsHeight = 4 * getResources().getDimension(R.dimen.widget_cell_font_size);
     }
@@ -143,11 +141,12 @@
         }
         // A naive estimation of the widgets recommendation table height without inflation.
         float totalHeight = 0;
+        DeviceProfile deviceProfile = Launcher.getLauncher(getContext()).getDeviceProfile();
         for (int i = 0; i < recommendedWidgetsInTable.size(); i++) {
             List<WidgetItem> widgetItems = recommendedWidgetsInTable.get(i);
             float rowHeight = 0;
             for (int j = 0; j < widgetItems.size(); j++) {
-                float previewHeight = widgetItems.get(j).spanY * mDeviceProfile.allAppsCellHeightPx
+                float previewHeight = widgetItems.get(j).spanY * deviceProfile.allAppsCellHeightPx
                         * previewScale;
                 rowHeight = Math.max(rowHeight, previewHeight + mWidgetCellTextViewsHeight);
             }
diff --git a/src/com/android/launcher3/widget/picker/WidgetsRecyclerView.java b/src/com/android/launcher3/widget/picker/WidgetsRecyclerView.java
index 69672f2..eb821d4 100644
--- a/src/com/android/launcher3/widget/picker/WidgetsRecyclerView.java
+++ b/src/com/android/launcher3/widget/picker/WidgetsRecyclerView.java
@@ -116,6 +116,7 @@
     public void onUpdateScrollbar(int dy) {
         // Skip early if widgets are not bound.
         if (isModelNotReady()) {
+            mScrollbar.setThumbOffsetY(-1);
             return;
         }
 
diff --git a/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java b/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java
index e7e245f..2a0f7bb 100644
--- a/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java
+++ b/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java
@@ -115,7 +115,6 @@
         if (TestHelpers.isInLauncherProcess()) {
             StrictMode.VmPolicy.Builder builder =
                     new StrictMode.VmPolicy.Builder()
-                            .detectActivityLeaks()
                             .penaltyLog()
                             .penaltyListener(Runnable::run, violation -> {
                                 if (sStrictmodeDetectedActivityLeak == null) {