Use WidgetSizes to estimate widget size instead of custom calculation

Also fix 2 bugs in WidgetCell
1. preview layout: padding is added to the AppWidgetHostView rather
   than the preview container. We shouldn't add padding to the size
   of the preview container.
2. widget_preview_shortcut_padding is a padding added to shortcuts but
    not widgets.

Test: Open full widgets picker and observe widgets are rendered
      correctly in the widgets recommendation table. Also, observe
      the generated bitmap images are correctly rendered.
      Run AddWidgetTest.
Bug: 189975670
Change-Id: I00db3200e0b61dc5e82f3c4bfdf34e197ea20314
This commit is contained in:
Steven Ng
2021-06-07 23:46:03 +01:00
parent 22b6039270
commit 7e06df2b50
3 changed files with 22 additions and 24 deletions

View File

@@ -27,10 +27,10 @@ import android.os.AsyncTask;
import android.os.CancellationSignal;
import android.os.Process;
import android.os.UserHandle;
import android.util.ArrayMap;
import android.util.Log;
import android.util.LongSparseArray;
import android.util.Pair;
import android.util.Size;
import androidx.annotation.Nullable;
@@ -50,6 +50,7 @@ import com.android.launcher3.util.Thunk;
import com.android.launcher3.widget.LauncherAppWidgetProviderInfo;
import com.android.launcher3.widget.WidgetCell;
import com.android.launcher3.widget.WidgetManagerHelper;
import com.android.launcher3.widget.util.WidgetSizes;
import java.util.ArrayList;
import java.util.Collections;
@@ -79,9 +80,6 @@ public class WidgetPreviewLoader {
private final UserCache mUserCache;
private final CacheDb mDb;
private final UserHandle mMyUser = Process.myUserHandle();
private final ArrayMap<UserHandle, Bitmap> mUserBadges = new ArrayMap<>();
public WidgetPreviewLoader(Context context, IconCache iconCache) {
mContext = context;
mIconCache = iconCache;
@@ -366,9 +364,9 @@ public class WidgetPreviewLoader {
previewHeight = drawable.getIntrinsicHeight();
} else {
DeviceProfile dp = launcher.getDeviceProfile();
int tileSize = Math.min(dp.cellWidthPx, dp.cellHeightPx);
previewWidth = tileSize * spanX;
previewHeight = tileSize * spanY;
Size widgetSize = WidgetSizes.getWidgetSizePx(dp, spanX, spanY);
previewWidth = widgetSize.getWidth();
previewHeight = widgetSize.getHeight();
}
// Scale to fit width only - let the widget preview be clipped in the

View File

@@ -21,12 +21,12 @@ import static com.android.launcher3.Utilities.ATLEAST_S;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.CancellationSignal;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Size;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
@@ -48,6 +48,7 @@ import com.android.launcher3.WidgetPreviewLoader;
import com.android.launcher3.icons.FastBitmapDrawable;
import com.android.launcher3.icons.RoundDrawableWrapper;
import com.android.launcher3.model.WidgetItem;
import com.android.launcher3.widget.util.WidgetSizes;
/**
* Represents the individual cell of the widget inside the widget tray. The preview is drawn
@@ -96,7 +97,7 @@ public class WidgetCell extends LinearLayout implements OnLayoutChangeListener {
protected final BaseActivity mActivity;
private final CheckLongPressHelper mLongPressHelper;
private final float mEnforcedCornerRadius;
private final int mPreviewPadding;
private final int mShortcutPreviewPadding;
private RemoteViews mRemoteViewsPreview;
private NavigableAppWidgetHostView mAppWidgetHostViewPreview;
@@ -114,14 +115,14 @@ public class WidgetCell extends LinearLayout implements OnLayoutChangeListener {
mActivity = BaseActivity.fromContext(context);
mLongPressHelper = new CheckLongPressHelper(this);
mLongPressHelper.setLongPressTimeoutFactor(1);
setContainerWidth();
setWillNotDraw(false);
setClipToPadding(false);
setAccessibilityDelegate(mActivity.getAccessibilityDelegate());
mEnforcedCornerRadius = RoundedCornerEnforcement.computeEnforcedRadius(context);
mPreviewPadding =
mShortcutPreviewPadding =
2 * getResources().getDimensionPixelSize(R.dimen.widget_preview_shortcut_padding);
}
@@ -200,6 +201,8 @@ public class WidgetCell extends LinearLayout implements OnLayoutChangeListener {
mWidgetPreviewLoader = loader;
if (item.activityInfo != null) {
setTag(new PendingAddShortcutInfo(item.activityInfo));
mPreviewWidth += mShortcutPreviewPadding;
mPreviewHeight += mShortcutPreviewPadding;
} else {
setTag(new PendingAddWidgetInfo(item.widgetInfo));
}
@@ -251,8 +254,6 @@ public class WidgetCell extends LinearLayout implements OnLayoutChangeListener {
}
appWidgetHostViewPreview.setPadding(padding.left, padding.top, padding.right,
padding.bottom);
mPreviewWidth += padding.left + padding.right;
mPreviewHeight += padding.top + padding.bottom;
appWidgetHostViewPreview.updateAppWidget(remoteViews);
}
@@ -299,7 +300,7 @@ public class WidgetCell extends LinearLayout implements OnLayoutChangeListener {
float scale = 1f;
if (getWidth() > 0 && getHeight() > 0) {
// Scale down the preview size if it's wider than the cell.
float maxWidth = getWidth() - mPreviewPadding;
float maxWidth = getWidth();
float previewWidth = drawable.getIntrinsicWidth() * mPreviewScale;
scale = Math.min(maxWidth / previewWidth, 1);
}
@@ -355,11 +356,9 @@ public class WidgetCell extends LinearLayout implements OnLayoutChangeListener {
/** Sets the widget preview image size, in number of cells, and preview scale. */
public void setPreviewSize(int spanX, int spanY, float previewScale) {
DeviceProfile deviceProfile = mActivity.getDeviceProfile();
Point cellSize = deviceProfile.getCellSize();
mPreviewWidth = cellSize.x * spanX + mPreviewPadding
+ deviceProfile.cellLayoutBorderSpacingPx * (spanX - 1);
mPreviewHeight = cellSize.y * spanY + mPreviewPadding
+ deviceProfile.cellLayoutBorderSpacingPx * (spanY - 1);
Size widgetSize = WidgetSizes.getWidgetSizePx(deviceProfile, spanX, spanY);
mPreviewWidth = widgetSize.getWidth();
mPreviewHeight = widgetSize.getHeight();
mPreviewScale = previewScale;
}

View File

@@ -18,6 +18,7 @@ package com.android.launcher3.widget.picker;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Size;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
@@ -33,6 +34,7 @@ import com.android.launcher3.LauncherAppState;
import com.android.launcher3.R;
import com.android.launcher3.model.WidgetItem;
import com.android.launcher3.widget.WidgetCell;
import com.android.launcher3.widget.util.WidgetSizes;
import java.util.ArrayList;
import java.util.List;
@@ -44,7 +46,6 @@ public final class WidgetsRecommendationTableLayout extends TableLayout {
private static final float MAX_DOWN_SCALE_RATIO = 0.5f;
private final float mWidgetsRecommendationTableVerticalPadding;
private final float mWidgetCellTextViewsHeight;
private final float mWidgetPreviewPadding;
private float mRecommendationTableMaxHeight = Float.MAX_VALUE;
@Nullable private OnLongClickListener mWidgetCellOnLongClickListener;
@@ -61,8 +62,6 @@ public final class WidgetsRecommendationTableLayout extends TableLayout {
mWidgetsRecommendationTableVerticalPadding = 2 * getResources()
.getDimensionPixelSize(R.dimen.widget_cell_vertical_padding);
mWidgetCellTextViewsHeight = 4 * getResources().getDimension(R.dimen.widget_cell_font_size);
mWidgetPreviewPadding = 2 * getResources()
.getDimensionPixelSize(R.dimen.widget_preview_shortcut_padding);
}
/** Sets a {@link android.view.View.OnLongClickListener} for all widget cells in this table. */
@@ -149,8 +148,10 @@ public final class WidgetsRecommendationTableLayout extends TableLayout {
List<WidgetItem> widgetItems = recommendedWidgetsInTable.get(i);
float rowHeight = 0;
for (int j = 0; j < widgetItems.size(); j++) {
float previewHeight = widgetItems.get(j).spanY * deviceProfile.cellHeightPx
* previewScale + mWidgetPreviewPadding;
WidgetItem widgetItem = widgetItems.get(j);
Size widgetSize = WidgetSizes.getWidgetSizePx(
deviceProfile, widgetItem.spanX, widgetItem.spanY);
float previewHeight = widgetSize.getHeight() * previewScale;
rowHeight = Math.max(rowHeight, previewHeight + mWidgetCellTextViewsHeight);
}
totalHeight += rowHeight;