From f089f741adf3da7a0a5c3900baa4af3ca2f426c0 Mon Sep 17 00:00:00 2001 From: Steven Ng Date: Thu, 15 Apr 2021 16:03:28 +0100 Subject: [PATCH] Fix app widget preview layout drag-n-drop & rendering 1. Before this CL, preview layouts are not rendered in both the BottomWidgetSheet and SearchPopupSheet because the preview width & height are not set correctly. 2. Also applies the right padding to preview layouts. Bug: 185374336 Test: Drag-n-drop preview layout from the full widgets picker, the bottom widgets picker and the search widgets picker. Change-Id: I2b6303bfe4eb86a4935a5569f6107e9d46448902 --- .../widget/LauncherAppWidgetHostView.java | 9 +++-- .../android/launcher3/widget/WidgetCell.java | 39 +++++++++---------- .../launcher3/widget/WidgetCellPreview.java | 25 ++++++++++++ 3 files changed, 49 insertions(+), 24 deletions(-) diff --git a/src/com/android/launcher3/widget/LauncherAppWidgetHostView.java b/src/com/android/launcher3/widget/LauncherAppWidgetHostView.java index 687318f775..0d53f207f8 100644 --- a/src/com/android/launcher3/widget/LauncherAppWidgetHostView.java +++ b/src/com/android/launcher3/widget/LauncherAppWidgetHostView.java @@ -325,9 +325,12 @@ public class LauncherAppWidgetHostView extends NavigableAppWidgetHostView float relativeScreenWidth = 1f / numScreens; float absoluteTop = widgetLocation.top; float absoluteBottom = widgetLocation.bottom; - for (View v = (View) getParent(); - v != null && v.getId() != R.id.launcher; - v = (View) v.getParent()) { + View v = this; + while (v.getParent() instanceof View) { + v = (View) v.getParent(); + if (v.getId() != R.id.launcher) { + break; + } absoluteBottom += v.getTop(); absoluteTop += v.getTop(); } diff --git a/src/com/android/launcher3/widget/WidgetCell.java b/src/com/android/launcher3/widget/WidgetCell.java index 5e7c961708..c08160bbef 100644 --- a/src/com/android/launcher3/widget/WidgetCell.java +++ b/src/com/android/launcher3/widget/WidgetCell.java @@ -21,14 +21,15 @@ import static com.android.launcher3.Utilities.ATLEAST_S; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; +import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.os.CancellationSignal; import android.util.AttributeSet; import android.util.Log; +import android.view.Gravity; import android.view.MotionEvent; import android.view.View; import android.view.View.OnLayoutChangeListener; -import android.view.ViewGroup; import android.view.ViewPropertyAnimator; import android.view.accessibility.AccessibilityNodeInfo; import android.widget.FrameLayout; @@ -96,7 +97,6 @@ public class WidgetCell extends LinearLayout implements OnLayoutChangeListener { private Drawable mDeferredDrawable; protected final BaseActivity mActivity; - protected final DeviceProfile mDeviceProfile; private final CheckLongPressHelper mLongPressHelper; private final float mEnforcedCornerRadius; @@ -115,7 +115,6 @@ public class WidgetCell extends LinearLayout implements OnLayoutChangeListener { super(context, attrs, defStyle); mActivity = BaseActivity.fromContext(context); - mDeviceProfile = mActivity.getDeviceProfile(); mLongPressHelper = new CheckLongPressHelper(this); mLongPressHelper.setLongPressTimeoutFactor(1); @@ -127,7 +126,7 @@ public class WidgetCell extends LinearLayout implements OnLayoutChangeListener { } private void setContainerWidth() { - mCellSize = (int) (mDeviceProfile.allAppsIconSizePx * WIDTH_SCALE); + mCellSize = (int) (mActivity.getDeviceProfile().allAppsIconSizePx * WIDTH_SCALE); mPresetPreviewSize = (int) (mCellSize * PREVIEW_SCALE); mPreviewWidth = mPreviewHeight = mPresetPreviewSize; } @@ -222,16 +221,11 @@ public class WidgetCell extends LinearLayout implements OnLayoutChangeListener { launcherAppWidgetProviderInfo.initialLayout = item.widgetInfo.previewLayout; mAppWidgetHostViewPreview.setAppWidget(/* appWidgetId= */ -1, launcherAppWidgetProviderInfo); - mAppWidgetHostViewPreview.setPadding(/* left= */ 0, /* top= */0, /* right= */ - 0, /* bottom= */ 0); + Rect padding = new Rect(); + mAppWidgetHostViewPreview.getWidgetInset(mActivity.getDeviceProfile(), padding); + mAppWidgetHostViewPreview.setPadding(padding.left, padding.top, padding.right, + padding.bottom); mAppWidgetHostViewPreview.updateAppWidget(/* remoteViews= */ null); - // Gravity 77 = "fill" - FrameLayout.LayoutParams params = new FrameLayout.LayoutParams( - ViewGroup.LayoutParams.MATCH_PARENT, - ViewGroup.LayoutParams.MATCH_PARENT, /* gravity= */ 77); - mAppWidgetHostViewPreview.setLayoutParams(params); - mWidgetImageContainer.addView(mAppWidgetHostViewPreview, /* index= */ 0); - mWidgetImage.setVisibility(View.GONE); } } @@ -284,7 +278,8 @@ public class WidgetCell extends LinearLayout implements OnLayoutChangeListener { } } Drawable badge = mWidgetPreviewLoader.getBadgeForUser(mItem.user, - BaseIconFactory.getBadgeSizeForIconSize(mDeviceProfile.allAppsIconSizePx)); + BaseIconFactory.getBadgeSizeForIconSize( + mActivity.getDeviceProfile().allAppsIconSizePx)); if (badge == null) { mWidgetBadge.setVisibility(View.GONE); } else { @@ -318,11 +313,12 @@ public class WidgetCell extends LinearLayout implements OnLayoutChangeListener { } if (mAppWidgetHostViewPreview != null) { - DeviceProfile dp = mActivity.getDeviceProfile(); - int viewWidth = dp.cellWidthPx * mItem.spanX; - int viewHeight = dp.cellHeightPx * mItem.spanY; - - setContainerSize(viewWidth, viewHeight); + setContainerSize(mPreviewWidth, mPreviewHeight); + FrameLayout.LayoutParams params = new FrameLayout.LayoutParams( + mPreviewWidth, mPreviewHeight, Gravity.FILL); + mAppWidgetHostViewPreview.setLayoutParams(params); + mWidgetImageContainer.addView(mAppWidgetHostViewPreview, /* index= */ 0); + mWidgetImage.setVisibility(View.GONE); applyPreview((Drawable) null); return; } @@ -342,8 +338,9 @@ public class WidgetCell extends LinearLayout implements OnLayoutChangeListener { public void setPreviewSize(int spanX, int spanY, float previewScale) { int padding = 2 * getResources() .getDimensionPixelSize(R.dimen.widget_preview_shortcut_padding); - mPreviewWidth = mDeviceProfile.cellWidthPx * spanX + padding; - mPreviewHeight = mDeviceProfile.cellHeightPx * spanY + padding; + DeviceProfile deviceProfile = mActivity.getDeviceProfile(); + mPreviewWidth = deviceProfile.cellWidthPx * spanX + padding; + mPreviewHeight = deviceProfile.cellHeightPx * spanY + padding; mPreviewScale = previewScale; } diff --git a/src/com/android/launcher3/widget/WidgetCellPreview.java b/src/com/android/launcher3/widget/WidgetCellPreview.java index ad3a61abb4..9f45d71c47 100644 --- a/src/com/android/launcher3/widget/WidgetCellPreview.java +++ b/src/com/android/launcher3/widget/WidgetCellPreview.java @@ -20,6 +20,8 @@ import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.FrameLayout; +import androidx.annotation.Nullable; + /** * View group managing the widget preview: either using a {@link WidgetImageView} or an actual * {@link LauncherAppWidgetHostView}. @@ -43,4 +45,27 @@ public class WidgetCellPreview extends FrameLayout { return true; } + /** Returns {@code true} if this container has a preview layout. */ + public boolean hasPreviewLayout() { + for (int i = 0; i < getChildCount(); i++) { + if (getChildAt(i) instanceof LauncherAppWidgetHostView) { + return true; + } + } + return false; + } + + /** + * Returns {@link LauncherAppWidgetHostView} if this container has a preview layout. Otherwise, + * returns null. + */ + @Nullable + public LauncherAppWidgetHostView getPreviewLayout() { + for (int i = 0; i < getChildCount(); i++) { + if (getChildAt(i) instanceof LauncherAppWidgetHostView) { + return (LauncherAppWidgetHostView) getChildAt(i); + } + } + return null; + } }