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