blob: 80758c99b9c90492888eed60b53525ecc4bcf570 [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;
Adam Cohen59400422014-03-05 18:07:04 -08005import android.content.Context;
6import android.content.pm.PackageManager;
Sunny Goyale5bb7052015-07-27 14:36:07 -07007import android.graphics.Point;
Sunny Goyal233ee962015-08-03 13:05:01 -07008import android.graphics.Rect;
Adam Cohen59400422014-03-05 18:07:04 -08009import android.os.Parcel;
10
Adam Cohen59400422014-03-05 18:07:04 -080011/**
12 * This class is a thin wrapper around the framework AppWidgetProviderInfo class. This class affords
13 * a common object for describing both framework provided AppWidgets as well as custom widgets
14 * (who's implementation is owned by the launcher). This object represents a widget type / class,
15 * as opposed to a widget instance, and so should not be confused with {@link LauncherAppWidgetInfo}
16 */
17public class LauncherAppWidgetProviderInfo extends AppWidgetProviderInfo {
18
Sunny Goyal952e63d2017-08-16 04:59:08 -070019 public static final String CLS_CUSTOM_WIDGET_PREFIX = "#custom-widget-";
Adam Cohen2e6da152015-05-06 11:42:25 -070020
Sunny Goyal233ee962015-08-03 13:05:01 -070021 public int spanX;
22 public int spanY;
23 public int minSpanX;
24 public int minSpanY;
Adam Cohen59400422014-03-05 18:07:04 -080025
26 public static LauncherAppWidgetProviderInfo fromProviderInfo(Context context,
27 AppWidgetProviderInfo info) {
Sunny Goyal8ad02b82016-12-29 13:31:43 -080028 final LauncherAppWidgetProviderInfo launcherInfo;
29 if (info instanceof LauncherAppWidgetProviderInfo) {
30 launcherInfo = (LauncherAppWidgetProviderInfo) info;
31 } else {
Adam Cohen59400422014-03-05 18:07:04 -080032
Sunny Goyal8ad02b82016-12-29 13:31:43 -080033 // 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 launcherInfo = new LauncherAppWidgetProviderInfo(p);
41 p.recycle();
42 }
43 launcherInfo.initSpans(context);
44 return launcherInfo;
Adam Cohen59400422014-03-05 18:07:04 -080045 }
46
Sunny Goyal952e63d2017-08-16 04:59:08 -070047 protected LauncherAppWidgetProviderInfo() {}
48
49 protected LauncherAppWidgetProviderInfo(Parcel in) {
Adam Cohen59400422014-03-05 18:07:04 -080050 super(in);
51 }
52
Sunny Goyal8ad02b82016-12-29 13:31:43 -080053 public void initSpans(Context context) {
Sunny Goyal87f784c2017-01-11 10:48:34 -080054 InvariantDeviceProfile idp = LauncherAppState.getIDP(context);
Sunny Goyal233ee962015-08-03 13:05:01 -070055
Sunny Goyal6c2975e2016-07-06 09:47:56 -070056 Point paddingLand = idp.landscapeProfile.getTotalWorkspacePadding();
57 Point paddingPort = idp.portraitProfile.getTotalWorkspacePadding();
Sunny Goyal233ee962015-08-03 13:05:01 -070058
59 // Always assume we're working with the smallest span to make sure we
60 // reserve enough space in both orientations.
61 float smallestCellWidth = DeviceProfile.calculateCellWidth(Math.min(
Sunny Goyal6c2975e2016-07-06 09:47:56 -070062 idp.landscapeProfile.widthPx - paddingLand.x,
63 idp.portraitProfile.widthPx - paddingPort.x),
Sunny Goyal233ee962015-08-03 13:05:01 -070064 idp.numColumns);
65 float smallestCellHeight = DeviceProfile.calculateCellWidth(Math.min(
Sunny Goyal6c2975e2016-07-06 09:47:56 -070066 idp.landscapeProfile.heightPx - paddingLand.y,
67 idp.portraitProfile.heightPx - paddingPort.y),
Sunny Goyal233ee962015-08-03 13:05:01 -070068 idp.numRows);
69
70 // We want to account for the extra amount of padding that we are adding to the widget
71 // to ensure that it gets the full amount of space that it has requested.
72 Rect widgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(
Sunny Goyal8ad02b82016-12-29 13:31:43 -080073 context, provider, null);
Sunny Goyal233ee962015-08-03 13:05:01 -070074 spanX = Math.max(1, (int) Math.ceil(
75 (minWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth));
76 spanY = Math.max(1, (int) Math.ceil(
77 (minHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight));
78
79 minSpanX = Math.max(1, (int) Math.ceil(
80 (minResizeWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth));
81 minSpanY = Math.max(1, (int) Math.ceil(
82 (minResizeHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight));
Adam Cohen59400422014-03-05 18:07:04 -080083 }
84
85 public String getLabel(PackageManager packageManager) {
Adam Cohen59400422014-03-05 18:07:04 -080086 return super.loadLabel(packageManager);
87 }
88
Sunny Goyal952e63d2017-08-16 04:59:08 -070089 public Point getMinSpans() {
90 return new Point((resizeMode & RESIZE_HORIZONTAL) != 0 ? minSpanX : -1,
91 (resizeMode & RESIZE_VERTICAL) != 0 ? minSpanY : -1);
Adam Cohen59400422014-03-05 18:07:04 -080092 }
93
Sunny Goyal952e63d2017-08-16 04:59:08 -070094 public boolean isCustomWidget() {
95 return provider.getClassName().startsWith(CLS_CUSTOM_WIDGET_PREFIX);
Sunny Goyala52ecb02016-12-16 15:04:51 -080096 }
Winson Chung1054d4e2018-03-05 19:39:21 +000097
98 public int getWidgetFeatures() {
99 if (Utilities.ATLEAST_P) {
100 return widgetFeatures;
101 } else {
102 return 0;
103 }
104 }
Adam Cohen59400422014-03-05 18:07:04 -0800105 }