blob: 85af92f30a60e477b6d929697f485b9db739e6b9 [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 }
Sunny Goyal53d7ee42015-05-22 12:25:45 -070070 return super.loadIcon(context,
71 LauncherAppState.getInstance().getInvariantDeviceProfile().fillResIconDpi);
Adam Cohen59400422014-03-05 18:07:04 -080072 }
73
Hyunyoung Song3f471442015-04-08 19:01:34 -070074 public String toString(PackageManager pm) {
Adam Cohen59400422014-03-05 18:07:04 -080075 if (isCustomWidget) {
Hyunyoung Song3f471442015-04-08 19:01:34 -070076 return "WidgetProviderInfo(" + provider + ")";
Adam Cohen59400422014-03-05 18:07:04 -080077 }
Adam Cohen2e6da152015-05-06 11:42:25 -070078 return String.format("WidgetProviderInfo provider:%s package:%s short:%s label:%s",
79 provider.toString(), provider.getPackageName(), provider.getShortClassName(), getLabel(pm));
80 }
81
82 public int getSpanX(Launcher launcher) {
83 lazyLoadSpans(launcher);
84 return mSpanX;
85 }
86
87 public int getSpanY(Launcher launcher) {
88 lazyLoadSpans(launcher);
89 return mSpanY;
90 }
91
92 public int getMinSpanX(Launcher launcher) {
93 lazyLoadSpans(launcher);
94 return mMinSpanX;
95 }
96
97 public int getMinSpanY(Launcher launcher) {
98 lazyLoadSpans(launcher);
99 return mMinSpanY;
100 }
101
102 private void lazyLoadSpans(Launcher launcher) {
103 if (mSpanX < 0 || mSpanY < 0 || mMinSpanX < 0 || mMinSpanY < 0) {
104 int[] minResizeSpan = launcher.getMinSpanForWidget(this);
105 int[] span = launcher.getSpanForWidget(this);
106
107 mSpanX = span[0];
108 mSpanY = span[1];
109 mMinSpanX = minResizeSpan[0];
110 mMinSpanY = minResizeSpan[1];
111 }
Adam Cohen59400422014-03-05 18:07:04 -0800112 }
113 }