blob: af680f2471a5b5f6b3b042e8fc0a9bfe12f5f353 [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
Adam Cohen59400422014-03-05 18:07:04 -080010/**
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 */
16public class LauncherAppWidgetProviderInfo extends AppWidgetProviderInfo {
17
18 public boolean isCustomWidget = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -070019 public int spanX = -1;
20 public int spanY = -1;
21 public int minSpanX = -1;
22 public int minSpanY = -1;
Adam Cohen59400422014-03-05 18:07:04 -080023
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 Chung82b016c2015-05-08 17:00:10 -070069 return Utilities.trim(label);
Adam Cohen59400422014-03-05 18:07:04 -080070 }
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 Song3f471442015-04-08 19:01:34 -070081 public String toString(PackageManager pm) {
Adam Cohen59400422014-03-05 18:07:04 -080082 if (isCustomWidget) {
Hyunyoung Song3f471442015-04-08 19:01:34 -070083 return "WidgetProviderInfo(" + provider + ")";
Adam Cohen59400422014-03-05 18:07:04 -080084 }
Hyunyoung Song3f471442015-04-08 19:01:34 -070085 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 Cohen59400422014-03-05 18:07:04 -080087 }
88 }