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