/* * Copyright (C) 2021 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.launcher3.widget.util; import static android.appwidget.AppWidgetHostView.getDefaultPaddingForWidget; import android.appwidget.AppWidgetHostView; import android.appwidget.AppWidgetManager; import android.content.ComponentName; import android.content.Context; import android.graphics.Point; import android.graphics.Rect; import android.os.Bundle; import android.util.Size; import android.util.SizeF; import androidx.annotation.Nullable; import com.android.launcher3.DeviceProfile; import com.android.launcher3.LauncherAppState; import com.android.launcher3.R; import com.android.launcher3.model.WidgetItem; import java.util.ArrayList; import java.util.List; /** A utility class for widget sizes related calculations. */ public final class WidgetSizes { /** * Returns the list of all possible sizes, in dp, for a widget of given spans on this device. * *
The returned sizes already take into account the system padding, and whether it is applied
* or not in that specific configuration.
*/
public static ArrayList This size is used by the widget picker. It should NEVER be shared with app widgets.
*
* For sizes shared with app widgets, please refer to
* {@link #getWidgetPaddedSizes(Context, ComponentName, int, int)} &
* {@link #getWidgetPaddedSizePx(Context, ComponentName, DeviceProfile, int, int)}.
*/
public static Size getWidgetItemSizePx(Context context, DeviceProfile profile,
WidgetItem widgetItem) {
if (widgetItem.isShortcut()) {
int dimension = profile.allAppsIconSizePx + 2 * context.getResources()
.getDimensionPixelSize(R.dimen.widget_preview_shortcut_padding);
return new Size(dimension, dimension);
}
Size widgetItemSize = getWidgetSizePx(profile, widgetItem.spanX,
widgetItem.spanY, /* recycledCellSize= */ null);
if (profile.shouldInsetWidgets()) {
Rect inset = new Rect();
AppWidgetHostView.getDefaultPaddingForWidget(context, widgetItem.componentName, inset);
return new Size(widgetItemSize.getWidth() + inset.left + inset.right,
widgetItemSize.getHeight() + inset.top + inset.bottom);
}
return widgetItemSize;
}
private static Size getWidgetSizePx(DeviceProfile profile, int spanX, int spanY,
@Nullable Point recycledCellSize) {
final int hBorderSpacing = (spanX - 1) * profile.cellLayoutBorderSpacePx.x;
final int vBorderSpacing = (spanY - 1) * profile.cellLayoutBorderSpacePx.y;
if (recycledCellSize == null) {
recycledCellSize = new Point();
}
profile.getCellSize(recycledCellSize);
return new Size(((spanX * recycledCellSize.x) + hBorderSpacing),
((spanY * recycledCellSize.y) + vBorderSpacing));
}
/**
* Updates a given {@code widgetView} with size, {@code spanX}, {@code spanY}.
*
* 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.
*/
public static void updateWidgetSizeRanges(AppWidgetHostView widgetView, Context context,
int spanX, int spanY) {
AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
int widgetId = widgetView.getAppWidgetId();
if (widgetId <= 0) {
return;
}
Bundle sizeOptions = getWidgetSizeOptions(context, widgetView.getAppWidgetInfo().provider,
spanX, spanY);
if (sizeOptions.