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

Commit 3f7550c1 authored by Sunny Goyal's avatar Sunny Goyal Committed by Android (Google) Code Review
Browse files

Merge "Calculating widget minSpans and spans statically/independent of...

Merge "Calculating widget minSpans and spans statically/independent of orientation  > Filtering the widget list and excluding widgets which dont fit the grid  > setting minSpans for the widget item when binding." into ub-launcher3-burnaby
parents c451da70 233ee964
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -75,8 +75,8 @@ public class AppWidgetResizeFrame extends FrameLayout {
        mResizeMode = info.resizeMode;
        mDragLayer = dragLayer;

        mMinHSpan = info.getMinSpanX(mLauncher);
        mMinVSpan = info.getMinSpanY(mLauncher);
        mMinHSpan = info.minSpanX;
        mMinVSpan = info.minSpanY;

        setBackgroundResource(R.drawable.widget_resize_shadow);
        setForeground(getResources().getDrawable(R.drawable.widget_resize_frame));
+0 −60
Original line number Diff line number Diff line
@@ -54,7 +54,6 @@ import com.android.launcher3.accessibility.DragAndDropAccessibilityDelegate;
import com.android.launcher3.accessibility.FolderAccessibilityHelper;
import com.android.launcher3.accessibility.WorkspaceAccessibilityHelper;
import com.android.launcher3.util.Thunk;
import com.android.launcher3.widget.PendingAddWidgetInfo;

import java.util.ArrayList;
import java.util.Arrays;
@@ -2686,65 +2685,6 @@ public class CellLayout extends ViewGroup implements BubbleTextShadowHandler {
        resultRect.set(x, y, x + width, y + height);
    }

    /**
     * Computes the required horizontal and vertical cell spans to always
     * fit the given rectangle.
     *
     * @param width Width in pixels
     * @param height Height in pixels
     * @param result An array of length 2 in which to store the result (may be null).
     */
    public static int[] rectToCell(Launcher launcher, int width, int height, int[] result) {
        return rectToCell(launcher.getDeviceProfile(), launcher, width, height, result);
    }

    public static int[] rectToCell(DeviceProfile grid, Context context, int width, int height,
            int[] result) {
        Rect padding = grid.getWorkspacePadding(Utilities.isRtl(context.getResources()));

        // Always assume we're working with the smallest span to make sure we
        // reserve enough space in both orientations.
        int parentWidth = DeviceProfile.calculateCellWidth(grid.widthPx
                - padding.left - padding.right, (int) grid.inv.numColumns);
        int parentHeight = DeviceProfile.calculateCellHeight(grid.heightPx
                - padding.top - padding.bottom, (int) grid.inv.numRows);
        int smallerSize = Math.min(parentWidth, parentHeight);

        // Always round up to next largest cell
        int spanX = (int) Math.ceil(width / (float) smallerSize);
        int spanY = (int) Math.ceil(height / (float) smallerSize);

        if (result == null) {
            return new int[] { spanX, spanY };
        }
        result[0] = spanX;
        result[1] = spanY;
        return result;
    }

    /**
     * Calculate the grid spans needed to fit given item
     */
    public void calculateSpans(ItemInfo info) {
        final int minWidth;
        final int minHeight;

        if (info instanceof LauncherAppWidgetInfo) {
            minWidth = ((LauncherAppWidgetInfo) info).minWidth;
            minHeight = ((LauncherAppWidgetInfo) info).minHeight;
        } else if (info instanceof PendingAddWidgetInfo) {
            minWidth = ((PendingAddWidgetInfo) info).minWidth;
            minHeight = ((PendingAddWidgetInfo) info).minHeight;
        } else {
            // It's not a widget, so it must be 1x1
            info.spanX = info.spanY = 1;
            return;
        }
        int[] spans = rectToCell(mLauncher, minWidth, minHeight, null);
        info.spanX = spans[0];
        info.spanY = spans[1];
    }

    private void clearOccupiedCells() {
        for (int x = 0; x < mCountX; x++) {
            for (int y = 0; y < mCountY; y++) {
+3 −18
Original line number Diff line number Diff line
@@ -1553,23 +1553,6 @@ public class Launcher extends Activity
        }
    }

    private int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight) {
        Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
        // We want to account for the extra amount of padding that we are adding to the widget
        // to ensure that it gets the full amount of space that it has requested
        int requiredWidth = minWidth + padding.left + padding.right;
        int requiredHeight = minHeight + padding.top + padding.bottom;
        return CellLayout.rectToCell(this, requiredWidth, requiredHeight, null);
    }

    public int[] getSpanForWidget(AppWidgetProviderInfo info) {
        return getSpanForWidget(info.provider, info.minWidth, info.minHeight);
    }

    public int[] getMinSpanForWidget(AppWidgetProviderInfo info) {
        return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight);
    }

    /**
     * Add a widget to the workspace.
     *
@@ -2218,7 +2201,7 @@ public class Launcher extends Activity
        mPendingAddInfo.screenId = -1;
        mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
        mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
        mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
        mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
        mPendingAddInfo.dropPos = null;
    }

@@ -3957,6 +3940,8 @@ public class Launcher extends Activity
            }

            item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
            item.minSpanX = appWidgetInfo.minSpanX;
            item.minSpanY = appWidgetInfo.minSpanY;
        } else {
            appWidgetInfo = null;
            PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
+0 −4
Original line number Diff line number Diff line
@@ -68,10 +68,6 @@ public class LauncherAppWidgetInfo extends ItemInfo {

    ComponentName providerName;

    // TODO: Are these necessary here?
    int minWidth = -1;
    int minHeight = -1;

    /**
     * Indicates the restore status of the widget.
     */
+44 −43
Original line number Diff line number Diff line
package com.android.launcher3;

import android.annotation.TargetApi;
import android.appwidget.AppWidgetHostView;
import android.appwidget.AppWidgetProviderInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Parcel;
@@ -20,10 +22,10 @@ public class LauncherAppWidgetProviderInfo extends AppWidgetProviderInfo {

    public boolean isCustomWidget = false;

    private int mSpanX = -1;
    private int mSpanY = -1;
    private int mMinSpanX = -1;
    private int mMinSpanY = -1;
    public int spanX;
    public int spanY;
    public int minSpanX;
    public int minSpanY;

    public static LauncherAppWidgetProviderInfo fromProviderInfo(Context context,
            AppWidgetProviderInfo info) {
@@ -42,6 +44,7 @@ public class LauncherAppWidgetProviderInfo extends AppWidgetProviderInfo {

    public LauncherAppWidgetProviderInfo(Parcel in) {
        super(in);
        initSpans();
    }

    public LauncherAppWidgetProviderInfo(Context context, CustomAppWidget widget) {
@@ -53,6 +56,41 @@ public class LauncherAppWidgetProviderInfo extends AppWidgetProviderInfo {
        previewImage = widget.getPreviewImage();
        initialLayout = widget.getWidgetLayout();
        resizeMode = widget.getResizeMode();
        initSpans();
    }

    private void initSpans() {
        LauncherAppState app = LauncherAppState.getInstance();
        InvariantDeviceProfile idp = app.getInvariantDeviceProfile();

        // We only care out the cell size, which is independent of the the layout direction.
        Rect paddingLand = idp.landscapeProfile.getWorkspacePadding(false /* isLayoutRtl */);
        Rect paddingPort = idp.portraitProfile.getWorkspacePadding(false /* isLayoutRtl */);

        // Always assume we're working with the smallest span to make sure we
        // reserve enough space in both orientations.
        float smallestCellWidth = DeviceProfile.calculateCellWidth(Math.min(
                idp.landscapeProfile.widthPx - paddingLand.left - paddingLand.right,
                idp.portraitProfile.widthPx - paddingPort.left - paddingPort.right),
                idp.numColumns);
        float smallestCellHeight = DeviceProfile.calculateCellWidth(Math.min(
                idp.landscapeProfile.heightPx - paddingLand.top - paddingLand.bottom,
                idp.portraitProfile.heightPx - paddingPort.top - paddingPort.bottom),
                idp.numRows);

        // We want to account for the extra amount of padding that we are adding to the widget
        // to ensure that it gets the full amount of space that it has requested.
        Rect widgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(
                app.getContext(), provider, null);
        spanX = Math.max(1, (int) Math.ceil(
                        (minWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth));
        spanY = Math.max(1, (int) Math.ceil(
                (minHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight));

        minSpanX = Math.max(1, (int) Math.ceil(
                (minResizeWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth));
        minSpanY = Math.max(1, (int) Math.ceil(
                (minResizeHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight));
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
@@ -80,46 +118,9 @@ public class LauncherAppWidgetProviderInfo extends AppWidgetProviderInfo {
                provider.toString(), provider.getPackageName(), provider.getShortClassName(), getLabel(pm));
    }

    public int getSpanX(Launcher launcher) {
        lazyLoadSpans(launcher);
        return mSpanX;
    }

    public int getSpanY(Launcher launcher) {
        lazyLoadSpans(launcher);
        return mSpanY;
    }

    public int getMinSpanX(Launcher launcher) {
        lazyLoadSpans(launcher);
        return mMinSpanX;
    }

    public int getMinSpanY(Launcher launcher) {
        lazyLoadSpans(launcher);
        return mMinSpanY;
    }

    private void lazyLoadSpans(Launcher launcher) {
        if (mSpanX < 0 || mSpanY < 0 || mMinSpanX < 0 || mMinSpanY < 0) {
            int[] minResizeSpan = launcher.getMinSpanForWidget(this);
            int[] span = launcher.getSpanForWidget(this);

            mSpanX = span[0];
            mSpanY = span[1];
            mMinSpanX = minResizeSpan[0];
            mMinSpanY = minResizeSpan[1];
        }
    }

    public Point getMinSpans(InvariantDeviceProfile idp, Context context) {
        // Calculate the spans corresponding to any one of the orientations as it should not change
        // based on orientation.
        // TODO: Use the max of both profiles
        int[] minSpans = CellLayout.rectToCell(
                idp.portraitProfile, context, minResizeWidth, minResizeHeight, null);
        return new Point(
                (resizeMode & RESIZE_HORIZONTAL) != 0 ? minSpans[0] : -1,
                        (resizeMode & RESIZE_VERTICAL) != 0 ? minSpans[1] : -1);
                (resizeMode & RESIZE_HORIZONTAL) != 0 ? minSpanX : -1,
                        (resizeMode & RESIZE_VERTICAL) != 0 ? minSpanY : -1);
    }
 }
Loading