The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.appwidget; |
| 18 | |
| 19 | import android.content.ComponentName; |
| 20 | import android.content.Context; |
Winson Chung | 81f39eb | 2011-01-11 18:05:01 -0800 | [diff] [blame] | 21 | import android.content.Intent; |
Adam Cohen | e8724c8 | 2012-04-19 17:11:40 -0700 | [diff] [blame] | 22 | import android.os.Bundle; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 23 | import android.os.IBinder; |
| 24 | import android.os.RemoteException; |
| 25 | import android.os.ServiceManager; |
Amith Yamasani | c566b43 | 2012-11-30 15:26:21 -0800 | [diff] [blame] | 26 | import android.os.UserHandle; |
Mitsuru Oshima | 8f25c42 | 2009-07-01 00:10:43 -0700 | [diff] [blame] | 27 | import android.util.DisplayMetrics; |
Mitsuru Oshima | 8f25c42 | 2009-07-01 00:10:43 -0700 | [diff] [blame] | 28 | import android.util.TypedValue; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 29 | import android.widget.RemoteViews; |
| 30 | |
| 31 | import com.android.internal.appwidget.IAppWidgetService; |
| 32 | |
| 33 | import java.lang.ref.WeakReference; |
| 34 | import java.util.List; |
| 35 | import java.util.WeakHashMap; |
| 36 | |
| 37 | /** |
| 38 | * Updates AppWidget state; gets information about installed AppWidget providers and other |
| 39 | * AppWidget related state. |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 40 | * |
| 41 | * <div class="special reference"> |
| 42 | * <h3>Developer Guides</h3> |
| 43 | * <p>For more information about creating app widgets, read the |
| 44 | * <a href="{@docRoot}guide/topics/appwidgets/index.html">App Widgets</a> developer guide.</p> |
| 45 | * </div> |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 46 | */ |
| 47 | public class AppWidgetManager { |
| 48 | static final String TAG = "AppWidgetManager"; |
| 49 | |
| 50 | /** |
Dianne Hackborn | a40cfeb | 2013-03-25 17:49:36 -0700 | [diff] [blame] | 51 | * Activity action to launch from your {@link AppWidgetHost} activity when you want to |
| 52 | * pick an AppWidget to display. The AppWidget picker activity will be launched. |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 53 | * <p> |
| 54 | * You must supply the following extras: |
| 55 | * <table> |
| 56 | * <tr> |
| 57 | * <td>{@link #EXTRA_APPWIDGET_ID}</td> |
| 58 | * <td>A newly allocated appWidgetId, which will be bound to the AppWidget provider |
| 59 | * once the user has selected one.</td> |
| 60 | * </tr> |
| 61 | * </table> |
| 62 | * |
| 63 | * <p> |
| 64 | * The system will respond with an onActivityResult call with the following extras in |
| 65 | * the intent: |
| 66 | * <table> |
| 67 | * <tr> |
| 68 | * <td>{@link #EXTRA_APPWIDGET_ID}</td> |
| 69 | * <td>The appWidgetId that you supplied in the original intent.</td> |
| 70 | * </tr> |
| 71 | * </table> |
| 72 | * <p> |
| 73 | * When you receive the result from the AppWidget pick activity, if the resultCode is |
| 74 | * {@link android.app.Activity#RESULT_OK}, an AppWidget has been selected. You should then |
| 75 | * check the AppWidgetProviderInfo for the returned AppWidget, and if it has one, launch its configuration |
| 76 | * activity. If {@link android.app.Activity#RESULT_CANCELED} is returned, you should delete |
| 77 | * the appWidgetId. |
| 78 | * |
| 79 | * @see #ACTION_APPWIDGET_CONFIGURE |
| 80 | */ |
| 81 | public static final String ACTION_APPWIDGET_PICK = "android.appwidget.action.APPWIDGET_PICK"; |
| 82 | |
| 83 | /** |
Michael Jurka | fc753c0 | 2012-10-30 18:30:52 -0700 | [diff] [blame] | 84 | * Similar to ACTION_APPWIDGET_PICK, but used from keyguard |
| 85 | * @hide |
| 86 | */ |
| 87 | public static final String |
| 88 | ACTION_KEYGUARD_APPWIDGET_PICK = "android.appwidget.action.KEYGUARD_APPWIDGET_PICK"; |
| 89 | |
| 90 | /** |
Dianne Hackborn | a40cfeb | 2013-03-25 17:49:36 -0700 | [diff] [blame] | 91 | * Activity action to launch from your {@link AppWidgetHost} activity when you want to bind |
| 92 | * an AppWidget to display and bindAppWidgetIdIfAllowed returns false. |
Michael Jurka | 61a5b01 | 2012-04-13 10:39:45 -0700 | [diff] [blame] | 93 | * <p> |
| 94 | * You must supply the following extras: |
| 95 | * <table> |
| 96 | * <tr> |
| 97 | * <td>{@link #EXTRA_APPWIDGET_ID}</td> |
| 98 | * <td>A newly allocated appWidgetId, which will be bound to the AppWidget provider |
| 99 | * you provide.</td> |
| 100 | * </tr> |
| 101 | * <tr> |
| 102 | * <td>{@link #EXTRA_APPWIDGET_PROVIDER}</td> |
| 103 | * <td>The BroadcastReceiver that will be the AppWidget provider for this AppWidget. |
| 104 | * </td> |
| 105 | * </tr> |
| 106 | * </table> |
| 107 | * |
| 108 | * <p> |
| 109 | * The system will respond with an onActivityResult call with the following extras in |
| 110 | * the intent: |
| 111 | * <table> |
| 112 | * <tr> |
| 113 | * <td>{@link #EXTRA_APPWIDGET_ID}</td> |
| 114 | * <td>The appWidgetId that you supplied in the original intent.</td> |
| 115 | * </tr> |
| 116 | * </table> |
| 117 | * <p> |
| 118 | * When you receive the result from the AppWidget bind activity, if the resultCode is |
| 119 | * {@link android.app.Activity#RESULT_OK}, the AppWidget has been bound. You should then |
| 120 | * check the AppWidgetProviderInfo for the returned AppWidget, and if it has one, launch its |
| 121 | * configuration activity. If {@link android.app.Activity#RESULT_CANCELED} is returned, you |
| 122 | * should delete |
| 123 | * the appWidgetId. |
| 124 | * |
| 125 | * @see #ACTION_APPWIDGET_CONFIGURE |
| 126 | * |
| 127 | */ |
| 128 | public static final String ACTION_APPWIDGET_BIND = "android.appwidget.action.APPWIDGET_BIND"; |
| 129 | |
| 130 | /** |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 131 | * Sent when it is time to configure your AppWidget while it is being added to a host. |
| 132 | * This action is not sent as a broadcast to the AppWidget provider, but as a startActivity |
| 133 | * to the activity specified in the {@link AppWidgetProviderInfo AppWidgetProviderInfo meta-data}. |
| 134 | * |
| 135 | * <p> |
| 136 | * The intent will contain the following extras: |
| 137 | * <table> |
| 138 | * <tr> |
| 139 | * <td>{@link #EXTRA_APPWIDGET_ID}</td> |
| 140 | * <td>The appWidgetId to configure.</td> |
| 141 | * </tr> |
| 142 | * </table> |
| 143 | * |
| 144 | * <p>If you return {@link android.app.Activity#RESULT_OK} using |
| 145 | * {@link android.app.Activity#setResult Activity.setResult()}, the AppWidget will be added, |
| 146 | * and you will receive an {@link #ACTION_APPWIDGET_UPDATE} broadcast for this AppWidget. |
| 147 | * If you return {@link android.app.Activity#RESULT_CANCELED}, the host will cancel the add |
| 148 | * and not display this AppWidget, and you will receive a {@link #ACTION_APPWIDGET_DELETED} broadcast. |
| 149 | */ |
| 150 | public static final String ACTION_APPWIDGET_CONFIGURE = "android.appwidget.action.APPWIDGET_CONFIGURE"; |
| 151 | |
| 152 | /** |
| 153 | * An intent extra that contains one appWidgetId. |
| 154 | * <p> |
| 155 | * The value will be an int that can be retrieved like this: |
| 156 | * {@sample frameworks/base/tests/appwidgets/AppWidgetHostTest/src/com/android/tests/appwidgethost/AppWidgetHostActivity.java getExtra_EXTRA_APPWIDGET_ID} |
| 157 | */ |
| 158 | public static final String EXTRA_APPWIDGET_ID = "appWidgetId"; |
| 159 | |
| 160 | /** |
Adam Cohen | 0aa2d42 | 2012-09-07 17:37:26 -0700 | [diff] [blame] | 161 | * A bundle extra that contains the lower bound on the current width, in dips, of a widget instance. |
Adam Cohen | e8724c8 | 2012-04-19 17:11:40 -0700 | [diff] [blame] | 162 | */ |
Adam Cohen | d2097eb | 2012-05-01 18:10:28 -0700 | [diff] [blame] | 163 | public static final String OPTION_APPWIDGET_MIN_WIDTH = "appWidgetMinWidth"; |
Adam Cohen | e8724c8 | 2012-04-19 17:11:40 -0700 | [diff] [blame] | 164 | |
| 165 | /** |
Adam Cohen | 0aa2d42 | 2012-09-07 17:37:26 -0700 | [diff] [blame] | 166 | * A bundle extra that contains the lower bound on the current height, in dips, of a widget instance. |
Adam Cohen | e8724c8 | 2012-04-19 17:11:40 -0700 | [diff] [blame] | 167 | */ |
Adam Cohen | d2097eb | 2012-05-01 18:10:28 -0700 | [diff] [blame] | 168 | public static final String OPTION_APPWIDGET_MIN_HEIGHT = "appWidgetMinHeight"; |
Adam Cohen | e8724c8 | 2012-04-19 17:11:40 -0700 | [diff] [blame] | 169 | |
| 170 | /** |
Adam Cohen | 0aa2d42 | 2012-09-07 17:37:26 -0700 | [diff] [blame] | 171 | * A bundle extra that contains the upper bound on the current width, in dips, of a widget instance. |
Adam Cohen | e8724c8 | 2012-04-19 17:11:40 -0700 | [diff] [blame] | 172 | */ |
Adam Cohen | d2097eb | 2012-05-01 18:10:28 -0700 | [diff] [blame] | 173 | public static final String OPTION_APPWIDGET_MAX_WIDTH = "appWidgetMaxWidth"; |
Adam Cohen | e8724c8 | 2012-04-19 17:11:40 -0700 | [diff] [blame] | 174 | |
| 175 | /** |
Adam Cohen | 0aa2d42 | 2012-09-07 17:37:26 -0700 | [diff] [blame] | 176 | * A bundle extra that contains the upper bound on the current width, in dips, of a widget instance. |
Adam Cohen | e8724c8 | 2012-04-19 17:11:40 -0700 | [diff] [blame] | 177 | */ |
Adam Cohen | d2097eb | 2012-05-01 18:10:28 -0700 | [diff] [blame] | 178 | public static final String OPTION_APPWIDGET_MAX_HEIGHT = "appWidgetMaxHeight"; |
Adam Cohen | e8724c8 | 2012-04-19 17:11:40 -0700 | [diff] [blame] | 179 | |
| 180 | /** |
Adam Cohen | 0aa2d42 | 2012-09-07 17:37:26 -0700 | [diff] [blame] | 181 | * A bundle extra that hints to the AppWidgetProvider the category of host that owns this |
| 182 | * this widget. Can have the value {@link |
| 183 | * AppWidgetProviderInfo#WIDGET_CATEGORY_HOME_SCREEN} or {@link |
| 184 | * AppWidgetProviderInfo#WIDGET_CATEGORY_KEYGUARD}. |
| 185 | */ |
| 186 | public static final String OPTION_APPWIDGET_HOST_CATEGORY = "appWidgetCategory"; |
| 187 | |
| 188 | /** |
Adam Cohen | e8724c8 | 2012-04-19 17:11:40 -0700 | [diff] [blame] | 189 | * An intent extra which points to a bundle of extra information for a particular widget id. |
| 190 | * In particular this bundle can contain EXTRA_APPWIDGET_WIDTH and EXTRA_APPWIDGET_HEIGHT. |
| 191 | */ |
Adam Cohen | d2097eb | 2012-05-01 18:10:28 -0700 | [diff] [blame] | 192 | public static final String EXTRA_APPWIDGET_OPTIONS = "appWidgetOptions"; |
Adam Cohen | e8724c8 | 2012-04-19 17:11:40 -0700 | [diff] [blame] | 193 | |
| 194 | /** |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 195 | * An intent extra that contains multiple appWidgetIds. |
| 196 | * <p> |
| 197 | * The value will be an int array that can be retrieved like this: |
| 198 | * {@sample frameworks/base/tests/appwidgets/AppWidgetHostTest/src/com/android/tests/appwidgethost/TestAppWidgetProvider.java getExtra_EXTRA_APPWIDGET_IDS} |
| 199 | */ |
| 200 | public static final String EXTRA_APPWIDGET_IDS = "appWidgetIds"; |
| 201 | |
| 202 | /** |
Michael Jurka | 61a5b01 | 2012-04-13 10:39:45 -0700 | [diff] [blame] | 203 | * An intent extra that contains the component name of a AppWidget provider. |
| 204 | * <p> |
| 205 | * The value will be an ComponentName. |
| 206 | */ |
| 207 | public static final String EXTRA_APPWIDGET_PROVIDER = "appWidgetProvider"; |
| 208 | |
| 209 | /** |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 210 | * An intent extra to pass to the AppWidget picker containing a {@link java.util.List} of |
| 211 | * {@link AppWidgetProviderInfo} objects to mix in to the list of AppWidgets that are |
| 212 | * installed. (This is how the launcher shows the search widget). |
| 213 | */ |
| 214 | public static final String EXTRA_CUSTOM_INFO = "customInfo"; |
| 215 | |
| 216 | /** |
| 217 | * An intent extra to pass to the AppWidget picker containing a {@link java.util.List} of |
| 218 | * {@link android.os.Bundle} objects to mix in to the list of AppWidgets that are |
| 219 | * installed. It will be added to the extras object on the {@link android.content.Intent} |
| 220 | * that is returned from the picker activity. |
| 221 | * |
| 222 | * {@more} |
| 223 | */ |
| 224 | public static final String EXTRA_CUSTOM_EXTRAS = "customExtras"; |
| 225 | |
| 226 | /** |
Adam Cohen | 8c09f63 | 2012-09-11 18:23:35 -0700 | [diff] [blame] | 227 | * An intent extra to pass to the AppWidget picker which allows the picker to filter |
| 228 | * the list based on the {@link AppWidgetProviderInfo#widgetCategory}. |
Jim Miller | f229e4d | 2012-09-12 20:32:50 -0700 | [diff] [blame] | 229 | * |
| 230 | * @hide |
Adam Cohen | 8c09f63 | 2012-09-11 18:23:35 -0700 | [diff] [blame] | 231 | */ |
Adam Cohen | 8c09f63 | 2012-09-11 18:23:35 -0700 | [diff] [blame] | 232 | public static final String EXTRA_CATEGORY_FILTER = "categoryFilter"; |
| 233 | |
| 234 | /** |
Jim Miller | f229e4d | 2012-09-12 20:32:50 -0700 | [diff] [blame] | 235 | * An intent extra to pass to the AppWidget picker to specify whether or not to sort |
| 236 | * the list of caller-specified extra AppWidgets along with the rest of the AppWidgets |
| 237 | * @hide |
| 238 | */ |
| 239 | public static final String EXTRA_CUSTOM_SORT = "customSort"; |
| 240 | |
| 241 | /** |
Scott Main | 6aad995 | 2013-01-07 18:51:49 -0800 | [diff] [blame] | 242 | * A sentinel value that the AppWidget manager will never return as a appWidgetId. |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 243 | */ |
| 244 | public static final int INVALID_APPWIDGET_ID = 0; |
| 245 | |
| 246 | /** |
| 247 | * Sent when it is time to update your AppWidget. |
| 248 | * |
| 249 | * <p>This may be sent in response to a new instance for this AppWidget provider having |
| 250 | * been instantiated, the requested {@link AppWidgetProviderInfo#updatePeriodMillis update interval} |
| 251 | * having lapsed, or the system booting. |
| 252 | * |
| 253 | * <p> |
| 254 | * The intent will contain the following extras: |
| 255 | * <table> |
| 256 | * <tr> |
| 257 | * <td>{@link #EXTRA_APPWIDGET_IDS}</td> |
| 258 | * <td>The appWidgetIds to update. This may be all of the AppWidgets created for this |
| 259 | * provider, or just a subset. The system tries to send updates for as few AppWidget |
| 260 | * instances as possible.</td> |
| 261 | * </tr> |
| 262 | * </table> |
Christian Mehlmauer | 10c543d | 2010-06-25 19:27:04 +0200 | [diff] [blame] | 263 | * |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 264 | * @see AppWidgetProvider#onUpdate AppWidgetProvider.onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) |
| 265 | */ |
| 266 | public static final String ACTION_APPWIDGET_UPDATE = "android.appwidget.action.APPWIDGET_UPDATE"; |
| 267 | |
| 268 | /** |
Adam Cohen | e8724c8 | 2012-04-19 17:11:40 -0700 | [diff] [blame] | 269 | * Sent when the custom extras for an AppWidget change. |
| 270 | * |
Dianne Hackborn | a40cfeb | 2013-03-25 17:49:36 -0700 | [diff] [blame] | 271 | * <p class="note">This is a protected intent that can only be sent |
| 272 | * by the system. |
| 273 | * |
Jim Miller | a75a883 | 2013-02-07 16:53:32 -0800 | [diff] [blame] | 274 | * @see AppWidgetProvider#onAppWidgetOptionsChanged |
| 275 | * AppWidgetProvider.onAppWidgetOptionsChanged(Context context, |
Katie McCormick | 0e9f34b | 2012-09-10 15:32:46 -0700 | [diff] [blame] | 276 | * AppWidgetManager appWidgetManager, int appWidgetId, Bundle newExtras) |
Adam Cohen | e8724c8 | 2012-04-19 17:11:40 -0700 | [diff] [blame] | 277 | */ |
Adam Cohen | d2097eb | 2012-05-01 18:10:28 -0700 | [diff] [blame] | 278 | public static final String ACTION_APPWIDGET_OPTIONS_CHANGED = "android.appwidget.action.APPWIDGET_UPDATE_OPTIONS"; |
Adam Cohen | e8724c8 | 2012-04-19 17:11:40 -0700 | [diff] [blame] | 279 | |
| 280 | /** |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 281 | * Sent when an instance of an AppWidget is deleted from its host. |
| 282 | * |
Dianne Hackborn | a40cfeb | 2013-03-25 17:49:36 -0700 | [diff] [blame] | 283 | * <p class="note">This is a protected intent that can only be sent |
| 284 | * by the system. |
| 285 | * |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 286 | * @see AppWidgetProvider#onDeleted AppWidgetProvider.onDeleted(Context context, int[] appWidgetIds) |
| 287 | */ |
| 288 | public static final String ACTION_APPWIDGET_DELETED = "android.appwidget.action.APPWIDGET_DELETED"; |
| 289 | |
| 290 | /** |
| 291 | * Sent when an instance of an AppWidget is removed from the last host. |
Christian Mehlmauer | 10c543d | 2010-06-25 19:27:04 +0200 | [diff] [blame] | 292 | * |
Dianne Hackborn | a40cfeb | 2013-03-25 17:49:36 -0700 | [diff] [blame] | 293 | * <p class="note">This is a protected intent that can only be sent |
| 294 | * by the system. |
| 295 | * |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 296 | * @see AppWidgetProvider#onEnabled AppWidgetProvider.onEnabled(Context context) |
| 297 | */ |
| 298 | public static final String ACTION_APPWIDGET_DISABLED = "android.appwidget.action.APPWIDGET_DISABLED"; |
| 299 | |
| 300 | /** |
| 301 | * Sent when an instance of an AppWidget is added to a host for the first time. |
| 302 | * This broadcast is sent at boot time if there is a AppWidgetHost installed with |
| 303 | * an instance for this provider. |
Christian Mehlmauer | 10c543d | 2010-06-25 19:27:04 +0200 | [diff] [blame] | 304 | * |
Dianne Hackborn | a40cfeb | 2013-03-25 17:49:36 -0700 | [diff] [blame] | 305 | * <p class="note">This is a protected intent that can only be sent |
| 306 | * by the system. |
| 307 | * |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 308 | * @see AppWidgetProvider#onEnabled AppWidgetProvider.onEnabled(Context context) |
| 309 | */ |
| 310 | public static final String ACTION_APPWIDGET_ENABLED = "android.appwidget.action.APPWIDGET_ENABLED"; |
| 311 | |
| 312 | /** |
| 313 | * Field for the manifest meta-data tag. |
| 314 | * |
| 315 | * @see AppWidgetProviderInfo |
| 316 | */ |
| 317 | public static final String META_DATA_APPWIDGET_PROVIDER = "android.appwidget.provider"; |
Christian Mehlmauer | 10c543d | 2010-06-25 19:27:04 +0200 | [diff] [blame] | 318 | |
Christian Mehlmauer | 10c543d | 2010-06-25 19:27:04 +0200 | [diff] [blame] | 319 | static WeakHashMap<Context, WeakReference<AppWidgetManager>> sManagerCache = |
| 320 | new WeakHashMap<Context, WeakReference<AppWidgetManager>>(); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 321 | static IAppWidgetService sService; |
Christian Mehlmauer | 10c543d | 2010-06-25 19:27:04 +0200 | [diff] [blame] | 322 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 323 | Context mContext; |
| 324 | |
Mitsuru Oshima | 8f25c42 | 2009-07-01 00:10:43 -0700 | [diff] [blame] | 325 | private DisplayMetrics mDisplayMetrics; |
| 326 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 327 | /** |
| 328 | * Get the AppWidgetManager instance to use for the supplied {@link android.content.Context |
| 329 | * Context} object. |
| 330 | */ |
| 331 | public static AppWidgetManager getInstance(Context context) { |
| 332 | synchronized (sManagerCache) { |
| 333 | if (sService == null) { |
| 334 | IBinder b = ServiceManager.getService(Context.APPWIDGET_SERVICE); |
| 335 | sService = IAppWidgetService.Stub.asInterface(b); |
| 336 | } |
| 337 | |
| 338 | WeakReference<AppWidgetManager> ref = sManagerCache.get(context); |
| 339 | AppWidgetManager result = null; |
| 340 | if (ref != null) { |
| 341 | result = ref.get(); |
| 342 | } |
| 343 | if (result == null) { |
| 344 | result = new AppWidgetManager(context); |
Christian Mehlmauer | 10c543d | 2010-06-25 19:27:04 +0200 | [diff] [blame] | 345 | sManagerCache.put(context, new WeakReference<AppWidgetManager>(result)); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 346 | } |
| 347 | return result; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | private AppWidgetManager(Context context) { |
| 352 | mContext = context; |
Mitsuru Oshima | 8f25c42 | 2009-07-01 00:10:43 -0700 | [diff] [blame] | 353 | mDisplayMetrics = context.getResources().getDisplayMetrics(); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Set the RemoteViews to use for the specified appWidgetIds. |
| 358 | * |
Adam Cohen | 2dd2197 | 2010-08-15 18:20:04 -0700 | [diff] [blame] | 359 | * Note that the RemoteViews parameter will be cached by the AppWidgetService, and hence should |
| 360 | * contain a complete representation of the widget. For performing partial widget updates, see |
| 361 | * {@link #partiallyUpdateAppWidget(int[], RemoteViews)}. |
| 362 | * |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 363 | * <p> |
| 364 | * It is okay to call this method both inside an {@link #ACTION_APPWIDGET_UPDATE} broadcast, |
| 365 | * and outside of the handler. |
| 366 | * This method will only work when called from the uid that owns the AppWidget provider. |
Jim Miller | a75a883 | 2013-02-07 16:53:32 -0800 | [diff] [blame] | 367 | * |
Adam Cohen | 311c79c | 2012-05-10 14:44:38 -0700 | [diff] [blame] | 368 | * <p> |
| 369 | * The total Bitmap memory used by the RemoteViews object cannot exceed that required to |
Michael Jurka | f25ab44 | 2012-06-25 15:11:21 -0700 | [diff] [blame] | 370 | * fill the screen 1.5 times, ie. (screen width x screen height x 4 x 1.5) bytes. |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 371 | * |
| 372 | * @param appWidgetIds The AppWidget instances for which to set the RemoteViews. |
| 373 | * @param views The RemoteViews object to show. |
| 374 | */ |
| 375 | public void updateAppWidget(int[] appWidgetIds, RemoteViews views) { |
| 376 | try { |
Jim Miller | a75a883 | 2013-02-07 16:53:32 -0800 | [diff] [blame] | 377 | sService.updateAppWidgetIds(appWidgetIds, views, mContext.getUserId()); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 378 | } |
| 379 | catch (RemoteException e) { |
| 380 | throw new RuntimeException("system server dead?", e); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | /** |
Adam Cohen | e8724c8 | 2012-04-19 17:11:40 -0700 | [diff] [blame] | 385 | * Update the extras for a given widget instance. |
| 386 | * |
| 387 | * The extras can be used to embed additional information about this widget to be accessed |
| 388 | * by the associated widget's AppWidgetProvider. |
| 389 | * |
Adam Cohen | d2097eb | 2012-05-01 18:10:28 -0700 | [diff] [blame] | 390 | * @see #getAppWidgetOptions(int) |
Adam Cohen | e8724c8 | 2012-04-19 17:11:40 -0700 | [diff] [blame] | 391 | * |
| 392 | * @param appWidgetId The AppWidget instances for which to set the RemoteViews. |
Adam Cohen | d2097eb | 2012-05-01 18:10:28 -0700 | [diff] [blame] | 393 | * @param options The options to associate with this widget |
Adam Cohen | e8724c8 | 2012-04-19 17:11:40 -0700 | [diff] [blame] | 394 | */ |
Adam Cohen | d2097eb | 2012-05-01 18:10:28 -0700 | [diff] [blame] | 395 | public void updateAppWidgetOptions(int appWidgetId, Bundle options) { |
Adam Cohen | e8724c8 | 2012-04-19 17:11:40 -0700 | [diff] [blame] | 396 | try { |
Jim Miller | a75a883 | 2013-02-07 16:53:32 -0800 | [diff] [blame] | 397 | sService.updateAppWidgetOptions(appWidgetId, options, mContext.getUserId()); |
Adam Cohen | e8724c8 | 2012-04-19 17:11:40 -0700 | [diff] [blame] | 398 | } |
| 399 | catch (RemoteException e) { |
| 400 | throw new RuntimeException("system server dead?", e); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Get the extras associated with a given widget instance. |
| 406 | * |
| 407 | * The extras can be used to embed additional information about this widget to be accessed |
| 408 | * by the associated widget's AppWidgetProvider. |
| 409 | * |
Adam Cohen | d2097eb | 2012-05-01 18:10:28 -0700 | [diff] [blame] | 410 | * @see #updateAppWidgetOptions(int, Bundle) |
Adam Cohen | e8724c8 | 2012-04-19 17:11:40 -0700 | [diff] [blame] | 411 | * |
| 412 | * @param appWidgetId The AppWidget instances for which to set the RemoteViews. |
Adam Cohen | d2097eb | 2012-05-01 18:10:28 -0700 | [diff] [blame] | 413 | * @return The options associated with the given widget instance. |
Adam Cohen | e8724c8 | 2012-04-19 17:11:40 -0700 | [diff] [blame] | 414 | */ |
Adam Cohen | d2097eb | 2012-05-01 18:10:28 -0700 | [diff] [blame] | 415 | public Bundle getAppWidgetOptions(int appWidgetId) { |
Adam Cohen | e8724c8 | 2012-04-19 17:11:40 -0700 | [diff] [blame] | 416 | try { |
Jim Miller | a75a883 | 2013-02-07 16:53:32 -0800 | [diff] [blame] | 417 | return sService.getAppWidgetOptions(appWidgetId, mContext.getUserId()); |
Adam Cohen | e8724c8 | 2012-04-19 17:11:40 -0700 | [diff] [blame] | 418 | } |
| 419 | catch (RemoteException e) { |
| 420 | throw new RuntimeException("system server dead?", e); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | /** |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 425 | * Set the RemoteViews to use for the specified appWidgetId. |
| 426 | * |
Adam Cohen | 2dd2197 | 2010-08-15 18:20:04 -0700 | [diff] [blame] | 427 | * Note that the RemoteViews parameter will be cached by the AppWidgetService, and hence should |
| 428 | * contain a complete representation of the widget. For performing partial widget updates, see |
| 429 | * {@link #partiallyUpdateAppWidget(int, RemoteViews)}. |
| 430 | * |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 431 | * <p> |
| 432 | * It is okay to call this method both inside an {@link #ACTION_APPWIDGET_UPDATE} broadcast, |
| 433 | * and outside of the handler. |
| 434 | * This method will only work when called from the uid that owns the AppWidget provider. |
| 435 | * |
Adam Cohen | 311c79c | 2012-05-10 14:44:38 -0700 | [diff] [blame] | 436 | * <p> |
| 437 | * The total Bitmap memory used by the RemoteViews object cannot exceed that required to |
Michael Jurka | f25ab44 | 2012-06-25 15:11:21 -0700 | [diff] [blame] | 438 | * fill the screen 1.5 times, ie. (screen width x screen height x 4 x 1.5) bytes. |
Adam Cohen | 311c79c | 2012-05-10 14:44:38 -0700 | [diff] [blame] | 439 | * |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 440 | * @param appWidgetId The AppWidget instance for which to set the RemoteViews. |
| 441 | * @param views The RemoteViews object to show. |
| 442 | */ |
| 443 | public void updateAppWidget(int appWidgetId, RemoteViews views) { |
| 444 | updateAppWidget(new int[] { appWidgetId }, views); |
| 445 | } |
| 446 | |
| 447 | /** |
Adam Cohen | 2dd2197 | 2010-08-15 18:20:04 -0700 | [diff] [blame] | 448 | * Perform an incremental update or command on the widget(s) specified by appWidgetIds. |
| 449 | * |
| 450 | * This update differs from {@link #updateAppWidget(int[], RemoteViews)} in that the |
Jim Miller | a75a883 | 2013-02-07 16:53:32 -0800 | [diff] [blame] | 451 | * RemoteViews object which is passed is understood to be an incomplete representation of the |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 452 | * widget, and hence does not replace the cached representation of the widget. As of API |
| 453 | * level 17, the new properties set within the views objects will be appended to the cached |
| 454 | * representation of the widget, and hence will persist. |
Adam Cohen | 2dd2197 | 2010-08-15 18:20:04 -0700 | [diff] [blame] | 455 | * |
| 456 | * Use with {@link RemoteViews#showNext(int)}, {@link RemoteViews#showPrevious(int)}, |
| 457 | * {@link RemoteViews#setScrollPosition(int, int)} and similar commands. |
| 458 | * |
| 459 | * <p> |
| 460 | * It is okay to call this method both inside an {@link #ACTION_APPWIDGET_UPDATE} broadcast, |
| 461 | * and outside of the handler. |
| 462 | * This method will only work when called from the uid that owns the AppWidget provider. |
| 463 | * |
Winson Chung | 6611988 | 2012-10-11 14:26:25 -0700 | [diff] [blame] | 464 | * <p> |
| 465 | * This method will be ignored if a widget has not received a full update via |
| 466 | * {@link #updateAppWidget(int[], RemoteViews)}. |
| 467 | * |
Adam Cohen | 2dd2197 | 2010-08-15 18:20:04 -0700 | [diff] [blame] | 468 | * @param appWidgetIds The AppWidget instances for which to set the RemoteViews. |
| 469 | * @param views The RemoteViews object containing the incremental update / command. |
| 470 | */ |
| 471 | public void partiallyUpdateAppWidget(int[] appWidgetIds, RemoteViews views) { |
| 472 | try { |
Jim Miller | a75a883 | 2013-02-07 16:53:32 -0800 | [diff] [blame] | 473 | sService.partiallyUpdateAppWidgetIds(appWidgetIds, views, mContext.getUserId()); |
Adam Cohen | 2dd2197 | 2010-08-15 18:20:04 -0700 | [diff] [blame] | 474 | } catch (RemoteException e) { |
| 475 | throw new RuntimeException("system server dead?", e); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Perform an incremental update or command on the widget specified by appWidgetId. |
| 481 | * |
| 482 | * This update differs from {@link #updateAppWidget(int, RemoteViews)} in that the RemoteViews |
| 483 | * object which is passed is understood to be an incomplete representation of the widget, and |
| 484 | * hence is not cached by the AppWidgetService. Note that because these updates are not cached, |
| 485 | * any state that they modify that is not restored by restoreInstanceState will not persist in |
| 486 | * the case that the widgets are restored using the cached version in AppWidgetService. |
| 487 | * |
| 488 | * Use with {@link RemoteViews#showNext(int)}, {@link RemoteViews#showPrevious(int)}, |
| 489 | * {@link RemoteViews#setScrollPosition(int, int)} and similar commands. |
| 490 | * |
| 491 | * <p> |
| 492 | * It is okay to call this method both inside an {@link #ACTION_APPWIDGET_UPDATE} broadcast, |
| 493 | * and outside of the handler. |
| 494 | * This method will only work when called from the uid that owns the AppWidget provider. |
| 495 | * |
Winson Chung | 6611988 | 2012-10-11 14:26:25 -0700 | [diff] [blame] | 496 | * <p> |
| 497 | * This method will be ignored if a widget has not received a full update via |
| 498 | * {@link #updateAppWidget(int[], RemoteViews)}. |
| 499 | * |
Adam Cohen | 2dd2197 | 2010-08-15 18:20:04 -0700 | [diff] [blame] | 500 | * @param appWidgetId The AppWidget instance for which to set the RemoteViews. |
| 501 | * @param views The RemoteViews object containing the incremental update / command. |
| 502 | */ |
| 503 | public void partiallyUpdateAppWidget(int appWidgetId, RemoteViews views) { |
| 504 | partiallyUpdateAppWidget(new int[] { appWidgetId }, views); |
| 505 | } |
| 506 | |
| 507 | /** |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 508 | * Set the RemoteViews to use for all AppWidget instances for the supplied AppWidget provider. |
| 509 | * |
| 510 | * <p> |
| 511 | * It is okay to call this method both inside an {@link #ACTION_APPWIDGET_UPDATE} broadcast, |
| 512 | * and outside of the handler. |
| 513 | * This method will only work when called from the uid that owns the AppWidget provider. |
| 514 | * |
| 515 | * @param provider The {@link ComponentName} for the {@link |
| 516 | * android.content.BroadcastReceiver BroadcastReceiver} provider |
| 517 | * for your AppWidget. |
| 518 | * @param views The RemoteViews object to show. |
| 519 | */ |
| 520 | public void updateAppWidget(ComponentName provider, RemoteViews views) { |
| 521 | try { |
Jim Miller | a75a883 | 2013-02-07 16:53:32 -0800 | [diff] [blame] | 522 | sService.updateAppWidgetProvider(provider, views, mContext.getUserId()); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 523 | } |
| 524 | catch (RemoteException e) { |
| 525 | throw new RuntimeException("system server dead?", e); |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | /** |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 530 | * Notifies the specified collection view in all the specified AppWidget instances |
John Spurlock | 77624cd | 2013-06-03 09:47:01 -0400 | [diff] [blame] | 531 | * to invalidate their data. |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 532 | * |
John Spurlock | 77624cd | 2013-06-03 09:47:01 -0400 | [diff] [blame] | 533 | * @param appWidgetIds The AppWidget instances to notify of view data changes. |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 534 | * @param viewId The collection view id. |
| 535 | */ |
Winson Chung | 6394c0e | 2010-08-16 10:14:56 -0700 | [diff] [blame] | 536 | public void notifyAppWidgetViewDataChanged(int[] appWidgetIds, int viewId) { |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 537 | try { |
Jim Miller | a75a883 | 2013-02-07 16:53:32 -0800 | [diff] [blame] | 538 | sService.notifyAppWidgetViewDataChanged(appWidgetIds, viewId, mContext.getUserId()); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 539 | } |
| 540 | catch (RemoteException e) { |
| 541 | throw new RuntimeException("system server dead?", e); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | /** |
John Spurlock | 77624cd | 2013-06-03 09:47:01 -0400 | [diff] [blame] | 546 | * Notifies the specified collection view in the specified AppWidget instance |
| 547 | * to invalidate its data. |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 548 | * |
John Spurlock | 77624cd | 2013-06-03 09:47:01 -0400 | [diff] [blame] | 549 | * @param appWidgetId The AppWidget instance to notify of view data changes. |
| 550 | * @param viewId The collection view id. |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 551 | */ |
Winson Chung | 6394c0e | 2010-08-16 10:14:56 -0700 | [diff] [blame] | 552 | public void notifyAppWidgetViewDataChanged(int appWidgetId, int viewId) { |
| 553 | notifyAppWidgetViewDataChanged(new int[] { appWidgetId }, viewId); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | /** |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 557 | * Return a list of the AppWidget providers that are currently installed. |
| 558 | */ |
| 559 | public List<AppWidgetProviderInfo> getInstalledProviders() { |
Adam Cohen | d9e5af3 | 2012-11-28 16:34:57 -0800 | [diff] [blame] | 560 | return getInstalledProviders(AppWidgetProviderInfo.WIDGET_CATEGORY_HOME_SCREEN); |
| 561 | } |
| 562 | |
| 563 | /** |
| 564 | * Return a list of the AppWidget providers that are currently installed. |
| 565 | * |
| 566 | * @param categoryFilter Will only return providers which register as any of the specified |
| 567 | * specified categories. See {@link AppWidgetProviderInfo#widgetCategory}. |
| 568 | * @hide |
| 569 | */ |
| 570 | public List<AppWidgetProviderInfo> getInstalledProviders(int categoryFilter) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 571 | try { |
Jim Miller | a75a883 | 2013-02-07 16:53:32 -0800 | [diff] [blame] | 572 | List<AppWidgetProviderInfo> providers = sService.getInstalledProviders(categoryFilter, |
| 573 | mContext.getUserId()); |
Patrick Dubroy | 5d14091 | 2010-07-12 10:25:27 -0700 | [diff] [blame] | 574 | for (AppWidgetProviderInfo info : providers) { |
| 575 | // Converting complex to dp. |
| 576 | info.minWidth = |
| 577 | TypedValue.complexToDimensionPixelSize(info.minWidth, mDisplayMetrics); |
| 578 | info.minHeight = |
| 579 | TypedValue.complexToDimensionPixelSize(info.minHeight, mDisplayMetrics); |
Adam Cohen | 324afba | 2011-07-22 11:51:45 -0700 | [diff] [blame] | 580 | info.minResizeWidth = |
| 581 | TypedValue.complexToDimensionPixelSize(info.minResizeWidth, mDisplayMetrics); |
| 582 | info.minResizeHeight = |
| 583 | TypedValue.complexToDimensionPixelSize(info.minResizeHeight, mDisplayMetrics); |
Patrick Dubroy | 5d14091 | 2010-07-12 10:25:27 -0700 | [diff] [blame] | 584 | } |
| 585 | return providers; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 586 | } |
| 587 | catch (RemoteException e) { |
| 588 | throw new RuntimeException("system server dead?", e); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | /** |
| 593 | * Get the available info about the AppWidget. |
| 594 | * |
| 595 | * @return A appWidgetId. If the appWidgetId has not been bound to a provider yet, or |
| 596 | * you don't have access to that appWidgetId, null is returned. |
| 597 | */ |
| 598 | public AppWidgetProviderInfo getAppWidgetInfo(int appWidgetId) { |
| 599 | try { |
Jim Miller | a75a883 | 2013-02-07 16:53:32 -0800 | [diff] [blame] | 600 | AppWidgetProviderInfo info = sService.getAppWidgetInfo(appWidgetId, |
| 601 | mContext.getUserId()); |
Mitsuru Oshima | 8f25c42 | 2009-07-01 00:10:43 -0700 | [diff] [blame] | 602 | if (info != null) { |
| 603 | // Converting complex to dp. |
Christian Mehlmauer | 10c543d | 2010-06-25 19:27:04 +0200 | [diff] [blame] | 604 | info.minWidth = |
Mitsuru Oshima | 8f25c42 | 2009-07-01 00:10:43 -0700 | [diff] [blame] | 605 | TypedValue.complexToDimensionPixelSize(info.minWidth, mDisplayMetrics); |
| 606 | info.minHeight = |
| 607 | TypedValue.complexToDimensionPixelSize(info.minHeight, mDisplayMetrics); |
Adam Cohen | 324afba | 2011-07-22 11:51:45 -0700 | [diff] [blame] | 608 | info.minResizeWidth = |
| 609 | TypedValue.complexToDimensionPixelSize(info.minResizeWidth, mDisplayMetrics); |
| 610 | info.minResizeHeight = |
| 611 | TypedValue.complexToDimensionPixelSize(info.minResizeHeight, mDisplayMetrics); |
Mitsuru Oshima | 8f25c42 | 2009-07-01 00:10:43 -0700 | [diff] [blame] | 612 | } |
| 613 | return info; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 614 | } |
| 615 | catch (RemoteException e) { |
| 616 | throw new RuntimeException("system server dead?", e); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | /** |
| 621 | * Set the component for a given appWidgetId. |
| 622 | * |
Michael Jurka | 61a5b01 | 2012-04-13 10:39:45 -0700 | [diff] [blame] | 623 | * <p class="note">You need the BIND_APPWIDGET permission or the user must have enabled binding |
| 624 | * widgets always for your component. This method is used by the AppWidget picker and |
| 625 | * should not be used by other apps. |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 626 | * |
| 627 | * @param appWidgetId The AppWidget instance for which to set the RemoteViews. |
| 628 | * @param provider The {@link android.content.BroadcastReceiver} that will be the AppWidget |
| 629 | * provider for this AppWidget. |
Michael Jurka | 61a5b01 | 2012-04-13 10:39:45 -0700 | [diff] [blame] | 630 | * @hide |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 631 | */ |
| 632 | public void bindAppWidgetId(int appWidgetId, ComponentName provider) { |
| 633 | try { |
Jim Miller | a75a883 | 2013-02-07 16:53:32 -0800 | [diff] [blame] | 634 | sService.bindAppWidgetId(appWidgetId, provider, null, mContext.getUserId()); |
Adam Cohen | 0aa2d42 | 2012-09-07 17:37:26 -0700 | [diff] [blame] | 635 | } |
| 636 | catch (RemoteException e) { |
| 637 | throw new RuntimeException("system server dead?", e); |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | /** |
| 642 | * Set the component for a given appWidgetId. |
| 643 | * |
| 644 | * <p class="note">You need the BIND_APPWIDGET permission or the user must have enabled binding |
| 645 | * widgets always for your component. This method is used by the AppWidget picker and |
| 646 | * should not be used by other apps. |
| 647 | * |
| 648 | * @param appWidgetId The AppWidget instance for which to set the RemoteViews. |
| 649 | * @param provider The {@link android.content.BroadcastReceiver} that will be the AppWidget |
| 650 | * provider for this AppWidget. |
| 651 | * @param options Bundle containing options for the AppWidget. See also |
| 652 | * {@link #updateAppWidgetOptions(int, Bundle)} |
| 653 | * |
| 654 | * @hide |
| 655 | */ |
| 656 | public void bindAppWidgetId(int appWidgetId, ComponentName provider, Bundle options) { |
| 657 | try { |
Jim Miller | a75a883 | 2013-02-07 16:53:32 -0800 | [diff] [blame] | 658 | sService.bindAppWidgetId(appWidgetId, provider, options, mContext.getUserId()); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 659 | } |
| 660 | catch (RemoteException e) { |
| 661 | throw new RuntimeException("system server dead?", e); |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | /** |
Michael Jurka | 61a5b01 | 2012-04-13 10:39:45 -0700 | [diff] [blame] | 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 | * @return true if this component has permission to bind the AppWidget |
| 677 | */ |
| 678 | public boolean bindAppWidgetIdIfAllowed(int appWidgetId, ComponentName provider) { |
| 679 | if (mContext == null) { |
| 680 | return false; |
| 681 | } |
| 682 | try { |
| 683 | return sService.bindAppWidgetIdIfAllowed( |
Jim Miller | a75a883 | 2013-02-07 16:53:32 -0800 | [diff] [blame] | 684 | mContext.getPackageName(), appWidgetId, provider, null, mContext.getUserId()); |
Adam Cohen | 0aa2d42 | 2012-09-07 17:37:26 -0700 | [diff] [blame] | 685 | } |
| 686 | catch (RemoteException e) { |
| 687 | throw new RuntimeException("system server dead?", e); |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | /** |
| 692 | * Set the component for a given appWidgetId. |
| 693 | * |
| 694 | * <p class="note">You need the BIND_APPWIDGET permission or the user must have enabled binding |
| 695 | * widgets always for your component. Should be used by apps that host widgets; if this |
| 696 | * method returns false, call {@link #ACTION_APPWIDGET_BIND} to request permission to |
| 697 | * bind |
| 698 | * |
| 699 | * @param appWidgetId The AppWidget instance for which to set the RemoteViews. |
| 700 | * @param provider The {@link android.content.BroadcastReceiver} that will be the AppWidget |
| 701 | * provider for this AppWidget. |
| 702 | * @param options Bundle containing options for the AppWidget. See also |
| 703 | * {@link #updateAppWidgetOptions(int, Bundle)} |
| 704 | * |
| 705 | * @return true if this component has permission to bind the AppWidget |
| 706 | */ |
| 707 | public boolean bindAppWidgetIdIfAllowed(int appWidgetId, ComponentName provider, |
| 708 | Bundle options) { |
| 709 | if (mContext == null) { |
| 710 | return false; |
| 711 | } |
| 712 | try { |
Jim Miller | a75a883 | 2013-02-07 16:53:32 -0800 | [diff] [blame] | 713 | return sService.bindAppWidgetIdIfAllowed(mContext.getPackageName(), appWidgetId, |
| 714 | provider, options, mContext.getUserId()); |
Michael Jurka | 61a5b01 | 2012-04-13 10:39:45 -0700 | [diff] [blame] | 715 | } |
| 716 | catch (RemoteException e) { |
| 717 | throw new RuntimeException("system server dead?", e); |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | /** |
| 722 | * Query if a given package was granted permission by the user to bind app widgets |
| 723 | * |
| 724 | * <p class="note">You need the MODIFY_APPWIDGET_BIND_PERMISSIONS permission |
| 725 | * |
| 726 | * @param packageName The package for which the permission is being queried |
| 727 | * @return true if the package was granted permission by the user to bind app widgets |
| 728 | * @hide |
| 729 | */ |
| 730 | public boolean hasBindAppWidgetPermission(String packageName) { |
| 731 | try { |
Jim Miller | a75a883 | 2013-02-07 16:53:32 -0800 | [diff] [blame] | 732 | return sService.hasBindAppWidgetPermission(packageName, mContext.getUserId()); |
Michael Jurka | 61a5b01 | 2012-04-13 10:39:45 -0700 | [diff] [blame] | 733 | } |
| 734 | catch (RemoteException e) { |
| 735 | throw new RuntimeException("system server dead?", e); |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | /** |
| 740 | * Changes any user-granted permission for the given package to bind app widgets |
| 741 | * |
| 742 | * <p class="note">You need the MODIFY_APPWIDGET_BIND_PERMISSIONS permission |
| 743 | * |
| 744 | * @param provider The package whose permission is being changed |
| 745 | * @param permission Whether to give the package permission to bind widgets |
| 746 | * @hide |
| 747 | */ |
| 748 | public void setBindAppWidgetPermission(String packageName, boolean permission) { |
| 749 | try { |
Jim Miller | a75a883 | 2013-02-07 16:53:32 -0800 | [diff] [blame] | 750 | sService.setBindAppWidgetPermission(packageName, permission, mContext.getUserId()); |
Michael Jurka | 61a5b01 | 2012-04-13 10:39:45 -0700 | [diff] [blame] | 751 | } |
| 752 | catch (RemoteException e) { |
| 753 | throw new RuntimeException("system server dead?", e); |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | /** |
Winson Chung | 81f39eb | 2011-01-11 18:05:01 -0800 | [diff] [blame] | 758 | * Binds the RemoteViewsService for a given appWidgetId and intent. |
| 759 | * |
| 760 | * The appWidgetId specified must already be bound to the calling AppWidgetHost via |
| 761 | * {@link android.appwidget.AppWidgetManager#bindAppWidgetId AppWidgetManager.bindAppWidgetId()}. |
| 762 | * |
| 763 | * @param appWidgetId The AppWidget instance for which to bind the RemoteViewsService. |
| 764 | * @param intent The intent of the service which will be providing the data to the |
| 765 | * RemoteViewsAdapter. |
| 766 | * @param connection The callback interface to be notified when a connection is made or lost. |
Amith Yamasani | c566b43 | 2012-11-30 15:26:21 -0800 | [diff] [blame] | 767 | * @param userHandle The user to bind to. |
Winson Chung | 81f39eb | 2011-01-11 18:05:01 -0800 | [diff] [blame] | 768 | * @hide |
| 769 | */ |
Amith Yamasani | c566b43 | 2012-11-30 15:26:21 -0800 | [diff] [blame] | 770 | public void bindRemoteViewsService(int appWidgetId, Intent intent, IBinder connection, |
| 771 | UserHandle userHandle) { |
Winson Chung | 81f39eb | 2011-01-11 18:05:01 -0800 | [diff] [blame] | 772 | try { |
Amith Yamasani | c566b43 | 2012-11-30 15:26:21 -0800 | [diff] [blame] | 773 | sService.bindRemoteViewsService(appWidgetId, intent, connection, |
| 774 | userHandle.getIdentifier()); |
Winson Chung | 81f39eb | 2011-01-11 18:05:01 -0800 | [diff] [blame] | 775 | } |
| 776 | catch (RemoteException e) { |
| 777 | throw new RuntimeException("system server dead?", e); |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | /** |
| 782 | * Unbinds the RemoteViewsService for a given appWidgetId and intent. |
| 783 | * |
| 784 | * The appWidgetId specified muse already be bound to the calling AppWidgetHost via |
| 785 | * {@link android.appwidget.AppWidgetManager#bindAppWidgetId AppWidgetManager.bindAppWidgetId()}. |
| 786 | * |
| 787 | * @param appWidgetId The AppWidget instance for which to bind the RemoteViewsService. |
| 788 | * @param intent The intent of the service which will be providing the data to the |
| 789 | * RemoteViewsAdapter. |
Amith Yamasani | c566b43 | 2012-11-30 15:26:21 -0800 | [diff] [blame] | 790 | * @param userHandle The user to unbind from. |
Winson Chung | 81f39eb | 2011-01-11 18:05:01 -0800 | [diff] [blame] | 791 | * @hide |
| 792 | */ |
Amith Yamasani | c566b43 | 2012-11-30 15:26:21 -0800 | [diff] [blame] | 793 | public void unbindRemoteViewsService(int appWidgetId, Intent intent, UserHandle userHandle) { |
Winson Chung | 81f39eb | 2011-01-11 18:05:01 -0800 | [diff] [blame] | 794 | try { |
Amith Yamasani | c566b43 | 2012-11-30 15:26:21 -0800 | [diff] [blame] | 795 | sService.unbindRemoteViewsService(appWidgetId, intent, userHandle.getIdentifier()); |
Winson Chung | 81f39eb | 2011-01-11 18:05:01 -0800 | [diff] [blame] | 796 | } |
| 797 | catch (RemoteException e) { |
| 798 | throw new RuntimeException("system server dead?", e); |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | /** |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 803 | * Get the list of appWidgetIds that have been bound to the given AppWidget |
| 804 | * provider. |
Christian Mehlmauer | 10c543d | 2010-06-25 19:27:04 +0200 | [diff] [blame] | 805 | * |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 806 | * @param provider The {@link android.content.BroadcastReceiver} that is the |
| 807 | * AppWidget provider to find appWidgetIds for. |
| 808 | */ |
| 809 | public int[] getAppWidgetIds(ComponentName provider) { |
| 810 | try { |
Jim Miller | a75a883 | 2013-02-07 16:53:32 -0800 | [diff] [blame] | 811 | return sService.getAppWidgetIds(provider, mContext.getUserId()); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 812 | } |
| 813 | catch (RemoteException e) { |
| 814 | throw new RuntimeException("system server dead?", e); |
| 815 | } |
| 816 | } |
| 817 | } |
| 818 | |