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

Commit 927dd27e authored by Shamali P's avatar Shamali P
Browse files

Update the widget previews to use the container sizes

Additionally, adjusts the margins on sides, to make space for displaying
multiple previews side by side.

As a result of this change, previews aren't cropped anymore (indirectly
also fixing b/317366201).

There is one more follow up pending - to limit the height of
recommendations in two pane sheet to only visible area to keep the
suggestions quick to look and concise. It will also help with better
scroll experience when size is same across pages.

* http://screencast/cast/NTQ0Njc4ODgyNjc5MTkzNnxjYzZiYTU2Ny02Yw
* http://screencast/cast/NTk4ODMyMDkxOTE1ODc4NHw3NzdlNTgxZS1kMw
* http://screencast/cast/NTYxOTc5NjIxNjM4MTQ0MHxiMjQ2Njc3OC0zOQ
* http://screencast/cast/NDk4MjIxNjI0MDEzNjE5MnxjY2IwYmIxNi00Mw

Bug: 319152349
Flag: N/A
Test: Image test and table utils test
Change-Id: I07465bd4d84597b560a2b998ff1ccbf9867c0192
parent c93f687f
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -17,7 +17,8 @@
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:paddingHorizontal="@dimen/widget_cell_horizontal_padding"
    android:layout_marginStart="@dimen/widget_cell_horizontal_padding"
    android:layout_marginEnd="@dimen/widget_cell_horizontal_padding"
    android:paddingVertical="@dimen/widget_cell_vertical_padding"
    android:layout_weight="1"
    android:orientation="vertical"
+0 −1
Original line number Diff line number Diff line
@@ -17,5 +17,4 @@
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingHorizontal="@dimen/widget_recommendations_table_horizontal_padding"
    android:paddingVertical="@dimen/widget_recommendations_table_vertical_padding" />
+1 −0
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@

<!-- Widget picker-->
    <dimen name="widget_list_horizontal_margin">30dp</dimen>
    <dimen name="widget_cell_horizontal_padding">16dp</dimen>

    <!--  Folder spaces  -->
    <dimen name="folder_footer_horiz_padding">24dp</dimen>
+3 −3
Original line number Diff line number Diff line
@@ -176,7 +176,7 @@

    <!-- Widget tray -->
    <dimen name="widget_cell_vertical_padding">8dp</dimen>
    <dimen name="widget_cell_horizontal_padding">16dp</dimen>
    <dimen name="widget_cell_horizontal_padding">8dp</dimen>
    <dimen name="widget_cell_font_size">14sp</dimen>
    <dimen name="widget_cell_app_icon_size">24dp</dimen>
    <dimen name="widget_cell_app_icon_padding">8dp</dimen>
@@ -187,7 +187,6 @@
    <dimen name="widget_picker_landscape_tablet_left_right_margin">117dp</dimen>
    <dimen name="widget_picker_two_panels_left_right_margin">0dp</dimen>
    <dimen name="widget_recommendations_table_vertical_padding">8dp</dimen>
    <dimen name="widget_recommendations_table_horizontal_padding">16dp</dimen>
    <!-- Bottom margin for the search and recommended widgets container without work profile -->
    <dimen name="search_and_recommended_widgets_container_bottom_margin">16dp</dimen>
    <!-- Bottom margin for the search and recommended widgets container with work profile -->
@@ -198,7 +197,8 @@

    <dimen name="widget_list_header_view_vertical_padding">20dp</dimen>
    <dimen name="widget_list_entry_spacing">2dp</dimen>
    <dimen name="widget_list_horizontal_margin">16dp</dimen>
    <!-- Less margin on sides to let widgets table width be close to the workspace width. -->
    <dimen name="widget_list_horizontal_margin">11dp</dimen>
    <!-- Margin applied to the recycler view with search bar & the list of widget apps below it. -->
    <dimen name="widget_list_left_pane_horizontal_margin">0dp</dimen>
    <dimen name="widget_list_horizontal_margin_two_pane">24dp</dimen>
+41 −19
Original line number Diff line number Diff line
@@ -57,6 +57,8 @@ import com.android.launcher3.model.data.ItemInfoWithIcon;
import com.android.launcher3.model.data.PackageItemInfo;
import com.android.launcher3.util.CancellableTask;
import com.android.launcher3.views.ActivityContext;
import com.android.launcher3.widget.picker.util.WidgetPreviewContainerSize;
import com.android.launcher3.widget.util.WidgetSizes;

import java.util.function.Consumer;

@@ -80,7 +82,7 @@ public class WidgetCell extends LinearLayout {
     * The requested scale of the preview container. It can be lower than this as well.
     */
    private float mPreviewContainerScale = 1f;

    private Size mPreviewContainerSize = new Size(0, 0);
    private FrameLayout mWidgetImageContainer;
    private WidgetImageView mWidgetImage;
    private ImageView mWidgetBadge;
@@ -176,6 +178,8 @@ public class WidgetCell extends LinearLayout {
        mWidgetDims.setText(null);
        mWidgetDescription.setText(null);
        mWidgetDescription.setVisibility(GONE);
        showDescription(true);
        showDimensions(true);

        if (mActiveRequest != null) {
            mActiveRequest.cancel();
@@ -186,6 +190,7 @@ public class WidgetCell extends LinearLayout {
            mWidgetImageContainer.removeView(mAppWidgetHostViewPreview);
        }
        mAppWidgetHostViewPreview = null;
        mPreviewContainerSize = new Size(0, 0);
        mAppWidgetHostViewScale = 1f;
        mPreviewContainerScale = 1f;
        mItem = null;
@@ -201,30 +206,21 @@ public class WidgetCell extends LinearLayout {
     * Applies the item to this view
     */
    public void applyFromCellItem(WidgetItem item) {
        applyFromCellItem(item, 1f);
    }

    /**
     * Applies the item to this view
     */
    public void applyFromCellItem(WidgetItem item, float previewScale) {
        applyFromCellItem(item, previewScale, this::applyPreview, null);
        applyFromCellItem(item, this::applyPreview, /*cachedPreview=*/null);
    }

    /**
     * Applies the item to this view
     * @param item item to apply
     * @param previewScale factor to scale the preview
     * @param callback callback when preview is loaded in case the preview is being loaded or cached
     * @param cachedPreview previously cached preview bitmap is present
     */
    public void applyFromCellItem(WidgetItem item, float previewScale,
            @NonNull Consumer<Bitmap> callback, @Nullable Bitmap cachedPreview) {
        mPreviewContainerScale = previewScale;

    public void applyFromCellItem(WidgetItem item, @NonNull Consumer<Bitmap> callback,
            @Nullable Bitmap cachedPreview) {
        Context context = getContext();
        mItem = item;
        mWidgetSize = getWidgetItemSizePx(getContext(), mActivity.getDeviceProfile(), mItem);
        initPreviewContainerSizeAndScale();

        mWidgetName.setText(mItem.label);
        mWidgetName.setContentDescription(
@@ -278,6 +274,17 @@ public class WidgetCell extends LinearLayout {
        }
    }

    private void initPreviewContainerSizeAndScale() {
        WidgetPreviewContainerSize previewSize = WidgetPreviewContainerSize.Companion.forItem(mItem,
                mActivity.getDeviceProfile());
        mPreviewContainerSize = WidgetSizes.getWidgetSizePx(mActivity.getDeviceProfile(),
                previewSize.spanX, previewSize.spanY);

        float scaleX = (float) mPreviewContainerSize.getWidth() / mWidgetSize.getWidth();
        float scaleY = (float) mPreviewContainerSize.getHeight() / mWidgetSize.getHeight();
        mPreviewContainerScale = Math.min(scaleX, scaleY);
    }

    private void setAppWidgetHostViewPreview(
            NavigableAppWidgetHostView appWidgetHostViewPreview,
            LauncherAppWidgetProviderInfo providerInfo,
@@ -383,6 +390,16 @@ public class WidgetCell extends LinearLayout {
        mWidgetDescription.setVisibility(show ? VISIBLE : GONE);
    }

    /**
     * Shows or hides the dimensions displayed below each widget.
     *
     * @param show a flag that shows the dimensions of the widget if {@code true}, hides it if
     *             {@code false}.
     */
    public void showDimensions(boolean show) {
        mWidgetDims.setVisibility(show ? VISIBLE : GONE);
    }

    /**
     * Set whether the app icon, for the app that provides the widget, should be shown next to the
     * title text of the widget.
@@ -448,17 +465,22 @@ public class WidgetCell extends LinearLayout {
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        ViewGroup.LayoutParams containerLp = mWidgetImageContainer.getLayoutParams();
        int maxWidth = MeasureSpec.getSize(widthMeasureSpec);

        // mPreviewContainerScale ensures the needed scaling with respect to original widget size.
        mAppWidgetHostViewScale = mPreviewContainerScale;
        int maxWidth = MeasureSpec.getSize(widthMeasureSpec);
        containerLp.width = Math.round(mWidgetSize.getWidth() * mAppWidgetHostViewScale);
        containerLp.width = mPreviewContainerSize.getWidth();
        containerLp.height = mPreviewContainerSize.getHeight();

        // If we don't have enough available width, scale the preview container to fit.
        if (containerLp.width > maxWidth) {
            containerLp.width = maxWidth;
            mAppWidgetHostViewScale = (float) containerLp.width / mWidgetSize.getWidth();
            mAppWidgetHostViewScale = (float) containerLp.width / mPreviewContainerSize.getWidth();
            containerLp.height = Math.round(
                    mPreviewContainerSize.getHeight() * mAppWidgetHostViewScale);
        }
        containerLp.height = Math.round(mWidgetSize.getHeight() * mAppWidgetHostViewScale);
        // No need to call mWidgetImageContainer.setLayoutParams as we are in measure pass

        // No need to call mWidgetImageContainer.setLayoutParams as we are in measure pass
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

Loading