blob: 6835368d576f6d904e3b57c116ed75eab3dd1b57 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070017package android.appwidget;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018
Svetoslav976e8bd2014-07-16 15:12:03 -070019import android.content.Context;
20import android.content.pm.ActivityInfo;
21import android.content.pm.PackageManager;
22import android.content.res.Resources;
Svetoslav976e8bd2014-07-16 15:12:03 -070023import android.graphics.drawable.Drawable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.os.Parcel;
25import android.os.Parcelable;
26import android.content.ComponentName;
Svetoslav976e8bd2014-07-16 15:12:03 -070027import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
29/**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070030 * Describes the meta data for an installed AppWidget provider. The fields in this class
31 * correspond to the fields in the <code>&lt;appwidget-provider&gt;</code> xml tag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070033public class AppWidgetProviderInfo implements Parcelable {
Adam Cohend2e20de2011-02-25 12:03:37 -080034
35 /**
36 * Widget is not resizable.
37 */
38 public static final int RESIZE_NONE = 0;
39 /**
40 * Widget is resizable in the horizontal axis only.
41 */
42 public static final int RESIZE_HORIZONTAL = 1;
43 /**
44 * Widget is resizable in the vertical axis only.
45 */
46 public static final int RESIZE_VERTICAL = 2;
47 /**
48 * Widget is resizable in both the horizontal and vertical axes.
49 */
50 public static final int RESIZE_BOTH = RESIZE_HORIZONTAL | RESIZE_VERTICAL;
51
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052 /**
Adam Cohen0aa2d422012-09-07 17:37:26 -070053 * Indicates that the widget can be displayed on the home screen. This is the default value.
54 */
55 public static final int WIDGET_CATEGORY_HOME_SCREEN = 1;
56
57 /**
58 * Indicates that the widget can be displayed on the keyguard.
59 */
60 public static final int WIDGET_CATEGORY_KEYGUARD = 2;
61
62 /**
Winson Chungf7bca432014-04-30 17:11:13 -070063 * Indicates that the widget can be displayed within recents.
Winson Chungf7bca432014-04-30 17:11:13 -070064 */
65 public static final int WIDGET_CATEGORY_RECENTS = 4;
66
67 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070068 * Identity of this AppWidget component. This component should be a {@link
69 * android.content.BroadcastReceiver}, and it will be sent the AppWidget intents
70 * {@link android.appwidget as described in the AppWidget package documentation}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 *
72 * <p>This field corresponds to the <code>android:name</code> attribute in
73 * the <code>&lt;receiver&gt;</code> element in the AndroidManifest.xml file.
74 */
75 public ComponentName provider;
76
77 /**
Adam Cohen1bfaf562011-07-19 18:05:33 -070078 * The default height of the widget when added to a host, in dp. The widget will get
79 * at least this width, and will often be given more, depending on the host.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 *
81 * <p>This field corresponds to the <code>android:minWidth</code> attribute in
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070082 * the AppWidget meta-data file.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 */
84 public int minWidth;
85
86 /**
Adam Cohen1bfaf562011-07-19 18:05:33 -070087 * The default height of the widget when added to a host, in dp. The widget will get
88 * at least this height, and will often be given more, depending on the host.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 *
90 * <p>This field corresponds to the <code>android:minHeight</code> attribute in
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070091 * the AppWidget meta-data file.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 */
93 public int minHeight;
94
95 /**
Adam Cohen1bfaf562011-07-19 18:05:33 -070096 * Minimum width (in dp) which the widget can be resized to. This field has no effect if it
97 * is greater than minWidth or if horizontal resizing isn't enabled (see {@link #resizeMode}).
98 *
99 * <p>This field corresponds to the <code>android:minResizeWidth</code> attribute in
100 * the AppWidget meta-data file.
101 */
102 public int minResizeWidth;
103
104 /**
105 * Minimum height (in dp) which the widget can be resized to. This field has no effect if it
106 * is greater than minHeight or if vertical resizing isn't enabled (see {@link #resizeMode}).
107 *
108 * <p>This field corresponds to the <code>android:minResizeHeight</code> attribute in
109 * the AppWidget meta-data file.
110 */
111 public int minResizeHeight;
112
113 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700114 * How often, in milliseconds, that this AppWidget wants to be updated.
115 * The AppWidget manager may place a limit on how often a AppWidget is updated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 *
117 * <p>This field corresponds to the <code>android:updatePeriodMillis</code> attribute in
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700118 * the AppWidget meta-data file.
Joe Onorato851da842009-07-14 19:49:27 -0700119 *
120 * <p class="note"><b>Note:</b> Updates requested with <code>updatePeriodMillis</code>
121 * will not be delivered more than once every 30 minutes.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 */
123 public int updatePeriodMillis;
124
125 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700126 * The resource id of the initial layout for this AppWidget. This should be
127 * displayed until the RemoteViews for the AppWidget is available.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 *
129 * <p>This field corresponds to the <code>android:initialLayout</code> attribute in
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700130 * the AppWidget meta-data file.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 */
132 public int initialLayout;
133
134 /**
Adam Cohen0aa2d422012-09-07 17:37:26 -0700135 * The resource id of the initial layout for this AppWidget when it is displayed on keyguard.
136 * This parameter only needs to be provided if the widget can be displayed on the keyguard,
137 * see {@link #widgetCategory}.
138 *
139 * <p>This field corresponds to the <code>android:initialKeyguardLayout</code> attribute in
140 * the AppWidget meta-data file.
141 */
142 public int initialKeyguardLayout;
143
144 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700145 * The activity to launch that will configure the AppWidget.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 *
147 * <p>This class name of field corresponds to the <code>android:configure</code> attribute in
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700148 * the AppWidget meta-data file. The package name always corresponds to the package containing
149 * the AppWidget provider.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 */
151 public ComponentName configure;
152
153 /**
Svetoslav976e8bd2014-07-16 15:12:03 -0700154 * The label to display to the user in the AppWidget picker.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 *
Svetoslav976e8bd2014-07-16 15:12:03 -0700156 * @deprecated Use {@link #loadLabel(android.content.pm.PackageManager)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 */
Svetoslav976e8bd2014-07-16 15:12:03 -0700158 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 public String label;
160
161 /**
Svetoslav976e8bd2014-07-16 15:12:03 -0700162 * The icon to display for this AppWidget in the AppWidget picker. If not supplied in the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 * xml, the application icon will be used.
164 *
165 * <p>This field corresponds to the <code>android:icon</code> attribute in
166 * the <code>&lt;receiver&gt;</code> element in the AndroidManifest.xml file.
167 */
168 public int icon;
Adam Cohena02fdf12010-11-03 13:27:40 -0700169
Romain Guyd2671e12010-03-11 18:06:42 -0800170 /**
Adam Cohena02fdf12010-11-03 13:27:40 -0700171 * The view id of the AppWidget subview which should be auto-advanced by the widget's host.
Adam Cohen9611f2e2011-02-28 13:39:38 -0800172 *
173 * <p>This field corresponds to the <code>android:autoAdvanceViewId</code> attribute in
174 * the AppWidget meta-data file.
Adam Cohena02fdf12010-11-03 13:27:40 -0700175 */
176 public int autoAdvanceViewId;
177
Patrick Dubroyd2db2a52010-06-23 14:56:28 -0700178 /**
179 * A preview of what the AppWidget will look like after it's configured.
180 * If not supplied, the AppWidget's icon will be used.
181 *
182 * <p>This field corresponds to the <code>android:previewImage</code> attribute in
183 * the <code>&lt;receiver&gt;</code> element in the AndroidManifest.xml file.
Patrick Dubroyd2db2a52010-06-23 14:56:28 -0700184 */
John Spurlock8a985d22014-02-25 09:40:05 -0500185 public int previewImage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186
Adam Cohend2e20de2011-02-25 12:03:37 -0800187 /**
188 * The rules by which a widget can be resized. See {@link #RESIZE_NONE},
189 * {@link #RESIZE_NONE}, {@link #RESIZE_HORIZONTAL},
190 * {@link #RESIZE_VERTICAL}, {@link #RESIZE_BOTH}.
Adam Cohen9611f2e2011-02-28 13:39:38 -0800191 *
192 * <p>This field corresponds to the <code>android:resizeMode</code> attribute in
193 * the AppWidget meta-data file.
Adam Cohend2e20de2011-02-25 12:03:37 -0800194 */
Adam Cohen9611f2e2011-02-28 13:39:38 -0800195 public int resizeMode;
Adam Cohend2e20de2011-02-25 12:03:37 -0800196
Adam Cohen0aa2d422012-09-07 17:37:26 -0700197 /**
198 * Determines whether this widget can be displayed on the home screen, the keyguard, or both.
199 * A widget which is displayed on both needs to ensure that it follows the design guidelines
200 * for both widget classes. This can be achieved by querying the AppWidget options in its
201 * widget provider's update method.
202 *
203 * <p>This field corresponds to the <code>widgetCategory</code> attribute in
204 * the AppWidget meta-data file.
205 */
206 public int widgetCategory;
207
Svetoslav976e8bd2014-07-16 15:12:03 -0700208 /** @hide */
209 public ActivityInfo providerInfo;
210
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700211 public AppWidgetProviderInfo() {
Svetoslav976e8bd2014-07-16 15:12:03 -0700212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 }
214
215 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700216 * Unflatten the AppWidgetProviderInfo from a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 */
Svetoslav976e8bd2014-07-16 15:12:03 -0700218 @SuppressWarnings("deprecation")
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700219 public AppWidgetProviderInfo(Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 if (0 != in.readInt()) {
221 this.provider = new ComponentName(in);
222 }
223 this.minWidth = in.readInt();
224 this.minHeight = in.readInt();
Adam Cohen324afba2011-07-22 11:51:45 -0700225 this.minResizeWidth = in.readInt();
226 this.minResizeHeight = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 this.updatePeriodMillis = in.readInt();
228 this.initialLayout = in.readInt();
Adam Cohen0aa2d422012-09-07 17:37:26 -0700229 this.initialKeyguardLayout = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 if (0 != in.readInt()) {
231 this.configure = new ComponentName(in);
232 }
233 this.label = in.readString();
234 this.icon = in.readInt();
Patrick Dubroyd2db2a52010-06-23 14:56:28 -0700235 this.previewImage = in.readInt();
Adam Cohena02fdf12010-11-03 13:27:40 -0700236 this.autoAdvanceViewId = in.readInt();
Adam Cohen9611f2e2011-02-28 13:39:38 -0800237 this.resizeMode = in.readInt();
Adam Cohen0aa2d422012-09-07 17:37:26 -0700238 this.widgetCategory = in.readInt();
Svetoslav976e8bd2014-07-16 15:12:03 -0700239 this.providerInfo = in.readParcelable(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 }
241
Svetoslav976e8bd2014-07-16 15:12:03 -0700242 /**
243 * Loads the localized label to display to the user in the AppWidget picker.
244 *
245 * @param packageManager Package manager instance for loading resources.
246 * @return The label for the current locale.
247 */
248 public final String loadLabel(PackageManager packageManager) {
249 CharSequence label = providerInfo.loadLabel(packageManager);
250 if (label != null) {
251 return label.toString().trim();
252 }
253 return null;
254 }
255
256 /**
257 * Loads the icon to display for this AppWidget in the AppWidget picker. If not
258 * supplied in the xml, the application icon will be used. A client can optionally
259 * provide a desired density such as {@link android.util.DisplayMetrics#DENSITY_LOW}
260 * {@link android.util.DisplayMetrics#DENSITY_MEDIUM}, etc. If no density is
261 * provided, the density of the current display will be used.
262 * <p>
263 * The loaded icon corresponds to the <code>android:icon</code> attribute in
264 * the <code>&lt;receiver&gt;</code> element in the AndroidManifest.xml file.
265 * </p>
Svetoslav976e8bd2014-07-16 15:12:03 -0700266 *
267 * @param context Context for accessing resources.
268 * @param density The optional desired density as per
269 * {@link android.util.DisplayMetrics#densityDpi}.
Svetoslavc71c42f2014-08-05 18:57:05 -0700270 * @return The provider icon.
Svetoslav976e8bd2014-07-16 15:12:03 -0700271 */
272 public final Drawable loadIcon(Context context, int density) {
273 return loadDrawable(context, density, providerInfo.getIconResource());
274 }
275
276 /**
277 * Loads a preview of what the AppWidget will look like after it's configured.
278 * If not supplied, the AppWidget's icon will be used. A client can optionally
279 * provide a desired deinsity such as {@link android.util.DisplayMetrics#DENSITY_LOW}
280 * {@link android.util.DisplayMetrics#DENSITY_MEDIUM}, etc. If no density is
281 * provided, the density of the current display will be used.
282 * <p>
283 * The loaded image corresponds to the <code>android:previewImage</code> attribute
284 * in the <code>&lt;receiver&gt;</code> element in the AndroidManifest.xml file.
285 * </p>
Svetoslav976e8bd2014-07-16 15:12:03 -0700286 *
287 * @param context Context for accessing resources.
288 * @param density The optional desired density as per
289 * {@link android.util.DisplayMetrics#densityDpi}.
Svetoslavc71c42f2014-08-05 18:57:05 -0700290 * @return The widget preview image.
Svetoslav976e8bd2014-07-16 15:12:03 -0700291 */
Svetoslav976e8bd2014-07-16 15:12:03 -0700292 public final Drawable loadPreviewImage(Context context, int density) {
293 return loadDrawable(context, density, previewImage);
294 }
295
296 /**
297 * Gets the user profile in which the provider resides.
298 *
299 * @return The hosting user profile.
300 */
301 public final UserHandle getProfile() {
302 return new UserHandle(UserHandle.getUserId(providerInfo.applicationInfo.uid));
303 }
304
305 @Override
306 @SuppressWarnings("deprecation")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 public void writeToParcel(android.os.Parcel out, int flags) {
308 if (this.provider != null) {
309 out.writeInt(1);
310 this.provider.writeToParcel(out, flags);
311 } else {
312 out.writeInt(0);
313 }
314 out.writeInt(this.minWidth);
315 out.writeInt(this.minHeight);
Adam Cohen324afba2011-07-22 11:51:45 -0700316 out.writeInt(this.minResizeWidth);
317 out.writeInt(this.minResizeHeight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 out.writeInt(this.updatePeriodMillis);
319 out.writeInt(this.initialLayout);
Adam Cohen0aa2d422012-09-07 17:37:26 -0700320 out.writeInt(this.initialKeyguardLayout);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 if (this.configure != null) {
322 out.writeInt(1);
323 this.configure.writeToParcel(out, flags);
324 } else {
325 out.writeInt(0);
326 }
327 out.writeString(this.label);
328 out.writeInt(this.icon);
Patrick Dubroyd2db2a52010-06-23 14:56:28 -0700329 out.writeInt(this.previewImage);
Adam Cohena02fdf12010-11-03 13:27:40 -0700330 out.writeInt(this.autoAdvanceViewId);
Adam Cohen9611f2e2011-02-28 13:39:38 -0800331 out.writeInt(this.resizeMode);
Adam Cohen0aa2d422012-09-07 17:37:26 -0700332 out.writeInt(this.widgetCategory);
Svetoslav976e8bd2014-07-16 15:12:03 -0700333 out.writeParcelable(this.providerInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 }
335
Adam Cohen3ff2d862012-09-26 14:07:57 -0700336 @Override
Svetoslav976e8bd2014-07-16 15:12:03 -0700337 @SuppressWarnings("deprecation")
Adam Cohen3ff2d862012-09-26 14:07:57 -0700338 public AppWidgetProviderInfo clone() {
339 AppWidgetProviderInfo that = new AppWidgetProviderInfo();
340 that.provider = this.provider == null ? null : this.provider.clone();
341 that.minWidth = this.minWidth;
342 that.minHeight = this.minHeight;
343 that.minResizeWidth = this.minResizeHeight;
344 that.minResizeHeight = this.minResizeHeight;
345 that.updatePeriodMillis = this.updatePeriodMillis;
Adam Cohen180cfd52012-11-04 12:03:12 -0800346 that.initialLayout = this.initialLayout;
Adam Cohen3ff2d862012-09-26 14:07:57 -0700347 that.initialKeyguardLayout = this.initialKeyguardLayout;
348 that.configure = this.configure == null ? null : this.configure.clone();
349 that.label = this.label == null ? null : this.label.substring(0);
350 that.icon = this.icon;
351 that.previewImage = this.previewImage;
352 that.autoAdvanceViewId = this.autoAdvanceViewId;
353 that.resizeMode = this.resizeMode;
Svetoslav976e8bd2014-07-16 15:12:03 -0700354 that.widgetCategory = this.widgetCategory;
355 that.providerInfo = this.providerInfo;
Adam Cohen3ff2d862012-09-26 14:07:57 -0700356 return that;
357 }
358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 public int describeContents() {
360 return 0;
361 }
362
Svetoslav976e8bd2014-07-16 15:12:03 -0700363 private Drawable loadDrawable(Context context, int density, int resourceId) {
364 try {
365 Resources resources = context.getPackageManager().getResourcesForApplication(
366 providerInfo.applicationInfo);
Svetoslav976e8bd2014-07-16 15:12:03 -0700367 if (resourceId > 0) {
368 if (density <= 0) {
369 density = context.getResources().getDisplayMetrics().densityDpi;
370 }
Svetoslavc71c42f2014-08-05 18:57:05 -0700371 return resources.getDrawableForDensity(resourceId, density);
Svetoslav976e8bd2014-07-16 15:12:03 -0700372 }
373 } catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) {
374 /* ignore */
375 }
Svetoslavc71c42f2014-08-05 18:57:05 -0700376 return providerInfo.loadIcon(context.getPackageManager());
Svetoslav976e8bd2014-07-16 15:12:03 -0700377 }
378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700380 * Parcelable.Creator that instantiates AppWidgetProviderInfo objects
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700382 public static final Parcelable.Creator<AppWidgetProviderInfo> CREATOR
383 = new Parcelable.Creator<AppWidgetProviderInfo>()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700385 public AppWidgetProviderInfo createFromParcel(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700387 return new AppWidgetProviderInfo(parcel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 }
389
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700390 public AppWidgetProviderInfo[] newArray(int size)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700392 return new AppWidgetProviderInfo[size];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 }
394 };
395
396 public String toString() {
Svetoslav976e8bd2014-07-16 15:12:03 -0700397 return "AppWidgetProviderInfo(" + getProfile() + '/' + provider + ')';
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 }
399}