The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [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.app; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.Intent; |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 21 | import android.content.IIntentReceiver; |
| 22 | import android.content.IIntentSender; |
| 23 | import android.content.IntentSender; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.os.Bundle; |
| 25 | import android.os.RemoteException; |
| 26 | import android.os.Handler; |
| 27 | import android.os.IBinder; |
| 28 | import android.os.Parcel; |
| 29 | import android.os.Parcelable; |
| 30 | import android.util.AndroidException; |
| 31 | |
| 32 | /** |
| 33 | * A description of an Intent and target action to perform with it. Instances |
| 34 | * of this class are created with {@link #getActivity}, |
| 35 | * {@link #getBroadcast}, {@link #getService}; the returned object can be |
| 36 | * handed to other applications so that they can perform the action you |
| 37 | * described on your behalf at a later time. |
| 38 | * |
| 39 | * <p>By giving a PendingIntent to another application, |
| 40 | * you are granting it the right to perform the operation you have specified |
| 41 | * as if the other application was yourself (with the same permissions and |
| 42 | * identity). As such, you should be careful about how you build the PendingIntent: |
| 43 | * often, for example, the base Intent you supply will have the component |
| 44 | * name explicitly set to one of your own components, to ensure it is ultimately |
| 45 | * sent there and nowhere else. |
| 46 | * |
| 47 | * <p>A PendingIntent itself is simply a reference to a token maintained by |
| 48 | * the system describing the original data used to retrieve it. This means |
| 49 | * that, even if its owning application's process is killed, the |
| 50 | * PendingIntent itself will remain usable from other processes that |
| 51 | * have been given it. If the creating application later re-retrieves the |
| 52 | * same kind of PendingIntent (same operation, same Intent action, data, |
| 53 | * categories, and components, and same flags), it will receive a PendingIntent |
| 54 | * representing the same token if that is still valid, and can thus call |
| 55 | * {@link #cancel} to remove it. |
| 56 | */ |
| 57 | public final class PendingIntent implements Parcelable { |
| 58 | private final IIntentSender mTarget; |
| 59 | |
| 60 | /** |
| 61 | * Flag for use with {@link #getActivity}, {@link #getBroadcast}, and |
| 62 | * {@link #getService}: this |
| 63 | * PendingIntent can only be used once. If set, after |
| 64 | * {@link #send()} is called on it, it will be automatically |
| 65 | * canceled for you and any future attempt to send through it will fail. |
| 66 | */ |
| 67 | public static final int FLAG_ONE_SHOT = 1<<30; |
| 68 | /** |
| 69 | * Flag for use with {@link #getActivity}, {@link #getBroadcast}, and |
| 70 | * {@link #getService}: if the described PendingIntent does not already |
| 71 | * exist, then simply return null instead of creating it. |
| 72 | */ |
| 73 | public static final int FLAG_NO_CREATE = 1<<29; |
| 74 | /** |
| 75 | * Flag for use with {@link #getActivity}, {@link #getBroadcast}, and |
| 76 | * {@link #getService}: if the described PendingIntent already exists, |
| 77 | * the current one is canceled before generating a new one. You can use |
| 78 | * this to retrieve a new PendingIntent when you are only changing the |
| 79 | * extra data in the Intent; by canceling the previous pending intent, |
| 80 | * this ensures that only entities given the new data will be able to |
| 81 | * launch it. If this assurance is not an issue, consider |
| 82 | * {@link #FLAG_UPDATE_CURRENT}. |
| 83 | */ |
| 84 | public static final int FLAG_CANCEL_CURRENT = 1<<28; |
| 85 | /** |
| 86 | * Flag for use with {@link #getActivity}, {@link #getBroadcast}, and |
| 87 | * {@link #getService}: if the described PendingIntent already exists, |
| 88 | * then keep it but its replace its extra data with what is in this new |
| 89 | * Intent. This can be used if you are creating intents where only the |
| 90 | * extras change, and don't care that any entities that received your |
| 91 | * previous PendingIntent will be able to launch it with your new |
| 92 | * extras even if they are not explicitly given to it. |
| 93 | */ |
| 94 | public static final int FLAG_UPDATE_CURRENT = 1<<27; |
| 95 | |
| 96 | /** |
| 97 | * Exception thrown when trying to send through a PendingIntent that |
| 98 | * has been canceled or is otherwise no longer able to execute the request. |
| 99 | */ |
| 100 | public static class CanceledException extends AndroidException { |
| 101 | public CanceledException() { |
| 102 | } |
| 103 | |
| 104 | public CanceledException(String name) { |
| 105 | super(name); |
| 106 | } |
| 107 | |
| 108 | public CanceledException(Exception cause) { |
| 109 | super(cause); |
| 110 | } |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 111 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * Callback interface for discovering when a send operation has |
| 115 | * completed. Primarily for use with a PendingIntent that is |
| 116 | * performing a broadcast, this provides the same information as |
| 117 | * calling {@link Context#sendOrderedBroadcast(Intent, String, |
| 118 | * android.content.BroadcastReceiver, Handler, int, String, Bundle) |
| 119 | * Context.sendBroadcast()} with a final BroadcastReceiver. |
| 120 | */ |
| 121 | public interface OnFinished { |
| 122 | /** |
| 123 | * Called when a send operation as completed. |
| 124 | * |
| 125 | * @param pendingIntent The PendingIntent this operation was sent through. |
| 126 | * @param intent The original Intent that was sent. |
| 127 | * @param resultCode The final result code determined by the send. |
| 128 | * @param resultData The final data collected by a broadcast. |
| 129 | * @param resultExtras The final extras collected by a broadcast. |
| 130 | */ |
| 131 | void onSendFinished(PendingIntent pendingIntent, Intent intent, |
| 132 | int resultCode, String resultData, Bundle resultExtras); |
| 133 | } |
| 134 | |
| 135 | private static class FinishedDispatcher extends IIntentReceiver.Stub |
| 136 | implements Runnable { |
| 137 | private final PendingIntent mPendingIntent; |
| 138 | private final OnFinished mWho; |
| 139 | private final Handler mHandler; |
| 140 | private Intent mIntent; |
| 141 | private int mResultCode; |
| 142 | private String mResultData; |
| 143 | private Bundle mResultExtras; |
| 144 | FinishedDispatcher(PendingIntent pi, OnFinished who, Handler handler) { |
| 145 | mPendingIntent = pi; |
| 146 | mWho = who; |
| 147 | mHandler = handler; |
| 148 | } |
| 149 | public void performReceive(Intent intent, int resultCode, |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 150 | String data, Bundle extras, boolean serialized, boolean sticky) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | mIntent = intent; |
| 152 | mResultCode = resultCode; |
| 153 | mResultData = data; |
| 154 | mResultExtras = extras; |
| 155 | if (mHandler == null) { |
| 156 | run(); |
| 157 | } else { |
| 158 | mHandler.post(this); |
| 159 | } |
| 160 | } |
| 161 | public void run() { |
| 162 | mWho.onSendFinished(mPendingIntent, mIntent, mResultCode, |
| 163 | mResultData, mResultExtras); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Retrieve a PendingIntent that will start a new activity, like calling |
| 169 | * {@link Context#startActivity(Intent) Context.startActivity(Intent)}. |
| 170 | * Note that the activity will be started outside of the context of an |
| 171 | * existing activity, so you must use the {@link Intent#FLAG_ACTIVITY_NEW_TASK |
| 172 | * Intent.FLAG_ACTIVITY_NEW_TASK} launch flag in the Intent. |
| 173 | * |
| 174 | * @param context The Context in which this PendingIntent should start |
| 175 | * the activity. |
| 176 | * @param requestCode Private request code for the sender (currently |
| 177 | * not used). |
| 178 | * @param intent Intent of the activity to be launched. |
| 179 | * @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE}, |
| 180 | * {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT}, |
| 181 | * or any of the flags as supported by |
| 182 | * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts |
| 183 | * of the intent that can be supplied when the actual send happens. |
| 184 | * |
| 185 | * @return Returns an existing or new PendingIntent matching the given |
| 186 | * parameters. May return null only if {@link #FLAG_NO_CREATE} has been |
| 187 | * supplied. |
| 188 | */ |
| 189 | public static PendingIntent getActivity(Context context, int requestCode, |
| 190 | Intent intent, int flags) { |
| 191 | String packageName = context.getPackageName(); |
| 192 | String resolvedType = intent != null ? intent.resolveTypeIfNeeded( |
| 193 | context.getContentResolver()) : null; |
| 194 | try { |
| 195 | IIntentSender target = |
| 196 | ActivityManagerNative.getDefault().getIntentSender( |
| 197 | IActivityManager.INTENT_SENDER_ACTIVITY, packageName, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 198 | null, null, requestCode, new Intent[] { intent }, |
| 199 | resolvedType != null ? new String[] { resolvedType } : null, flags); |
| 200 | return target != null ? new PendingIntent(target) : null; |
| 201 | } catch (RemoteException e) { |
| 202 | } |
| 203 | return null; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Like {@link #getActivity(Context, int, Intent, int)}, but allows an |
| 208 | * array of Intents to be supplied. The first Intent in the array is |
| 209 | * taken as the primary key for the PendingIntent, like the single Intent |
| 210 | * given to {@link #getActivity(Context, int, Intent, int)}. Upon sending |
| 211 | * the resulting PendingIntent, all of the Intents are started in the same |
| 212 | * way as they would be by passing them to {@link Context#startActivities(Intent[])}. |
| 213 | * |
| 214 | * <p class="note"> |
| 215 | * The <em>first</em> intent in the array will be started outside of the context of an |
| 216 | * existing activity, so you must use the {@link Intent#FLAG_ACTIVITY_NEW_TASK |
| 217 | * Intent.FLAG_ACTIVITY_NEW_TASK} launch flag in the Intent. (Activities after |
| 218 | * the first in the array are started in the context of the previous activity |
| 219 | * in the array, so FLAG_ACTIVITY_NEW_TASK is not needed nor desired for them.) |
| 220 | * </p> |
| 221 | * |
| 222 | * <p class="note"> |
| 223 | * The <em>last</em> intent in the array represents the key for the |
| 224 | * PendingIntent. In other words, it is the significant element for matching |
| 225 | * (as done with the single intent given to {@link #getActivity(Context, int, Intent, int)}, |
| 226 | * its content will be the subject of replacement by |
| 227 | * {@link #send(Context, int, Intent)} and {@link #FLAG_UPDATE_CURRENT}, etc. |
| 228 | * This is because it is the most specific of the supplied intents, and the |
| 229 | * UI the user actually sees when the intents are started. |
| 230 | * </p> |
| 231 | * |
| 232 | * @param context The Context in which this PendingIntent should start |
| 233 | * the activity. |
| 234 | * @param requestCode Private request code for the sender (currently |
| 235 | * not used). |
| 236 | * @param intents Array of Intents of the activities to be launched. |
| 237 | * @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE}, |
| 238 | * {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT}, |
| 239 | * or any of the flags as supported by |
| 240 | * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts |
| 241 | * of the intent that can be supplied when the actual send happens. |
| 242 | * |
| 243 | * @return Returns an existing or new PendingIntent matching the given |
| 244 | * parameters. May return null only if {@link #FLAG_NO_CREATE} has been |
| 245 | * supplied. |
| 246 | */ |
| 247 | public static PendingIntent getActivities(Context context, int requestCode, |
| 248 | Intent[] intents, int flags) { |
| 249 | String packageName = context.getPackageName(); |
| 250 | String[] resolvedTypes = new String[intents.length]; |
| 251 | for (int i=0; i<intents.length; i++) { |
| 252 | resolvedTypes[i] = intents[i].resolveTypeIfNeeded(context.getContentResolver()); |
| 253 | } |
| 254 | try { |
| 255 | IIntentSender target = |
| 256 | ActivityManagerNative.getDefault().getIntentSender( |
| 257 | IActivityManager.INTENT_SENDER_ACTIVITY, packageName, |
| 258 | null, null, requestCode, intents, resolvedTypes, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | return target != null ? new PendingIntent(target) : null; |
| 260 | } catch (RemoteException e) { |
| 261 | } |
| 262 | return null; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Retrieve a PendingIntent that will perform a broadcast, like calling |
| 267 | * {@link Context#sendBroadcast(Intent) Context.sendBroadcast()}. |
| 268 | * |
| 269 | * @param context The Context in which this PendingIntent should perform |
| 270 | * the broadcast. |
| 271 | * @param requestCode Private request code for the sender (currently |
| 272 | * not used). |
| 273 | * @param intent The Intent to be broadcast. |
| 274 | * @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE}, |
| 275 | * {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT}, |
| 276 | * or any of the flags as supported by |
| 277 | * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts |
| 278 | * of the intent that can be supplied when the actual send happens. |
| 279 | * |
| 280 | * @return Returns an existing or new PendingIntent matching the given |
| 281 | * parameters. May return null only if {@link #FLAG_NO_CREATE} has been |
| 282 | * supplied. |
| 283 | */ |
| 284 | public static PendingIntent getBroadcast(Context context, int requestCode, |
| 285 | Intent intent, int flags) { |
| 286 | String packageName = context.getPackageName(); |
| 287 | String resolvedType = intent != null ? intent.resolveTypeIfNeeded( |
| 288 | context.getContentResolver()) : null; |
| 289 | try { |
| 290 | IIntentSender target = |
| 291 | ActivityManagerNative.getDefault().getIntentSender( |
| 292 | IActivityManager.INTENT_SENDER_BROADCAST, packageName, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 293 | null, null, requestCode, new Intent[] { intent }, |
| 294 | resolvedType != null ? new String[] { resolvedType } : null, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | return target != null ? new PendingIntent(target) : null; |
| 296 | } catch (RemoteException e) { |
| 297 | } |
| 298 | return null; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Retrieve a PendingIntent that will start a service, like calling |
| 303 | * {@link Context#startService Context.startService()}. The start |
| 304 | * arguments given to the service will come from the extras of the Intent. |
| 305 | * |
| 306 | * @param context The Context in which this PendingIntent should start |
| 307 | * the service. |
| 308 | * @param requestCode Private request code for the sender (currently |
| 309 | * not used). |
| 310 | * @param intent An Intent describing the service to be started. |
| 311 | * @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE}, |
| 312 | * {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT}, |
| 313 | * or any of the flags as supported by |
| 314 | * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts |
| 315 | * of the intent that can be supplied when the actual send happens. |
| 316 | * |
| 317 | * @return Returns an existing or new PendingIntent matching the given |
| 318 | * parameters. May return null only if {@link #FLAG_NO_CREATE} has been |
| 319 | * supplied. |
| 320 | */ |
| 321 | public static PendingIntent getService(Context context, int requestCode, |
| 322 | Intent intent, int flags) { |
| 323 | String packageName = context.getPackageName(); |
| 324 | String resolvedType = intent != null ? intent.resolveTypeIfNeeded( |
| 325 | context.getContentResolver()) : null; |
| 326 | try { |
| 327 | IIntentSender target = |
| 328 | ActivityManagerNative.getDefault().getIntentSender( |
| 329 | IActivityManager.INTENT_SENDER_SERVICE, packageName, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 330 | null, null, requestCode, new Intent[] { intent }, |
| 331 | resolvedType != null ? new String[] { resolvedType } : null, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | return target != null ? new PendingIntent(target) : null; |
| 333 | } catch (RemoteException e) { |
| 334 | } |
| 335 | return null; |
| 336 | } |
| 337 | |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 338 | /** |
| 339 | * Retrieve a IntentSender object that wraps the existing sender of the PendingIntent |
| 340 | * |
| 341 | * @return Returns a IntentSender object that wraps the sender of PendingIntent |
| 342 | * |
| 343 | */ |
| 344 | public IntentSender getIntentSender() { |
Dianne Hackborn | 7f20543 | 2009-07-28 00:13:47 -0700 | [diff] [blame] | 345 | return new IntentSender(mTarget); |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 346 | } |
| 347 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | /** |
| 349 | * Cancel a currently active PendingIntent. Only the original application |
| 350 | * owning an PendingIntent can cancel it. |
| 351 | */ |
| 352 | public void cancel() { |
| 353 | try { |
| 354 | ActivityManagerNative.getDefault().cancelIntentSender(mTarget); |
| 355 | } catch (RemoteException e) { |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Perform the operation associated with this PendingIntent. |
| 361 | * |
| 362 | * @see #send(Context, int, Intent, android.app.PendingIntent.OnFinished, Handler) |
| 363 | * |
| 364 | * @throws CanceledException Throws CanceledException if the PendingIntent |
| 365 | * is no longer allowing more intents to be sent through it. |
| 366 | */ |
| 367 | public void send() throws CanceledException { |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 368 | send(null, 0, null, null, null, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Perform the operation associated with this PendingIntent. |
| 373 | * |
| 374 | * @param code Result code to supply back to the PendingIntent's target. |
| 375 | * |
| 376 | * @see #send(Context, int, Intent, android.app.PendingIntent.OnFinished, Handler) |
| 377 | * |
| 378 | * @throws CanceledException Throws CanceledException if the PendingIntent |
| 379 | * is no longer allowing more intents to be sent through it. |
| 380 | */ |
| 381 | public void send(int code) throws CanceledException { |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 382 | send(null, code, null, null, null, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Perform the operation associated with this PendingIntent, allowing the |
| 387 | * caller to specify information about the Intent to use. |
| 388 | * |
| 389 | * @param context The Context of the caller. |
| 390 | * @param code Result code to supply back to the PendingIntent's target. |
| 391 | * @param intent Additional Intent data. See {@link Intent#fillIn |
| 392 | * Intent.fillIn()} for information on how this is applied to the |
| 393 | * original Intent. |
| 394 | * |
| 395 | * @see #send(Context, int, Intent, android.app.PendingIntent.OnFinished, Handler) |
| 396 | * |
| 397 | * @throws CanceledException Throws CanceledException if the PendingIntent |
| 398 | * is no longer allowing more intents to be sent through it. |
| 399 | */ |
| 400 | public void send(Context context, int code, Intent intent) |
| 401 | throws CanceledException { |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 402 | send(context, code, intent, null, null, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /** |
| 406 | * Perform the operation associated with this PendingIntent, allowing the |
| 407 | * caller to be notified when the send has completed. |
| 408 | * |
| 409 | * @param code Result code to supply back to the PendingIntent's target. |
| 410 | * @param onFinished The object to call back on when the send has |
| 411 | * completed, or null for no callback. |
| 412 | * @param handler Handler identifying the thread on which the callback |
| 413 | * should happen. If null, the callback will happen from the thread |
| 414 | * pool of the process. |
| 415 | * |
| 416 | * @see #send(Context, int, Intent, android.app.PendingIntent.OnFinished, Handler) |
| 417 | * |
| 418 | * @throws CanceledException Throws CanceledException if the PendingIntent |
| 419 | * is no longer allowing more intents to be sent through it. |
| 420 | */ |
| 421 | public void send(int code, OnFinished onFinished, Handler handler) |
| 422 | throws CanceledException { |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 423 | send(null, code, null, onFinished, handler, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Perform the operation associated with this PendingIntent, allowing the |
| 428 | * caller to specify information about the Intent to use and be notified |
| 429 | * when the send has completed. |
| 430 | * |
| 431 | * <p>For the intent parameter, a PendingIntent |
| 432 | * often has restrictions on which fields can be supplied here, based on |
| 433 | * how the PendingIntent was retrieved in {@link #getActivity}, |
| 434 | * {@link #getBroadcast}, or {@link #getService}. |
| 435 | * |
| 436 | * @param context The Context of the caller. This may be null if |
| 437 | * <var>intent</var> is also null. |
| 438 | * @param code Result code to supply back to the PendingIntent's target. |
| 439 | * @param intent Additional Intent data. See {@link Intent#fillIn |
| 440 | * Intent.fillIn()} for information on how this is applied to the |
| 441 | * original Intent. Use null to not modify the original Intent. |
| 442 | * @param onFinished The object to call back on when the send has |
| 443 | * completed, or null for no callback. |
| 444 | * @param handler Handler identifying the thread on which the callback |
| 445 | * should happen. If null, the callback will happen from the thread |
| 446 | * pool of the process. |
| 447 | * |
| 448 | * @see #send() |
| 449 | * @see #send(int) |
| 450 | * @see #send(Context, int, Intent) |
| 451 | * @see #send(int, android.app.PendingIntent.OnFinished, Handler) |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 452 | * @see #send(Context, int, Intent, OnFinished, Handler, String) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | * |
| 454 | * @throws CanceledException Throws CanceledException if the PendingIntent |
| 455 | * is no longer allowing more intents to be sent through it. |
| 456 | */ |
| 457 | public void send(Context context, int code, Intent intent, |
| 458 | OnFinished onFinished, Handler handler) throws CanceledException { |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 459 | send(context, code, intent, onFinished, handler, null); |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * Perform the operation associated with this PendingIntent, allowing the |
| 464 | * caller to specify information about the Intent to use and be notified |
| 465 | * when the send has completed. |
| 466 | * |
| 467 | * <p>For the intent parameter, a PendingIntent |
| 468 | * often has restrictions on which fields can be supplied here, based on |
| 469 | * how the PendingIntent was retrieved in {@link #getActivity}, |
| 470 | * {@link #getBroadcast}, or {@link #getService}. |
| 471 | * |
| 472 | * @param context The Context of the caller. This may be null if |
| 473 | * <var>intent</var> is also null. |
| 474 | * @param code Result code to supply back to the PendingIntent's target. |
| 475 | * @param intent Additional Intent data. See {@link Intent#fillIn |
| 476 | * Intent.fillIn()} for information on how this is applied to the |
| 477 | * original Intent. Use null to not modify the original Intent. |
| 478 | * @param onFinished The object to call back on when the send has |
| 479 | * completed, or null for no callback. |
| 480 | * @param handler Handler identifying the thread on which the callback |
| 481 | * should happen. If null, the callback will happen from the thread |
| 482 | * pool of the process. |
| 483 | * @param requiredPermission Name of permission that a recipient of the PendingIntent |
| 484 | * is required to hold. This is only valid for broadcast intents, and |
| 485 | * corresponds to the permission argument in |
| 486 | * {@link Context#sendBroadcast(Intent, String) Context.sendOrderedBroadcast(Intent, String)}. |
| 487 | * If null, no permission is required. |
| 488 | * |
| 489 | * @see #send() |
| 490 | * @see #send(int) |
| 491 | * @see #send(Context, int, Intent) |
| 492 | * @see #send(int, android.app.PendingIntent.OnFinished, Handler) |
| 493 | * @see #send(Context, int, Intent, OnFinished, Handler) |
| 494 | * |
| 495 | * @throws CanceledException Throws CanceledException if the PendingIntent |
| 496 | * is no longer allowing more intents to be sent through it. |
| 497 | */ |
| 498 | public void send(Context context, int code, Intent intent, |
| 499 | OnFinished onFinished, Handler handler, String requiredPermission) |
| 500 | throws CanceledException { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | try { |
| 502 | String resolvedType = intent != null ? |
| 503 | intent.resolveTypeIfNeeded(context.getContentResolver()) |
| 504 | : null; |
| 505 | int res = mTarget.send(code, intent, resolvedType, |
| 506 | onFinished != null |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 507 | ? new FinishedDispatcher(this, onFinished, handler) |
| 508 | : null, |
| 509 | requiredPermission); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 510 | if (res < 0) { |
| 511 | throw new CanceledException(); |
| 512 | } |
| 513 | } catch (RemoteException e) { |
| 514 | throw new CanceledException(e); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * Return the package name of the application that created this |
| 520 | * PendingIntent, that is the identity under which you will actually be |
| 521 | * sending the Intent. The returned string is supplied by the system, so |
| 522 | * that an application can not spoof its package. |
| 523 | * |
| 524 | * @return The package name of the PendingIntent, or null if there is |
| 525 | * none associated with it. |
| 526 | */ |
| 527 | public String getTargetPackage() { |
| 528 | try { |
| 529 | return ActivityManagerNative.getDefault() |
| 530 | .getPackageForIntentSender(mTarget); |
| 531 | } catch (RemoteException e) { |
| 532 | // Should never happen. |
| 533 | return null; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | /** |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 538 | * @hide |
| 539 | * Check to verify that this PendingIntent targets a specific package. |
| 540 | */ |
| 541 | public boolean isTargetedToPackage() { |
| 542 | try { |
| 543 | return ActivityManagerNative.getDefault() |
| 544 | .isIntentSenderTargetedToPackage(mTarget); |
| 545 | } catch (RemoteException e) { |
| 546 | // Should never happen. |
| 547 | return false; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 552 | * Comparison operator on two PendingIntent objects, such that true |
| 553 | * is returned then they both represent the same operation from the |
| 554 | * same package. This allows you to use {@link #getActivity}, |
| 555 | * {@link #getBroadcast}, or {@link #getService} multiple times (even |
| 556 | * across a process being killed), resulting in different PendingIntent |
| 557 | * objects but whose equals() method identifies them as being the same |
| 558 | * operation. |
| 559 | */ |
| 560 | @Override |
| 561 | public boolean equals(Object otherObj) { |
| 562 | if (otherObj instanceof PendingIntent) { |
| 563 | return mTarget.asBinder().equals(((PendingIntent)otherObj) |
| 564 | .mTarget.asBinder()); |
| 565 | } |
| 566 | return false; |
| 567 | } |
| 568 | |
| 569 | @Override |
| 570 | public int hashCode() { |
| 571 | return mTarget.asBinder().hashCode(); |
| 572 | } |
| 573 | |
| 574 | @Override |
| 575 | public String toString() { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 576 | StringBuilder sb = new StringBuilder(128); |
| 577 | sb.append("PendingIntent{"); |
| 578 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 579 | sb.append(": "); |
| 580 | sb.append(mTarget != null ? mTarget.asBinder() : null); |
| 581 | sb.append('}'); |
| 582 | return sb.toString(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | public int describeContents() { |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | public void writeToParcel(Parcel out, int flags) { |
| 590 | out.writeStrongBinder(mTarget.asBinder()); |
| 591 | } |
| 592 | |
| 593 | public static final Parcelable.Creator<PendingIntent> CREATOR |
| 594 | = new Parcelable.Creator<PendingIntent>() { |
| 595 | public PendingIntent createFromParcel(Parcel in) { |
| 596 | IBinder target = in.readStrongBinder(); |
| 597 | return target != null ? new PendingIntent(target) : null; |
| 598 | } |
| 599 | |
| 600 | public PendingIntent[] newArray(int size) { |
| 601 | return new PendingIntent[size]; |
| 602 | } |
| 603 | }; |
| 604 | |
| 605 | /** |
| 606 | * Convenience function for writing either a PendingIntent or null pointer to |
| 607 | * a Parcel. You must use this with {@link #readPendingIntentOrNullFromParcel} |
| 608 | * for later reading it. |
| 609 | * |
| 610 | * @param sender The PendingIntent to write, or null. |
| 611 | * @param out Where to write the PendingIntent. |
| 612 | */ |
| 613 | public static void writePendingIntentOrNullToParcel(PendingIntent sender, |
| 614 | Parcel out) { |
| 615 | out.writeStrongBinder(sender != null ? sender.mTarget.asBinder() |
| 616 | : null); |
| 617 | } |
| 618 | |
| 619 | /** |
| 620 | * Convenience function for reading either a Messenger or null pointer from |
| 621 | * a Parcel. You must have previously written the Messenger with |
| 622 | * {@link #writePendingIntentOrNullToParcel}. |
| 623 | * |
| 624 | * @param in The Parcel containing the written Messenger. |
| 625 | * |
| 626 | * @return Returns the Messenger read from the Parcel, or null if null had |
| 627 | * been written. |
| 628 | */ |
| 629 | public static PendingIntent readPendingIntentOrNullFromParcel(Parcel in) { |
| 630 | IBinder b = in.readStrongBinder(); |
| 631 | return b != null ? new PendingIntent(b) : null; |
| 632 | } |
| 633 | |
| 634 | /*package*/ PendingIntent(IIntentSender target) { |
| 635 | mTarget = target; |
| 636 | } |
| 637 | |
| 638 | /*package*/ PendingIntent(IBinder target) { |
| 639 | mTarget = IIntentSender.Stub.asInterface(target); |
| 640 | } |
| 641 | |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 642 | /** @hide */ |
| 643 | public IIntentSender getTarget() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 644 | return mTarget; |
| 645 | } |
| 646 | } |