blob: b4d79b484c89b51891e704a3d38b99b57ade8613 [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
Svetoslav8e1d2992014-08-08 12:48:06 -070019import android.annotation.NonNull;
Svetoslav976e8bd2014-07-16 15:12:03 -070020import android.content.Context;
21import android.content.pm.ActivityInfo;
22import android.content.pm.PackageManager;
23import android.content.res.Resources;
Svetoslav976e8bd2014-07-16 15:12:03 -070024import android.graphics.drawable.Drawable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.os.Parcel;
26import android.os.Parcelable;
27import android.content.ComponentName;
Svetoslav976e8bd2014-07-16 15:12:03 -070028import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029
30/**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070031 * Describes the meta data for an installed AppWidget provider. The fields in this class
32 * correspond to the fields in the <code>&lt;appwidget-provider&gt;</code> xml tag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070034public class AppWidgetProviderInfo implements Parcelable {
Adam Cohend2e20de2011-02-25 12:03:37 -080035
36 /**
37 * Widget is not resizable.
38 */
39 public static final int RESIZE_NONE = 0;
40 /**
41 * Widget is resizable in the horizontal axis only.
42 */
43 public static final int RESIZE_HORIZONTAL = 1;
44 /**
45 * Widget is resizable in the vertical axis only.
46 */
47 public static final int RESIZE_VERTICAL = 2;
48 /**
49 * Widget is resizable in both the horizontal and vertical axes.
50 */
51 public static final int RESIZE_BOTH = RESIZE_HORIZONTAL | RESIZE_VERTICAL;
52
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 /**
Adam Cohen0aa2d422012-09-07 17:37:26 -070054 * Indicates that the widget can be displayed on the home screen. This is the default value.
55 */
56 public static final int WIDGET_CATEGORY_HOME_SCREEN = 1;
57
58 /**
59 * Indicates that the widget can be displayed on the keyguard.
60 */
61 public static final int WIDGET_CATEGORY_KEYGUARD = 2;
62
63 /**
Winson Chungee0b1212014-09-04 16:42:53 +020064 * Indicates that the widget can be displayed within a space reserved for the search box.
Winson Chungf7bca432014-04-30 17:11:13 -070065 */
Winson Chungee0b1212014-09-04 16:42:53 +020066 public static final int WIDGET_CATEGORY_SEARCHBOX = 4;
Winson Chungf7bca432014-04-30 17:11:13 -070067
68 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070069 * Identity of this AppWidget component. This component should be a {@link
70 * android.content.BroadcastReceiver}, and it will be sent the AppWidget intents
71 * {@link android.appwidget as described in the AppWidget package documentation}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 *
73 * <p>This field corresponds to the <code>android:name</code> attribute in
74 * the <code>&lt;receiver&gt;</code> element in the AndroidManifest.xml file.
75 */
76 public ComponentName provider;
77
78 /**
Adam Cohen1bfaf562011-07-19 18:05:33 -070079 * The default height of the widget when added to a host, in dp. The widget will get
80 * at least this width, and will often be given more, depending on the host.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 *
82 * <p>This field corresponds to the <code>android:minWidth</code> attribute in
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070083 * the AppWidget meta-data file.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 */
85 public int minWidth;
86
87 /**
Adam Cohen1bfaf562011-07-19 18:05:33 -070088 * The default height of the widget when added to a host, in dp. The widget will get
89 * at least this height, and will often be given more, depending on the host.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 *
91 * <p>This field corresponds to the <code>android:minHeight</code> attribute in
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070092 * the AppWidget meta-data file.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 */
94 public int minHeight;
95
96 /**
Adam Cohen1bfaf562011-07-19 18:05:33 -070097 * Minimum width (in dp) which the widget can be resized to. This field has no effect if it
98 * is greater than minWidth or if horizontal resizing isn't enabled (see {@link #resizeMode}).
99 *
100 * <p>This field corresponds to the <code>android:minResizeWidth</code> attribute in
101 * the AppWidget meta-data file.
102 */
103 public int minResizeWidth;
104
105 /**
106 * Minimum height (in dp) which the widget can be resized to. This field has no effect if it
107 * is greater than minHeight or if vertical resizing isn't enabled (see {@link #resizeMode}).
108 *
109 * <p>This field corresponds to the <code>android:minResizeHeight</code> attribute in
110 * the AppWidget meta-data file.
111 */
112 public int minResizeHeight;
113
114 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700115 * How often, in milliseconds, that this AppWidget wants to be updated.
116 * The AppWidget manager may place a limit on how often a AppWidget is updated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 *
118 * <p>This field corresponds to the <code>android:updatePeriodMillis</code> attribute in
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700119 * the AppWidget meta-data file.
Joe Onorato851da842009-07-14 19:49:27 -0700120 *
121 * <p class="note"><b>Note:</b> Updates requested with <code>updatePeriodMillis</code>
122 * will not be delivered more than once every 30 minutes.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 */
124 public int updatePeriodMillis;
125
126 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700127 * The resource id of the initial layout for this AppWidget. This should be
128 * displayed until the RemoteViews for the AppWidget is available.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 *
130 * <p>This field corresponds to the <code>android:initialLayout</code> attribute in
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700131 * the AppWidget meta-data file.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 */
133 public int initialLayout;
134
135 /**
Adam Cohen0aa2d422012-09-07 17:37:26 -0700136 * The resource id of the initial layout for this AppWidget when it is displayed on keyguard.
137 * This parameter only needs to be provided if the widget can be displayed on the keyguard,
138 * see {@link #widgetCategory}.
139 *
140 * <p>This field corresponds to the <code>android:initialKeyguardLayout</code> attribute in
141 * the AppWidget meta-data file.
142 */
143 public int initialKeyguardLayout;
144
145 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700146 * The activity to launch that will configure the AppWidget.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 *
148 * <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 -0700149 * the AppWidget meta-data file. The package name always corresponds to the package containing
150 * the AppWidget provider.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 */
152 public ComponentName configure;
153
154 /**
Svetoslav976e8bd2014-07-16 15:12:03 -0700155 * The label to display to the user in the AppWidget picker.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 *
Svetoslav976e8bd2014-07-16 15:12:03 -0700157 * @deprecated Use {@link #loadLabel(android.content.pm.PackageManager)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 */
Svetoslav976e8bd2014-07-16 15:12:03 -0700159 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 public String label;
161
162 /**
Svetoslav976e8bd2014-07-16 15:12:03 -0700163 * 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 -0800164 * xml, the application icon will be used.
165 *
166 * <p>This field corresponds to the <code>android:icon</code> attribute in
167 * the <code>&lt;receiver&gt;</code> element in the AndroidManifest.xml file.
168 */
169 public int icon;
Adam Cohena02fdf12010-11-03 13:27:40 -0700170
Romain Guyd2671e12010-03-11 18:06:42 -0800171 /**
Adam Cohena02fdf12010-11-03 13:27:40 -0700172 * The view id of the AppWidget subview which should be auto-advanced by the widget's host.
Adam Cohen9611f2e2011-02-28 13:39:38 -0800173 *
174 * <p>This field corresponds to the <code>android:autoAdvanceViewId</code> attribute in
175 * the AppWidget meta-data file.
Adam Cohena02fdf12010-11-03 13:27:40 -0700176 */
177 public int autoAdvanceViewId;
178
Patrick Dubroyd2db2a52010-06-23 14:56:28 -0700179 /**
180 * A preview of what the AppWidget will look like after it's configured.
181 * If not supplied, the AppWidget's icon will be used.
182 *
183 * <p>This field corresponds to the <code>android:previewImage</code> attribute in
184 * the <code>&lt;receiver&gt;</code> element in the AndroidManifest.xml file.
Patrick Dubroyd2db2a52010-06-23 14:56:28 -0700185 */
John Spurlock8a985d22014-02-25 09:40:05 -0500186 public int previewImage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187
Adam Cohend2e20de2011-02-25 12:03:37 -0800188 /**
189 * The rules by which a widget can be resized. See {@link #RESIZE_NONE},
190 * {@link #RESIZE_NONE}, {@link #RESIZE_HORIZONTAL},
191 * {@link #RESIZE_VERTICAL}, {@link #RESIZE_BOTH}.
Adam Cohen9611f2e2011-02-28 13:39:38 -0800192 *
193 * <p>This field corresponds to the <code>android:resizeMode</code> attribute in
194 * the AppWidget meta-data file.
Adam Cohend2e20de2011-02-25 12:03:37 -0800195 */
Adam Cohen9611f2e2011-02-28 13:39:38 -0800196 public int resizeMode;
Adam Cohend2e20de2011-02-25 12:03:37 -0800197
Adam Cohen0aa2d422012-09-07 17:37:26 -0700198 /**
199 * Determines whether this widget can be displayed on the home screen, the keyguard, or both.
200 * A widget which is displayed on both needs to ensure that it follows the design guidelines
201 * for both widget classes. This can be achieved by querying the AppWidget options in its
202 * widget provider's update method.
203 *
204 * <p>This field corresponds to the <code>widgetCategory</code> attribute in
205 * the AppWidget meta-data file.
206 */
207 public int widgetCategory;
208
Svetoslav976e8bd2014-07-16 15:12:03 -0700209 /** @hide */
210 public ActivityInfo providerInfo;
211
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700212 public AppWidgetProviderInfo() {
Svetoslav976e8bd2014-07-16 15:12:03 -0700213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 }
215
216 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700217 * Unflatten the AppWidgetProviderInfo from a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 */
Svetoslav976e8bd2014-07-16 15:12:03 -0700219 @SuppressWarnings("deprecation")
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700220 public AppWidgetProviderInfo(Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 if (0 != in.readInt()) {
222 this.provider = new ComponentName(in);
223 }
224 this.minWidth = in.readInt();
225 this.minHeight = in.readInt();
Adam Cohen324afba2011-07-22 11:51:45 -0700226 this.minResizeWidth = in.readInt();
227 this.minResizeHeight = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 this.updatePeriodMillis = in.readInt();
229 this.initialLayout = in.readInt();
Adam Cohen0aa2d422012-09-07 17:37:26 -0700230 this.initialKeyguardLayout = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 if (0 != in.readInt()) {
232 this.configure = new ComponentName(in);
233 }
234 this.label = in.readString();
235 this.icon = in.readInt();
Patrick Dubroyd2db2a52010-06-23 14:56:28 -0700236 this.previewImage = in.readInt();
Adam Cohena02fdf12010-11-03 13:27:40 -0700237 this.autoAdvanceViewId = in.readInt();
Adam Cohen9611f2e2011-02-28 13:39:38 -0800238 this.resizeMode = in.readInt();
Adam Cohen0aa2d422012-09-07 17:37:26 -0700239 this.widgetCategory = in.readInt();
Svetoslav976e8bd2014-07-16 15:12:03 -0700240 this.providerInfo = in.readParcelable(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 }
242
Svetoslav976e8bd2014-07-16 15:12:03 -0700243 /**
244 * Loads the localized label to display to the user in the AppWidget picker.
245 *
246 * @param packageManager Package manager instance for loading resources.
247 * @return The label for the current locale.
248 */
249 public final String loadLabel(PackageManager packageManager) {
250 CharSequence label = providerInfo.loadLabel(packageManager);
251 if (label != null) {
252 return label.toString().trim();
253 }
254 return null;
255 }
256
257 /**
258 * Loads the icon to display for this AppWidget in the AppWidget picker. If not
259 * supplied in the xml, the application icon will be used. A client can optionally
260 * provide a desired density such as {@link android.util.DisplayMetrics#DENSITY_LOW}
261 * {@link android.util.DisplayMetrics#DENSITY_MEDIUM}, etc. If no density is
262 * provided, the density of the current display will be used.
263 * <p>
264 * The loaded icon corresponds to the <code>android:icon</code> attribute in
265 * the <code>&lt;receiver&gt;</code> element in the AndroidManifest.xml file.
266 * </p>
Svetoslav976e8bd2014-07-16 15:12:03 -0700267 *
268 * @param context Context for accessing resources.
269 * @param density The optional desired density as per
270 * {@link android.util.DisplayMetrics#densityDpi}.
Svetoslavc71c42f2014-08-05 18:57:05 -0700271 * @return The provider icon.
Svetoslav976e8bd2014-07-16 15:12:03 -0700272 */
Svetoslav8e1d2992014-08-08 12:48:06 -0700273 public final Drawable loadIcon(@NonNull Context context, int density) {
Sunny Goyal092e1962014-08-15 12:57:11 -0700274 return loadDrawable(context, density, providerInfo.getIconResource(), true);
Svetoslav976e8bd2014-07-16 15:12:03 -0700275 }
276
277 /**
278 * Loads a preview of what the AppWidget will look like after it's configured.
Sunny Goyal092e1962014-08-15 12:57:11 -0700279 * A client can optionally provide a desired density such as
280 * {@link android.util.DisplayMetrics#DENSITY_LOW}
Svetoslav976e8bd2014-07-16 15:12:03 -0700281 * {@link android.util.DisplayMetrics#DENSITY_MEDIUM}, etc. If no density is
282 * provided, the density of the current display will be used.
283 * <p>
284 * The loaded image corresponds to the <code>android:previewImage</code> attribute
285 * in the <code>&lt;receiver&gt;</code> element in the AndroidManifest.xml file.
286 * </p>
Svetoslav976e8bd2014-07-16 15:12:03 -0700287 *
288 * @param context Context for accessing resources.
289 * @param density The optional desired density as per
290 * {@link android.util.DisplayMetrics#densityDpi}.
Sunny Goyal0308d9a2014-08-15 17:39:54 -0700291 * @return The widget preview image or null if preview image is not available.
Svetoslav976e8bd2014-07-16 15:12:03 -0700292 */
Svetoslav8e1d2992014-08-08 12:48:06 -0700293 public final Drawable loadPreviewImage(@NonNull Context context, int density) {
Sunny Goyal092e1962014-08-15 12:57:11 -0700294 return loadDrawable(context, density, previewImage, false);
Svetoslav976e8bd2014-07-16 15:12:03 -0700295 }
296
297 /**
298 * Gets the user profile in which the provider resides.
299 *
300 * @return The hosting user profile.
301 */
302 public final UserHandle getProfile() {
303 return new UserHandle(UserHandle.getUserId(providerInfo.applicationInfo.uid));
304 }
305
306 @Override
307 @SuppressWarnings("deprecation")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 public void writeToParcel(android.os.Parcel out, int flags) {
309 if (this.provider != null) {
310 out.writeInt(1);
311 this.provider.writeToParcel(out, flags);
312 } else {
313 out.writeInt(0);
314 }
315 out.writeInt(this.minWidth);
316 out.writeInt(this.minHeight);
Adam Cohen324afba2011-07-22 11:51:45 -0700317 out.writeInt(this.minResizeWidth);
318 out.writeInt(this.minResizeHeight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 out.writeInt(this.updatePeriodMillis);
320 out.writeInt(this.initialLayout);
Adam Cohen0aa2d422012-09-07 17:37:26 -0700321 out.writeInt(this.initialKeyguardLayout);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 if (this.configure != null) {
323 out.writeInt(1);
324 this.configure.writeToParcel(out, flags);
325 } else {
326 out.writeInt(0);
327 }
328 out.writeString(this.label);
329 out.writeInt(this.icon);
Patrick Dubroyd2db2a52010-06-23 14:56:28 -0700330 out.writeInt(this.previewImage);
Adam Cohena02fdf12010-11-03 13:27:40 -0700331 out.writeInt(this.autoAdvanceViewId);
Adam Cohen9611f2e2011-02-28 13:39:38 -0800332 out.writeInt(this.resizeMode);
Adam Cohen0aa2d422012-09-07 17:37:26 -0700333 out.writeInt(this.widgetCategory);
Svetoslav976e8bd2014-07-16 15:12:03 -0700334 out.writeParcelable(this.providerInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 }
336
Adam Cohen3ff2d862012-09-26 14:07:57 -0700337 @Override
Svetoslav976e8bd2014-07-16 15:12:03 -0700338 @SuppressWarnings("deprecation")
Adam Cohen3ff2d862012-09-26 14:07:57 -0700339 public AppWidgetProviderInfo clone() {
340 AppWidgetProviderInfo that = new AppWidgetProviderInfo();
341 that.provider = this.provider == null ? null : this.provider.clone();
342 that.minWidth = this.minWidth;
343 that.minHeight = this.minHeight;
344 that.minResizeWidth = this.minResizeHeight;
345 that.minResizeHeight = this.minResizeHeight;
346 that.updatePeriodMillis = this.updatePeriodMillis;
Adam Cohen180cfd52012-11-04 12:03:12 -0800347 that.initialLayout = this.initialLayout;
Adam Cohen3ff2d862012-09-26 14:07:57 -0700348 that.initialKeyguardLayout = this.initialKeyguardLayout;
349 that.configure = this.configure == null ? null : this.configure.clone();
350 that.label = this.label == null ? null : this.label.substring(0);
351 that.icon = this.icon;
352 that.previewImage = this.previewImage;
353 that.autoAdvanceViewId = this.autoAdvanceViewId;
354 that.resizeMode = this.resizeMode;
Svetoslav976e8bd2014-07-16 15:12:03 -0700355 that.widgetCategory = this.widgetCategory;
356 that.providerInfo = this.providerInfo;
Adam Cohen3ff2d862012-09-26 14:07:57 -0700357 return that;
358 }
359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 public int describeContents() {
361 return 0;
362 }
363
Sunny Goyal092e1962014-08-15 12:57:11 -0700364 private Drawable loadDrawable(Context context, int density, int resourceId,
365 boolean loadDefaultIcon) {
Svetoslav976e8bd2014-07-16 15:12:03 -0700366 try {
367 Resources resources = context.getPackageManager().getResourcesForApplication(
368 providerInfo.applicationInfo);
Svetoslav976e8bd2014-07-16 15:12:03 -0700369 if (resourceId > 0) {
370 if (density <= 0) {
371 density = context.getResources().getDisplayMetrics().densityDpi;
372 }
Svetoslavc71c42f2014-08-05 18:57:05 -0700373 return resources.getDrawableForDensity(resourceId, density);
Svetoslav976e8bd2014-07-16 15:12:03 -0700374 }
375 } catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) {
376 /* ignore */
377 }
Sunny Goyal092e1962014-08-15 12:57:11 -0700378 return loadDefaultIcon ? providerInfo.loadIcon(context.getPackageManager()) : null;
Svetoslav976e8bd2014-07-16 15:12:03 -0700379 }
380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700382 * Parcelable.Creator that instantiates AppWidgetProviderInfo objects
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700384 public static final Parcelable.Creator<AppWidgetProviderInfo> CREATOR
385 = new Parcelable.Creator<AppWidgetProviderInfo>()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700387 public AppWidgetProviderInfo createFromParcel(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700389 return new AppWidgetProviderInfo(parcel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 }
391
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700392 public AppWidgetProviderInfo[] newArray(int size)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700394 return new AppWidgetProviderInfo[size];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 }
396 };
397
398 public String toString() {
Svetoslav976e8bd2014-07-16 15:12:03 -0700399 return "AppWidgetProviderInfo(" + getProfile() + '/' + provider + ')';
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 }
401}