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

Commit c167b0bd authored by Pierre Barbier de Reuille's avatar Pierre Barbier de Reuille Committed by Automerger Merge Worker
Browse files

Merge changes Id2a8d12e,I06353ba4 into sc-dev am: 6788b739

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Launcher3/+/15171715

Change-Id: I40b331817125cd8f4f56302d004bb77e79ca635b
parents 3022d5f9 6788b739
Loading
Loading
Loading
Loading
+2 −1
Original line number Original line Diff line number Diff line
@@ -417,7 +417,8 @@ public class DatabaseWidgetPreviewLoader implements WidgetPreviewLoader {
            previewHeight = drawable.getIntrinsicHeight();
            previewHeight = drawable.getIntrinsicHeight();
        } else {
        } else {
            DeviceProfile dp = launcher.getDeviceProfile();
            DeviceProfile dp = launcher.getDeviceProfile();
            Size widgetSize = WidgetSizes.getWidgetSizePx(dp, spanX, spanY);
            Size widgetSize = WidgetSizes.getWidgetPaddedSizePx(mContext, info.provider, dp, spanX,
                    spanY);
            previewWidth = widgetSize.getWidth();
            previewWidth = widgetSize.getWidth();
            previewHeight = widgetSize.getHeight();
            previewHeight = widgetSize.getHeight();
        }
        }
+3 −1
Original line number Original line Diff line number Diff line
@@ -363,7 +363,9 @@ public class WidgetCell extends LinearLayout implements OnLayoutChangeListener {
    /** Sets the widget preview image size, in number of cells, and preview scale. */
    /** Sets the widget preview image size, in number of cells, and preview scale. */
    public Size setPreviewSize(int spanX, int spanY, float previewScale) {
    public Size setPreviewSize(int spanX, int spanY, float previewScale) {
        DeviceProfile deviceProfile = mActivity.getDeviceProfile();
        DeviceProfile deviceProfile = mActivity.getDeviceProfile();
        Size widgetSize = WidgetSizes.getWidgetSizePx(deviceProfile, spanX, spanY);
        Size widgetSize =
                mItem != null ? WidgetSizes.getWidgetItemSizePx(getContext(), deviceProfile, mItem)
                : WidgetSizes.getWidgetSizePx(deviceProfile, spanX, spanY);
        mPreviewWidth = widgetSize.getWidth();
        mPreviewWidth = widgetSize.getWidth();
        mPreviewHeight = widgetSize.getHeight();
        mPreviewHeight = widgetSize.getHeight();
        mPreviewScale = previewScale;
        mPreviewScale = previewScale;
+2 −5
Original line number Original line Diff line number Diff line
@@ -296,11 +296,8 @@ public class WidgetsListAdapter extends Adapter<ViewHolder> implements OnHeaderC
            for (int i = 0; i < entry.mWidgets.size(); i++) {
            for (int i = 0; i < entry.mWidgets.size(); i++) {
                WidgetItem widgetItem = entry.mWidgets.get(i);
                WidgetItem widgetItem = entry.mWidgets.get(i);
                DeviceProfile deviceProfile = activity.getDeviceProfile();
                DeviceProfile deviceProfile = activity.getDeviceProfile();
                Size widgetSize =
                Size widgetSize = WidgetSizes.getWidgetItemSizePx(mContext, deviceProfile,
                        WidgetSizes.getWidgetSizePx(
                        widgetItem);
                                deviceProfile,
                                widgetItem.spanX,
                                widgetItem.spanY);
                if (widgetItem.isShortcut()) {
                if (widgetItem.isShortcut()) {
                    widgetSize =
                    widgetSize =
                            new Size(
                            new Size(
+2 −2
Original line number Original line Diff line number Diff line
@@ -155,8 +155,8 @@ public final class WidgetsRecommendationTableLayout extends TableLayout {
            float rowHeight = 0;
            float rowHeight = 0;
            for (int j = 0; j < widgetItems.size(); j++) {
            for (int j = 0; j < widgetItems.size(); j++) {
                WidgetItem widgetItem = widgetItems.get(j);
                WidgetItem widgetItem = widgetItems.get(j);
                Size widgetSize = WidgetSizes.getWidgetSizePx(
                Size widgetSize = WidgetSizes.getWidgetItemSizePx(getContext(), deviceProfile,
                        deviceProfile, widgetItem.spanX, widgetItem.spanY);
                        widgetItem);
                float previewHeight = widgetSize.getHeight() * previewScale;
                float previewHeight = widgetSize.getHeight() * previewScale;
                rowHeight = Math.max(rowHeight,
                rowHeight = Math.max(rowHeight,
                        previewHeight + mWidgetCellTextViewsHeight + mWidgetCellVerticalPadding);
                        previewHeight + mWidgetCellTextViewsHeight + mWidgetCellVerticalPadding);
+52 −23
Original line number Original line Diff line number Diff line
@@ -17,9 +17,7 @@ package com.android.launcher3.widget.util;


import static android.appwidget.AppWidgetHostView.getDefaultPaddingForWidget;
import static android.appwidget.AppWidgetHostView.getDefaultPaddingForWidget;


import static com.android.launcher3.Utilities.ATLEAST_S;


import android.annotation.SuppressLint;
import android.appwidget.AppWidgetHostView;
import android.appwidget.AppWidgetHostView;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.ComponentName;
@@ -34,24 +32,34 @@ import androidx.annotation.Nullable;


import com.android.launcher3.DeviceProfile;
import com.android.launcher3.DeviceProfile;
import com.android.launcher3.LauncherAppState;
import com.android.launcher3.LauncherAppState;
import com.android.launcher3.model.WidgetItem;


import java.util.ArrayList;
import java.util.ArrayList;
import java.util.List;
import java.util.List;
import java.util.stream.Collectors;


/** A utility class for widget sizes related calculations. */
/** A utility class for widget sizes related calculations. */
public final class WidgetSizes {
public final class WidgetSizes {


    /**
    /**
     * Returns the list of all possible sizes, in dp, for a widget of given spans on this device.
     * Returns the list of all possible sizes, in dp, for a widget of given spans on this device.
     *
     * <p>The returned sizes already take into account the system padding, and whether it is applied
     * or not in that specific configuration.
     */
     */
    public static ArrayList<SizeF> getWidgetSizes(Context context, int spanX, int spanY) {
    public static ArrayList<SizeF> getWidgetPaddedSizes(Context context, ComponentName provider,
            int spanX, int spanY) {
        Rect padding = getDefaultPaddingForWidget(context, provider, /* padding= */ null);

        ArrayList<SizeF> sizes = new ArrayList<>(2);
        ArrayList<SizeF> sizes = new ArrayList<>(2);
        final float density = context.getResources().getDisplayMetrics().density;
        final float density = context.getResources().getDisplayMetrics().density;
        final Point cellSize = new Point();
        final Point cellSize = new Point();


        for (DeviceProfile profile : LauncherAppState.getIDP(context).supportedProfiles) {
        for (DeviceProfile profile : LauncherAppState.getIDP(context).supportedProfiles) {
            Size widgetSizePx = getWidgetSizePx(profile, spanX, spanY, cellSize);
            Size widgetSizePx = getWidgetSizePx(profile, spanX, spanY, cellSize);
            if (!profile.shouldInsetWidgets()) {
                widgetSizePx = new Size(widgetSizePx.getWidth() - padding.left - padding.right,
                        widgetSizePx.getHeight() - padding.top - padding.bottom);
            }
            sizes.add(new SizeF(widgetSizePx.getWidth() / density,
            sizes.add(new SizeF(widgetSizePx.getWidth() / density,
                    widgetSizePx.getHeight() / density));
                    widgetSizePx.getHeight() / density));
        }
        }
@@ -63,6 +71,32 @@ public final class WidgetSizes {
        return getWidgetSizePx(profile, spanX, spanY, /* recycledCellSize= */ null);
        return getWidgetSizePx(profile, spanX, spanY, /* recycledCellSize= */ null);
    }
    }


    /**
     * Returns the size, in pixels and removing padding, a widget of given spans & {@code profile}.
     */
    public static Size getWidgetPaddedSizePx(Context context, ComponentName component,
            DeviceProfile profile, int spanX, int spanY) {
        Size size = getWidgetSizePx(profile, spanX, spanY);
        if (profile.shouldInsetWidgets()) {
            return size;
        }
        Rect padding = getDefaultPaddingForWidget(context, component, /* padding= */ null);
        return new Size(size.getWidth() - padding.left - padding.right,
                size.getHeight() - padding.top - padding.bottom);
    }

    /**
     * Returns the size of a WidgetItem.
     */
    public static Size getWidgetItemSizePx(Context context, DeviceProfile profile,
            WidgetItem widgetItem) {
        if (widgetItem.isShortcut()) {
            return getWidgetSizePx(profile, widgetItem.spanX, widgetItem.spanY);
        }
        return getWidgetPaddedSizePx(context, widgetItem.componentName, profile, widgetItem.spanX,
                widgetItem.spanY);
    }

    private static Size getWidgetSizePx(DeviceProfile profile, int spanX, int spanY,
    private static Size getWidgetSizePx(DeviceProfile profile, int spanX, int spanY,
            @Nullable Point recycledCellSize) {
            @Nullable Point recycledCellSize) {
        final int hBorderSpacing = (spanX - 1) * profile.cellLayoutBorderSpacingPx;
        final int hBorderSpacing = (spanX - 1) * profile.cellLayoutBorderSpacingPx;
@@ -81,17 +115,22 @@ public final class WidgetSizes {
     * <p>On Android S+, it also updates the given {@code widgetView} with a list of sizes derived
     * <p>On Android S+, it also updates the given {@code widgetView} with a list of sizes derived
     * from {@code spanX}, {@code spanY} in all supported device profiles.
     * from {@code spanX}, {@code spanY} in all supported device profiles.
     */
     */
    @SuppressLint("NewApi") // Already added API check.
    public static void updateWidgetSizeRanges(AppWidgetHostView widgetView, Context context,
    public static void updateWidgetSizeRanges(AppWidgetHostView widgetView, Context context,
            int spanX, int spanY) {
            int spanX, int spanY) {
        List<SizeF> sizes = getWidgetSizes(context, spanX, spanY);
        AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
        if (ATLEAST_S) {
        int widgetId = widgetView.getAppWidgetId();
            widgetView.updateAppWidgetSize(new Bundle(), sizes);
        if (widgetId <= 0) {
        } else {
            return;
            Rect bounds = getMinMaxSizes(sizes);
        }
            widgetView.updateAppWidgetSize(new Bundle(), bounds.left, bounds.top, bounds.right,
        Bundle sizeOptions = getWidgetSizeOptions(context, widgetView.getAppWidgetInfo().provider,
                    bounds.bottom);
                spanX, spanY);
        if (sizeOptions.<SizeF>getParcelableArrayList(
                AppWidgetManager.OPTION_APPWIDGET_SIZES).equals(
                widgetManager.getAppWidgetOptions(widgetId).<SizeF>getParcelableArrayList(
                        AppWidgetManager.OPTION_APPWIDGET_SIZES))) {
            return;
        }
        }
        widgetManager.updateAppWidgetOptions(widgetId, sizeOptions);
    }
    }


    /**
    /**
@@ -99,17 +138,7 @@ public final class WidgetSizes {
     */
     */
    public static Bundle getWidgetSizeOptions(Context context, ComponentName provider, int spanX,
    public static Bundle getWidgetSizeOptions(Context context, ComponentName provider, int spanX,
            int spanY) {
            int spanY) {
        ArrayList<SizeF> sizes = getWidgetSizes(context, spanX, spanY);
        ArrayList<SizeF> paddedSizes = getWidgetPaddedSizes(context, provider, spanX, spanY);
        Rect padding = getDefaultPaddingForWidget(context, provider, null);
        float density = context.getResources().getDisplayMetrics().density;
        float xPaddingDips = (padding.left + padding.right) / density;
        float yPaddingDips = (padding.top + padding.bottom) / density;

        ArrayList<SizeF> paddedSizes = sizes.stream()
                .map(size -> new SizeF(
                        Math.max(0.f, size.getWidth() - xPaddingDips),
                        Math.max(0.f, size.getHeight() - yPaddingDips)))
                .collect(Collectors.toCollection(ArrayList::new));


        Rect rect = getMinMaxSizes(paddedSizes);
        Rect rect = getMinMaxSizes(paddedSizes);
        Bundle options = new Bundle();
        Bundle options = new Bundle();