blob: 618b5de79ee6a9260c950ffe0951b66ca9d28d6f [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;
Sunny Goyalac8154a2018-09-26 12:00:30 -07005import android.content.ComponentName;
Adam Cohen59400422014-03-05 18:07:04 -08006import 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;
Sunny Goyal18204e42020-02-06 11:28:01 -080010import android.graphics.drawable.Drawable;
Adam Cohen59400422014-03-05 18:07:04 -080011import android.os.Parcel;
Sunny Goyalac8154a2018-09-26 12:00:30 -070012import android.os.UserHandle;
13
Sunny Goyal18204e42020-02-06 11:28:01 -080014import com.android.launcher3.icons.ComponentWithLabelAndIcon;
15import com.android.launcher3.icons.IconCache;
Sunny Goyale396abf2020-04-06 15:11:17 -070016import com.android.launcher3.model.data.LauncherAppWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -080017
Adam Cohen59400422014-03-05 18:07:04 -080018/**
19 * This class is a thin wrapper around the framework AppWidgetProviderInfo class. This class affords
20 * a common object for describing both framework provided AppWidgets as well as custom widgets
21 * (who's implementation is owned by the launcher). This object represents a widget type / class,
22 * as opposed to a widget instance, and so should not be confused with {@link LauncherAppWidgetInfo}
23 */
Sunny Goyalac8154a2018-09-26 12:00:30 -070024public class LauncherAppWidgetProviderInfo extends AppWidgetProviderInfo
Sunny Goyal18204e42020-02-06 11:28:01 -080025 implements ComponentWithLabelAndIcon {
Adam Cohen59400422014-03-05 18:07:04 -080026
Sunny Goyal952e63d2017-08-16 04:59:08 -070027 public static final String CLS_CUSTOM_WIDGET_PREFIX = "#custom-widget-";
Adam Cohen2e6da152015-05-06 11:42:25 -070028
Sunny Goyal233ee962015-08-03 13:05:01 -070029 public int spanX;
30 public int spanY;
31 public int minSpanX;
32 public int minSpanY;
Adam Cohen59400422014-03-05 18:07:04 -080033
34 public static LauncherAppWidgetProviderInfo fromProviderInfo(Context context,
35 AppWidgetProviderInfo info) {
Sunny Goyal8ad02b82016-12-29 13:31:43 -080036 final LauncherAppWidgetProviderInfo launcherInfo;
37 if (info instanceof LauncherAppWidgetProviderInfo) {
38 launcherInfo = (LauncherAppWidgetProviderInfo) info;
39 } else {
Adam Cohen59400422014-03-05 18:07:04 -080040
Sunny Goyal8ad02b82016-12-29 13:31:43 -080041 // In lieu of a public super copy constructor, we first write the AppWidgetProviderInfo
42 // into a parcel, and then construct a new LauncherAppWidgetProvider info from the
43 // associated super parcel constructor. This allows us to copy non-public members without
44 // using reflection.
45 Parcel p = Parcel.obtain();
46 info.writeToParcel(p, 0);
47 p.setDataPosition(0);
48 launcherInfo = new LauncherAppWidgetProviderInfo(p);
49 p.recycle();
50 }
51 launcherInfo.initSpans(context);
52 return launcherInfo;
Adam Cohen59400422014-03-05 18:07:04 -080053 }
54
Sunny Goyal952e63d2017-08-16 04:59:08 -070055 protected LauncherAppWidgetProviderInfo() {}
56
57 protected LauncherAppWidgetProviderInfo(Parcel in) {
Adam Cohen59400422014-03-05 18:07:04 -080058 super(in);
59 }
60
Sunny Goyal8ad02b82016-12-29 13:31:43 -080061 public void initSpans(Context context) {
Sunny Goyal87f784c2017-01-11 10:48:34 -080062 InvariantDeviceProfile idp = LauncherAppState.getIDP(context);
Sunny Goyal233ee962015-08-03 13:05:01 -070063
Jon Miranda4f1545d2017-09-20 11:09:42 -070064 Point landCellSize = idp.landscapeProfile.getCellSize();
65 Point portCellSize = idp.portraitProfile.getCellSize();
Sunny Goyal233ee962015-08-03 13:05:01 -070066
67 // Always assume we're working with the smallest span to make sure we
68 // reserve enough space in both orientations.
Jon Miranda4f1545d2017-09-20 11:09:42 -070069 float smallestCellWidth = Math.min(landCellSize.x, portCellSize.x);
70 float smallestCellHeight = Math.min(landCellSize.y, portCellSize.y);
Sunny Goyal233ee962015-08-03 13:05:01 -070071
72 // We want to account for the extra amount of padding that we are adding to the widget
73 // to ensure that it gets the full amount of space that it has requested.
74 Rect widgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(
Sunny Goyal8ad02b82016-12-29 13:31:43 -080075 context, provider, null);
Sunny Goyal233ee962015-08-03 13:05:01 -070076 spanX = Math.max(1, (int) Math.ceil(
77 (minWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth));
78 spanY = Math.max(1, (int) Math.ceil(
79 (minHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight));
80
81 minSpanX = Math.max(1, (int) Math.ceil(
82 (minResizeWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth));
83 minSpanY = Math.max(1, (int) Math.ceil(
84 (minResizeHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight));
Adam Cohen59400422014-03-05 18:07:04 -080085 }
86
87 public String getLabel(PackageManager packageManager) {
Adam Cohen59400422014-03-05 18:07:04 -080088 return super.loadLabel(packageManager);
89 }
90
Sunny Goyal952e63d2017-08-16 04:59:08 -070091 public Point getMinSpans() {
92 return new Point((resizeMode & RESIZE_HORIZONTAL) != 0 ? minSpanX : -1,
93 (resizeMode & RESIZE_VERTICAL) != 0 ? minSpanY : -1);
Adam Cohen59400422014-03-05 18:07:04 -080094 }
95
Sunny Goyal952e63d2017-08-16 04:59:08 -070096 public boolean isCustomWidget() {
97 return provider.getClassName().startsWith(CLS_CUSTOM_WIDGET_PREFIX);
Sunny Goyala52ecb02016-12-16 15:04:51 -080098 }
Winson Chung1054d4e2018-03-05 19:39:21 +000099
100 public int getWidgetFeatures() {
101 if (Utilities.ATLEAST_P) {
102 return widgetFeatures;
103 } else {
104 return 0;
105 }
106 }
Sunny Goyalac8154a2018-09-26 12:00:30 -0700107
108 @Override
109 public final ComponentName getComponent() {
110 return provider;
111 }
112
113 @Override
114 public final UserHandle getUser() {
115 return getProfile();
116 }
Sunny Goyal18204e42020-02-06 11:28:01 -0800117
118 @Override
119 public Drawable getFullResIcon(IconCache cache) {
120 return cache.getFullResIcon(provider.getPackageName(), icon);
121 }
Sunny Goyalac8154a2018-09-26 12:00:30 -0700122}