blob: e7f49b2ce4a9f3335c437be672de936edab514cf [file] [log] [blame]
Adam Cohen59400422014-03-05 18:07:04 -08001package com.android.launcher3;
2
3import android.appwidget.AppWidgetProviderInfo;
4import android.content.ComponentName;
5import android.content.Context;
6import android.content.pm.PackageManager;
7import android.graphics.drawable.Drawable;
8import android.os.Parcel;
9
10import java.lang.reflect.Field;
11
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 */
18public class LauncherAppWidgetProviderInfo extends AppWidgetProviderInfo {
19
20 public boolean isCustomWidget = false;
21 int spanX = -1;
22 int spanY = -1;
23 int minSpanX = -1;
24 int minSpanY = -1;
25
26 public static LauncherAppWidgetProviderInfo fromProviderInfo(Context context,
27 AppWidgetProviderInfo info) {
28
29 // In lieu of a public super copy constructor, we first write the AppWidgetProviderInfo
30 // into a parcel, and then construct a new LauncherAppWidgetProvider info from the
31 // associated super parcel constructor. This allows us to copy non-public members without
32 // using reflection.
33 Parcel p = Parcel.obtain();
34 info.writeToParcel(p, 0);
35 p.setDataPosition(0);
36 LauncherAppWidgetProviderInfo lawpi = new LauncherAppWidgetProviderInfo(p);
37
38 int[] minResizeSpan = Launcher.getMinSpanForWidget(context, lawpi);
39 int[] span = Launcher.getSpanForWidget(context, lawpi);
40
41 lawpi.spanX = span[0];
42 lawpi.spanY = span[1];
43 lawpi.minSpanX = minResizeSpan[0];
44 lawpi.minSpanY = minResizeSpan[1];
45
46 return lawpi;
47 }
48
49 public LauncherAppWidgetProviderInfo(Parcel in) {
50 super(in);
51 }
52
53 public LauncherAppWidgetProviderInfo(Context context, CustomAppWidget widget) {
54 isCustomWidget = true;
55
56 provider = new ComponentName(context, widget.getClass().getName());
57 icon = widget.getIcon();
58 label = widget.getLabel();
59 previewImage = widget.getPreviewImage();
60 initialLayout = widget.getWidgetLayout();
61 resizeMode = widget.getResizeMode();
62
63 spanX = widget.getSpanX();
64 spanY = widget.getSpanY();
65 minSpanX = widget.getMinSpanX();
66 minSpanY = widget.getMinSpanY();
67 }
68
69 public String getLabel(PackageManager packageManager) {
70 if (isCustomWidget) {
71 return label.toString().trim();
72 }
73 return super.loadLabel(packageManager);
74 }
75
76 public Drawable getIcon(Context context, IconCache cache) {
77 if (isCustomWidget) {
78 return cache.getFullResIcon(provider.getPackageName(), icon);
79 }
80 return super.loadIcon(context, cache.getFullResIconDpi());
81 }
82
83 public String toString() {
84 if (isCustomWidget) {
85 return "LauncherAppWidgetProviderInfo(" + provider + ")";
86 }
87 return super.toString();
88 }
89 }