Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 16180801 authored by Sihua Ma's avatar Sihua Ma
Browse files

Update widget picker row logic to resolve cropping

Using pixels instead of cell spans for calculating the widget picker row
binding logic.

Before: https://screenshot.googleplex.com/3uXX2m7xWh3om5t
Now: https://screenshot.googleplex.com/C593mrQfqTahNrQ

Test: Manual
Fix: 269790954
Change-Id: Ib591a6f6d3cf8b72766e0714b9bc8ceb86b2ba4f
parent 24b1890c
Loading
Loading
Loading
Loading
+11 −20
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import android.widget.Toast;

import androidx.annotation.GuardedBy;
import androidx.annotation.Nullable;
import androidx.annotation.Px;
import androidx.core.view.ViewCompat;

import com.android.launcher3.DeviceProfile;
@@ -61,7 +62,7 @@ public abstract class BaseWidgetSheet extends AbstractSlideInView<Launcher>
        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;
    public static final int DEFAULT_MAX_HORIZONTAL_SPANS = 4;

    protected static final String KEY_WIDGETS_EDUCATION_TIP_SEEN =
            "launcher.widgets_education_tip_seen";
@@ -70,15 +71,18 @@ public abstract class BaseWidgetSheet extends AbstractSlideInView<Launcher>
    /* Touch handling related member variables. */
    private Toast mWidgetInstructionToast;

    private int mContentHorizontalMarginInPx;
    @Px protected int mContentHorizontalMargin;
    @Px protected int mWidgetCellHorizontalPadding;

    protected int mNavBarScrimHeight;
    private final Paint mNavBarScrimPaint;

    public BaseWidgetSheet(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContentHorizontalMarginInPx = getResources().getDimensionPixelSize(
        mContentHorizontalMargin = getResources().getDimensionPixelSize(
                R.dimen.widget_list_horizontal_margin);
        mWidgetCellHorizontalPadding = getResources().getDimensionPixelSize(
                R.dimen.widget_cell_horizontal_padding);
        mNavBarScrimPaint = new Paint();
        mNavBarScrimPaint.setColor(Themes.getAttrColor(context, R.attr.allAppsNavBarScrimColor));
    }
@@ -138,11 +142,11 @@ public abstract class BaseWidgetSheet extends AbstractSlideInView<Launcher>
    @Override
    public void setInsets(Rect insets) {
        mInsets.set(insets);
        int contentHorizontalMarginInPx = getResources().getDimensionPixelSize(
        @Px int contentHorizontalMargin = getResources().getDimensionPixelSize(
                R.dimen.widget_list_horizontal_margin);
        if (contentHorizontalMarginInPx != mContentHorizontalMarginInPx) {
            onContentHorizontalMarginChanged(contentHorizontalMarginInPx);
            mContentHorizontalMarginInPx = contentHorizontalMarginInPx;
        if (contentHorizontalMargin != mContentHorizontalMargin) {
            onContentHorizontalMarginChanged(contentHorizontalMargin);
            mContentHorizontalMargin = contentHorizontalMargin;
        }
    }

@@ -198,19 +202,6 @@ public abstract class BaseWidgetSheet extends AbstractSlideInView<Launcher>
                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
                - (2 * mContentHorizontalMarginInPx);
        Point cellSize = deviceProfile.getCellSize();
        if (cellSize.x > 0) {
            return availableWidth / cellSize.x;
        }
        return DEFAULT_MAX_HORIZONTAL_SPANS;
    }

    private boolean beginDraggingWidget(WidgetCell v) {
        if (TestProtocol.sDebugTracing) {
            Log.d(TestProtocol.NO_DROP_TARGET, "2");
+7 −6
Original line number Diff line number Diff line
@@ -36,6 +36,8 @@ import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import androidx.annotation.Px;

import com.android.launcher3.R;
import com.android.launcher3.anim.PendingAnimation;
import com.android.launcher3.model.WidgetItem;
@@ -69,8 +71,7 @@ public class WidgetsBottomSheet extends BaseWidgetSheet {
    private static final long EDUCATION_TIP_DELAY_MS = 300;

    private ItemInfo mOriginalItemInfo;
    private int mMaxHorizontalSpan = DEFAULT_MAX_HORIZONTAL_SPANS;
    private final int mWidgetCellHorizontalPadding;
    @Px private int mMaxHorizontalSpan;

    private final OnLayoutChangeListener mLayoutChangeListenerToShowTips =
            new OnLayoutChangeListener() {
@@ -111,8 +112,6 @@ public class WidgetsBottomSheet extends BaseWidgetSheet {
        if (!hasSeenEducationTip()) {
            addOnLayoutChangeListener(mLayoutChangeListenerToShowTips);
        }
        mWidgetCellHorizontalPadding = getResources().getDimensionPixelSize(
                R.dimen.widget_cell_horizontal_padding);
        setContentBackground(getContext().getDrawable(R.drawable.bg_rounded_corner_bottom_sheet));
    }

@@ -134,7 +133,7 @@ public class WidgetsBottomSheet extends BaseWidgetSheet {
    private boolean updateMaxSpansPerRow() {
        if (getMeasuredWidth() == 0) return false;

        int maxHorizontalSpan = computeMaxHorizontalSpans(mContent, mWidgetCellHorizontalPadding);
        @Px int maxHorizontalSpan = mContent.getMeasuredWidth() - (2 * mContentHorizontalMargin);
        if (mMaxHorizontalSpan != maxHorizontalSpan) {
            // Ensure the table layout is showing widgets in the right column after measure.
            mMaxHorizontalSpan = maxHorizontalSpan;
@@ -184,7 +183,9 @@ public class WidgetsBottomSheet extends BaseWidgetSheet {
        TableLayout widgetsTable = findViewById(R.id.widgets_table);
        widgetsTable.removeAllViews();

        WidgetsTableUtils.groupWidgetItemsIntoTableWithReordering(widgets, mMaxHorizontalSpan)
        WidgetsTableUtils.groupWidgetItemsUsingRowPxWithReordering(widgets, mActivityContext,
                mActivityContext.getDeviceProfile(), mMaxHorizontalSpan,
                mWidgetCellHorizontalPadding)
                .forEach(row -> {
                    TableRow tableRow = new TableRow(getContext());
                    tableRow.setGravity(Gravity.TOP);
+19 −17
Original line number Diff line number Diff line
@@ -15,6 +15,8 @@
 */
package com.android.launcher3.widget.model;

import androidx.annotation.Px;

import com.android.launcher3.model.WidgetItem;
import com.android.launcher3.model.data.PackageItemInfo;

@@ -26,7 +28,7 @@ import java.util.List;
 */
public final class WidgetsListContentEntry extends WidgetsListBaseEntry {

    private final int mMaxSpanSizeInCells;
    @Px private final int mMaxSpanSize;

    /**
     * Constructor for {@link WidgetsListContentEntry}.
@@ -37,7 +39,7 @@ public final class WidgetsListContentEntry extends WidgetsListBaseEntry {
     */
    public WidgetsListContentEntry(PackageItemInfo pkgItem, String titleSectionName,
            List<WidgetItem> items) {
        this(pkgItem, titleSectionName, items, /* maxSpanSizeInCells= */ 0);
        this(pkgItem, titleSectionName, items, /* maxSpanSize= */ 0);
    }

    /**
@@ -46,43 +48,43 @@ public final class WidgetsListContentEntry extends WidgetsListBaseEntry {
     * @param pkgItem package info associated with the entry
     * @param titleSectionName title section name associated with the entry.
     * @param items list of widgets for the package.
     * @param maxSpanSizeInCells the max horizontal span in cells that is allowed for grouping more
     * @param maxSpanSize the max horizontal span in pixels that is allowed for grouping more
     *                           than one widgets in a table row.
     */
    public WidgetsListContentEntry(PackageItemInfo pkgItem, String titleSectionName,
            List<WidgetItem> items, int maxSpanSizeInCells) {
            List<WidgetItem> items, @Px int maxSpanSize) {
        super(pkgItem, titleSectionName, items);
        mMaxSpanSizeInCells = maxSpanSizeInCells;
        mMaxSpanSize = maxSpanSize;
    }

    @Override
    public String toString() {
        return "Content:" + mPkgItem.packageName + ":" + mWidgets.size() + " maxSpanSizeInCells: "
                + mMaxSpanSizeInCells;
        return "Content:" + mPkgItem.packageName + ":" + mWidgets.size() + " maxSpanSize: "
                + mMaxSpanSize;
    }

    /**
     * Returns a copy of this {@link WidgetsListContentEntry} with updated
     * {@param maxSpanSizeInCells}.
     * Returns a copy of this {@link WidgetsListContentEntry} with updated {@code maxSpanSize}.
     *
     * @param maxSpanSizeInCells the maximum horizontal span in cells that is allowed for grouping
     * @param maxSpanSize the maximum horizontal span in pixels that is allowed for grouping
     *                           more than one widgets in a table row.
     */
    public WidgetsListContentEntry withMaxSpanSize(int maxSpanSizeInCells) {
        if (mMaxSpanSizeInCells == maxSpanSizeInCells) return this;
    public WidgetsListContentEntry withMaxSpanSize(@Px int maxSpanSize) {
        if (mMaxSpanSize == maxSpanSize) return this;
        return new WidgetsListContentEntry(
                mPkgItem,
                mTitleSectionName,
                mWidgets,
                /* maxSpanSizeInCells= */ maxSpanSizeInCells);
                /* maxSpanSize= */ maxSpanSize);
    }

    /**
     * Returns the max horizontal span size in cells that is allowed for grouping more than one
     * Returns the max horizontal span size in pixels that is allowed for grouping more than one
     * widget in a table row.
     */
    public int getMaxSpanSizeInCells() {
        return mMaxSpanSizeInCells;
    @Px
    public int getMaxSpanSize() {
        return mMaxSpanSize;
    }

    @Override
@@ -91,6 +93,6 @@ public final class WidgetsListContentEntry extends WidgetsListBaseEntry {
        WidgetsListContentEntry otherEntry = (WidgetsListContentEntry) obj;
        return mWidgets.equals(otherEntry.mWidgets) && mPkgItem.equals(otherEntry.mPkgItem)
                && mTitleSectionName.equals(otherEntry.mTitleSectionName)
                && mMaxSpanSizeInCells == otherEntry.mMaxSpanSizeInCells;
                && mMaxSpanSize == otherEntry.mMaxSpanSize;
    }
}
+20 −20
Original line number Diff line number Diff line
@@ -53,6 +53,7 @@ import android.widget.TextView;
import androidx.annotation.FloatRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.Px;
import androidx.annotation.VisibleForTesting;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.RecyclerView;
@@ -181,14 +182,13 @@ public class WidgetsFullSheet extends BaseWidgetSheet
        }
    };

    private final int mTabsHeight;
    private final int mWidgetSheetContentHorizontalPadding;
    @Px private final int mTabsHeight;

    @Nullable private WidgetsRecyclerView mCurrentWidgetsRecyclerView;
    @Nullable private PersonalWorkPagedView mViewPager;
    private boolean mIsInSearchMode;
    private boolean mIsNoWidgetsViewNeeded;
    private int mMaxSpansPerRow = DEFAULT_MAX_HORIZONTAL_SPANS;
    @Px private int mMaxSpanPerRow;
    private TextView mNoWidgetsView;

    private StickyHeaderLayout mSearchScrollView;
@@ -221,8 +221,6 @@ public class WidgetsFullSheet extends BaseWidgetSheet
        mTabsHeight = mHasWorkProfile
                ? resources.getDimensionPixelSize(R.dimen.all_apps_header_pill_height)
                : 0;
        mWidgetSheetContentHorizontalPadding = 2 * resources.getDimensionPixelSize(
                R.dimen.widget_cell_horizontal_padding);

        mUserManagerState.init(UserCache.INSTANCE.get(context),
                context.getSystemService(UserManager.class));
@@ -334,7 +332,7 @@ public class WidgetsFullSheet extends BaseWidgetSheet
                : mSearchScrollView.findViewById(R.id.title);
        mRightPane = mIsTwoPane ? mContent.findViewById(R.id.right_pane) : null;
        mWidgetsListTableViewHolderBinder =
                new WidgetsListTableViewHolderBinder(layoutInflater, this, this);
                new WidgetsListTableViewHolderBinder(mActivityContext, layoutInflater, this, this);
        onRecommendedWidgetsBound();
        onWidgetsBound();

@@ -533,22 +531,20 @@ public class WidgetsFullSheet extends BaseWidgetSheet
        View content = mHasWorkProfile
                ? mViewPager
                : mAdapters.get(AdapterHolder.PRIMARY).mWidgetsRecyclerView;

        if (mIsTwoPane && mRightPane != null) {
            content = mRightPane;
        }

        int maxHorizontalSpans = computeMaxHorizontalSpans(content,
                mWidgetSheetContentHorizontalPadding);
        if (mMaxSpansPerRow != maxHorizontalSpans) {
            mMaxSpansPerRow = maxHorizontalSpans;
            mAdapters.get(AdapterHolder.PRIMARY).mWidgetsListAdapter.setMaxHorizontalSpansPerRow(
                    mMaxSpansPerRow);
            mAdapters.get(AdapterHolder.SEARCH).mWidgetsListAdapter.setMaxHorizontalSpansPerRow(
                    mMaxSpansPerRow);
        @Px int maxHorizontalSpan = content.getMeasuredWidth() - (2 * mContentHorizontalMargin);
        if (mMaxSpanPerRow != maxHorizontalSpan) {
            mMaxSpanPerRow = maxHorizontalSpan;
            mAdapters.get(AdapterHolder.PRIMARY).mWidgetsListAdapter.setMaxHorizontalSpansPxPerRow(
                    maxHorizontalSpan);
            mAdapters.get(AdapterHolder.SEARCH).mWidgetsListAdapter.setMaxHorizontalSpansPxPerRow(
                    maxHorizontalSpan);
            if (mHasWorkProfile) {
                mAdapters.get(AdapterHolder.WORK).mWidgetsListAdapter.setMaxHorizontalSpansPerRow(
                        mMaxSpansPerRow);
                mAdapters.get(AdapterHolder.WORK).mWidgetsListAdapter.setMaxHorizontalSpansPxPerRow(
                        maxHorizontalSpan);
            }
            onRecommendedWidgetsBound();
            return true;
@@ -697,8 +693,12 @@ public class WidgetsFullSheet extends BaseWidgetSheet
                    - noWidgetsViewHeight) * RECOMMENDATION_TABLE_HEIGHT_RATIO;

            List<ArrayList<WidgetItem>> recommendedWidgetsInTable =
                    WidgetsTableUtils.groupWidgetItemsIntoTableWithoutReordering(
                            recommendedWidgets, mMaxSpansPerRow);
                    WidgetsTableUtils.groupWidgetItemsUsingRowPxWithoutReordering(
                            recommendedWidgets,
                            mActivityContext,
                            mActivityContext.getDeviceProfile(),
                            mMaxSpanPerRow,
                            mWidgetCellHorizontalPadding);
            mRecommendedWidgetsTable.setRecommendedWidgets(
                    recommendedWidgetsInTable, maxTableHeight);
        } else {
@@ -1046,7 +1046,7 @@ public class WidgetsFullSheet extends BaseWidgetSheet
            if (mAdapterType == PRIMARY || mAdapterType == WORK) {
                mWidgetsRecyclerView.addOnAttachStateChangeListener(mBindScrollbarInSearchMode);
            }
            mWidgetsListAdapter.setMaxHorizontalSpansPerRow(mMaxSpansPerRow);
            mWidgetsListAdapter.setMaxHorizontalSpansPxPerRow(mMaxSpanPerRow);
        }
    }

+13 −6
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCH
import static com.android.launcher3.recyclerview.ViewHolderBinder.POSITION_DEFAULT;
import static com.android.launcher3.recyclerview.ViewHolderBinder.POSITION_FIRST;
import static com.android.launcher3.recyclerview.ViewHolderBinder.POSITION_LAST;
import static com.android.launcher3.widget.BaseWidgetSheet.DEFAULT_MAX_HORIZONTAL_SPANS;

import android.content.Context;
import android.os.Process;
@@ -32,6 +33,7 @@ import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.Px;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.DiffUtil.DiffResult;
import androidx.recyclerview.widget.LinearLayoutManager;
@@ -49,6 +51,7 @@ import com.android.launcher3.widget.model.WidgetListSpaceEntry;
import com.android.launcher3.widget.model.WidgetsListBaseEntry;
import com.android.launcher3.widget.model.WidgetsListContentEntry;
import com.android.launcher3.widget.model.WidgetsListHeaderEntry;
import com.android.launcher3.widget.util.WidgetSizes;

import java.util.ArrayList;
import java.util.Arrays;
@@ -99,7 +102,7 @@ public class WidgetsListAdapter extends Adapter<ViewHolder> implements OnHeaderC
    @Nullable private Predicate<WidgetsListBaseEntry> mFilter = null;
    @Nullable private RecyclerView mRecyclerView;
    @Nullable private PackageUserKey mPendingClickHeader;
    private int mMaxSpanSize = 4;
    @Px private int mMaxHorizontalSpan;

    public WidgetsListAdapter(Context context, LayoutInflater layoutInflater,
            IntSupplier emptySpaceHeightProvider, OnClickListener iconClickListener,
@@ -107,11 +110,14 @@ public class WidgetsListAdapter extends Adapter<ViewHolder> implements OnHeaderC
            WidgetsFullSheet.HeaderChangeListener headerChangeListener) {
        mHeaderChangeListener = headerChangeListener;
        mContext = context;
        mMaxHorizontalSpan = WidgetSizes.getWidgetSizePx(
                ActivityContext.lookupContext(context).getDeviceProfile(),
                        DEFAULT_MAX_HORIZONTAL_SPANS, 1).getWidth();

        mViewHolderBinders.put(
                VIEW_TYPE_WIDGETS_LIST,
                new WidgetsListTableViewHolderBinder(
                        layoutInflater, iconClickListener, iconLongClickListener));
                        mContext, layoutInflater, iconClickListener, iconLongClickListener));
        mViewHolderBinders.put(
                VIEW_TYPE_WIDGETS_HEADER,
                new WidgetsListHeaderViewHolderBinder(
@@ -199,7 +205,8 @@ public class WidgetsListAdapter extends Adapter<ViewHolder> implements OnHeaderC
                    } else if (entry instanceof WidgetsListContentEntry) {
                        // Adjust the original content entries to accommodate for the current
                        // maxSpanSize.
                        return ((WidgetsListContentEntry) entry).withMaxSpanSize(mMaxSpanSize);
                        return ((WidgetsListContentEntry) entry).withMaxSpanSize(
                                mMaxHorizontalSpan);
                    }
                    return entry;
                })
@@ -407,11 +414,11 @@ public class WidgetsListAdapter extends Adapter<ViewHolder> implements OnHeaderC
    }

    /**
     * Sets the max horizontal span in cells that is allowed for grouping more than one widget in a
     * Sets the max horizontal span in pixels that is allowed for grouping more than one widget in a
     * table row.
     */
    public void setMaxHorizontalSpansPerRow(int maxHorizontalSpans) {
        mMaxSpanSize = maxHorizontalSpans;
    public void setMaxHorizontalSpansPxPerRow(@Px int maxHorizontalSpan) {
        mMaxHorizontalSpan = maxHorizontalSpan;
        updateVisibleEntries();
    }

Loading