• 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 
21 import android.appwidget.AppWidgetHostView;
22 import android.appwidget.AppWidgetManager;
23 import android.content.ComponentName;
24 import android.content.Context;
25 import android.graphics.Point;
26 import android.graphics.Rect;
27 import android.os.Bundle;
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 WidgetItem.
91      */
getWidgetItemSizePx(Context context, DeviceProfile profile, WidgetItem widgetItem)92     public static Size getWidgetItemSizePx(Context context, DeviceProfile profile,
93             WidgetItem widgetItem) {
94         if (widgetItem.isShortcut()) {
95             int dimension = profile.allAppsIconSizePx + 2 * context.getResources()
96                     .getDimensionPixelSize(R.dimen.widget_preview_shortcut_padding);
97             return new Size(dimension, dimension);
98         }
99         return getWidgetPaddedSizePx(context, widgetItem.componentName, profile, widgetItem.spanX,
100                 widgetItem.spanY);
101     }
102 
getWidgetSizePx(DeviceProfile profile, int spanX, int spanY, @Nullable Point recycledCellSize)103     private static Size getWidgetSizePx(DeviceProfile profile, int spanX, int spanY,
104             @Nullable Point recycledCellSize) {
105         final int hBorderSpacing = (spanX - 1) * profile.cellLayoutBorderSpacingPx;
106         final int vBorderSpacing = (spanY - 1) * profile.cellLayoutBorderSpacingPx;
107         if (recycledCellSize == null) {
108             recycledCellSize = new Point();
109         }
110         profile.getCellSize(recycledCellSize);
111         return new Size(((spanX * recycledCellSize.x) + hBorderSpacing),
112                 ((spanY * recycledCellSize.y) + vBorderSpacing));
113     }
114 
115     /**
116      * Updates a given {@code widgetView} with size, {@code spanX}, {@code spanY}.
117      *
118      * <p>On Android S+, it also updates the given {@code widgetView} with a list of sizes derived
119      * from {@code spanX}, {@code spanY} in all supported device profiles.
120      */
updateWidgetSizeRanges(AppWidgetHostView widgetView, Context context, int spanX, int spanY)121     public static void updateWidgetSizeRanges(AppWidgetHostView widgetView, Context context,
122             int spanX, int spanY) {
123         AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
124         int widgetId = widgetView.getAppWidgetId();
125         if (widgetId <= 0) {
126             return;
127         }
128         Bundle sizeOptions = getWidgetSizeOptions(context, widgetView.getAppWidgetInfo().provider,
129                 spanX, spanY);
130         if (sizeOptions.<SizeF>getParcelableArrayList(
131                 AppWidgetManager.OPTION_APPWIDGET_SIZES).equals(
132                 widgetManager.getAppWidgetOptions(widgetId).<SizeF>getParcelableArrayList(
133                         AppWidgetManager.OPTION_APPWIDGET_SIZES))) {
134             return;
135         }
136         widgetManager.updateAppWidgetOptions(widgetId, sizeOptions);
137     }
138 
139     /**
140      * Returns the bundle to be used as the default options for a widget with provided size.
141      */
getWidgetSizeOptions(Context context, ComponentName provider, int spanX, int spanY)142     public static Bundle getWidgetSizeOptions(Context context, ComponentName provider, int spanX,
143             int spanY) {
144         ArrayList<SizeF> paddedSizes = getWidgetPaddedSizes(context, provider, spanX, spanY);
145 
146         Rect rect = getMinMaxSizes(paddedSizes);
147         Bundle options = new Bundle();
148         options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, rect.left);
149         options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, rect.top);
150         options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, rect.right);
151         options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, rect.bottom);
152         options.putParcelableArrayList(AppWidgetManager.OPTION_APPWIDGET_SIZES, paddedSizes);
153         return options;
154     }
155 
156     /**
157      * Returns the min and max widths and heights given a list of sizes, in dp.
158      *
159      * @param sizes List of sizes to get the min/max from.
160      * @return A rectangle with the left (resp. top) is used for the min width (resp. height) and
161      * the right (resp. bottom) for the max. The returned rectangle is set with 0s if the list is
162      * empty.
163      */
getMinMaxSizes(List<SizeF> sizes)164     private static Rect getMinMaxSizes(List<SizeF> sizes) {
165         if (sizes.isEmpty()) {
166             return new Rect();
167         } else {
168             SizeF first = sizes.get(0);
169             Rect result = new Rect((int) first.getWidth(), (int) first.getHeight(),
170                     (int) first.getWidth(), (int) first.getHeight());
171             for (int i = 1; i < sizes.size(); i++) {
172                 result.union((int) sizes.get(i).getWidth(), (int) sizes.get(i).getHeight());
173             }
174             return result;
175         }
176     }
177 }
178