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 com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR; 19 20 import android.appwidget.AppWidgetHostView; 21 import android.appwidget.AppWidgetManager; 22 import android.appwidget.AppWidgetProviderInfo; 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.Log; 29 import android.util.Size; 30 import android.util.SizeF; 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 */ getWidgetSizesDp(Context context, int spanX, int spanY)46 public static ArrayList<SizeF> getWidgetSizesDp(Context context, int spanX, int spanY) { 47 ArrayList<SizeF> sizes = new ArrayList<>(2); 48 final float density = context.getResources().getDisplayMetrics().density; 49 50 for (DeviceProfile profile : LauncherAppState.getIDP(context).supportedProfiles) { 51 Size widgetSizePx = getWidgetSizePx(profile, spanX, spanY); 52 sizes.add(new SizeF(widgetSizePx.getWidth() / density, 53 widgetSizePx.getHeight() / density)); 54 } 55 return sizes; 56 } 57 58 /** Returns the size, in pixels, a widget of given spans & {@code profile}. */ getWidgetSizePx(DeviceProfile profile, int spanX, int spanY)59 public static Size getWidgetSizePx(DeviceProfile profile, int spanX, int spanY) { 60 final int hBorderSpacing = (spanX - 1) * profile.cellLayoutBorderSpacePx.x; 61 final int vBorderSpacing = (spanY - 1) * profile.cellLayoutBorderSpacePx.y; 62 63 Point cellSize = profile.getCellSize(); 64 Rect padding = profile.widgetPadding; 65 66 return new Size( 67 (spanX * cellSize.x) + hBorderSpacing - padding.left - padding.right, 68 (spanY * cellSize.y) + vBorderSpacing - padding.top - padding.bottom); 69 } 70 71 /** 72 * Returns the size of a {@link WidgetItem}. 73 * 74 * <p>This size is used by the widget picker. It should NEVER be shared with app widgets. 75 * 76 * <p>For sizes shared with app widgets, please refer to 77 * {@link #getWidgetSizesDp(Context, int, int)} & 78 */ getWidgetItemSizePx(Context context, DeviceProfile profile, WidgetItem widgetItem)79 public static Size getWidgetItemSizePx(Context context, DeviceProfile profile, 80 WidgetItem widgetItem) { 81 if (widgetItem.isShortcut()) { 82 int dimension = profile.allAppsIconSizePx + 2 * context.getResources() 83 .getDimensionPixelSize(R.dimen.widget_preview_shortcut_padding); 84 return new Size(dimension, dimension); 85 } 86 return getWidgetSizePx(profile, widgetItem.spanX, widgetItem.spanY); 87 } 88 89 /** 90 * Updates a given {@code widgetView} with size, {@code spanX}, {@code spanY}. 91 * 92 * <p>On Android S+, it also updates the given {@code widgetView} with a list of sizes derived 93 * from {@code spanX}, {@code spanY} in all supported device profiles. 94 */ updateWidgetSizeRanges(AppWidgetHostView widgetView, Context context, int spanX, int spanY)95 public static void updateWidgetSizeRanges(AppWidgetHostView widgetView, Context context, 96 int spanX, int spanY) { 97 updateWidgetSizeRangesAsync( 98 widgetView.getAppWidgetId(), widgetView.getAppWidgetInfo(), context, spanX, spanY); 99 } 100 101 /** 102 * Updates a given {@code widgetId} with size, {@code spanX}, {@code spanY} asynchronously. 103 * 104 * <p>On Android S+, it also updates the given {@code widgetView} with a list of sizes derived 105 * from {@code spanX}, {@code spanY} in all supported device profiles. 106 */ updateWidgetSizeRangesAsync(int widgetId, AppWidgetProviderInfo info, Context context, int spanX, int spanY)107 public static void updateWidgetSizeRangesAsync(int widgetId, 108 AppWidgetProviderInfo info, Context context, int spanX, int spanY) { 109 if (widgetId <= 0 || info == null) { 110 return; 111 } 112 113 UI_HELPER_EXECUTOR.execute(() -> { 114 AppWidgetManager widgetManager = AppWidgetManager.getInstance(context); 115 Bundle sizeOptions = getWidgetSizeOptions(context, info.provider, spanX, spanY); 116 if (sizeOptions.<SizeF>getParcelableArrayList( 117 AppWidgetManager.OPTION_APPWIDGET_SIZES).equals( 118 widgetManager.getAppWidgetOptions(widgetId).<SizeF>getParcelableArrayList( 119 AppWidgetManager.OPTION_APPWIDGET_SIZES))) { 120 return; 121 } 122 widgetManager.updateAppWidgetOptions(widgetId, sizeOptions); 123 }); 124 } 125 126 /** 127 * Returns the bundle to be used as the default options for a widget with provided size. 128 */ getWidgetSizeOptions(Context context, ComponentName provider, int spanX, int spanY)129 public static Bundle getWidgetSizeOptions(Context context, ComponentName provider, int spanX, 130 int spanY) { 131 ArrayList<SizeF> paddedSizes = getWidgetSizesDp(context, spanX, spanY); 132 133 Rect rect = getMinMaxSizes(paddedSizes); 134 Bundle options = new Bundle(); 135 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, rect.left); 136 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, rect.top); 137 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, rect.right); 138 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, rect.bottom); 139 options.putParcelableArrayList(AppWidgetManager.OPTION_APPWIDGET_SIZES, paddedSizes); 140 Log.d("b/267448330", "provider: " + provider + ", paddedSizes: " + paddedSizes 141 + ", getMinMaxSizes: " + rect); 142 return options; 143 } 144 145 /** 146 * Returns the min and max widths and heights given a list of sizes, in dp. 147 * 148 * @param sizes List of sizes to get the min/max from. 149 * @return A rectangle with the left (resp. top) is used for the min width (resp. height) and 150 * the right (resp. bottom) for the max. The returned rectangle is set with 0s if the list is 151 * empty. 152 */ getMinMaxSizes(List<SizeF> sizes)153 private static Rect getMinMaxSizes(List<SizeF> sizes) { 154 if (sizes.isEmpty()) { 155 return new Rect(); 156 } else { 157 SizeF first = sizes.get(0); 158 Rect result = new Rect((int) first.getWidth(), (int) first.getHeight(), 159 (int) first.getWidth(), (int) first.getHeight()); 160 for (int i = 1; i < sizes.size(); i++) { 161 result.union((int) sizes.get(i).getWidth(), (int) sizes.get(i).getHeight()); 162 } 163 return result; 164 } 165 } 166 } 167