blob: b2b05b17bb52a5afa7aa51659619777cae2ab18c [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;
Adam Cohen59400422014-03-05 18:07:04 -080010import android.os.Parcel;
Sunny Goyalac8154a2018-09-26 12:00:30 -070011import android.os.UserHandle;
12
13import com.android.launcher3.icons.ComponentWithLabel;
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 */
Sunny Goyalac8154a2018-09-26 12:00:30 -070021public class LauncherAppWidgetProviderInfo extends AppWidgetProviderInfo
22 implements ComponentWithLabel {
Adam Cohen59400422014-03-05 18:07:04 -080023
Sunny Goyal952e63d2017-08-16 04:59:08 -070024 public static final String CLS_CUSTOM_WIDGET_PREFIX = "#custom-widget-";
Adam Cohen2e6da152015-05-06 11:42:25 -070025
Sunny Goyal233ee962015-08-03 13:05:01 -070026 public int spanX;
27 public int spanY;
28 public int minSpanX;
29 public int minSpanY;
Adam Cohen59400422014-03-05 18:07:04 -080030
31 public static LauncherAppWidgetProviderInfo fromProviderInfo(Context context,
32 AppWidgetProviderInfo info) {
Sunny Goyal8ad02b82016-12-29 13:31:43 -080033 final LauncherAppWidgetProviderInfo launcherInfo;
34 if (info instanceof LauncherAppWidgetProviderInfo) {
35 launcherInfo = (LauncherAppWidgetProviderInfo) info;
36 } else {
Adam Cohen59400422014-03-05 18:07:04 -080037
Sunny Goyal8ad02b82016-12-29 13:31:43 -080038 // In lieu of a public super copy constructor, we first write the AppWidgetProviderInfo
39 // into a parcel, and then construct a new LauncherAppWidgetProvider info from the
40 // associated super parcel constructor. This allows us to copy non-public members without
41 // using reflection.
42 Parcel p = Parcel.obtain();
43 info.writeToParcel(p, 0);
44 p.setDataPosition(0);
45 launcherInfo = new LauncherAppWidgetProviderInfo(p);
46 p.recycle();
47 }
48 launcherInfo.initSpans(context);
49 return launcherInfo;
Adam Cohen59400422014-03-05 18:07:04 -080050 }
51
Sunny Goyal952e63d2017-08-16 04:59:08 -070052 protected LauncherAppWidgetProviderInfo() {}
53
54 protected LauncherAppWidgetProviderInfo(Parcel in) {
Adam Cohen59400422014-03-05 18:07:04 -080055 super(in);
56 }
57
Sunny Goyal8ad02b82016-12-29 13:31:43 -080058 public void initSpans(Context context) {
Sunny Goyal87f784c2017-01-11 10:48:34 -080059 InvariantDeviceProfile idp = LauncherAppState.getIDP(context);
Sunny Goyal233ee962015-08-03 13:05:01 -070060
Sunny Goyal6c2975e2016-07-06 09:47:56 -070061 Point paddingLand = idp.landscapeProfile.getTotalWorkspacePadding();
62 Point paddingPort = idp.portraitProfile.getTotalWorkspacePadding();
Sunny Goyal233ee962015-08-03 13:05:01 -070063
64 // Always assume we're working with the smallest span to make sure we
65 // reserve enough space in both orientations.
66 float smallestCellWidth = DeviceProfile.calculateCellWidth(Math.min(
Sunny Goyal6c2975e2016-07-06 09:47:56 -070067 idp.landscapeProfile.widthPx - paddingLand.x,
68 idp.portraitProfile.widthPx - paddingPort.x),
Sunny Goyal233ee962015-08-03 13:05:01 -070069 idp.numColumns);
70 float smallestCellHeight = DeviceProfile.calculateCellWidth(Math.min(
Sunny Goyal6c2975e2016-07-06 09:47:56 -070071 idp.landscapeProfile.heightPx - paddingLand.y,
72 idp.portraitProfile.heightPx - paddingPort.y),
Sunny Goyal233ee962015-08-03 13:05:01 -070073 idp.numRows);
74
75 // We want to account for the extra amount of padding that we are adding to the widget
76 // to ensure that it gets the full amount of space that it has requested.
77 Rect widgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(
Sunny Goyal8ad02b82016-12-29 13:31:43 -080078 context, provider, null);
Sunny Goyal233ee962015-08-03 13:05:01 -070079 spanX = Math.max(1, (int) Math.ceil(
80 (minWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth));
81 spanY = Math.max(1, (int) Math.ceil(
82 (minHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight));
83
84 minSpanX = Math.max(1, (int) Math.ceil(
85 (minResizeWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth));
86 minSpanY = Math.max(1, (int) Math.ceil(
87 (minResizeHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight));
Adam Cohen59400422014-03-05 18:07:04 -080088 }
89
90 public String getLabel(PackageManager packageManager) {
Adam Cohen59400422014-03-05 18:07:04 -080091 return super.loadLabel(packageManager);
92 }
93
Sunny Goyal952e63d2017-08-16 04:59:08 -070094 public Point getMinSpans() {
95 return new Point((resizeMode & RESIZE_HORIZONTAL) != 0 ? minSpanX : -1,
96 (resizeMode & RESIZE_VERTICAL) != 0 ? minSpanY : -1);
Adam Cohen59400422014-03-05 18:07:04 -080097 }
98
Sunny Goyal952e63d2017-08-16 04:59:08 -070099 public boolean isCustomWidget() {
100 return provider.getClassName().startsWith(CLS_CUSTOM_WIDGET_PREFIX);
Sunny Goyala52ecb02016-12-16 15:04:51 -0800101 }
Winson Chung1054d4e2018-03-05 19:39:21 +0000102
103 public int getWidgetFeatures() {
104 if (Utilities.ATLEAST_P) {
105 return widgetFeatures;
106 } else {
107 return 0;
108 }
109 }
Sunny Goyalac8154a2018-09-26 12:00:30 -0700110
111 @Override
112 public final ComponentName getComponent() {
113 return provider;
114 }
115
116 @Override
117 public final UserHandle getUser() {
118 return getProfile();
119 }
120}