From 452e8b85122c646eb9b643ea0fb15322f65782ff Mon Sep 17 00:00:00 2001 From: Steven Ng Date: Fri, 2 Jul 2021 17:51:39 +0100 Subject: [PATCH] Correct # of cells estimation that can fit horizontally in widgets picker Fix: 192661517 Test: check widgets are fit correctly in both full & bottom widgets picker in portrait and landscape in different grid settings. Change-Id: Ic1b0589b7ccf62ff1ec1fd5713b4249ba58246a5 --- .../launcher3/widget/BaseWidgetSheet.java | 13 ++++++++++ .../launcher3/widget/WidgetsBottomSheet.java | 13 ++++++---- .../widget/picker/WidgetsFullSheet.java | 25 ++++++++++--------- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/com/android/launcher3/widget/BaseWidgetSheet.java b/src/com/android/launcher3/widget/BaseWidgetSheet.java index a5c142db66..12fd026055 100644 --- a/src/com/android/launcher3/widget/BaseWidgetSheet.java +++ b/src/com/android/launcher3/widget/BaseWidgetSheet.java @@ -51,6 +51,8 @@ import com.android.launcher3.views.ArrowTipView; public abstract class BaseWidgetSheet extends AbstractSlideInView implements OnClickListener, OnLongClickListener, DragSource, PopupDataProvider.PopupDataChangeListener, Insettable { + /** The default number of cells that can fit horizontally in a widget sheet. */ + protected static final int DEFAULT_MAX_HORIZONTAL_SPANS = 4; /** * The maximum scale, [0, 1], of the device screen width that the widgets picker can consume * on large screen devices. @@ -152,6 +154,17 @@ public abstract class BaseWidgetSheet extends AbstractSlideInView MeasureSpec.getSize(heightMeasureSpec)); } + /** Returns the number of cells that can fit horizontally in a given {@code content}. */ + protected int computeMaxHorizontalSpans(View content, int contentHorizontalPaddingPx) { + DeviceProfile deviceProfile = mActivityContext.getDeviceProfile(); + int availableWidth = content.getMeasuredWidth() - contentHorizontalPaddingPx; + Point cellSize = deviceProfile.getCellSize(); + if (cellSize.x > 0) { + return availableWidth / cellSize.x; + } + return DEFAULT_MAX_HORIZONTAL_SPANS; + } + private boolean beginDraggingWidget(WidgetCell v) { // Get the widget preview as the drag representation WidgetImageView image = v.getWidgetView(); diff --git a/src/com/android/launcher3/widget/WidgetsBottomSheet.java b/src/com/android/launcher3/widget/WidgetsBottomSheet.java index c045cf1b43..14aeaf6048 100644 --- a/src/com/android/launcher3/widget/WidgetsBottomSheet.java +++ b/src/com/android/launcher3/widget/WidgetsBottomSheet.java @@ -70,9 +70,11 @@ public class WidgetsBottomSheet extends BaseWidgetSheet { private static final int DEFAULT_CLOSE_DURATION = 200; private static final long EDUCATION_TIP_DELAY_MS = 300; + private final int mWidgetSheetContentHorizontalPadding; + private ItemInfo mOriginalItemInfo; private final int mMaxTableHeight; - private int mMaxHorizontalSpan = 4; + private int mMaxHorizontalSpan = DEFAULT_MAX_HORIZONTAL_SPANS; private final OnLayoutChangeListener mLayoutChangeListenerToShowTips = new OnLayoutChangeListener() { @@ -117,6 +119,9 @@ public class WidgetsBottomSheet extends BaseWidgetSheet { if (!hasSeenEducationTip()) { addOnLayoutChangeListener(mLayoutChangeListenerToShowTips); } + + mWidgetSheetContentHorizontalPadding = getResources().getDimensionPixelSize( + R.dimen.widget_list_horizontal_margin); } @Override @@ -137,10 +142,8 @@ public class WidgetsBottomSheet extends BaseWidgetSheet { private boolean updateMaxSpansPerRow() { if (getMeasuredWidth() == 0) return false; - int paddingPx = 2 * getResources().getDimensionPixelOffset( - R.dimen.widget_cell_horizontal_padding); - int maxHorizontalSpan = findViewById(R.id.widgets_table).getMeasuredWidth() - / (mActivityContext.getDeviceProfile().cellWidthPx + paddingPx); + int maxHorizontalSpan = computeMaxHorizontalSpans(mContent, + mWidgetSheetContentHorizontalPadding); if (mMaxHorizontalSpan != maxHorizontalSpan) { // Ensure the table layout is showing widgets in the right column after measure. mMaxHorizontalSpan = maxHorizontalSpan; diff --git a/src/com/android/launcher3/widget/picker/WidgetsFullSheet.java b/src/com/android/launcher3/widget/picker/WidgetsFullSheet.java index 6c2cca6024..5e1a534bb0 100644 --- a/src/com/android/launcher3/widget/picker/WidgetsFullSheet.java +++ b/src/com/android/launcher3/widget/picker/WidgetsFullSheet.java @@ -25,6 +25,7 @@ import android.animation.PropertyValuesHolder; import android.content.Context; import android.content.pm.LauncherApps; import android.content.res.Configuration; +import android.content.res.Resources; import android.graphics.Rect; import android.os.Process; import android.os.UserHandle; @@ -148,13 +149,13 @@ public class WidgetsFullSheet extends BaseWidgetSheet private final int mTabsHeight; private final int mViewPagerTopPadding; private final int mSearchAndRecommendationContainerBottomMargin; - private final int mWidgetCellHorizontalPadding; + private final int mWidgetSheetContentHorizontalPadding; @Nullable private WidgetsRecyclerView mCurrentWidgetsRecyclerView; @Nullable private PersonalWorkPagedView mViewPager; private boolean mIsInSearchMode; private boolean mIsNoWidgetsViewNeeded; - private int mMaxSpansPerRow = 4; + private int mMaxSpansPerRow = DEFAULT_MAX_HORIZONTAL_SPANS; private View mTabsView; private TextView mNoWidgetsView; private SearchAndRecommendationViewHolder mSearchAndRecommendationViewHolder; @@ -166,19 +167,20 @@ public class WidgetsFullSheet extends BaseWidgetSheet mAdapters.put(AdapterHolder.PRIMARY, new AdapterHolder(AdapterHolder.PRIMARY)); mAdapters.put(AdapterHolder.WORK, new AdapterHolder(AdapterHolder.WORK)); mAdapters.put(AdapterHolder.SEARCH, new AdapterHolder(AdapterHolder.SEARCH)); + + Resources resources = getResources(); mTabsHeight = mHasWorkProfile - ? getContext().getResources() - .getDimensionPixelSize(R.dimen.all_apps_header_pill_height) + ? resources.getDimensionPixelSize(R.dimen.all_apps_header_pill_height) : 0; mViewPagerTopPadding = mHasWorkProfile ? getContext().getResources() .getDimensionPixelSize(R.dimen.widget_picker_view_pager_top_padding) : 0; - mSearchAndRecommendationContainerBottomMargin = getContext().getResources() - .getDimensionPixelSize(mHasWorkProfile + mSearchAndRecommendationContainerBottomMargin = resources.getDimensionPixelSize( + mHasWorkProfile ? R.dimen.search_and_recommended_widgets_container_small_bottom_margin : R.dimen.search_and_recommended_widgets_container_bottom_margin); - mWidgetCellHorizontalPadding = 2 * getResources().getDimensionPixelOffset( + mWidgetSheetContentHorizontalPadding = 2 * resources.getDimensionPixelSize( R.dimen.widget_cell_horizontal_padding); } @@ -375,11 +377,10 @@ public class WidgetsFullSheet extends BaseWidgetSheet private boolean updateMaxSpansPerRow() { if (getMeasuredWidth() == 0) return false; - int previousMaxSpansPerRow = mMaxSpansPerRow; - mMaxSpansPerRow = getMeasuredWidth() - / (mActivityContext.getDeviceProfile().cellWidthPx + mWidgetCellHorizontalPadding); - - if (previousMaxSpansPerRow != mMaxSpansPerRow) { + int maxHorizontalSpans = computeMaxHorizontalSpans(mContent, + mWidgetSheetContentHorizontalPadding); + if (mMaxSpansPerRow != maxHorizontalSpans) { + mMaxSpansPerRow = maxHorizontalSpans; mAdapters.get(AdapterHolder.PRIMARY).mWidgetsListAdapter.setMaxHorizontalSpansPerRow( mMaxSpansPerRow); mAdapters.get(AdapterHolder.SEARCH).mWidgetsListAdapter.setMaxHorizontalSpansPerRow(