blob: 56cce786595c544bfaff48561b13aa752421b02e [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;
Adam Cohen59400422014-03-05 18:07:04 -080016
Adam Cohen59400422014-03-05 18:07:04 -080017/**
18 * This class is a thin wrapper around the framework AppWidgetProviderInfo class. This class affords
19 * a common object for describing both framework provided AppWidgets as well as custom widgets
20 * (who's implementation is owned by the launcher). This object represents a widget type / class,
21 * as opposed to a widget instance, and so should not be confused with {@link LauncherAppWidgetInfo}
22 */
Sunny Goyalac8154a2018-09-26 12:00:30 -070023public class LauncherAppWidgetProviderInfo extends AppWidgetProviderInfo
Sunny Goyal18204e42020-02-06 11:28:01 -080024 implements ComponentWithLabelAndIcon {
Adam Cohen59400422014-03-05 18:07:04 -080025
Sunny Goyal952e63d2017-08-16 04:59:08 -070026 public static final String CLS_CUSTOM_WIDGET_PREFIX = "#custom-widget-";
Adam Cohen2e6da152015-05-06 11:42:25 -070027
Sunny Goyal233ee962015-08-03 13:05:01 -070028 public int spanX;
29 public int spanY;
30 public int minSpanX;
31 public int minSpanY;
Adam Cohen59400422014-03-05 18:07:04 -080032
33 public static LauncherAppWidgetProviderInfo fromProviderInfo(Context context,
34 AppWidgetProviderInfo info) {
Sunny Goyal8ad02b82016-12-29 13:31:43 -080035 final LauncherAppWidgetProviderInfo launcherInfo;
36 if (info instanceof LauncherAppWidgetProviderInfo) {
37 launcherInfo = (LauncherAppWidgetProviderInfo) info;
38 } else {
Adam Cohen59400422014-03-05 18:07:04 -080039
Sunny Goyal8ad02b82016-12-29 13:31:43 -080040 // In lieu of a public super copy constructor, we first write the AppWidgetProviderInfo
41 // into a parcel, and then construct a new LauncherAppWidgetProvider info from the
42 // associated super parcel constructor. This allows us to copy non-public members without
43 // using reflection.
44 Parcel p = Parcel.obtain();
45 info.writeToParcel(p, 0);
46 p.setDataPosition(0);
47 launcherInfo = new LauncherAppWidgetProviderInfo(p);
48 p.recycle();
49 }
50 launcherInfo.initSpans(context);
51 return launcherInfo;
Adam Cohen59400422014-03-05 18:07:04 -080052 }
53
Sunny Goyal952e63d2017-08-16 04:59:08 -070054 protected LauncherAppWidgetProviderInfo() {}
55
56 protected LauncherAppWidgetProviderInfo(Parcel in) {
Adam Cohen59400422014-03-05 18:07:04 -080057 super(in);
58 }
59
Sunny Goyal8ad02b82016-12-29 13:31:43 -080060 public void initSpans(Context context) {
Sunny Goyal87f784c2017-01-11 10:48:34 -080061 InvariantDeviceProfile idp = LauncherAppState.getIDP(context);
Sunny Goyal233ee962015-08-03 13:05:01 -070062
Jon Miranda4f1545d2017-09-20 11:09:42 -070063 Point landCellSize = idp.landscapeProfile.getCellSize();
64 Point portCellSize = idp.portraitProfile.getCellSize();
Sunny Goyal233ee962015-08-03 13:05:01 -070065
66 // Always assume we're working with the smallest span to make sure we
67 // reserve enough space in both orientations.
Jon Miranda4f1545d2017-09-20 11:09:42 -070068 float smallestCellWidth = Math.min(landCellSize.x, portCellSize.x);
69 float smallestCellHeight = Math.min(landCellSize.y, portCellSize.y);
Sunny Goyal233ee962015-08-03 13:05:01 -070070
71 // We want to account for the extra amount of padding that we are adding to the widget
72 // to ensure that it gets the full amount of space that it has requested.
73 Rect widgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(
Sunny Goyal8ad02b82016-12-29 13:31:43 -080074 context, provider, null);
Sunny Goyal233ee962015-08-03 13:05:01 -070075 spanX = Math.max(1, (int) Math.ceil(
76 (minWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth));
77 spanY = Math.max(1, (int) Math.ceil(
78 (minHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight));
79
80 minSpanX = Math.max(1, (int) Math.ceil(
81 (minResizeWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth));
82 minSpanY = Math.max(1, (int) Math.ceil(
83 (minResizeHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight));
Adam Cohen59400422014-03-05 18:07:04 -080084 }
85
86 public String getLabel(PackageManager packageManager) {
Adam Cohen59400422014-03-05 18:07:04 -080087 return super.loadLabel(packageManager);
88 }
89
Sunny Goyal952e63d2017-08-16 04:59:08 -070090 public Point getMinSpans() {
91 return new Point((resizeMode & RESIZE_HORIZONTAL) != 0 ? minSpanX : -1,
92 (resizeMode & RESIZE_VERTICAL) != 0 ? minSpanY : -1);
Adam Cohen59400422014-03-05 18:07:04 -080093 }
94
Sunny Goyal952e63d2017-08-16 04:59:08 -070095 public boolean isCustomWidget() {
96 return provider.getClassName().startsWith(CLS_CUSTOM_WIDGET_PREFIX);
Sunny Goyala52ecb02016-12-16 15:04:51 -080097 }
Winson Chung1054d4e2018-03-05 19:39:21 +000098
99 public int getWidgetFeatures() {
100 if (Utilities.ATLEAST_P) {
101 return widgetFeatures;
102 } else {
103 return 0;
104 }
105 }
Sunny Goyalac8154a2018-09-26 12:00:30 -0700106
107 @Override
108 public final ComponentName getComponent() {
109 return provider;
110 }
111
112 @Override
113 public final UserHandle getUser() {
114 return getProfile();
115 }
Sunny Goyal18204e42020-02-06 11:28:01 -0800116
117 @Override
118 public Drawable getFullResIcon(IconCache cache) {
119 return cache.getFullResIcon(provider.getPackageName(), icon);
120 }
Sunny Goyalac8154a2018-09-26 12:00:30 -0700121}