1 package com.android.launcher3;
2 
3 import android.appwidget.AppWidgetHostView;
4 import android.appwidget.AppWidgetProviderInfo;
5 import android.content.ComponentName;
6 import android.content.Context;
7 import android.content.pm.PackageManager;
8 import android.graphics.Point;
9 import android.graphics.Rect;
10 import android.os.Parcel;
11 import android.os.UserHandle;
12 
13 import com.android.launcher3.icons.ComponentWithLabel;
14 
15 /**
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  */
21 public class LauncherAppWidgetProviderInfo extends AppWidgetProviderInfo
22         implements ComponentWithLabel {
23 
24     public static final String CLS_CUSTOM_WIDGET_PREFIX = "#custom-widget-";
25 
26     public int spanX;
27     public int spanY;
28     public int minSpanX;
29     public int minSpanY;
30 
fromProviderInfo(Context context, AppWidgetProviderInfo info)31     public static LauncherAppWidgetProviderInfo fromProviderInfo(Context context,
32             AppWidgetProviderInfo info) {
33         final LauncherAppWidgetProviderInfo launcherInfo;
34         if (info instanceof LauncherAppWidgetProviderInfo) {
35             launcherInfo = (LauncherAppWidgetProviderInfo) info;
36         } else {
37 
38             // 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;
50     }
51 
LauncherAppWidgetProviderInfo()52     protected LauncherAppWidgetProviderInfo() {}
53 
LauncherAppWidgetProviderInfo(Parcel in)54     protected LauncherAppWidgetProviderInfo(Parcel in) {
55         super(in);
56     }
57 
initSpans(Context context)58     public void initSpans(Context context) {
59         InvariantDeviceProfile idp = LauncherAppState.getIDP(context);
60 
61         Point landCellSize = idp.landscapeProfile.getCellSize();
62         Point portCellSize = idp.portraitProfile.getCellSize();
63 
64         // Always assume we're working with the smallest span to make sure we
65         // reserve enough space in both orientations.
66         float smallestCellWidth = Math.min(landCellSize.x, portCellSize.x);
67         float smallestCellHeight = Math.min(landCellSize.y, portCellSize.y);
68 
69         // We want to account for the extra amount of padding that we are adding to the widget
70         // to ensure that it gets the full amount of space that it has requested.
71         Rect widgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(
72                 context, provider, null);
73         spanX = Math.max(1, (int) Math.ceil(
74                         (minWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth));
75         spanY = Math.max(1, (int) Math.ceil(
76                 (minHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight));
77 
78         minSpanX = Math.max(1, (int) Math.ceil(
79                 (minResizeWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth));
80         minSpanY = Math.max(1, (int) Math.ceil(
81                 (minResizeHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight));
82     }
83 
getLabel(PackageManager packageManager)84     public String getLabel(PackageManager packageManager) {
85         return super.loadLabel(packageManager);
86     }
87 
getMinSpans()88     public Point getMinSpans() {
89         return new Point((resizeMode & RESIZE_HORIZONTAL) != 0 ? minSpanX : -1,
90                 (resizeMode & RESIZE_VERTICAL) != 0 ? minSpanY : -1);
91     }
92 
isCustomWidget()93     public boolean isCustomWidget() {
94         return provider.getClassName().startsWith(CLS_CUSTOM_WIDGET_PREFIX);
95     }
96 
getWidgetFeatures()97     public int getWidgetFeatures() {
98         if (Utilities.ATLEAST_P) {
99             return widgetFeatures;
100         } else {
101             return 0;
102         }
103     }
104 
105     @Override
getComponent()106     public final ComponentName getComponent() {
107         return provider;
108     }
109 
110     @Override
getUser()111     public final UserHandle getUser() {
112         return getProfile();
113     }
114 }
115