blob: 3dd640c151bc293167f31540a415d1c717b5df20 [file] [log] [blame]
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001/*
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
17package android.appwidget;
18
19import android.content.ComponentName;
20import android.content.Context;
Winson Chung81f39eb2011-01-11 18:05:01 -080021import android.content.Intent;
Adam Cohene8724c82012-04-19 17:11:40 -070022import android.os.Bundle;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070023import android.os.IBinder;
24import android.os.RemoteException;
25import android.os.ServiceManager;
Mitsuru Oshima8f25c422009-07-01 00:10:43 -070026import android.util.DisplayMetrics;
Mitsuru Oshima8f25c422009-07-01 00:10:43 -070027import android.util.TypedValue;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070028import android.widget.RemoteViews;
29
30import com.android.internal.appwidget.IAppWidgetService;
31
32import java.lang.ref.WeakReference;
33import java.util.List;
34import java.util.WeakHashMap;
35
36/**
37 * Updates AppWidget state; gets information about installed AppWidget providers and other
38 * AppWidget related state.
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080039 *
40 * <div class="special reference">
41 * <h3>Developer Guides</h3>
42 * <p>For more information about creating app widgets, read the
43 * <a href="{@docRoot}guide/topics/appwidgets/index.html">App Widgets</a> developer guide.</p>
44 * </div>
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070045 */
46public class AppWidgetManager {
47 static final String TAG = "AppWidgetManager";
48
49 /**
50 * Send this from your {@link AppWidgetHost} activity when you want to pick an AppWidget to display.
51 * The AppWidget picker activity will be launched.
52 * <p>
53 * You must supply the following extras:
54 * <table>
55 * <tr>
56 * <td>{@link #EXTRA_APPWIDGET_ID}</td>
57 * <td>A newly allocated appWidgetId, which will be bound to the AppWidget provider
58 * once the user has selected one.</td>
59 * </tr>
60 * </table>
61 *
62 * <p>
63 * The system will respond with an onActivityResult call with the following extras in
64 * the intent:
65 * <table>
66 * <tr>
67 * <td>{@link #EXTRA_APPWIDGET_ID}</td>
68 * <td>The appWidgetId that you supplied in the original intent.</td>
69 * </tr>
70 * </table>
71 * <p>
72 * When you receive the result from the AppWidget pick activity, if the resultCode is
73 * {@link android.app.Activity#RESULT_OK}, an AppWidget has been selected. You should then
74 * check the AppWidgetProviderInfo for the returned AppWidget, and if it has one, launch its configuration
75 * activity. If {@link android.app.Activity#RESULT_CANCELED} is returned, you should delete
76 * the appWidgetId.
77 *
78 * @see #ACTION_APPWIDGET_CONFIGURE
79 */
80 public static final String ACTION_APPWIDGET_PICK = "android.appwidget.action.APPWIDGET_PICK";
81
82 /**
Michael Jurkafc753c02012-10-30 18:30:52 -070083 * Similar to ACTION_APPWIDGET_PICK, but used from keyguard
84 * @hide
85 */
86 public static final String
87 ACTION_KEYGUARD_APPWIDGET_PICK = "android.appwidget.action.KEYGUARD_APPWIDGET_PICK";
88
89 /**
Michael Jurka61a5b012012-04-13 10:39:45 -070090 * Send this from your {@link AppWidgetHost} activity when you want to bind an AppWidget to
91 * display and bindAppWidgetIdIfAllowed returns false.
92 * <p>
93 * You must supply the following extras:
94 * <table>
95 * <tr>
96 * <td>{@link #EXTRA_APPWIDGET_ID}</td>
97 * <td>A newly allocated appWidgetId, which will be bound to the AppWidget provider
98 * you provide.</td>
99 * </tr>
100 * <tr>
101 * <td>{@link #EXTRA_APPWIDGET_PROVIDER}</td>
102 * <td>The BroadcastReceiver that will be the AppWidget provider for this AppWidget.
103 * </td>
104 * </tr>
105 * </table>
106 *
107 * <p>
108 * The system will respond with an onActivityResult call with the following extras in
109 * the intent:
110 * <table>
111 * <tr>
112 * <td>{@link #EXTRA_APPWIDGET_ID}</td>
113 * <td>The appWidgetId that you supplied in the original intent.</td>
114 * </tr>
115 * </table>
116 * <p>
117 * When you receive the result from the AppWidget bind activity, if the resultCode is
118 * {@link android.app.Activity#RESULT_OK}, the AppWidget has been bound. You should then
119 * check the AppWidgetProviderInfo for the returned AppWidget, and if it has one, launch its
120 * configuration activity. If {@link android.app.Activity#RESULT_CANCELED} is returned, you
121 * should delete
122 * the appWidgetId.
123 *
124 * @see #ACTION_APPWIDGET_CONFIGURE
125 *
126 */
127 public static final String ACTION_APPWIDGET_BIND = "android.appwidget.action.APPWIDGET_BIND";
128
129 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700130 * Sent when it is time to configure your AppWidget while it is being added to a host.
131 * This action is not sent as a broadcast to the AppWidget provider, but as a startActivity
132 * to the activity specified in the {@link AppWidgetProviderInfo AppWidgetProviderInfo meta-data}.
133 *
134 * <p>
135 * The intent will contain the following extras:
136 * <table>
137 * <tr>
138 * <td>{@link #EXTRA_APPWIDGET_ID}</td>
139 * <td>The appWidgetId to configure.</td>
140 * </tr>
141 * </table>
142 *
143 * <p>If you return {@link android.app.Activity#RESULT_OK} using
144 * {@link android.app.Activity#setResult Activity.setResult()}, the AppWidget will be added,
145 * and you will receive an {@link #ACTION_APPWIDGET_UPDATE} broadcast for this AppWidget.
146 * If you return {@link android.app.Activity#RESULT_CANCELED}, the host will cancel the add
147 * and not display this AppWidget, and you will receive a {@link #ACTION_APPWIDGET_DELETED} broadcast.
148 */
149 public static final String ACTION_APPWIDGET_CONFIGURE = "android.appwidget.action.APPWIDGET_CONFIGURE";
150
151 /**
152 * An intent extra that contains one appWidgetId.
153 * <p>
154 * The value will be an int that can be retrieved like this:
155 * {@sample frameworks/base/tests/appwidgets/AppWidgetHostTest/src/com/android/tests/appwidgethost/AppWidgetHostActivity.java getExtra_EXTRA_APPWIDGET_ID}
156 */
157 public static final String EXTRA_APPWIDGET_ID = "appWidgetId";
158
159 /**
Adam Cohen0aa2d422012-09-07 17:37:26 -0700160 * A bundle extra that contains the lower bound on the current width, in dips, of a widget instance.
Adam Cohene8724c82012-04-19 17:11:40 -0700161 */
Adam Cohend2097eb2012-05-01 18:10:28 -0700162 public static final String OPTION_APPWIDGET_MIN_WIDTH = "appWidgetMinWidth";
Adam Cohene8724c82012-04-19 17:11:40 -0700163
164 /**
Adam Cohen0aa2d422012-09-07 17:37:26 -0700165 * A bundle extra that contains the lower bound on the current height, in dips, of a widget instance.
Adam Cohene8724c82012-04-19 17:11:40 -0700166 */
Adam Cohend2097eb2012-05-01 18:10:28 -0700167 public static final String OPTION_APPWIDGET_MIN_HEIGHT = "appWidgetMinHeight";
Adam Cohene8724c82012-04-19 17:11:40 -0700168
169 /**
Adam Cohen0aa2d422012-09-07 17:37:26 -0700170 * A bundle extra that contains the upper bound on the current width, in dips, of a widget instance.
Adam Cohene8724c82012-04-19 17:11:40 -0700171 */
Adam Cohend2097eb2012-05-01 18:10:28 -0700172 public static final String OPTION_APPWIDGET_MAX_WIDTH = "appWidgetMaxWidth";
Adam Cohene8724c82012-04-19 17:11:40 -0700173
174 /**
Adam Cohen0aa2d422012-09-07 17:37:26 -0700175 * A bundle extra that contains the upper bound on the current width, in dips, of a widget instance.
Adam Cohene8724c82012-04-19 17:11:40 -0700176 */
Adam Cohend2097eb2012-05-01 18:10:28 -0700177 public static final String OPTION_APPWIDGET_MAX_HEIGHT = "appWidgetMaxHeight";
Adam Cohene8724c82012-04-19 17:11:40 -0700178
179 /**
Adam Cohen0aa2d422012-09-07 17:37:26 -0700180 * A bundle extra that hints to the AppWidgetProvider the category of host that owns this
181 * this widget. Can have the value {@link
182 * AppWidgetProviderInfo#WIDGET_CATEGORY_HOME_SCREEN} or {@link
183 * AppWidgetProviderInfo#WIDGET_CATEGORY_KEYGUARD}.
184 */
185 public static final String OPTION_APPWIDGET_HOST_CATEGORY = "appWidgetCategory";
186
187 /**
Adam Cohene8724c82012-04-19 17:11:40 -0700188 * An intent extra which points to a bundle of extra information for a particular widget id.
189 * In particular this bundle can contain EXTRA_APPWIDGET_WIDTH and EXTRA_APPWIDGET_HEIGHT.
190 */
Adam Cohend2097eb2012-05-01 18:10:28 -0700191 public static final String EXTRA_APPWIDGET_OPTIONS = "appWidgetOptions";
Adam Cohene8724c82012-04-19 17:11:40 -0700192
193 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700194 * An intent extra that contains multiple appWidgetIds.
195 * <p>
196 * The value will be an int array that can be retrieved like this:
197 * {@sample frameworks/base/tests/appwidgets/AppWidgetHostTest/src/com/android/tests/appwidgethost/TestAppWidgetProvider.java getExtra_EXTRA_APPWIDGET_IDS}
198 */
199 public static final String EXTRA_APPWIDGET_IDS = "appWidgetIds";
200
201 /**
Michael Jurka61a5b012012-04-13 10:39:45 -0700202 * An intent extra that contains the component name of a AppWidget provider.
203 * <p>
204 * The value will be an ComponentName.
205 */
206 public static final String EXTRA_APPWIDGET_PROVIDER = "appWidgetProvider";
207
208 /**
The Android Open Source Project10592532009-03-18 17:39:46 -0700209 * An intent extra to pass to the AppWidget picker containing a {@link java.util.List} of
210 * {@link AppWidgetProviderInfo} objects to mix in to the list of AppWidgets that are
211 * installed. (This is how the launcher shows the search widget).
212 */
213 public static final String EXTRA_CUSTOM_INFO = "customInfo";
214
215 /**
216 * An intent extra to pass to the AppWidget picker containing a {@link java.util.List} of
217 * {@link android.os.Bundle} objects to mix in to the list of AppWidgets that are
218 * installed. It will be added to the extras object on the {@link android.content.Intent}
219 * that is returned from the picker activity.
220 *
221 * {@more}
222 */
223 public static final String EXTRA_CUSTOM_EXTRAS = "customExtras";
224
225 /**
Adam Cohen8c09f632012-09-11 18:23:35 -0700226 * An intent extra to pass to the AppWidget picker which allows the picker to filter
227 * the list based on the {@link AppWidgetProviderInfo#widgetCategory}.
Jim Millerf229e4d2012-09-12 20:32:50 -0700228 *
229 * @hide
Adam Cohen8c09f632012-09-11 18:23:35 -0700230 */
Adam Cohen8c09f632012-09-11 18:23:35 -0700231 public static final String EXTRA_CATEGORY_FILTER = "categoryFilter";
232
233 /**
Jim Millerf229e4d2012-09-12 20:32:50 -0700234 * An intent extra to pass to the AppWidget picker to specify whether or not to sort
235 * the list of caller-specified extra AppWidgets along with the rest of the AppWidgets
236 * @hide
237 */
238 public static final String EXTRA_CUSTOM_SORT = "customSort";
239
240 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700241 * A sentiel value that the AppWidget manager will never return as a appWidgetId.
242 */
243 public static final int INVALID_APPWIDGET_ID = 0;
244
245 /**
246 * Sent when it is time to update your AppWidget.
247 *
248 * <p>This may be sent in response to a new instance for this AppWidget provider having
249 * been instantiated, the requested {@link AppWidgetProviderInfo#updatePeriodMillis update interval}
250 * having lapsed, or the system booting.
251 *
252 * <p>
253 * The intent will contain the following extras:
254 * <table>
255 * <tr>
256 * <td>{@link #EXTRA_APPWIDGET_IDS}</td>
257 * <td>The appWidgetIds to update. This may be all of the AppWidgets created for this
258 * provider, or just a subset. The system tries to send updates for as few AppWidget
259 * instances as possible.</td>
260 * </tr>
261 * </table>
Christian Mehlmauer10c543d2010-06-25 19:27:04 +0200262 *
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700263 * @see AppWidgetProvider#onUpdate AppWidgetProvider.onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
264 */
265 public static final String ACTION_APPWIDGET_UPDATE = "android.appwidget.action.APPWIDGET_UPDATE";
266
267 /**
Adam Cohene8724c82012-04-19 17:11:40 -0700268 * Sent when the custom extras for an AppWidget change.
269 *
Katie McCormick0e9f34b2012-09-10 15:32:46 -0700270 * @see AppWidgetProvider#onAppWidgetOptionsChanged
271 * AppWidgetProvider.onAppWidgetOptionsChanged(Context context,
272 * AppWidgetManager appWidgetManager, int appWidgetId, Bundle newExtras)
Adam Cohene8724c82012-04-19 17:11:40 -0700273 */
Adam Cohend2097eb2012-05-01 18:10:28 -0700274 public static final String ACTION_APPWIDGET_OPTIONS_CHANGED = "android.appwidget.action.APPWIDGET_UPDATE_OPTIONS";
Adam Cohene8724c82012-04-19 17:11:40 -0700275
276 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700277 * Sent when an instance of an AppWidget is deleted from its host.
278 *
279 * @see AppWidgetProvider#onDeleted AppWidgetProvider.onDeleted(Context context, int[] appWidgetIds)
280 */
281 public static final String ACTION_APPWIDGET_DELETED = "android.appwidget.action.APPWIDGET_DELETED";
282
283 /**
284 * Sent when an instance of an AppWidget is removed from the last host.
Christian Mehlmauer10c543d2010-06-25 19:27:04 +0200285 *
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700286 * @see AppWidgetProvider#onEnabled AppWidgetProvider.onEnabled(Context context)
287 */
288 public static final String ACTION_APPWIDGET_DISABLED = "android.appwidget.action.APPWIDGET_DISABLED";
289
290 /**
291 * Sent when an instance of an AppWidget is added to a host for the first time.
292 * This broadcast is sent at boot time if there is a AppWidgetHost installed with
293 * an instance for this provider.
Christian Mehlmauer10c543d2010-06-25 19:27:04 +0200294 *
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700295 * @see AppWidgetProvider#onEnabled AppWidgetProvider.onEnabled(Context context)
296 */
297 public static final String ACTION_APPWIDGET_ENABLED = "android.appwidget.action.APPWIDGET_ENABLED";
298
299 /**
300 * Field for the manifest meta-data tag.
301 *
302 * @see AppWidgetProviderInfo
303 */
304 public static final String META_DATA_APPWIDGET_PROVIDER = "android.appwidget.provider";
Christian Mehlmauer10c543d2010-06-25 19:27:04 +0200305
Christian Mehlmauer10c543d2010-06-25 19:27:04 +0200306 static WeakHashMap<Context, WeakReference<AppWidgetManager>> sManagerCache =
307 new WeakHashMap<Context, WeakReference<AppWidgetManager>>();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700308 static IAppWidgetService sService;
Christian Mehlmauer10c543d2010-06-25 19:27:04 +0200309
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700310 Context mContext;
311
Mitsuru Oshima8f25c422009-07-01 00:10:43 -0700312 private DisplayMetrics mDisplayMetrics;
313
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700314 /**
315 * Get the AppWidgetManager instance to use for the supplied {@link android.content.Context
316 * Context} object.
317 */
318 public static AppWidgetManager getInstance(Context context) {
319 synchronized (sManagerCache) {
320 if (sService == null) {
321 IBinder b = ServiceManager.getService(Context.APPWIDGET_SERVICE);
322 sService = IAppWidgetService.Stub.asInterface(b);
323 }
324
325 WeakReference<AppWidgetManager> ref = sManagerCache.get(context);
326 AppWidgetManager result = null;
327 if (ref != null) {
328 result = ref.get();
329 }
330 if (result == null) {
331 result = new AppWidgetManager(context);
Christian Mehlmauer10c543d2010-06-25 19:27:04 +0200332 sManagerCache.put(context, new WeakReference<AppWidgetManager>(result));
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700333 }
334 return result;
335 }
336 }
337
338 private AppWidgetManager(Context context) {
339 mContext = context;
Mitsuru Oshima8f25c422009-07-01 00:10:43 -0700340 mDisplayMetrics = context.getResources().getDisplayMetrics();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700341 }
342
343 /**
344 * Set the RemoteViews to use for the specified appWidgetIds.
345 *
Adam Cohen2dd21972010-08-15 18:20:04 -0700346 * Note that the RemoteViews parameter will be cached by the AppWidgetService, and hence should
347 * contain a complete representation of the widget. For performing partial widget updates, see
348 * {@link #partiallyUpdateAppWidget(int[], RemoteViews)}.
349 *
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700350 * <p>
351 * It is okay to call this method both inside an {@link #ACTION_APPWIDGET_UPDATE} broadcast,
352 * and outside of the handler.
353 * This method will only work when called from the uid that owns the AppWidget provider.
Adam Cohen311c79c2012-05-10 14:44:38 -0700354 *
355 * <p>
356 * The total Bitmap memory used by the RemoteViews object cannot exceed that required to
Michael Jurkaf25ab442012-06-25 15:11:21 -0700357 * fill the screen 1.5 times, ie. (screen width x screen height x 4 x 1.5) bytes.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700358 *
359 * @param appWidgetIds The AppWidget instances for which to set the RemoteViews.
360 * @param views The RemoteViews object to show.
361 */
362 public void updateAppWidget(int[] appWidgetIds, RemoteViews views) {
363 try {
364 sService.updateAppWidgetIds(appWidgetIds, views);
365 }
366 catch (RemoteException e) {
367 throw new RuntimeException("system server dead?", e);
368 }
369 }
370
371 /**
Adam Cohene8724c82012-04-19 17:11:40 -0700372 * Update the extras for a given widget instance.
373 *
374 * The extras can be used to embed additional information about this widget to be accessed
375 * by the associated widget's AppWidgetProvider.
376 *
Adam Cohend2097eb2012-05-01 18:10:28 -0700377 * @see #getAppWidgetOptions(int)
Adam Cohene8724c82012-04-19 17:11:40 -0700378 *
379 * @param appWidgetId The AppWidget instances for which to set the RemoteViews.
Adam Cohend2097eb2012-05-01 18:10:28 -0700380 * @param options The options to associate with this widget
Adam Cohene8724c82012-04-19 17:11:40 -0700381 */
Adam Cohend2097eb2012-05-01 18:10:28 -0700382 public void updateAppWidgetOptions(int appWidgetId, Bundle options) {
Adam Cohene8724c82012-04-19 17:11:40 -0700383 try {
Adam Cohend2097eb2012-05-01 18:10:28 -0700384 sService.updateAppWidgetOptions(appWidgetId, options);
Adam Cohene8724c82012-04-19 17:11:40 -0700385 }
386 catch (RemoteException e) {
387 throw new RuntimeException("system server dead?", e);
388 }
389 }
390
391 /**
392 * Get the extras associated with a given widget instance.
393 *
394 * The extras can be used to embed additional information about this widget to be accessed
395 * by the associated widget's AppWidgetProvider.
396 *
Adam Cohend2097eb2012-05-01 18:10:28 -0700397 * @see #updateAppWidgetOptions(int, Bundle)
Adam Cohene8724c82012-04-19 17:11:40 -0700398 *
399 * @param appWidgetId The AppWidget instances for which to set the RemoteViews.
Adam Cohend2097eb2012-05-01 18:10:28 -0700400 * @return The options associated with the given widget instance.
Adam Cohene8724c82012-04-19 17:11:40 -0700401 */
Adam Cohend2097eb2012-05-01 18:10:28 -0700402 public Bundle getAppWidgetOptions(int appWidgetId) {
Adam Cohene8724c82012-04-19 17:11:40 -0700403 try {
Adam Cohend2097eb2012-05-01 18:10:28 -0700404 return sService.getAppWidgetOptions(appWidgetId);
Adam Cohene8724c82012-04-19 17:11:40 -0700405 }
406 catch (RemoteException e) {
407 throw new RuntimeException("system server dead?", e);
408 }
409 }
410
411 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700412 * Set the RemoteViews to use for the specified appWidgetId.
413 *
Adam Cohen2dd21972010-08-15 18:20:04 -0700414 * Note that the RemoteViews parameter will be cached by the AppWidgetService, and hence should
415 * contain a complete representation of the widget. For performing partial widget updates, see
416 * {@link #partiallyUpdateAppWidget(int, RemoteViews)}.
417 *
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700418 * <p>
419 * It is okay to call this method both inside an {@link #ACTION_APPWIDGET_UPDATE} broadcast,
420 * and outside of the handler.
421 * This method will only work when called from the uid that owns the AppWidget provider.
422 *
Adam Cohen311c79c2012-05-10 14:44:38 -0700423 * <p>
424 * The total Bitmap memory used by the RemoteViews object cannot exceed that required to
Michael Jurkaf25ab442012-06-25 15:11:21 -0700425 * fill the screen 1.5 times, ie. (screen width x screen height x 4 x 1.5) bytes.
Adam Cohen311c79c2012-05-10 14:44:38 -0700426 *
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700427 * @param appWidgetId The AppWidget instance for which to set the RemoteViews.
428 * @param views The RemoteViews object to show.
429 */
430 public void updateAppWidget(int appWidgetId, RemoteViews views) {
431 updateAppWidget(new int[] { appWidgetId }, views);
432 }
433
434 /**
Adam Cohen2dd21972010-08-15 18:20:04 -0700435 * Perform an incremental update or command on the widget(s) specified by appWidgetIds.
436 *
437 * This update differs from {@link #updateAppWidget(int[], RemoteViews)} in that the
438 * RemoteViews object which is passed is understood to be an incomplete representation of the
Adam Cohenfbe44b72012-09-19 20:36:23 -0700439 * widget, and hence does not replace the cached representation of the widget. As of API
440 * level 17, the new properties set within the views objects will be appended to the cached
441 * representation of the widget, and hence will persist.
Adam Cohen2dd21972010-08-15 18:20:04 -0700442 *
443 * Use with {@link RemoteViews#showNext(int)}, {@link RemoteViews#showPrevious(int)},
444 * {@link RemoteViews#setScrollPosition(int, int)} and similar commands.
445 *
446 * <p>
447 * It is okay to call this method both inside an {@link #ACTION_APPWIDGET_UPDATE} broadcast,
448 * and outside of the handler.
449 * This method will only work when called from the uid that owns the AppWidget provider.
450 *
Winson Chung66119882012-10-11 14:26:25 -0700451 * <p>
452 * This method will be ignored if a widget has not received a full update via
453 * {@link #updateAppWidget(int[], RemoteViews)}.
454 *
Adam Cohen2dd21972010-08-15 18:20:04 -0700455 * @param appWidgetIds The AppWidget instances for which to set the RemoteViews.
456 * @param views The RemoteViews object containing the incremental update / command.
457 */
458 public void partiallyUpdateAppWidget(int[] appWidgetIds, RemoteViews views) {
459 try {
460 sService.partiallyUpdateAppWidgetIds(appWidgetIds, views);
461 } catch (RemoteException e) {
462 throw new RuntimeException("system server dead?", e);
463 }
464 }
465
466 /**
467 * Perform an incremental update or command on the widget specified by appWidgetId.
468 *
469 * This update differs from {@link #updateAppWidget(int, RemoteViews)} in that the RemoteViews
470 * object which is passed is understood to be an incomplete representation of the widget, and
471 * hence is not cached by the AppWidgetService. Note that because these updates are not cached,
472 * any state that they modify that is not restored by restoreInstanceState will not persist in
473 * the case that the widgets are restored using the cached version in AppWidgetService.
474 *
475 * Use with {@link RemoteViews#showNext(int)}, {@link RemoteViews#showPrevious(int)},
476 * {@link RemoteViews#setScrollPosition(int, int)} and similar commands.
477 *
478 * <p>
479 * It is okay to call this method both inside an {@link #ACTION_APPWIDGET_UPDATE} broadcast,
480 * and outside of the handler.
481 * This method will only work when called from the uid that owns the AppWidget provider.
482 *
Winson Chung66119882012-10-11 14:26:25 -0700483 * <p>
484 * This method will be ignored if a widget has not received a full update via
485 * {@link #updateAppWidget(int[], RemoteViews)}.
486 *
Adam Cohen2dd21972010-08-15 18:20:04 -0700487 * @param appWidgetId The AppWidget instance for which to set the RemoteViews.
488 * @param views The RemoteViews object containing the incremental update / command.
489 */
490 public void partiallyUpdateAppWidget(int appWidgetId, RemoteViews views) {
491 partiallyUpdateAppWidget(new int[] { appWidgetId }, views);
492 }
493
494 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700495 * Set the RemoteViews to use for all AppWidget instances for the supplied AppWidget provider.
496 *
497 * <p>
498 * It is okay to call this method both inside an {@link #ACTION_APPWIDGET_UPDATE} broadcast,
499 * and outside of the handler.
500 * This method will only work when called from the uid that owns the AppWidget provider.
501 *
502 * @param provider The {@link ComponentName} for the {@link
503 * android.content.BroadcastReceiver BroadcastReceiver} provider
504 * for your AppWidget.
505 * @param views The RemoteViews object to show.
506 */
507 public void updateAppWidget(ComponentName provider, RemoteViews views) {
508 try {
509 sService.updateAppWidgetProvider(provider, views);
510 }
511 catch (RemoteException e) {
512 throw new RuntimeException("system server dead?", e);
513 }
514 }
515
516 /**
Winson Chung499cb9f2010-07-16 11:18:17 -0700517 * Notifies the specified collection view in all the specified AppWidget instances
518 * to invalidate their currently data.
519 *
520 * @param appWidgetIds The AppWidget instances for which to notify of view data changes.
Winson Chung499cb9f2010-07-16 11:18:17 -0700521 * @param viewId The collection view id.
522 */
Winson Chung6394c0e2010-08-16 10:14:56 -0700523 public void notifyAppWidgetViewDataChanged(int[] appWidgetIds, int viewId) {
Winson Chung499cb9f2010-07-16 11:18:17 -0700524 try {
Winson Chung6394c0e2010-08-16 10:14:56 -0700525 sService.notifyAppWidgetViewDataChanged(appWidgetIds, viewId);
Winson Chung499cb9f2010-07-16 11:18:17 -0700526 }
527 catch (RemoteException e) {
528 throw new RuntimeException("system server dead?", e);
529 }
530 }
531
532 /**
533 * Notifies the specified collection view in all the specified AppWidget instance
534 * to invalidate it's currently data.
535 *
536 * @param appWidgetId The AppWidget instance for which to notify of view data changes.
Winson Chung499cb9f2010-07-16 11:18:17 -0700537 * @param viewId The collection view id.
538 */
Winson Chung6394c0e2010-08-16 10:14:56 -0700539 public void notifyAppWidgetViewDataChanged(int appWidgetId, int viewId) {
540 notifyAppWidgetViewDataChanged(new int[] { appWidgetId }, viewId);
Winson Chung499cb9f2010-07-16 11:18:17 -0700541 }
542
543 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700544 * Return a list of the AppWidget providers that are currently installed.
545 */
546 public List<AppWidgetProviderInfo> getInstalledProviders() {
547 try {
Patrick Dubroy5d140912010-07-12 10:25:27 -0700548 List<AppWidgetProviderInfo> providers = sService.getInstalledProviders();
549 for (AppWidgetProviderInfo info : providers) {
550 // Converting complex to dp.
551 info.minWidth =
552 TypedValue.complexToDimensionPixelSize(info.minWidth, mDisplayMetrics);
553 info.minHeight =
554 TypedValue.complexToDimensionPixelSize(info.minHeight, mDisplayMetrics);
Adam Cohen324afba2011-07-22 11:51:45 -0700555 info.minResizeWidth =
556 TypedValue.complexToDimensionPixelSize(info.minResizeWidth, mDisplayMetrics);
557 info.minResizeHeight =
558 TypedValue.complexToDimensionPixelSize(info.minResizeHeight, mDisplayMetrics);
Patrick Dubroy5d140912010-07-12 10:25:27 -0700559 }
560 return providers;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700561 }
562 catch (RemoteException e) {
563 throw new RuntimeException("system server dead?", e);
564 }
565 }
566
567 /**
568 * Get the available info about the AppWidget.
569 *
570 * @return A appWidgetId. If the appWidgetId has not been bound to a provider yet, or
571 * you don't have access to that appWidgetId, null is returned.
572 */
573 public AppWidgetProviderInfo getAppWidgetInfo(int appWidgetId) {
574 try {
Mitsuru Oshima8f25c422009-07-01 00:10:43 -0700575 AppWidgetProviderInfo info = sService.getAppWidgetInfo(appWidgetId);
576 if (info != null) {
577 // Converting complex to dp.
Christian Mehlmauer10c543d2010-06-25 19:27:04 +0200578 info.minWidth =
Mitsuru Oshima8f25c422009-07-01 00:10:43 -0700579 TypedValue.complexToDimensionPixelSize(info.minWidth, mDisplayMetrics);
580 info.minHeight =
581 TypedValue.complexToDimensionPixelSize(info.minHeight, mDisplayMetrics);
Adam Cohen324afba2011-07-22 11:51:45 -0700582 info.minResizeWidth =
583 TypedValue.complexToDimensionPixelSize(info.minResizeWidth, mDisplayMetrics);
584 info.minResizeHeight =
585 TypedValue.complexToDimensionPixelSize(info.minResizeHeight, mDisplayMetrics);
Mitsuru Oshima8f25c422009-07-01 00:10:43 -0700586 }
587 return info;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700588 }
589 catch (RemoteException e) {
590 throw new RuntimeException("system server dead?", e);
591 }
592 }
593
594 /**
595 * Set the component for a given appWidgetId.
596 *
Michael Jurka61a5b012012-04-13 10:39:45 -0700597 * <p class="note">You need the BIND_APPWIDGET permission or the user must have enabled binding
598 * widgets always for your component. This method is used by the AppWidget picker and
599 * should not be used by other apps.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700600 *
601 * @param appWidgetId The AppWidget instance for which to set the RemoteViews.
602 * @param provider The {@link android.content.BroadcastReceiver} that will be the AppWidget
603 * provider for this AppWidget.
Michael Jurka61a5b012012-04-13 10:39:45 -0700604 * @hide
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700605 */
606 public void bindAppWidgetId(int appWidgetId, ComponentName provider) {
607 try {
Adam Cohen0aa2d422012-09-07 17:37:26 -0700608 sService.bindAppWidgetId(appWidgetId, provider, null);
609 }
610 catch (RemoteException e) {
611 throw new RuntimeException("system server dead?", e);
612 }
613 }
614
615 /**
616 * Set the component for a given appWidgetId.
617 *
618 * <p class="note">You need the BIND_APPWIDGET permission or the user must have enabled binding
619 * widgets always for your component. This method is used by the AppWidget picker and
620 * should not be used by other apps.
621 *
622 * @param appWidgetId The AppWidget instance for which to set the RemoteViews.
623 * @param provider The {@link android.content.BroadcastReceiver} that will be the AppWidget
624 * provider for this AppWidget.
625 * @param options Bundle containing options for the AppWidget. See also
626 * {@link #updateAppWidgetOptions(int, Bundle)}
627 *
628 * @hide
629 */
630 public void bindAppWidgetId(int appWidgetId, ComponentName provider, Bundle options) {
631 try {
632 sService.bindAppWidgetId(appWidgetId, provider, options);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700633 }
634 catch (RemoteException e) {
635 throw new RuntimeException("system server dead?", e);
636 }
637 }
638
639 /**
Michael Jurka61a5b012012-04-13 10:39:45 -0700640 * Set the component for a given appWidgetId.
641 *
642 * <p class="note">You need the BIND_APPWIDGET permission or the user must have enabled binding
643 * widgets always for your component. Should be used by apps that host widgets; if this
644 * method returns false, call {@link #ACTION_APPWIDGET_BIND} to request permission to
645 * bind
646 *
647 * @param appWidgetId The AppWidget instance for which to set the RemoteViews.
648 * @param provider The {@link android.content.BroadcastReceiver} that will be the AppWidget
649 * provider for this AppWidget.
650 * @return true if this component has permission to bind the AppWidget
651 */
652 public boolean bindAppWidgetIdIfAllowed(int appWidgetId, ComponentName provider) {
653 if (mContext == null) {
654 return false;
655 }
656 try {
657 return sService.bindAppWidgetIdIfAllowed(
Adam Cohen0aa2d422012-09-07 17:37:26 -0700658 mContext.getPackageName(), appWidgetId, provider, null);
659 }
660 catch (RemoteException e) {
661 throw new RuntimeException("system server dead?", e);
662 }
663 }
664
665 /**
666 * Set the component for a given appWidgetId.
667 *
668 * <p class="note">You need the BIND_APPWIDGET permission or the user must have enabled binding
669 * widgets always for your component. Should be used by apps that host widgets; if this
670 * method returns false, call {@link #ACTION_APPWIDGET_BIND} to request permission to
671 * bind
672 *
673 * @param appWidgetId The AppWidget instance for which to set the RemoteViews.
674 * @param provider The {@link android.content.BroadcastReceiver} that will be the AppWidget
675 * provider for this AppWidget.
676 * @param options Bundle containing options for the AppWidget. See also
677 * {@link #updateAppWidgetOptions(int, Bundle)}
678 *
679 * @return true if this component has permission to bind the AppWidget
680 */
681 public boolean bindAppWidgetIdIfAllowed(int appWidgetId, ComponentName provider,
682 Bundle options) {
683 if (mContext == null) {
684 return false;
685 }
686 try {
687 return sService.bindAppWidgetIdIfAllowed(
688 mContext.getPackageName(), appWidgetId, provider, options);
Michael Jurka61a5b012012-04-13 10:39:45 -0700689 }
690 catch (RemoteException e) {
691 throw new RuntimeException("system server dead?", e);
692 }
693 }
694
695 /**
696 * Query if a given package was granted permission by the user to bind app widgets
697 *
698 * <p class="note">You need the MODIFY_APPWIDGET_BIND_PERMISSIONS permission
699 *
700 * @param packageName The package for which the permission is being queried
701 * @return true if the package was granted permission by the user to bind app widgets
702 * @hide
703 */
704 public boolean hasBindAppWidgetPermission(String packageName) {
705 try {
706 return sService.hasBindAppWidgetPermission(packageName);
707 }
708 catch (RemoteException e) {
709 throw new RuntimeException("system server dead?", e);
710 }
711 }
712
713 /**
714 * Changes any user-granted permission for the given package to bind app widgets
715 *
716 * <p class="note">You need the MODIFY_APPWIDGET_BIND_PERMISSIONS permission
717 *
718 * @param provider The package whose permission is being changed
719 * @param permission Whether to give the package permission to bind widgets
720 * @hide
721 */
722 public void setBindAppWidgetPermission(String packageName, boolean permission) {
723 try {
724 sService.setBindAppWidgetPermission(packageName, permission);
725 }
726 catch (RemoteException e) {
727 throw new RuntimeException("system server dead?", e);
728 }
729 }
730
731 /**
Winson Chung81f39eb2011-01-11 18:05:01 -0800732 * Binds the RemoteViewsService for a given appWidgetId and intent.
733 *
734 * The appWidgetId specified must already be bound to the calling AppWidgetHost via
735 * {@link android.appwidget.AppWidgetManager#bindAppWidgetId AppWidgetManager.bindAppWidgetId()}.
736 *
737 * @param appWidgetId The AppWidget instance for which to bind the RemoteViewsService.
738 * @param intent The intent of the service which will be providing the data to the
739 * RemoteViewsAdapter.
740 * @param connection The callback interface to be notified when a connection is made or lost.
741 * @hide
742 */
743 public void bindRemoteViewsService(int appWidgetId, Intent intent, IBinder connection) {
744 try {
745 sService.bindRemoteViewsService(appWidgetId, intent, connection);
746 }
747 catch (RemoteException e) {
748 throw new RuntimeException("system server dead?", e);
749 }
750 }
751
752 /**
753 * Unbinds the RemoteViewsService for a given appWidgetId and intent.
754 *
755 * The appWidgetId specified muse already be bound to the calling AppWidgetHost via
756 * {@link android.appwidget.AppWidgetManager#bindAppWidgetId AppWidgetManager.bindAppWidgetId()}.
757 *
758 * @param appWidgetId The AppWidget instance for which to bind the RemoteViewsService.
759 * @param intent The intent of the service which will be providing the data to the
760 * RemoteViewsAdapter.
761 * @hide
762 */
763 public void unbindRemoteViewsService(int appWidgetId, Intent intent) {
764 try {
765 sService.unbindRemoteViewsService(appWidgetId, intent);
766 }
767 catch (RemoteException e) {
768 throw new RuntimeException("system server dead?", e);
769 }
770 }
771
772 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700773 * Get the list of appWidgetIds that have been bound to the given AppWidget
774 * provider.
Christian Mehlmauer10c543d2010-06-25 19:27:04 +0200775 *
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700776 * @param provider The {@link android.content.BroadcastReceiver} that is the
777 * AppWidget provider to find appWidgetIds for.
778 */
779 public int[] getAppWidgetIds(ComponentName provider) {
780 try {
781 return sService.getAppWidgetIds(provider);
782 }
783 catch (RemoteException e) {
784 throw new RuntimeException("system server dead?", e);
785 }
786 }
787}
788