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