From a28182d9c6eff335a9786d9ee0b2621fd7cde8f5 Mon Sep 17 00:00:00 2001 From: Sihua Ma Date: Wed, 28 Sep 2022 13:36:06 -0700 Subject: [PATCH] Correctly show the widget in the recommendation table Test: Manual Bug: 246121954 Change-Id: Ib32a20683b3dc456417549284744642ad451a829 --- .../widget/LauncherAppWidgetHostView.java | 24 ------- .../android/launcher3/widget/WidgetCell.java | 62 +++++++++++++++++-- 2 files changed, 57 insertions(+), 29 deletions(-) diff --git a/src/com/android/launcher3/widget/LauncherAppWidgetHostView.java b/src/com/android/launcher3/widget/LauncherAppWidgetHostView.java index fc1e880373..bc3889fd26 100644 --- a/src/com/android/launcher3/widget/LauncherAppWidgetHostView.java +++ b/src/com/android/launcher3/widget/LauncherAppWidgetHostView.java @@ -92,10 +92,6 @@ public class LauncherAppWidgetHostView extends BaseLauncherAppWidgetHostView // The following member variables are only used during drag-n-drop. private boolean mIsInDragMode = false; - /** The drag content width which is only set when the drag content scale is not 1f. */ - private int mDragContentWidth = 0; - /** The drag content height which is only set when the drag content scale is not 1f. */ - private int mDragContentHeight = 0; private boolean mTrackingWidgetUpdate = false; @@ -314,27 +310,9 @@ public class LauncherAppWidgetHostView extends BaseLauncherAppWidgetHostView } } - @Override - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { - super.onMeasure(widthMeasureSpec, heightMeasureSpec); - if (mIsInDragMode && mDragContentWidth > 0 && mDragContentHeight > 0 - && getChildCount() == 1) { - measureChild(getChildAt(0), MeasureSpec.getSize(mDragContentWidth), - MeasureSpec.getSize(mDragContentHeight)); - } - } - /** Starts the drag mode. */ public void startDrag() { mIsInDragMode = true; - // In the case of dragging a scaled preview from widgets picker, we should reuse the - // previously measured dimension from WidgetCell#measureAndComputeWidgetPreviewScale, which - // measures the dimension of a widget preview without its parent's bound before scaling - // down. - if ((getScaleX() != 1f || getScaleY() != 1f) && getChildCount() == 1) { - mDragContentWidth = getChildAt(0).getMeasuredWidth(); - mDragContentHeight = getChildAt(0).getMeasuredHeight(); - } } /** Handles a drag event occurred on a workspace page corresponding to the {@code screenId}. */ @@ -347,8 +325,6 @@ public class LauncherAppWidgetHostView extends BaseLauncherAppWidgetHostView /** Ends the drag mode. */ public void endDrag() { mIsInDragMode = false; - mDragContentWidth = 0; - mDragContentHeight = 0; requestLayout(); } diff --git a/src/com/android/launcher3/widget/WidgetCell.java b/src/com/android/launcher3/widget/WidgetCell.java index 2796721c63..ce47d70c25 100644 --- a/src/com/android/launcher3/widget/WidgetCell.java +++ b/src/com/android/launcher3/widget/WidgetCell.java @@ -284,6 +284,40 @@ public class WidgetCell extends LinearLayout { ensurePreviewWithCallback(callback, cachedPreview); } + private static class ScaledAppWidgetHostView extends LauncherAppWidgetHostView { + private boolean mKeepOrigForDragging = true; + + ScaledAppWidgetHostView(Context context) { + super(context); + } + + /** + * Set if the view will keep its original scale when dragged + * @param isKeepOrig True if keep original scale when dragged, false otherwise + */ + public void setKeepOrigForDragging(boolean isKeepOrig) { + mKeepOrigForDragging = isKeepOrig; + } + + /** + * @return True if the view is set to preserve original scale when dragged, false otherwise + */ + public boolean isKeepOrigForDragging() { + return mKeepOrigForDragging; + } + + @Override + public void startDrag() { + super.startDrag(); + if (!isKeepOrigForDragging()) { + // restore to original scale when being dragged, if set to do so + setScaleToFit(1.0f); + } + // When the drag start, translations need to be set to zero to center the view + setTranslationForCentering(0f, 0f); + } + } + private void applyPreviewOnAppWidgetHostView(WidgetItem item) { if (mRemoteViewsPreview != null) { mAppWidgetHostViewPreview = createAppWidgetHostView(getContext()); @@ -299,7 +333,7 @@ public class WidgetCell extends LinearLayout { // a preview during drag & drop. And thus, we should use LauncherAppWidgetHostView, which // supports applying local color extraction during drag & drop. mAppWidgetHostViewPreview = isLauncherContext(context) - ? new LauncherAppWidgetHostView(context) + ? new ScaledAppWidgetHostView(context) : createAppWidgetHostView(context); LauncherAppWidgetProviderInfo launcherAppWidgetProviderInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(context, item.widgetInfo.clone()); @@ -398,23 +432,41 @@ public class WidgetCell extends LinearLayout { int containerWidth = (int) (mTargetPreviewWidth * mPreviewContainerScale); int containerHeight = (int) (mTargetPreviewHeight * mPreviewContainerScale); setContainerSize(containerWidth, containerHeight); + boolean shouldMeasureAndScale = false; if (mAppWidgetHostViewPreview.getChildCount() == 1) { View widgetContent = mAppWidgetHostViewPreview.getChildAt(0); ViewGroup.LayoutParams layoutParams = widgetContent.getLayoutParams(); // We only scale preview if both the width & height of the outermost view group are // not set to MATCH_PARENT. - boolean shouldScale = + shouldMeasureAndScale = layoutParams.width != MATCH_PARENT && layoutParams.height != MATCH_PARENT; - if (shouldScale) { + if (shouldMeasureAndScale) { setNoClip(mWidgetImageContainer); setNoClip(mAppWidgetHostViewPreview); mAppWidgetHostViewScale = measureAndComputeWidgetPreviewScale(); - mAppWidgetHostViewPreview.setScaleToFit(mAppWidgetHostViewScale); } } + FrameLayout.LayoutParams params = new FrameLayout.LayoutParams( - containerWidth, containerHeight, Gravity.FILL); + mTargetPreviewWidth, mTargetPreviewHeight, Gravity.FILL); mAppWidgetHostViewPreview.setLayoutParams(params); + + if (!shouldMeasureAndScale + && mAppWidgetHostViewPreview instanceof ScaledAppWidgetHostView) { + // If the view is not measured & scaled, at least one side will match the grid size, + // so it should be safe to restore the original scale once it is dragged. + ScaledAppWidgetHostView tempView = + (ScaledAppWidgetHostView) mAppWidgetHostViewPreview; + tempView.setKeepOrigForDragging(false); + tempView.setScaleToFit(mPreviewContainerScale); + } else if (!shouldMeasureAndScale) { + mAppWidgetHostViewPreview.setScaleToFit(mPreviewContainerScale); + } else { + mAppWidgetHostViewPreview.setScaleToFit(mAppWidgetHostViewScale); + } + mAppWidgetHostViewPreview.setTranslationForCentering( + -(params.width - (params.width * mPreviewContainerScale)) / 2.0f, + -(params.height - (params.height * mPreviewContainerScale)) / 2.0f); mWidgetImageContainer.addView(mAppWidgetHostViewPreview, /* index= */ 0); mWidgetImage.setVisibility(View.GONE); applyPreview(null);