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; |
| 5 | import android.content.ComponentName; |
| 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; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 10 | import android.graphics.drawable.Drawable; |
| 11 | import android.os.Parcel; |
Sunny Goyal | a52ecb0 | 2016-12-16 15:04:51 -0800 | [diff] [blame] | 12 | import android.os.Process; |
| 13 | import android.os.UserHandle; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 14 | |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 15 | /** |
| 16 | * This class is a thin wrapper around the framework AppWidgetProviderInfo class. This class affords |
| 17 | * a common object for describing both framework provided AppWidgets as well as custom widgets |
| 18 | * (who's implementation is owned by the launcher). This object represents a widget type / class, |
| 19 | * as opposed to a widget instance, and so should not be confused with {@link LauncherAppWidgetInfo} |
| 20 | */ |
| 21 | public class LauncherAppWidgetProviderInfo extends AppWidgetProviderInfo { |
| 22 | |
| 23 | public boolean isCustomWidget = false; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 24 | |
Sunny Goyal | 233ee96 | 2015-08-03 13:05:01 -0700 | [diff] [blame] | 25 | public int spanX; |
| 26 | public int spanY; |
| 27 | public int minSpanX; |
| 28 | public int minSpanY; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 29 | |
| 30 | public static LauncherAppWidgetProviderInfo fromProviderInfo(Context context, |
| 31 | AppWidgetProviderInfo info) { |
Sunny Goyal | 8ad02b8 | 2016-12-29 13:31:43 -0800 | [diff] [blame^] | 32 | final LauncherAppWidgetProviderInfo launcherInfo; |
| 33 | if (info instanceof LauncherAppWidgetProviderInfo) { |
| 34 | launcherInfo = (LauncherAppWidgetProviderInfo) info; |
| 35 | } else { |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 36 | |
Sunny Goyal | 8ad02b8 | 2016-12-29 13:31:43 -0800 | [diff] [blame^] | 37 | // In lieu of a public super copy constructor, we first write the AppWidgetProviderInfo |
| 38 | // into a parcel, and then construct a new LauncherAppWidgetProvider info from the |
| 39 | // associated super parcel constructor. This allows us to copy non-public members without |
| 40 | // using reflection. |
| 41 | Parcel p = Parcel.obtain(); |
| 42 | info.writeToParcel(p, 0); |
| 43 | p.setDataPosition(0); |
| 44 | launcherInfo = new LauncherAppWidgetProviderInfo(p); |
| 45 | p.recycle(); |
| 46 | } |
| 47 | launcherInfo.initSpans(context); |
| 48 | return launcherInfo; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Sunny Goyal | 8ad02b8 | 2016-12-29 13:31:43 -0800 | [diff] [blame^] | 51 | private LauncherAppWidgetProviderInfo(Parcel in) { |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 52 | super(in); |
| 53 | } |
| 54 | |
| 55 | public LauncherAppWidgetProviderInfo(Context context, CustomAppWidget widget) { |
| 56 | isCustomWidget = true; |
| 57 | |
| 58 | provider = new ComponentName(context, widget.getClass().getName()); |
| 59 | icon = widget.getIcon(); |
| 60 | label = widget.getLabel(); |
| 61 | previewImage = widget.getPreviewImage(); |
| 62 | initialLayout = widget.getWidgetLayout(); |
| 63 | resizeMode = widget.getResizeMode(); |
Sunny Goyal | 8ad02b8 | 2016-12-29 13:31:43 -0800 | [diff] [blame^] | 64 | initSpans(context); |
Sunny Goyal | 233ee96 | 2015-08-03 13:05:01 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Sunny Goyal | 8ad02b8 | 2016-12-29 13:31:43 -0800 | [diff] [blame^] | 67 | public void initSpans(Context context) { |
| 68 | InvariantDeviceProfile idp = LauncherAppState.getInstance().getInvariantDeviceProfile(); |
Sunny Goyal | 233ee96 | 2015-08-03 13:05:01 -0700 | [diff] [blame] | 69 | |
Sunny Goyal | 6c2975e | 2016-07-06 09:47:56 -0700 | [diff] [blame] | 70 | Point paddingLand = idp.landscapeProfile.getTotalWorkspacePadding(); |
| 71 | Point paddingPort = idp.portraitProfile.getTotalWorkspacePadding(); |
Sunny Goyal | 233ee96 | 2015-08-03 13:05:01 -0700 | [diff] [blame] | 72 | |
| 73 | // Always assume we're working with the smallest span to make sure we |
| 74 | // reserve enough space in both orientations. |
| 75 | float smallestCellWidth = DeviceProfile.calculateCellWidth(Math.min( |
Sunny Goyal | 6c2975e | 2016-07-06 09:47:56 -0700 | [diff] [blame] | 76 | idp.landscapeProfile.widthPx - paddingLand.x, |
| 77 | idp.portraitProfile.widthPx - paddingPort.x), |
Sunny Goyal | 233ee96 | 2015-08-03 13:05:01 -0700 | [diff] [blame] | 78 | idp.numColumns); |
| 79 | float smallestCellHeight = DeviceProfile.calculateCellWidth(Math.min( |
Sunny Goyal | 6c2975e | 2016-07-06 09:47:56 -0700 | [diff] [blame] | 80 | idp.landscapeProfile.heightPx - paddingLand.y, |
| 81 | idp.portraitProfile.heightPx - paddingPort.y), |
Sunny Goyal | 233ee96 | 2015-08-03 13:05:01 -0700 | [diff] [blame] | 82 | idp.numRows); |
| 83 | |
| 84 | // We want to account for the extra amount of padding that we are adding to the widget |
| 85 | // to ensure that it gets the full amount of space that it has requested. |
| 86 | Rect widgetPadding = AppWidgetHostView.getDefaultPaddingForWidget( |
Sunny Goyal | 8ad02b8 | 2016-12-29 13:31:43 -0800 | [diff] [blame^] | 87 | context, provider, null); |
Sunny Goyal | 233ee96 | 2015-08-03 13:05:01 -0700 | [diff] [blame] | 88 | spanX = Math.max(1, (int) Math.ceil( |
| 89 | (minWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth)); |
| 90 | spanY = Math.max(1, (int) Math.ceil( |
| 91 | (minHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight)); |
| 92 | |
| 93 | minSpanX = Math.max(1, (int) Math.ceil( |
| 94 | (minResizeWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth)); |
| 95 | minSpanY = Math.max(1, (int) Math.ceil( |
| 96 | (minResizeHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight)); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | public String getLabel(PackageManager packageManager) { |
| 100 | if (isCustomWidget) { |
Winson Chung | 82b016c | 2015-05-08 17:00:10 -0700 | [diff] [blame] | 101 | return Utilities.trim(label); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 102 | } |
| 103 | return super.loadLabel(packageManager); |
| 104 | } |
| 105 | |
| 106 | public Drawable getIcon(Context context, IconCache cache) { |
| 107 | if (isCustomWidget) { |
| 108 | return cache.getFullResIcon(provider.getPackageName(), icon); |
| 109 | } |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 110 | return super.loadIcon(context, |
| 111 | LauncherAppState.getInstance().getInvariantDeviceProfile().fillResIconDpi); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 114 | public String toString(PackageManager pm) { |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 115 | if (isCustomWidget) { |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 116 | return "WidgetProviderInfo(" + provider + ")"; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 117 | } |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 118 | return String.format("WidgetProviderInfo provider:%s package:%s short:%s label:%s", |
| 119 | provider.toString(), provider.getPackageName(), provider.getShortClassName(), getLabel(pm)); |
| 120 | } |
| 121 | |
Sunny Goyal | e5bb705 | 2015-07-27 14:36:07 -0700 | [diff] [blame] | 122 | public Point getMinSpans(InvariantDeviceProfile idp, Context context) { |
Sunny Goyal | e5bb705 | 2015-07-27 14:36:07 -0700 | [diff] [blame] | 123 | return new Point( |
Sunny Goyal | 233ee96 | 2015-08-03 13:05:01 -0700 | [diff] [blame] | 124 | (resizeMode & RESIZE_HORIZONTAL) != 0 ? minSpanX : -1, |
| 125 | (resizeMode & RESIZE_VERTICAL) != 0 ? minSpanY : -1); |
Sunny Goyal | e5bb705 | 2015-07-27 14:36:07 -0700 | [diff] [blame] | 126 | } |
Sunny Goyal | a52ecb0 | 2016-12-16 15:04:51 -0800 | [diff] [blame] | 127 | |
| 128 | public UserHandle getUser() { |
| 129 | return isCustomWidget ? Process.myUserHandle() : getProfile(); |
| 130 | } |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 131 | } |