blob: 9ba78530d3df2b5a320f427dff5281c75884de34 [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;
Sunny Goyale5bb7052015-07-27 14:36:07 -07008import android.graphics.Point;
Adam Cohen59400422014-03-05 18:07:04 -08009import android.graphics.drawable.Drawable;
Sunny Goyal70660032015-05-14 00:07:08 -070010import android.os.Build;
Adam Cohen59400422014-03-05 18:07:04 -080011import android.os.Parcel;
12
Adam Cohen59400422014-03-05 18:07:04 -080013/**
14 * This class is a thin wrapper around the framework AppWidgetProviderInfo class. This class affords
15 * a common object for describing both framework provided AppWidgets as well as custom widgets
16 * (who's implementation is owned by the launcher). This object represents a widget type / class,
17 * as opposed to a widget instance, and so should not be confused with {@link LauncherAppWidgetInfo}
18 */
19public class LauncherAppWidgetProviderInfo extends AppWidgetProviderInfo {
20
21 public boolean isCustomWidget = false;
Adam Cohen2e6da152015-05-06 11:42:25 -070022
23 private int mSpanX = -1;
24 private int mSpanY = -1;
25 private int mMinSpanX = -1;
26 private int mMinSpanY = -1;
Adam Cohen59400422014-03-05 18:07:04 -080027
28 public static LauncherAppWidgetProviderInfo fromProviderInfo(Context context,
29 AppWidgetProviderInfo info) {
30
31 // In lieu of a public super copy constructor, we first write the AppWidgetProviderInfo
32 // into a parcel, and then construct a new LauncherAppWidgetProvider info from the
33 // associated super parcel constructor. This allows us to copy non-public members without
34 // using reflection.
35 Parcel p = Parcel.obtain();
36 info.writeToParcel(p, 0);
37 p.setDataPosition(0);
38 LauncherAppWidgetProviderInfo lawpi = new LauncherAppWidgetProviderInfo(p);
Sunny Goyal70660032015-05-14 00:07:08 -070039 p.recycle();
Adam Cohen59400422014-03-05 18:07:04 -080040 return lawpi;
41 }
42
43 public LauncherAppWidgetProviderInfo(Parcel in) {
44 super(in);
45 }
46
47 public LauncherAppWidgetProviderInfo(Context context, CustomAppWidget widget) {
48 isCustomWidget = true;
49
50 provider = new ComponentName(context, widget.getClass().getName());
51 icon = widget.getIcon();
52 label = widget.getLabel();
53 previewImage = widget.getPreviewImage();
54 initialLayout = widget.getWidgetLayout();
55 resizeMode = widget.getResizeMode();
Adam Cohen59400422014-03-05 18:07:04 -080056 }
57
Sunny Goyal70660032015-05-14 00:07:08 -070058 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen59400422014-03-05 18:07:04 -080059 public String getLabel(PackageManager packageManager) {
60 if (isCustomWidget) {
Winson Chung82b016c2015-05-08 17:00:10 -070061 return Utilities.trim(label);
Adam Cohen59400422014-03-05 18:07:04 -080062 }
63 return super.loadLabel(packageManager);
64 }
65
Sunny Goyal70660032015-05-14 00:07:08 -070066 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen59400422014-03-05 18:07:04 -080067 public Drawable getIcon(Context context, IconCache cache) {
68 if (isCustomWidget) {
69 return cache.getFullResIcon(provider.getPackageName(), icon);
70 }
Sunny Goyal53d7ee42015-05-22 12:25:45 -070071 return super.loadIcon(context,
72 LauncherAppState.getInstance().getInvariantDeviceProfile().fillResIconDpi);
Adam Cohen59400422014-03-05 18:07:04 -080073 }
74
Hyunyoung Song3f471442015-04-08 19:01:34 -070075 public String toString(PackageManager pm) {
Adam Cohen59400422014-03-05 18:07:04 -080076 if (isCustomWidget) {
Hyunyoung Song3f471442015-04-08 19:01:34 -070077 return "WidgetProviderInfo(" + provider + ")";
Adam Cohen59400422014-03-05 18:07:04 -080078 }
Adam Cohen2e6da152015-05-06 11:42:25 -070079 return String.format("WidgetProviderInfo provider:%s package:%s short:%s label:%s",
80 provider.toString(), provider.getPackageName(), provider.getShortClassName(), getLabel(pm));
81 }
82
83 public int getSpanX(Launcher launcher) {
84 lazyLoadSpans(launcher);
85 return mSpanX;
86 }
87
88 public int getSpanY(Launcher launcher) {
89 lazyLoadSpans(launcher);
90 return mSpanY;
91 }
92
93 public int getMinSpanX(Launcher launcher) {
94 lazyLoadSpans(launcher);
95 return mMinSpanX;
96 }
97
98 public int getMinSpanY(Launcher launcher) {
99 lazyLoadSpans(launcher);
100 return mMinSpanY;
101 }
102
103 private void lazyLoadSpans(Launcher launcher) {
104 if (mSpanX < 0 || mSpanY < 0 || mMinSpanX < 0 || mMinSpanY < 0) {
105 int[] minResizeSpan = launcher.getMinSpanForWidget(this);
106 int[] span = launcher.getSpanForWidget(this);
107
108 mSpanX = span[0];
109 mSpanY = span[1];
110 mMinSpanX = minResizeSpan[0];
111 mMinSpanY = minResizeSpan[1];
112 }
Adam Cohen59400422014-03-05 18:07:04 -0800113 }
Sunny Goyale5bb7052015-07-27 14:36:07 -0700114
115 public Point getMinSpans(InvariantDeviceProfile idp, Context context) {
116 // Calculate the spans corresponding to any one of the orientations as it should not change
117 // based on orientation.
118 // TODO: Use the max of both profiles
119 int[] minSpans = CellLayout.rectToCell(
120 idp.portraitProfile, context, minResizeWidth, minResizeHeight, null);
121 return new Point(
122 (resizeMode & RESIZE_HORIZONTAL) != 0 ? minSpans[0] : -1,
123 (resizeMode & RESIZE_VERTICAL) != 0 ? minSpans[1] : -1);
124 }
Adam Cohen59400422014-03-05 18:07:04 -0800125 }