Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 1 | package com.android.launcher3; |
| 2 | |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 3 | import android.annotation.TargetApi; |
Sunny Goyal | 233ee96 | 2015-08-03 13:05:01 -0700 | [diff] [blame] | 4 | import android.appwidget.AppWidgetHostView; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 5 | import android.appwidget.AppWidgetProviderInfo; |
| 6 | import android.content.ComponentName; |
| 7 | import android.content.Context; |
| 8 | import android.content.pm.PackageManager; |
Sunny Goyal | e5bb705 | 2015-07-27 14:36:07 -0700 | [diff] [blame] | 9 | import android.graphics.Point; |
Sunny Goyal | 233ee96 | 2015-08-03 13:05:01 -0700 | [diff] [blame] | 10 | import android.graphics.Rect; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 11 | import android.graphics.drawable.Drawable; |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 12 | import android.os.Build; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 13 | import android.os.Parcel; |
| 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) { |
| 32 | |
| 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 | LauncherAppWidgetProviderInfo lawpi = new LauncherAppWidgetProviderInfo(p); |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 41 | p.recycle(); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 42 | return lawpi; |
| 43 | } |
| 44 | |
| 45 | public LauncherAppWidgetProviderInfo(Parcel in) { |
| 46 | super(in); |
Sunny Goyal | 233ee96 | 2015-08-03 13:05:01 -0700 | [diff] [blame] | 47 | initSpans(); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | public LauncherAppWidgetProviderInfo(Context context, CustomAppWidget widget) { |
| 51 | isCustomWidget = true; |
| 52 | |
| 53 | provider = new ComponentName(context, widget.getClass().getName()); |
| 54 | icon = widget.getIcon(); |
| 55 | label = widget.getLabel(); |
| 56 | previewImage = widget.getPreviewImage(); |
| 57 | initialLayout = widget.getWidgetLayout(); |
| 58 | resizeMode = widget.getResizeMode(); |
Sunny Goyal | 233ee96 | 2015-08-03 13:05:01 -0700 | [diff] [blame] | 59 | initSpans(); |
| 60 | } |
| 61 | |
| 62 | private void initSpans() { |
| 63 | LauncherAppState app = LauncherAppState.getInstance(); |
| 64 | InvariantDeviceProfile idp = app.getInvariantDeviceProfile(); |
| 65 | |
| 66 | // We only care out the cell size, which is independent of the the layout direction. |
| 67 | Rect paddingLand = idp.landscapeProfile.getWorkspacePadding(false /* isLayoutRtl */); |
| 68 | Rect paddingPort = idp.portraitProfile.getWorkspacePadding(false /* isLayoutRtl */); |
| 69 | |
| 70 | // Always assume we're working with the smallest span to make sure we |
| 71 | // reserve enough space in both orientations. |
| 72 | float smallestCellWidth = DeviceProfile.calculateCellWidth(Math.min( |
| 73 | idp.landscapeProfile.widthPx - paddingLand.left - paddingLand.right, |
| 74 | idp.portraitProfile.widthPx - paddingPort.left - paddingPort.right), |
| 75 | idp.numColumns); |
| 76 | float smallestCellHeight = DeviceProfile.calculateCellWidth(Math.min( |
| 77 | idp.landscapeProfile.heightPx - paddingLand.top - paddingLand.bottom, |
| 78 | idp.portraitProfile.heightPx - paddingPort.top - paddingPort.bottom), |
| 79 | idp.numRows); |
| 80 | |
| 81 | // We want to account for the extra amount of padding that we are adding to the widget |
| 82 | // to ensure that it gets the full amount of space that it has requested. |
| 83 | Rect widgetPadding = AppWidgetHostView.getDefaultPaddingForWidget( |
| 84 | app.getContext(), provider, null); |
| 85 | spanX = Math.max(1, (int) Math.ceil( |
| 86 | (minWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth)); |
| 87 | spanY = Math.max(1, (int) Math.ceil( |
| 88 | (minHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight)); |
| 89 | |
| 90 | minSpanX = Math.max(1, (int) Math.ceil( |
| 91 | (minResizeWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth)); |
| 92 | minSpanY = Math.max(1, (int) Math.ceil( |
| 93 | (minResizeHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight)); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 96 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 97 | public String getLabel(PackageManager packageManager) { |
| 98 | if (isCustomWidget) { |
Winson Chung | 82b016c | 2015-05-08 17:00:10 -0700 | [diff] [blame] | 99 | return Utilities.trim(label); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 100 | } |
| 101 | return super.loadLabel(packageManager); |
| 102 | } |
| 103 | |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 104 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 105 | public Drawable getIcon(Context context, IconCache cache) { |
| 106 | if (isCustomWidget) { |
| 107 | return cache.getFullResIcon(provider.getPackageName(), icon); |
| 108 | } |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 109 | return super.loadIcon(context, |
| 110 | LauncherAppState.getInstance().getInvariantDeviceProfile().fillResIconDpi); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 113 | public String toString(PackageManager pm) { |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 114 | if (isCustomWidget) { |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 115 | return "WidgetProviderInfo(" + provider + ")"; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 116 | } |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 117 | return String.format("WidgetProviderInfo provider:%s package:%s short:%s label:%s", |
| 118 | provider.toString(), provider.getPackageName(), provider.getShortClassName(), getLabel(pm)); |
| 119 | } |
| 120 | |
Sunny Goyal | e5bb705 | 2015-07-27 14:36:07 -0700 | [diff] [blame] | 121 | public Point getMinSpans(InvariantDeviceProfile idp, Context context) { |
Sunny Goyal | e5bb705 | 2015-07-27 14:36:07 -0700 | [diff] [blame] | 122 | return new Point( |
Sunny Goyal | 233ee96 | 2015-08-03 13:05:01 -0700 | [diff] [blame] | 123 | (resizeMode & RESIZE_HORIZONTAL) != 0 ? minSpanX : -1, |
| 124 | (resizeMode & RESIZE_VERTICAL) != 0 ? minSpanY : -1); |
Sunny Goyal | e5bb705 | 2015-07-27 14:36:07 -0700 | [diff] [blame] | 125 | } |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 126 | } |