1 package com.android.launcher3.widget; 2 3 import static com.android.launcher3.Utilities.ATLEAST_S; 4 5 import android.appwidget.AppWidgetHostView; 6 import android.appwidget.AppWidgetProviderInfo; 7 import android.content.ComponentName; 8 import android.content.Context; 9 import android.content.pm.PackageManager; 10 import android.graphics.Point; 11 import android.graphics.Rect; 12 import android.graphics.drawable.Drawable; 13 import android.os.Parcel; 14 import android.os.UserHandle; 15 16 import com.android.launcher3.DeviceProfile; 17 import com.android.launcher3.InvariantDeviceProfile; 18 import com.android.launcher3.LauncherAppState; 19 import com.android.launcher3.Utilities; 20 import com.android.launcher3.icons.ComponentWithLabelAndIcon; 21 import com.android.launcher3.icons.IconCache; 22 import com.android.launcher3.model.data.LauncherAppWidgetInfo; 23 24 /** 25 * This class is a thin wrapper around the framework AppWidgetProviderInfo class. This class affords 26 * a common object for describing both framework provided AppWidgets as well as custom widgets 27 * (who's implementation is owned by the launcher). This object represents a widget type / class, 28 * as opposed to a widget instance, and so should not be confused with {@link LauncherAppWidgetInfo} 29 */ 30 public class LauncherAppWidgetProviderInfo extends AppWidgetProviderInfo 31 implements ComponentWithLabelAndIcon { 32 33 public static final String CLS_CUSTOM_WIDGET_PREFIX = "#custom-widget-"; 34 35 /** 36 * The desired number of cells that this widget occupies horizontally in 37 * {@link com.android.launcher3.CellLayout}. 38 */ 39 public int spanX; 40 41 /** 42 * The desired number of cells that this widget occupies vertically in 43 * {@link com.android.launcher3.CellLayout}. 44 */ 45 public int spanY; 46 47 /** 48 * The minimum number of cells that this widget can occupy horizontally in 49 * {@link com.android.launcher3.CellLayout}. 50 */ 51 public int minSpanX; 52 53 /** 54 * The minimum number of cells that this widget can occupy vertically in 55 * {@link com.android.launcher3.CellLayout}. 56 */ 57 public int minSpanY; 58 59 /** 60 * The maximum number of cells that this widget can occupy horizontally in 61 * {@link com.android.launcher3.CellLayout}. 62 */ 63 public int maxSpanX; 64 65 /** 66 * The maximum number of cells that this widget can occupy vertically in 67 * {@link com.android.launcher3.CellLayout}. 68 */ 69 public int maxSpanY; 70 71 private boolean mIsMinSizeFulfilled; 72 fromProviderInfo(Context context, AppWidgetProviderInfo info)73 public static LauncherAppWidgetProviderInfo fromProviderInfo(Context context, 74 AppWidgetProviderInfo info) { 75 final LauncherAppWidgetProviderInfo launcherInfo; 76 if (info instanceof LauncherAppWidgetProviderInfo) { 77 launcherInfo = (LauncherAppWidgetProviderInfo) info; 78 } else { 79 80 // In lieu of a public super copy constructor, we first write the AppWidgetProviderInfo 81 // into a parcel, and then construct a new LauncherAppWidgetProvider info from the 82 // associated super parcel constructor. This allows us to copy non-public members without 83 // using reflection. 84 Parcel p = Parcel.obtain(); 85 info.writeToParcel(p, 0); 86 p.setDataPosition(0); 87 launcherInfo = new LauncherAppWidgetProviderInfo(p); 88 p.recycle(); 89 } 90 launcherInfo.initSpans(context, LauncherAppState.getIDP(context)); 91 return launcherInfo; 92 } 93 LauncherAppWidgetProviderInfo()94 protected LauncherAppWidgetProviderInfo() {} 95 LauncherAppWidgetProviderInfo(Parcel in)96 protected LauncherAppWidgetProviderInfo(Parcel in) { 97 super(in); 98 } 99 initSpans(Context context, InvariantDeviceProfile idp)100 public void initSpans(Context context, InvariantDeviceProfile idp) { 101 int minSpanX = 0; 102 int minSpanY = 0; 103 int maxSpanX = idp.numColumns; 104 int maxSpanY = idp.numRows; 105 int spanX = 0; 106 int spanY = 0; 107 108 Rect widgetPadding = new Rect(); 109 Rect localPadding = new Rect(); 110 AppWidgetHostView.getDefaultPaddingForWidget(context, provider, widgetPadding); 111 112 Point cellSize = new Point(); 113 for (DeviceProfile dp : idp.supportedProfiles) { 114 dp.getCellSize(cellSize); 115 // We want to account for the extra amount of padding that we are adding to the widget 116 // to ensure that it gets the full amount of space that it has requested. 117 // If grids supports insetting widgets, we do not account for widget padding. 118 if (dp.shouldInsetWidgets()) { 119 localPadding.setEmpty(); 120 } else { 121 localPadding.set(widgetPadding); 122 } 123 minSpanX = Math.max(minSpanX, 124 getSpanX(localPadding, minResizeWidth, dp.cellLayoutBorderSpacingPx, 125 cellSize.x)); 126 minSpanY = Math.max(minSpanY, 127 getSpanY(localPadding, minResizeHeight, dp.cellLayoutBorderSpacingPx, 128 cellSize.y)); 129 130 if (ATLEAST_S) { 131 if (maxResizeWidth > 0) { 132 maxSpanX = Math.min(maxSpanX, 133 getSpanX(localPadding, maxResizeWidth, dp.cellLayoutBorderSpacingPx, 134 cellSize.x)); 135 } 136 if (maxResizeHeight > 0) { 137 maxSpanY = Math.min(maxSpanY, 138 getSpanY(localPadding, maxResizeHeight, dp.cellLayoutBorderSpacingPx, 139 cellSize.y)); 140 } 141 } 142 143 spanX = Math.max(spanX, 144 getSpanX(localPadding, minWidth, dp.cellLayoutBorderSpacingPx, cellSize.x)); 145 spanY = Math.max(spanY, 146 getSpanY(localPadding, minHeight, dp.cellLayoutBorderSpacingPx, cellSize.y)); 147 } 148 149 if (ATLEAST_S) { 150 // Ensures maxSpan >= minSpan 151 maxSpanX = Math.max(maxSpanX, minSpanX); 152 maxSpanY = Math.max(maxSpanY, minSpanY); 153 154 // Use targetCellWidth/Height if it is within the min/max ranges. 155 // Otherwise, use the span of minWidth/Height. 156 if (targetCellWidth >= minSpanX && targetCellWidth <= maxSpanX 157 && targetCellHeight >= minSpanY && targetCellHeight <= maxSpanY) { 158 spanX = targetCellWidth; 159 spanY = targetCellHeight; 160 } 161 } 162 163 // If minSpanX/Y > spanX/Y, ignore the minSpanX/Y to match the behavior described in 164 // minResizeWidth & minResizeHeight Android documentation. See 165 // https://developer.android.com/reference/android/appwidget/AppWidgetProviderInfo 166 this.minSpanX = Math.min(spanX, minSpanX); 167 this.minSpanY = Math.min(spanY, minSpanY); 168 this.maxSpanX = maxSpanX; 169 this.maxSpanY = maxSpanY; 170 this.mIsMinSizeFulfilled = Math.min(spanX, minSpanX) <= idp.numColumns 171 && Math.min(spanY, minSpanY) <= idp.numRows; 172 // Ensures the default span X and span Y will not exceed the current grid size. 173 this.spanX = Math.min(spanX, idp.numColumns); 174 this.spanY = Math.min(spanY, idp.numRows); 175 } 176 177 /** 178 * Returns {@code true} if the widget's minimum size requirement can be fulfilled in the device 179 * grid setting, {@link InvariantDeviceProfile}, that was passed in 180 * {@link #initSpans(Context, InvariantDeviceProfile)}. 181 */ isMinSizeFulfilled()182 public boolean isMinSizeFulfilled() { 183 return mIsMinSizeFulfilled; 184 } 185 getSpanX(Rect widgetPadding, int widgetWidth, int cellSpacing, float cellWidth)186 private int getSpanX(Rect widgetPadding, int widgetWidth, int cellSpacing, float cellWidth) { 187 return Math.max(1, (int) Math.ceil( 188 (widgetWidth + widgetPadding.left + widgetPadding.right + cellSpacing) / (cellWidth 189 + cellSpacing))); 190 } 191 getSpanY(Rect widgetPadding, int widgetHeight, int cellSpacing, float cellHeight)192 private int getSpanY(Rect widgetPadding, int widgetHeight, int cellSpacing, float cellHeight) { 193 return Math.max(1, (int) Math.ceil( 194 (widgetHeight + widgetPadding.top + widgetPadding.bottom + cellSpacing) / ( 195 cellHeight + cellSpacing))); 196 } 197 getLabel(PackageManager packageManager)198 public String getLabel(PackageManager packageManager) { 199 return super.loadLabel(packageManager); 200 } 201 getMinSpans()202 public Point getMinSpans() { 203 return new Point((resizeMode & RESIZE_HORIZONTAL) != 0 ? minSpanX : -1, 204 (resizeMode & RESIZE_VERTICAL) != 0 ? minSpanY : -1); 205 } 206 isCustomWidget()207 public boolean isCustomWidget() { 208 return provider.getClassName().startsWith(CLS_CUSTOM_WIDGET_PREFIX); 209 } 210 getWidgetFeatures()211 public int getWidgetFeatures() { 212 if (Utilities.ATLEAST_P) { 213 return widgetFeatures; 214 } else { 215 return 0; 216 } 217 } 218 isReconfigurable()219 public boolean isReconfigurable() { 220 return configure != null && (getWidgetFeatures() & WIDGET_FEATURE_RECONFIGURABLE) != 0; 221 } 222 isConfigurationOptional()223 public boolean isConfigurationOptional() { 224 return ATLEAST_S 225 && isReconfigurable() 226 && (getWidgetFeatures() & WIDGET_FEATURE_CONFIGURATION_OPTIONAL) != 0; 227 } 228 229 @Override getComponent()230 public final ComponentName getComponent() { 231 return provider; 232 } 233 234 @Override getUser()235 public final UserHandle getUser() { 236 return getProfile(); 237 } 238 239 @Override getFullResIcon(IconCache cache)240 public Drawable getFullResIcon(IconCache cache) { 241 return cache.getFullResIcon(provider.getPackageName(), icon); 242 } 243 }