Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 1 | package com.android.launcher3; |
| 2 | |
Sunny Goyal | 233ee96 | 2015-08-03 13:05:01 -0700 | [diff] [blame] | 3 | import android.appwidget.AppWidgetHostView; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 4 | import android.appwidget.AppWidgetProviderInfo; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 5 | import android.content.Context; |
| 6 | import android.content.pm.PackageManager; |
Sunny Goyal | e5bb705 | 2015-07-27 14:36:07 -0700 | [diff] [blame] | 7 | import android.graphics.Point; |
Sunny Goyal | 233ee96 | 2015-08-03 13:05:01 -0700 | [diff] [blame] | 8 | import android.graphics.Rect; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 9 | import android.os.Parcel; |
| 10 | |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 11 | /** |
| 12 | * This class is a thin wrapper around the framework AppWidgetProviderInfo class. This class affords |
| 13 | * a common object for describing both framework provided AppWidgets as well as custom widgets |
| 14 | * (who's implementation is owned by the launcher). This object represents a widget type / class, |
| 15 | * as opposed to a widget instance, and so should not be confused with {@link LauncherAppWidgetInfo} |
| 16 | */ |
| 17 | public class LauncherAppWidgetProviderInfo extends AppWidgetProviderInfo { |
| 18 | |
Sunny Goyal | 952e63d | 2017-08-16 04:59:08 -0700 | [diff] [blame] | 19 | public static final String CLS_CUSTOM_WIDGET_PREFIX = "#custom-widget-"; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 20 | |
Sunny Goyal | 233ee96 | 2015-08-03 13:05:01 -0700 | [diff] [blame] | 21 | public int spanX; |
| 22 | public int spanY; |
| 23 | public int minSpanX; |
| 24 | public int minSpanY; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 25 | |
| 26 | public static LauncherAppWidgetProviderInfo fromProviderInfo(Context context, |
| 27 | AppWidgetProviderInfo info) { |
Sunny Goyal | 8ad02b8 | 2016-12-29 13:31:43 -0800 | [diff] [blame] | 28 | final LauncherAppWidgetProviderInfo launcherInfo; |
| 29 | if (info instanceof LauncherAppWidgetProviderInfo) { |
| 30 | launcherInfo = (LauncherAppWidgetProviderInfo) info; |
| 31 | } else { |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 32 | |
Sunny Goyal | 8ad02b8 | 2016-12-29 13:31:43 -0800 | [diff] [blame] | 33 | // In lieu of a public super copy constructor, we first write the AppWidgetProviderInfo |
| 34 | // into a parcel, and then construct a new LauncherAppWidgetProvider info from the |
| 35 | // associated super parcel constructor. This allows us to copy non-public members without |
| 36 | // using reflection. |
| 37 | Parcel p = Parcel.obtain(); |
| 38 | info.writeToParcel(p, 0); |
| 39 | p.setDataPosition(0); |
| 40 | launcherInfo = new LauncherAppWidgetProviderInfo(p); |
| 41 | p.recycle(); |
| 42 | } |
| 43 | launcherInfo.initSpans(context); |
| 44 | return launcherInfo; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Sunny Goyal | 952e63d | 2017-08-16 04:59:08 -0700 | [diff] [blame] | 47 | protected LauncherAppWidgetProviderInfo() {} |
| 48 | |
| 49 | protected LauncherAppWidgetProviderInfo(Parcel in) { |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 50 | super(in); |
| 51 | } |
| 52 | |
Sunny Goyal | 8ad02b8 | 2016-12-29 13:31:43 -0800 | [diff] [blame] | 53 | public void initSpans(Context context) { |
Sunny Goyal | 87f784c | 2017-01-11 10:48:34 -0800 | [diff] [blame] | 54 | InvariantDeviceProfile idp = LauncherAppState.getIDP(context); |
Sunny Goyal | 233ee96 | 2015-08-03 13:05:01 -0700 | [diff] [blame] | 55 | |
Jon Miranda | 4f1545d | 2017-09-20 11:09:42 -0700 | [diff] [blame^] | 56 | Point landCellSize = idp.landscapeProfile.getCellSize(); |
| 57 | Point portCellSize = idp.portraitProfile.getCellSize(); |
Sunny Goyal | 233ee96 | 2015-08-03 13:05:01 -0700 | [diff] [blame] | 58 | |
| 59 | // Always assume we're working with the smallest span to make sure we |
| 60 | // reserve enough space in both orientations. |
Jon Miranda | 4f1545d | 2017-09-20 11:09:42 -0700 | [diff] [blame^] | 61 | float smallestCellWidth = Math.min(landCellSize.x, portCellSize.x); |
| 62 | float smallestCellHeight = Math.min(landCellSize.y, portCellSize.y); |
Sunny Goyal | 233ee96 | 2015-08-03 13:05:01 -0700 | [diff] [blame] | 63 | |
| 64 | // We want to account for the extra amount of padding that we are adding to the widget |
| 65 | // to ensure that it gets the full amount of space that it has requested. |
| 66 | Rect widgetPadding = AppWidgetHostView.getDefaultPaddingForWidget( |
Sunny Goyal | 8ad02b8 | 2016-12-29 13:31:43 -0800 | [diff] [blame] | 67 | context, provider, null); |
Sunny Goyal | 233ee96 | 2015-08-03 13:05:01 -0700 | [diff] [blame] | 68 | spanX = Math.max(1, (int) Math.ceil( |
| 69 | (minWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth)); |
| 70 | spanY = Math.max(1, (int) Math.ceil( |
| 71 | (minHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight)); |
| 72 | |
| 73 | minSpanX = Math.max(1, (int) Math.ceil( |
| 74 | (minResizeWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth)); |
| 75 | minSpanY = Math.max(1, (int) Math.ceil( |
| 76 | (minResizeHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight)); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | public String getLabel(PackageManager packageManager) { |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 80 | return super.loadLabel(packageManager); |
| 81 | } |
| 82 | |
Sunny Goyal | 952e63d | 2017-08-16 04:59:08 -0700 | [diff] [blame] | 83 | public Point getMinSpans() { |
| 84 | return new Point((resizeMode & RESIZE_HORIZONTAL) != 0 ? minSpanX : -1, |
| 85 | (resizeMode & RESIZE_VERTICAL) != 0 ? minSpanY : -1); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Sunny Goyal | 952e63d | 2017-08-16 04:59:08 -0700 | [diff] [blame] | 88 | public boolean isCustomWidget() { |
| 89 | return provider.getClassName().startsWith(CLS_CUSTOM_WIDGET_PREFIX); |
Sunny Goyal | a52ecb0 | 2016-12-16 15:04:51 -0800 | [diff] [blame] | 90 | } |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 91 | } |