summaryrefslogtreecommitdiff
path: root/src/com/android/launcher3/widget/LauncherAppWidgetProviderInfo.java
blob: bba1016f34c80c49215c6f730284b2dedc71e1f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package com.android.launcher3.widget;

import static com.android.launcher3.Utilities.ATLEAST_S;

import android.appwidget.AppWidgetHostView;
import android.appwidget.AppWidgetProviderInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Parcel;
import android.os.UserHandle;

import com.android.launcher3.DeviceProfile;
import com.android.launcher3.InvariantDeviceProfile;
import com.android.launcher3.LauncherAppState;
import com.android.launcher3.Utilities;
import com.android.launcher3.icons.ComponentWithLabelAndIcon;
import com.android.launcher3.icons.IconCache;
import com.android.launcher3.model.data.LauncherAppWidgetInfo;

/**
 * This class is a thin wrapper around the framework AppWidgetProviderInfo class. This class affords
 * a common object for describing both framework provided AppWidgets as well as custom widgets
 * (who's implementation is owned by the launcher). This object represents a widget type / class,
 * as opposed to a widget instance, and so should not be confused with {@link LauncherAppWidgetInfo}
 */
public class LauncherAppWidgetProviderInfo extends AppWidgetProviderInfo
        implements ComponentWithLabelAndIcon {

    public static final String CLS_CUSTOM_WIDGET_PREFIX = "#custom-widget-";

    /**
     * The desired number of cells that this widget occupies horizontally in
     * {@link com.android.launcher3.CellLayout}.
     */
    public int spanX;

    /**
     * The desired number of cells that this widget occupies vertically in
     * {@link com.android.launcher3.CellLayout}.
     */
    public int spanY;

    /**
     * The minimum number of cells that this widget can occupy horizontally in
     * {@link com.android.launcher3.CellLayout}.
     */
    public int minSpanX;

    /**
     * The minimum number of cells that this widget can occupy vertically in
     * {@link com.android.launcher3.CellLayout}.
     */
    public int minSpanY;

    /**
     * The maximum number of cells that this widget can occupy horizontally in
     * {@link com.android.launcher3.CellLayout}.
     */
    public int maxSpanX;

    /**
     * The maximum number of cells that this widget can occupy vertically in
     * {@link com.android.launcher3.CellLayout}.
     */
    public int maxSpanY;

    private boolean mIsMinSizeFulfilled;

    public static LauncherAppWidgetProviderInfo fromProviderInfo(Context context,
            AppWidgetProviderInfo info) {
        final LauncherAppWidgetProviderInfo launcherInfo;
        if (info instanceof LauncherAppWidgetProviderInfo) {
            launcherInfo = (LauncherAppWidgetProviderInfo) info;
        } else {

            // In lieu of a public super copy constructor, we first write the AppWidgetProviderInfo
            // into a parcel, and then construct a new LauncherAppWidgetProvider info from the
            // associated super parcel constructor. This allows us to copy non-public members without
            // using reflection.
            Parcel p = Parcel.obtain();
            info.writeToParcel(p, 0);
            p.setDataPosition(0);
            launcherInfo = new LauncherAppWidgetProviderInfo(p);
            p.recycle();
        }
        launcherInfo.initSpans(context, LauncherAppState.getIDP(context));
        return launcherInfo;
    }

    protected LauncherAppWidgetProviderInfo() {}

    protected LauncherAppWidgetProviderInfo(Parcel in) {
        super(in);
    }

    public void initSpans(Context context, InvariantDeviceProfile idp) {
        int minSpanX = 0;
        int minSpanY = 0;
        int maxSpanX = idp.numColumns;
        int maxSpanY = idp.numRows;
        int spanX = 0;
        int spanY = 0;

        Rect widgetPadding = new Rect();
        Rect localPadding = new Rect();
        AppWidgetHostView.getDefaultPaddingForWidget(context, provider, widgetPadding);

        Point cellSize = new Point();
        for (DeviceProfile dp : idp.supportedProfiles) {
            dp.getCellSize(cellSize);
            // We want to account for the extra amount of padding that we are adding to the widget
            // to ensure that it gets the full amount of space that it has requested.
            // If grids supports insetting widgets, we do not account for widget padding.
            if (dp.shouldInsetWidgets()) {
                localPadding.setEmpty();
            } else {
                localPadding.set(widgetPadding);
            }
            minSpanX = Math.max(minSpanX,
                    getSpanX(localPadding, minResizeWidth, dp.cellLayoutBorderSpacePx.x,
                            cellSize.x));
            minSpanY = Math.max(minSpanY,
                    getSpanY(localPadding, minResizeHeight, dp.cellLayoutBorderSpacePx.y,
                            cellSize.y));

            if (ATLEAST_S) {
                if (maxResizeWidth > 0) {
                    maxSpanX = Math.min(maxSpanX, getSpanX(localPadding, maxResizeWidth,
                            dp.cellLayoutBorderSpacePx.x, cellSize.x));
                }
                if (maxResizeHeight > 0) {
                    maxSpanY = Math.min(maxSpanY, getSpanY(localPadding, maxResizeHeight,
                            dp.cellLayoutBorderSpacePx.y, cellSize.y));
                }
            }

            spanX = Math.max(spanX,
                    getSpanX(localPadding, minWidth, dp.cellLayoutBorderSpacePx.x,
                            cellSize.x));
            spanY = Math.max(spanY,
                    getSpanY(localPadding, minHeight, dp.cellLayoutBorderSpacePx.y,
                            cellSize.y));
        }

        if (ATLEAST_S) {
            // Ensures maxSpan >= minSpan
            maxSpanX = Math.max(maxSpanX, minSpanX);
            maxSpanY = Math.max(maxSpanY, minSpanY);

            // Use targetCellWidth/Height if it is within the min/max ranges.
            // Otherwise, use the span of minWidth/Height.
            if (targetCellWidth >= minSpanX && targetCellWidth <= maxSpanX
                    && targetCellHeight >= minSpanY && targetCellHeight <= maxSpanY) {
                spanX = targetCellWidth;
                spanY = targetCellHeight;
            }
        }

        // If minSpanX/Y > spanX/Y, ignore the minSpanX/Y to match the behavior described in
        // minResizeWidth & minResizeHeight Android documentation. See
        // https://developer.android.com/reference/android/appwidget/AppWidgetProviderInfo
        this.minSpanX = Math.min(spanX, minSpanX);
        this.minSpanY = Math.min(spanY, minSpanY);
        this.maxSpanX = maxSpanX;
        this.maxSpanY = maxSpanY;
        this.mIsMinSizeFulfilled = Math.min(spanX, minSpanX) <= idp.numColumns
            && Math.min(spanY, minSpanY) <= idp.numRows;
        // Ensures the default span X and span Y will not exceed the current grid size.
        this.spanX = Math.min(spanX, idp.numColumns);
        this.spanY = Math.min(spanY, idp.numRows);
    }

    /**
     * Returns {@code true} if the widget's minimum size requirement can be fulfilled in the device
     * grid setting, {@link InvariantDeviceProfile}, that was passed in
     * {@link #initSpans(Context, InvariantDeviceProfile)}.
     */
    public boolean isMinSizeFulfilled() {
        return mIsMinSizeFulfilled;
    }

    private int getSpanX(Rect widgetPadding, int widgetWidth, int cellSpacing, float cellWidth) {
        return Math.max(1, (int) Math.ceil(
                (widgetWidth + widgetPadding.left + widgetPadding.right + cellSpacing) / (cellWidth
                        + cellSpacing)));
    }

    private int getSpanY(Rect widgetPadding, int widgetHeight, int cellSpacing, float cellHeight) {
        return Math.max(1, (int) Math.ceil(
                (widgetHeight + widgetPadding.top + widgetPadding.bottom + cellSpacing) / (
                        cellHeight + cellSpacing)));
    }

    public String getLabel(PackageManager packageManager) {
        return super.loadLabel(packageManager);
    }

    public Point getMinSpans() {
        return new Point((resizeMode & RESIZE_HORIZONTAL) != 0 ? minSpanX : -1,
                (resizeMode & RESIZE_VERTICAL) != 0 ? minSpanY : -1);
    }

    public boolean isCustomWidget() {
        return provider.getClassName().startsWith(CLS_CUSTOM_WIDGET_PREFIX);
    }

    public int getWidgetFeatures() {
        if (Utilities.ATLEAST_P) {
            return widgetFeatures;
        } else {
            return 0;
        }
    }

    public boolean isReconfigurable() {
        return configure != null && (getWidgetFeatures() & WIDGET_FEATURE_RECONFIGURABLE) != 0;
    }

    public boolean isConfigurationOptional() {
        return ATLEAST_S
                && isReconfigurable()
                && (getWidgetFeatures() & WIDGET_FEATURE_CONFIGURATION_OPTIONAL) != 0;
    }

    @Override
    public final ComponentName getComponent() {
        return provider;
    }

    @Override
    public final UserHandle getUser() {
        return getProfile();
    }

    @Override
    public Drawable getFullResIcon(IconCache cache) {
        return cache.getFullResIcon(provider.getPackageName(), icon);
    }
}