blob: 7ca4fe325c2526e45b5f4a8d714269e5baba4919 [file] [log] [blame]
Adam Cohen59400422014-03-05 18:07:04 -08001package com.android.launcher3;
2
Sunny Goyal70660032015-05-14 00:07:08 -07003import android.annotation.TargetApi;
Adam Cohen59400422014-03-05 18:07:04 -08004import android.appwidget.AppWidgetProviderInfo;
5import android.content.ComponentName;
6import android.content.Context;
7import android.content.pm.PackageManager;
8import android.graphics.drawable.Drawable;
Sunny Goyal70660032015-05-14 00:07:08 -07009import android.os.Build;
Adam Cohen59400422014-03-05 18:07:04 -080010import android.os.Parcel;
11
Adam Cohen59400422014-03-05 18:07:04 -080012/**
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;
Adam Cohen2e6da152015-05-06 11:42:25 -070021
22 private int mSpanX = -1;
23 private int mSpanY = -1;
24 private int mMinSpanX = -1;
25 private int mMinSpanY = -1;
Adam Cohen59400422014-03-05 18:07:04 -080026
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 Goyal70660032015-05-14 00:07:08 -070038 p.recycle();
Adam Cohen59400422014-03-05 18:07:04 -080039 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 Cohen59400422014-03-05 18:07:04 -080055 }
56
Sunny Goyal70660032015-05-14 00:07:08 -070057 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen59400422014-03-05 18:07:04 -080058 public String getLabel(PackageManager packageManager) {
59 if (isCustomWidget) {
Winson Chung82b016c2015-05-08 17:00:10 -070060 return Utilities.trim(label);
Adam Cohen59400422014-03-05 18:07:04 -080061 }
62 return super.loadLabel(packageManager);
63 }
64
Sunny Goyal70660032015-05-14 00:07:08 -070065 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen59400422014-03-05 18:07:04 -080066 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 Song3f471442015-04-08 19:01:34 -070073 public String toString(PackageManager pm) {
Adam Cohen59400422014-03-05 18:07:04 -080074 if (isCustomWidget) {
Hyunyoung Song3f471442015-04-08 19:01:34 -070075 return "WidgetProviderInfo(" + provider + ")";
Adam Cohen59400422014-03-05 18:07:04 -080076 }
Adam Cohen2e6da152015-05-06 11:42:25 -070077 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 Cohen59400422014-03-05 18:07:04 -0800111 }
112 }