1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.launcher3.widget.util;
17 
18 import static android.appwidget.AppWidgetHostView.getDefaultPaddingForWidget;
19 
20 import android.appwidget.AppWidgetHostView;
21 import android.appwidget.AppWidgetManager;
22 import android.content.ComponentName;
23 import android.content.Context;
24 import android.graphics.Point;
25 import android.graphics.Rect;
26 import android.os.Bundle;
27 import android.util.Size;
28 import android.util.SizeF;
29 
30 import androidx.annotation.Nullable;
31 
32 import com.android.launcher3.DeviceProfile;
33 import com.android.launcher3.LauncherAppState;
34 import com.android.launcher3.R;
35 import com.android.launcher3.model.WidgetItem;
36 
37 import java.util.ArrayList;
38 import java.util.List;
39 
40 /** A utility class for widget sizes related calculations. */
41 public final class WidgetSizes {
42 
43     /**
44      * Returns the list of all possible sizes, in dp, for a widget of given spans on this device.
45      *
46      * <p>The returned sizes already take into account the system padding, and whether it is applied
47      * or not in that specific configuration.
48      */
getWidgetPaddedSizes(Context context, ComponentName provider, int spanX, int spanY)49     public static ArrayList<SizeF> getWidgetPaddedSizes(Context context, ComponentName provider,
50             int spanX, int spanY) {
51         Rect padding = getDefaultPaddingForWidget(context, provider, /* padding= */ null);
52 
53         ArrayList<SizeF> sizes = new ArrayList<>(2);
54         final float density = context.getResources().getDisplayMetrics().density;
55         final Point cellSize = new Point();
56 
57         for (DeviceProfile profile : LauncherAppState.getIDP(context).supportedProfiles) {
58             Size widgetSizePx = getWidgetSizePx(profile, spanX, spanY, cellSize);
59             if (!profile.shouldInsetWidgets()) {
60                 widgetSizePx = new Size(widgetSizePx.getWidth() - padding.left - padding.right,
61                         widgetSizePx.getHeight() - padding.top - padding.bottom);
62             }
63             sizes.add(new SizeF(widgetSizePx.getWidth() / density,
64                     widgetSizePx.getHeight() / density));
65         }
66         return sizes;
67     }
68 
69     /** Returns the size, in pixels, a widget of given spans & {@code profile}. */
getWidgetSizePx(DeviceProfile profile, int spanX, int spanY)70     public static Size getWidgetSizePx(DeviceProfile profile, int spanX, int spanY) {
71         return getWidgetSizePx(profile, spanX, spanY, /* recycledCellSize= */ null);
72     }
73 
74     /**
75      * Returns the size, in pixels and removing padding, a widget of given spans & {@code profile}.
76      */
getWidgetPaddedSizePx(Context context, ComponentName component, DeviceProfile profile, int spanX, int spanY)77     public static Size getWidgetPaddedSizePx(Context context, ComponentName component,
78             DeviceProfile profile, int spanX, int spanY) {
79         Size size = getWidgetSizePx(profile, spanX, spanY);
80         if (profile.shouldInsetWidgets()) {
81             return size;
82         }
83         Rect padding = getDefaultPaddingForWidget(context, component, /* padding= */ null);
84         return new Size(size.getWidth() - padding.left - padding.right,
85                 size.getHeight() - padding.top - padding.bottom);
86     }
87 
88     /**
89      * Returns the size of a {@link WidgetItem}.
90      *
91      * <p>This size is used by the widget picker. It should NEVER be shared with app widgets.
92      *
93      * <p>For sizes shared with app widgets, please refer to
94      * {@link #getWidgetPaddedSizes(Context, ComponentName, int, int)} &
95      * {@link #getWidgetPaddedSizePx(Context, ComponentName, DeviceProfile, int, int)}.
96      */
getWidgetItemSizePx(Context context, DeviceProfile profile, WidgetItem widgetItem)97     public static Size getWidgetItemSizePx(Context context, DeviceProfile profile,
98             WidgetItem widgetItem) {
99         if (widgetItem.isShortcut()) {
100             int dimension = profile.allAppsIconSizePx + 2 * context.getResources()
101                     .getDimensionPixelSize(R.dimen.widget_preview_shortcut_padding);
102             return new Size(dimension, dimension);
103         }
104         Size widgetItemSize = getWidgetSizePx(profile, widgetItem.spanX,
105                 widgetItem.spanY, /* recycledCellSize= */ null);
106         if (profile.shouldInsetWidgets()) {
107             Rect inset = new Rect();
108             AppWidgetHostView.getDefaultPaddingForWidget(context, widgetItem.componentName, inset);
109             return new Size(widgetItemSize.getWidth() + inset.left + inset.right,
110                     widgetItemSize.getHeight() + inset.top + inset.bottom);
111         }
112         return widgetItemSize;
113     }
114 
getWidgetSizePx(DeviceProfile profile, int spanX, int spanY, @Nullable Point recycledCellSize)115     private static Size getWidgetSizePx(DeviceProfile profile, int spanX, int spanY,
116             @Nullable Point recycledCellSize) {
117         final int hBorderSpacing = (spanX - 1) * profile.cellLayoutBorderSpacePx.x;
118         final int vBorderSpacing = (spanY - 1) * profile.cellLayoutBorderSpacePx.y;
119         if (recycledCellSize == null) {
120             recycledCellSize = new Point();
121         }
122         profile.getCellSize(recycledCellSize);
123         return new Size(((spanX * recycledCellSize.x) + hBorderSpacing),
124                 ((spanY * recycledCellSize.y) + vBorderSpacing));
125     }
126 
127     /**
128      * Updates a given {@code widgetView} with size, {@code spanX}, {@code spanY}.
129      *
130      * <p>On Android S+, it also updates the given {@code widgetView} with a list of sizes derived
131      * from {@code spanX}, {@code spanY} in all supported device profiles.
132      */
updateWidgetSizeRanges(AppWidgetHostView widgetView, Context context, int spanX, int spanY)133     public static void updateWidgetSizeRanges(AppWidgetHostView widgetView, Context context,
134             int spanX, int spanY) {
135         AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
136         int widgetId = widgetView.getAppWidgetId();
137         if (widgetId <= 0) {
138             return;
139         }
140         Bundle sizeOptions = getWidgetSizeOptions(context, widgetView.getAppWidgetInfo().provider,
141                 spanX, spanY);
142         if (sizeOptions.<SizeF>getParcelableArrayList(
143                 AppWidgetManager.OPTION_APPWIDGET_SIZES).equals(
144                 widgetManager.getAppWidgetOptions(widgetId).<SizeF>getParcelableArrayList(
145                         AppWidgetManager.OPTION_APPWIDGET_SIZES))) {
146             return;
147         }
148         widgetManager.updateAppWidgetOptions(widgetId, sizeOptions);
149     }
150 
151     /**
152      * Returns the bundle to be used as the default options for a widget with provided size.
153      */
getWidgetSizeOptions(Context context, ComponentName provider, int spanX, int spanY)154     public static Bundle getWidgetSizeOptions(Context context, ComponentName provider, int spanX,
155             int spanY) {
156         ArrayList<SizeF> paddedSizes = getWidgetPaddedSizes(context, provider, spanX, spanY);
157 
158         Rect rect = getMinMaxSizes(paddedSizes);
159         Bundle options = new Bundle();
160         options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, rect.left);
161         options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, rect.top);
162         options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, rect.right);
163         options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, rect.bottom);
164         options.putParcelableArrayList(AppWidgetManager.OPTION_APPWIDGET_SIZES, paddedSizes);
165         return options;
166     }
167 
168     /**
169      * Returns the min and max widths and heights given a list of sizes, in dp.
170      *
171      * @param sizes List of sizes to get the min/max from.
172      * @return A rectangle with the left (resp. top) is used for the min width (resp. height) and
173      * the right (resp. bottom) for the max. The returned rectangle is set with 0s if the list is
174      * empty.
175      */
getMinMaxSizes(List<SizeF> sizes)176     private static Rect getMinMaxSizes(List<SizeF> sizes) {
177         if (sizes.isEmpty()) {
178             return new Rect();
179         } else {
180             SizeF first = sizes.get(0);
181             Rect result = new Rect((int) first.getWidth(), (int) first.getHeight(),
182                     (int) first.getWidth(), (int) first.getHeight());
183             for (int i = 1; i < sizes.size(); i++) {
184                 result.union((int) sizes.get(i).getWidth(), (int) sizes.get(i).getHeight());
185             }
186             return result;
187         }
188     }
189 }
190