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; |
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; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 9 | import android.graphics.drawable.Drawable; |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 10 | import android.os.Build; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 11 | import android.os.Parcel; |
| 12 | |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 13 | /** |
| 14 | * This class is a thin wrapper around the framework AppWidgetProviderInfo class. This class affords |
| 15 | * a common object for describing both framework provided AppWidgets as well as custom widgets |
| 16 | * (who's implementation is owned by the launcher). This object represents a widget type / class, |
| 17 | * as opposed to a widget instance, and so should not be confused with {@link LauncherAppWidgetInfo} |
| 18 | */ |
| 19 | public class LauncherAppWidgetProviderInfo extends AppWidgetProviderInfo { |
| 20 | |
| 21 | public boolean isCustomWidget = false; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 22 | |
| 23 | private int mSpanX = -1; |
| 24 | private int mSpanY = -1; |
| 25 | private int mMinSpanX = -1; |
| 26 | private int mMinSpanY = -1; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 27 | |
| 28 | public static LauncherAppWidgetProviderInfo fromProviderInfo(Context context, |
| 29 | AppWidgetProviderInfo info) { |
| 30 | |
| 31 | // In lieu of a public super copy constructor, we first write the AppWidgetProviderInfo |
| 32 | // into a parcel, and then construct a new LauncherAppWidgetProvider info from the |
| 33 | // associated super parcel constructor. This allows us to copy non-public members without |
| 34 | // using reflection. |
| 35 | Parcel p = Parcel.obtain(); |
| 36 | info.writeToParcel(p, 0); |
| 37 | p.setDataPosition(0); |
| 38 | LauncherAppWidgetProviderInfo lawpi = new LauncherAppWidgetProviderInfo(p); |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 39 | p.recycle(); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 40 | return lawpi; |
| 41 | } |
| 42 | |
| 43 | public LauncherAppWidgetProviderInfo(Parcel in) { |
| 44 | super(in); |
| 45 | } |
| 46 | |
| 47 | public LauncherAppWidgetProviderInfo(Context context, CustomAppWidget widget) { |
| 48 | isCustomWidget = true; |
| 49 | |
| 50 | provider = new ComponentName(context, widget.getClass().getName()); |
| 51 | icon = widget.getIcon(); |
| 52 | label = widget.getLabel(); |
| 53 | previewImage = widget.getPreviewImage(); |
| 54 | initialLayout = widget.getWidgetLayout(); |
| 55 | resizeMode = widget.getResizeMode(); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 58 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 59 | public String getLabel(PackageManager packageManager) { |
| 60 | if (isCustomWidget) { |
Winson Chung | 82b016c | 2015-05-08 17:00:10 -0700 | [diff] [blame] | 61 | return Utilities.trim(label); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 62 | } |
| 63 | return super.loadLabel(packageManager); |
| 64 | } |
| 65 | |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 66 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 67 | public Drawable getIcon(Context context, IconCache cache) { |
| 68 | if (isCustomWidget) { |
| 69 | return cache.getFullResIcon(provider.getPackageName(), icon); |
| 70 | } |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 71 | return super.loadIcon(context, |
| 72 | LauncherAppState.getInstance().getInvariantDeviceProfile().fillResIconDpi); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 75 | public String toString(PackageManager pm) { |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 76 | if (isCustomWidget) { |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 77 | return "WidgetProviderInfo(" + provider + ")"; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 78 | } |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 79 | return String.format("WidgetProviderInfo provider:%s package:%s short:%s label:%s", |
| 80 | provider.toString(), provider.getPackageName(), provider.getShortClassName(), getLabel(pm)); |
| 81 | } |
| 82 | |
| 83 | public int getSpanX(Launcher launcher) { |
| 84 | lazyLoadSpans(launcher); |
| 85 | return mSpanX; |
| 86 | } |
| 87 | |
| 88 | public int getSpanY(Launcher launcher) { |
| 89 | lazyLoadSpans(launcher); |
| 90 | return mSpanY; |
| 91 | } |
| 92 | |
| 93 | public int getMinSpanX(Launcher launcher) { |
| 94 | lazyLoadSpans(launcher); |
| 95 | return mMinSpanX; |
| 96 | } |
| 97 | |
| 98 | public int getMinSpanY(Launcher launcher) { |
| 99 | lazyLoadSpans(launcher); |
| 100 | return mMinSpanY; |
| 101 | } |
| 102 | |
| 103 | private void lazyLoadSpans(Launcher launcher) { |
| 104 | if (mSpanX < 0 || mSpanY < 0 || mMinSpanX < 0 || mMinSpanY < 0) { |
| 105 | int[] minResizeSpan = launcher.getMinSpanForWidget(this); |
| 106 | int[] span = launcher.getSpanForWidget(this); |
| 107 | |
| 108 | mSpanX = span[0]; |
| 109 | mSpanY = span[1]; |
| 110 | mMinSpanX = minResizeSpan[0]; |
| 111 | mMinSpanY = minResizeSpan[1]; |
| 112 | } |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 113 | } |
Sunny Goyal | e5bb705 | 2015-07-27 14:36:07 -0700 | [diff] [blame^] | 114 | |
| 115 | public Point getMinSpans(InvariantDeviceProfile idp, Context context) { |
| 116 | // Calculate the spans corresponding to any one of the orientations as it should not change |
| 117 | // based on orientation. |
| 118 | // TODO: Use the max of both profiles |
| 119 | int[] minSpans = CellLayout.rectToCell( |
| 120 | idp.portraitProfile, context, minResizeWidth, minResizeHeight, null); |
| 121 | return new Point( |
| 122 | (resizeMode & RESIZE_HORIZONTAL) != 0 ? minSpans[0] : -1, |
| 123 | (resizeMode & RESIZE_VERTICAL) != 0 ? minSpans[1] : -1); |
| 124 | } |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 125 | } |