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