blob: 01e26241ebb2ae2259ef07b7b42d055c96838584 [file] [log] [blame]
Adam Cohen59400422014-03-05 18:07:04 -08001package com.android.launcher3;
2
Sunny Goyal233ee962015-08-03 13:05:01 -07003import android.appwidget.AppWidgetHostView;
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;
Sunny Goyal233ee962015-08-03 13:05:01 -07009import android.graphics.Rect;
Adam Cohen59400422014-03-05 18:07:04 -080010import android.graphics.drawable.Drawable;
11import android.os.Parcel;
Sunny Goyala52ecb02016-12-16 15:04:51 -080012import android.os.Process;
13import android.os.UserHandle;
Adam Cohen59400422014-03-05 18:07:04 -080014
Adam Cohen59400422014-03-05 18:07:04 -080015/**
16 * This class is a thin wrapper around the framework AppWidgetProviderInfo class. This class affords
17 * a common object for describing both framework provided AppWidgets as well as custom widgets
18 * (who's implementation is owned by the launcher). This object represents a widget type / class,
19 * as opposed to a widget instance, and so should not be confused with {@link LauncherAppWidgetInfo}
20 */
21public class LauncherAppWidgetProviderInfo extends AppWidgetProviderInfo {
22
23 public boolean isCustomWidget = false;
Adam Cohen2e6da152015-05-06 11:42:25 -070024
Sunny Goyal233ee962015-08-03 13:05:01 -070025 public int spanX;
26 public int spanY;
27 public int minSpanX;
28 public int minSpanY;
Adam Cohen59400422014-03-05 18:07:04 -080029
30 public static LauncherAppWidgetProviderInfo fromProviderInfo(Context context,
31 AppWidgetProviderInfo info) {
32
33 // In lieu of a public super copy constructor, we first write the AppWidgetProviderInfo
34 // into a parcel, and then construct a new LauncherAppWidgetProvider info from the
35 // associated super parcel constructor. This allows us to copy non-public members without
36 // using reflection.
37 Parcel p = Parcel.obtain();
38 info.writeToParcel(p, 0);
39 p.setDataPosition(0);
40 LauncherAppWidgetProviderInfo lawpi = new LauncherAppWidgetProviderInfo(p);
Sunny Goyal70660032015-05-14 00:07:08 -070041 p.recycle();
Adam Cohen59400422014-03-05 18:07:04 -080042 return lawpi;
43 }
44
45 public LauncherAppWidgetProviderInfo(Parcel in) {
46 super(in);
Sunny Goyal233ee962015-08-03 13:05:01 -070047 initSpans();
Adam Cohen59400422014-03-05 18:07:04 -080048 }
49
50 public LauncherAppWidgetProviderInfo(Context context, CustomAppWidget widget) {
51 isCustomWidget = true;
52
53 provider = new ComponentName(context, widget.getClass().getName());
54 icon = widget.getIcon();
55 label = widget.getLabel();
56 previewImage = widget.getPreviewImage();
57 initialLayout = widget.getWidgetLayout();
58 resizeMode = widget.getResizeMode();
Sunny Goyal233ee962015-08-03 13:05:01 -070059 initSpans();
60 }
61
Sunny Goyalec882042015-10-05 10:36:54 -070062 public void initSpans() {
Sunny Goyal233ee962015-08-03 13:05:01 -070063 LauncherAppState app = LauncherAppState.getInstance();
64 InvariantDeviceProfile idp = app.getInvariantDeviceProfile();
65
Sunny Goyal6c2975e2016-07-06 09:47:56 -070066 Point paddingLand = idp.landscapeProfile.getTotalWorkspacePadding();
67 Point paddingPort = idp.portraitProfile.getTotalWorkspacePadding();
Sunny Goyal233ee962015-08-03 13:05:01 -070068
69 // Always assume we're working with the smallest span to make sure we
70 // reserve enough space in both orientations.
71 float smallestCellWidth = DeviceProfile.calculateCellWidth(Math.min(
Sunny Goyal6c2975e2016-07-06 09:47:56 -070072 idp.landscapeProfile.widthPx - paddingLand.x,
73 idp.portraitProfile.widthPx - paddingPort.x),
Sunny Goyal233ee962015-08-03 13:05:01 -070074 idp.numColumns);
75 float smallestCellHeight = DeviceProfile.calculateCellWidth(Math.min(
Sunny Goyal6c2975e2016-07-06 09:47:56 -070076 idp.landscapeProfile.heightPx - paddingLand.y,
77 idp.portraitProfile.heightPx - paddingPort.y),
Sunny Goyal233ee962015-08-03 13:05:01 -070078 idp.numRows);
79
80 // We want to account for the extra amount of padding that we are adding to the widget
81 // to ensure that it gets the full amount of space that it has requested.
82 Rect widgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(
83 app.getContext(), provider, null);
84 spanX = Math.max(1, (int) Math.ceil(
85 (minWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth));
86 spanY = Math.max(1, (int) Math.ceil(
87 (minHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight));
88
89 minSpanX = Math.max(1, (int) Math.ceil(
90 (minResizeWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth));
91 minSpanY = Math.max(1, (int) Math.ceil(
92 (minResizeHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight));
Adam Cohen59400422014-03-05 18:07:04 -080093 }
94
95 public String getLabel(PackageManager packageManager) {
96 if (isCustomWidget) {
Winson Chung82b016c2015-05-08 17:00:10 -070097 return Utilities.trim(label);
Adam Cohen59400422014-03-05 18:07:04 -080098 }
99 return super.loadLabel(packageManager);
100 }
101
102 public Drawable getIcon(Context context, IconCache cache) {
103 if (isCustomWidget) {
104 return cache.getFullResIcon(provider.getPackageName(), icon);
105 }
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700106 return super.loadIcon(context,
107 LauncherAppState.getInstance().getInvariantDeviceProfile().fillResIconDpi);
Adam Cohen59400422014-03-05 18:07:04 -0800108 }
109
Hyunyoung Song3f471442015-04-08 19:01:34 -0700110 public String toString(PackageManager pm) {
Adam Cohen59400422014-03-05 18:07:04 -0800111 if (isCustomWidget) {
Hyunyoung Song3f471442015-04-08 19:01:34 -0700112 return "WidgetProviderInfo(" + provider + ")";
Adam Cohen59400422014-03-05 18:07:04 -0800113 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700114 return String.format("WidgetProviderInfo provider:%s package:%s short:%s label:%s",
115 provider.toString(), provider.getPackageName(), provider.getShortClassName(), getLabel(pm));
116 }
117
Sunny Goyale5bb7052015-07-27 14:36:07 -0700118 public Point getMinSpans(InvariantDeviceProfile idp, Context context) {
Sunny Goyale5bb7052015-07-27 14:36:07 -0700119 return new Point(
Sunny Goyal233ee962015-08-03 13:05:01 -0700120 (resizeMode & RESIZE_HORIZONTAL) != 0 ? minSpanX : -1,
121 (resizeMode & RESIZE_VERTICAL) != 0 ? minSpanY : -1);
Sunny Goyale5bb7052015-07-27 14:36:07 -0700122 }
Sunny Goyala52ecb02016-12-16 15:04:51 -0800123
124 public UserHandle getUser() {
125 return isCustomWidget ? Process.myUserHandle() : getProfile();
126 }
Adam Cohen59400422014-03-05 18:07:04 -0800127 }