• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Log;
28 import android.util.Size;
29 import android.util.SizeF;
30 
31 import androidx.annotation.Nullable;
32 
33 import com.android.launcher3.DeviceProfile;
34 import com.android.launcher3.LauncherAppState;
35 import com.android.launcher3.R;
36 import com.android.launcher3.model.WidgetItem;
37 
38 import java.util.ArrayList;
39 import java.util.List;
40 
41 /** A utility class for widget sizes related calculations. */
42 public final class WidgetSizes {
43 
44     /**
45      * Returns the list of all possible sizes, in dp, for a widget of given spans on this device.
46      *
47      * <p>The returned sizes already take into account the system padding, and whether it is applied
48      * or not in that specific configuration.
49      */
getWidgetPaddedSizes(Context context, ComponentName provider, int spanX, int spanY)50     public static ArrayList<SizeF> getWidgetPaddedSizes(Context context, ComponentName provider,
51             int spanX, int spanY) {
52         Rect padding = getDefaultPaddingForWidget(context, provider, /* padding= */ null);
53 
54         ArrayList<SizeF> sizes = new ArrayList<>(2);
55         final float density = context.getResources().getDisplayMetrics().density;
56         final Point cellSize = new Point();
57 
58         for (DeviceProfile profile : LauncherAppState.getIDP(context).supportedProfiles) {
59             Size widgetSizePx = getWidgetSizePx(profile, spanX, spanY, cellSize);
60             if (!profile.shouldInsetWidgets()) {
61                 widgetSizePx = new Size(widgetSizePx.getWidth() - padding.left - padding.right,
62                         widgetSizePx.getHeight() - padding.top - padding.bottom);
63             }
64             sizes.add(new SizeF(widgetSizePx.getWidth() / density,
65                     widgetSizePx.getHeight() / density));
66         }
67         return sizes;
68     }
69 
70     /** Returns the size, in pixels, a widget of given spans & {@code profile}. */
getWidgetSizePx(DeviceProfile profile, int spanX, int spanY)71     public static Size getWidgetSizePx(DeviceProfile profile, int spanX, int spanY) {
72         return getWidgetSizePx(profile, spanX, spanY, /* recycledCellSize= */ null);
73     }
74 
75     /**
76      * Returns the size, in pixels and removing padding, a widget of given spans & {@code profile}.
77      */
getWidgetPaddedSizePx(Context context, ComponentName component, DeviceProfile profile, int spanX, int spanY)78     public static Size getWidgetPaddedSizePx(Context context, ComponentName component,
79             DeviceProfile profile, int spanX, int spanY) {
80         Size size = getWidgetSizePx(profile, spanX, spanY);
81         if (profile.shouldInsetWidgets()) {
82             return size;
83         }
84         Rect padding = getDefaultPaddingForWidget(context, component, /* padding= */ null);
85         return new Size(size.getWidth() - padding.left - padding.right,
86                 size.getHeight() - padding.top - padding.bottom);
87     }
88 
89     /**
90      * Returns the size of a {@link WidgetItem}.
91      *
92      * <p>This size is used by the widget picker. It should NEVER be shared with app widgets.
93      *
94      * <p>For sizes shared with app widgets, please refer to
95      * {@link #getWidgetPaddedSizes(Context, ComponentName, int, int)} &
96      * {@link #getWidgetPaddedSizePx(Context, ComponentName, DeviceProfile, int, int)}.
97      */
getWidgetItemSizePx(Context context, DeviceProfile profile, WidgetItem widgetItem)98     public static Size getWidgetItemSizePx(Context context, DeviceProfile profile,
99             WidgetItem widgetItem) {
100         if (widgetItem.isShortcut()) {
101             int dimension = profile.allAppsIconSizePx + 2 * context.getResources()
102                     .getDimensionPixelSize(R.dimen.widget_preview_shortcut_padding);
103             return new Size(dimension, dimension);
104         }
105         Size widgetItemSize = getWidgetSizePx(profile, widgetItem.spanX,
106                 widgetItem.spanY, /* recycledCellSize= */ null);
107         if (profile.shouldInsetWidgets()) {
108             Rect inset = new Rect();
109             AppWidgetHostView.getDefaultPaddingForWidget(context, widgetItem.componentName, inset);
110             return new Size(widgetItemSize.getWidth() + inset.left + inset.right,
111                     widgetItemSize.getHeight() + inset.top + inset.bottom);
112         }
113         return widgetItemSize;
114     }
115 
getWidgetSizePx(DeviceProfile profile, int spanX, int spanY, @Nullable Point recycledCellSize)116     private static Size getWidgetSizePx(DeviceProfile profile, int spanX, int spanY,
117             @Nullable Point recycledCellSize) {
118         final int hBorderSpacing = (spanX - 1) * profile.cellLayoutBorderSpacePx.x;
119         final int vBorderSpacing = (spanY - 1) * profile.cellLayoutBorderSpacePx.y;
120         if (recycledCellSize == null) {
121             recycledCellSize = new Point();
122         }
123         profile.getCellSize(recycledCellSize);
124         return new Size(((spanX * recycledCellSize.x) + hBorderSpacing),
125                 ((spanY * recycledCellSize.y) + vBorderSpacing));
126     }
127 
128     /**
129      * Updates a given {@code widgetView} with size, {@code spanX}, {@code spanY}.
130      *
131      * <p>On Android S+, it also updates the given {@code widgetView} with a list of sizes derived
132      * from {@code spanX}, {@code spanY} in all supported device profiles.
133      */
updateWidgetSizeRanges(AppWidgetHostView widgetView, Context context, int spanX, int spanY)134     public static void updateWidgetSizeRanges(AppWidgetHostView widgetView, Context context,
135             int spanX, int spanY) {
136         AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
137         int widgetId = widgetView.getAppWidgetId();
138         if (widgetId <= 0) {
139             return;
140         }
141         Bundle sizeOptions = getWidgetSizeOptions(context, widgetView.getAppWidgetInfo().provider,
142                 spanX, spanY);
143         if (sizeOptions.<SizeF>getParcelableArrayList(
144                 AppWidgetManager.OPTION_APPWIDGET_SIZES).equals(
145                 widgetManager.getAppWidgetOptions(widgetId).<SizeF>getParcelableArrayList(
146                         AppWidgetManager.OPTION_APPWIDGET_SIZES))) {
147             return;
148         }
149         widgetManager.updateAppWidgetOptions(widgetId, sizeOptions);
150     }
151 
152     /**
153      * Returns the bundle to be used as the default options for a widget with provided size.
154      */
getWidgetSizeOptions(Context context, ComponentName provider, int spanX, int spanY)155     public static Bundle getWidgetSizeOptions(Context context, ComponentName provider, int spanX,
156             int spanY) {
157         ArrayList<SizeF> paddedSizes = getWidgetPaddedSizes(context, provider, spanX, spanY);
158 
159         Rect rect = getMinMaxSizes(paddedSizes);
160         Bundle options = new Bundle();
161         options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, rect.left);
162         options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, rect.top);
163         options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, rect.right);
164         options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, rect.bottom);
165         options.putParcelableArrayList(AppWidgetManager.OPTION_APPWIDGET_SIZES, paddedSizes);
166         Log.d("b/267448330", "provider: " + provider + ", paddedSizes: " + paddedSizes
167                 + ", getMinMaxSizes: " + rect);
168         return options;
169     }
170 
171     /**
172      * Returns the min and max widths and heights given a list of sizes, in dp.
173      *
174      * @param sizes List of sizes to get the min/max from.
175      * @return A rectangle with the left (resp. top) is used for the min width (resp. height) and
176      * the right (resp. bottom) for the max. The returned rectangle is set with 0s if the list is
177      * empty.
178      */
getMinMaxSizes(List<SizeF> sizes)179     private static Rect getMinMaxSizes(List<SizeF> sizes) {
180         if (sizes.isEmpty()) {
181             return new Rect();
182         } else {
183             SizeF first = sizes.get(0);
184             Rect result = new Rect((int) first.getWidth(), (int) first.getHeight(),
185                     (int) first.getWidth(), (int) first.getHeight());
186             for (int i = 1; i < sizes.size(); i++) {
187                 result.union((int) sizes.get(i).getWidth(), (int) sizes.get(i).getHeight());
188             }
189             return result;
190         }
191     }
192 }
193