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 17package android.app; 18 19import android.content.Context; 20import android.content.Intent; 21import android.content.IIntentReceiver; 22import android.content.IIntentSender; 23import android.content.IntentSender; 24import android.os.Bundle; 25import android.os.RemoteException; 26import android.os.Handler; 27import android.os.IBinder; 28import android.os.Parcel; 29import android.os.Parcelable; 30import 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 */ 57public 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 } 111 } 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, 150 String data, Bundle extras, boolean serialized, boolean sticky) { 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 return getActivity(context, requestCode, intent, flags, null); 192 } 193 194 /** 195 * Retrieve a PendingIntent that will start a new activity, like calling 196 * {@link Context#startActivity(Intent) Context.startActivity(Intent)}. 197 * Note that the activity will be started outside of the context of an 198 * existing activity, so you must use the {@link Intent#FLAG_ACTIVITY_NEW_TASK 199 * Intent.FLAG_ACTIVITY_NEW_TASK} launch flag in the Intent. 200 * 201 * @param context The Context in which this PendingIntent should start 202 * the activity. 203 * @param requestCode Private request code for the sender (currently 204 * not used). 205 * @param intent Intent of the activity to be launched. 206 * @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE}, 207 * {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT}, 208 * or any of the flags as supported by 209 * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts 210 * of the intent that can be supplied when the actual send happens. 211 * @param options Additional options for how the Activity should be started. 212 * May be null if there are no options. 213 * 214 * @return Returns an existing or new PendingIntent matching the given 215 * parameters. May return null only if {@link #FLAG_NO_CREATE} has been 216 * supplied. 217 */ 218 public static PendingIntent getActivity(Context context, int requestCode, 219 Intent intent, int flags, Bundle options) { 220 String packageName = context.getPackageName(); 221 String resolvedType = intent != null ? intent.resolveTypeIfNeeded( 222 context.getContentResolver()) : null; 223 try { 224 intent.setAllowFds(false); 225 IIntentSender target = 226 ActivityManagerNative.getDefault().getIntentSender( 227 ActivityManager.INTENT_SENDER_ACTIVITY, packageName, 228 null, null, requestCode, new Intent[] { intent }, 229 resolvedType != null ? new String[] { resolvedType } : null, 230 flags, options); 231 return target != null ? new PendingIntent(target) : null; 232 } catch (RemoteException e) { 233 } 234 return null; 235 } 236 237 /** 238 * Like {@link #getActivity(Context, int, Intent, int)}, but allows an 239 * array of Intents to be supplied. The first Intent in the array is 240 * taken as the primary key for the PendingIntent, like the single Intent 241 * given to {@link #getActivity(Context, int, Intent, int)}. Upon sending 242 * the resulting PendingIntent, all of the Intents are started in the same 243 * way as they would be by passing them to {@link Context#startActivities(Intent[])}. 244 * 245 * <p class="note"> 246 * The <em>first</em> intent in the array will be started outside of the context of an 247 * existing activity, so you must use the {@link Intent#FLAG_ACTIVITY_NEW_TASK 248 * Intent.FLAG_ACTIVITY_NEW_TASK} launch flag in the Intent. (Activities after 249 * the first in the array are started in the context of the previous activity 250 * in the array, so FLAG_ACTIVITY_NEW_TASK is not needed nor desired for them.) 251 * </p> 252 * 253 * <p class="note"> 254 * The <em>last</em> intent in the array represents the key for the 255 * PendingIntent. In other words, it is the significant element for matching 256 * (as done with the single intent given to {@link #getActivity(Context, int, Intent, int)}, 257 * its content will be the subject of replacement by 258 * {@link #send(Context, int, Intent)} and {@link #FLAG_UPDATE_CURRENT}, etc. 259 * This is because it is the most specific of the supplied intents, and the 260 * UI the user actually sees when the intents are started. 261 * </p> 262 * 263 * @param context The Context in which this PendingIntent should start 264 * the activity. 265 * @param requestCode Private request code for the sender (currently 266 * not used). 267 * @param intents Array of Intents of the activities to be launched. 268 * @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE}, 269 * {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT}, 270 * or any of the flags as supported by 271 * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts 272 * of the intent that can be supplied when the actual send happens. 273 * 274 * @return Returns an existing or new PendingIntent matching the given 275 * parameters. May return null only if {@link #FLAG_NO_CREATE} has been 276 * supplied. 277 */ 278 public static PendingIntent getActivities(Context context, int requestCode, 279 Intent[] intents, int flags) { 280 return getActivities(context, requestCode, intents, flags, null); 281 } 282 283 /** 284 * Like {@link #getActivity(Context, int, Intent, int)}, but allows an 285 * array of Intents to be supplied. The first Intent in the array is 286 * taken as the primary key for the PendingIntent, like the single Intent 287 * given to {@link #getActivity(Context, int, Intent, int)}. Upon sending 288 * the resulting PendingIntent, all of the Intents are started in the same 289 * way as they would be by passing them to {@link Context#startActivities(Intent[])}. 290 * 291 * <p class="note"> 292 * The <em>first</em> intent in the array will be started outside of the context of an 293 * existing activity, so you must use the {@link Intent#FLAG_ACTIVITY_NEW_TASK 294 * Intent.FLAG_ACTIVITY_NEW_TASK} launch flag in the Intent. (Activities after 295 * the first in the array are started in the context of the previous activity 296 * in the array, so FLAG_ACTIVITY_NEW_TASK is not needed nor desired for them.) 297 * </p> 298 * 299 * <p class="note"> 300 * The <em>last</em> intent in the array represents the key for the 301 * PendingIntent. In other words, it is the significant element for matching 302 * (as done with the single intent given to {@link #getActivity(Context, int, Intent, int)}, 303 * its content will be the subject of replacement by 304 * {@link #send(Context, int, Intent)} and {@link #FLAG_UPDATE_CURRENT}, etc. 305 * This is because it is the most specific of the supplied intents, and the 306 * UI the user actually sees when the intents are started. 307 * </p> 308 * 309 * @param context The Context in which this PendingIntent should start 310 * the activity. 311 * @param requestCode Private request code for the sender (currently 312 * not used). 313 * @param intents Array of Intents of the activities to be launched. 314 * @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE}, 315 * {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT}, 316 * or any of the flags as supported by 317 * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts 318 * of the intent that can be supplied when the actual send happens. 319 * 320 * @return Returns an existing or new PendingIntent matching the given 321 * parameters. May return null only if {@link #FLAG_NO_CREATE} has been 322 * supplied. 323 */ 324 public static PendingIntent getActivities(Context context, int requestCode, 325 Intent[] intents, int flags, Bundle options) { 326 String packageName = context.getPackageName(); 327 String[] resolvedTypes = new String[intents.length]; 328 for (int i=0; i<intents.length; i++) { 329 intents[i].setAllowFds(false); 330 resolvedTypes[i] = intents[i].resolveTypeIfNeeded(context.getContentResolver()); 331 } 332 try { 333 IIntentSender target = 334 ActivityManagerNative.getDefault().getIntentSender( 335 ActivityManager.INTENT_SENDER_ACTIVITY, packageName, 336 null, null, requestCode, intents, resolvedTypes, flags, options); 337 return target != null ? new PendingIntent(target) : null; 338 } catch (RemoteException e) { 339 } 340 return null; 341 } 342 343 /** 344 * Retrieve a PendingIntent that will perform a broadcast, like calling 345 * {@link Context#sendBroadcast(Intent) Context.sendBroadcast()}. 346 * 347 * @param context The Context in which this PendingIntent should perform 348 * the broadcast. 349 * @param requestCode Private request code for the sender (currently 350 * not used). 351 * @param intent The Intent to be broadcast. 352 * @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE}, 353 * {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT}, 354 * or any of the flags as supported by 355 * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts 356 * of the intent that can be supplied when the actual send happens. 357 * 358 * @return Returns an existing or new PendingIntent matching the given 359 * parameters. May return null only if {@link #FLAG_NO_CREATE} has been 360 * supplied. 361 */ 362 public static PendingIntent getBroadcast(Context context, int requestCode, 363 Intent intent, int flags) { 364 String packageName = context.getPackageName(); 365 String resolvedType = intent != null ? intent.resolveTypeIfNeeded( 366 context.getContentResolver()) : null; 367 try { 368 intent.setAllowFds(false); 369 IIntentSender target = 370 ActivityManagerNative.getDefault().getIntentSender( 371 ActivityManager.INTENT_SENDER_BROADCAST, packageName, 372 null, null, requestCode, new Intent[] { intent }, 373 resolvedType != null ? new String[] { resolvedType } : null, 374 flags, null); 375 return target != null ? new PendingIntent(target) : null; 376 } catch (RemoteException e) { 377 } 378 return null; 379 } 380 381 /** 382 * Retrieve a PendingIntent that will start a service, like calling 383 * {@link Context#startService Context.startService()}. The start 384 * arguments given to the service will come from the extras of the Intent. 385 * 386 * @param context The Context in which this PendingIntent should start 387 * the service. 388 * @param requestCode Private request code for the sender (currently 389 * not used). 390 * @param intent An Intent describing the service to be started. 391 * @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE}, 392 * {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT}, 393 * or any of the flags as supported by 394 * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts 395 * of the intent that can be supplied when the actual send happens. 396 * 397 * @return Returns an existing or new PendingIntent matching the given 398 * parameters. May return null only if {@link #FLAG_NO_CREATE} has been 399 * supplied. 400 */ 401 public static PendingIntent getService(Context context, int requestCode, 402 Intent intent, int flags) { 403 String packageName = context.getPackageName(); 404 String resolvedType = intent != null ? intent.resolveTypeIfNeeded( 405 context.getContentResolver()) : null; 406 try { 407 intent.setAllowFds(false); 408 IIntentSender target = 409 ActivityManagerNative.getDefault().getIntentSender( 410 ActivityManager.INTENT_SENDER_SERVICE, packageName, 411 null, null, requestCode, new Intent[] { intent }, 412 resolvedType != null ? new String[] { resolvedType } : null, 413 flags, null); 414 return target != null ? new PendingIntent(target) : null; 415 } catch (RemoteException e) { 416 } 417 return null; 418 } 419 420 /** 421 * Retrieve a IntentSender object that wraps the existing sender of the PendingIntent 422 * 423 * @return Returns a IntentSender object that wraps the sender of PendingIntent 424 * 425 */ 426 public IntentSender getIntentSender() { 427 return new IntentSender(mTarget); 428 } 429 430 /** 431 * Cancel a currently active PendingIntent. Only the original application 432 * owning a PendingIntent can cancel it. 433 */ 434 public void cancel() { 435 try { 436 ActivityManagerNative.getDefault().cancelIntentSender(mTarget); 437 } catch (RemoteException e) { 438 } 439 } 440 441 /** 442 * Perform the operation associated with this PendingIntent. 443 * 444 * @see #send(Context, int, Intent, android.app.PendingIntent.OnFinished, Handler) 445 * 446 * @throws CanceledException Throws CanceledException if the PendingIntent 447 * is no longer allowing more intents to be sent through it. 448 */ 449 public void send() throws CanceledException { 450 send(null, 0, null, null, null, null); 451 } 452 453 /** 454 * Perform the operation associated with this PendingIntent. 455 * 456 * @param code Result code to supply back to the PendingIntent's target. 457 * 458 * @see #send(Context, int, Intent, android.app.PendingIntent.OnFinished, Handler) 459 * 460 * @throws CanceledException Throws CanceledException if the PendingIntent 461 * is no longer allowing more intents to be sent through it. 462 */ 463 public void send(int code) throws CanceledException { 464 send(null, code, null, null, null, null); 465 } 466 467 /** 468 * Perform the operation associated with this PendingIntent, allowing the 469 * caller to specify information about the Intent to use. 470 * 471 * @param context The Context of the caller. 472 * @param code Result code to supply back to the PendingIntent's target. 473 * @param intent Additional Intent data. See {@link Intent#fillIn 474 * Intent.fillIn()} for information on how this is applied to the 475 * original Intent. 476 * 477 * @see #send(Context, int, Intent, android.app.PendingIntent.OnFinished, Handler) 478 * 479 * @throws CanceledException Throws CanceledException if the PendingIntent 480 * is no longer allowing more intents to be sent through it. 481 */ 482 public void send(Context context, int code, Intent intent) 483 throws CanceledException { 484 send(context, code, intent, null, null, null); 485 } 486 487 /** 488 * Perform the operation associated with this PendingIntent, allowing the 489 * caller to be notified when the send has completed. 490 * 491 * @param code Result code to supply back to the PendingIntent's target. 492 * @param onFinished The object to call back on when the send has 493 * completed, or null for no callback. 494 * @param handler Handler identifying the thread on which the callback 495 * should happen. If null, the callback will happen from the thread 496 * pool of the process. 497 * 498 * @see #send(Context, int, Intent, android.app.PendingIntent.OnFinished, Handler) 499 * 500 * @throws CanceledException Throws CanceledException if the PendingIntent 501 * is no longer allowing more intents to be sent through it. 502 */ 503 public void send(int code, OnFinished onFinished, Handler handler) 504 throws CanceledException { 505 send(null, code, null, onFinished, handler, null); 506 } 507 508 /** 509 * Perform the operation associated with this PendingIntent, allowing the 510 * caller to specify information about the Intent to use and be notified 511 * when the send has completed. 512 * 513 * <p>For the intent parameter, a PendingIntent 514 * often has restrictions on which fields can be supplied here, based on 515 * how the PendingIntent was retrieved in {@link #getActivity}, 516 * {@link #getBroadcast}, or {@link #getService}. 517 * 518 * @param context The Context of the caller. This may be null if 519 * <var>intent</var> is also null. 520 * @param code Result code to supply back to the PendingIntent's target. 521 * @param intent Additional Intent data. See {@link Intent#fillIn 522 * Intent.fillIn()} for information on how this is applied to the 523 * original Intent. Use null to not modify the original Intent. 524 * @param onFinished The object to call back on when the send has 525 * completed, or null for no callback. 526 * @param handler Handler identifying the thread on which the callback 527 * should happen. If null, the callback will happen from the thread 528 * pool of the process. 529 * 530 * @see #send() 531 * @see #send(int) 532 * @see #send(Context, int, Intent) 533 * @see #send(int, android.app.PendingIntent.OnFinished, Handler) 534 * @see #send(Context, int, Intent, OnFinished, Handler, String) 535 * 536 * @throws CanceledException Throws CanceledException if the PendingIntent 537 * is no longer allowing more intents to be sent through it. 538 */ 539 public void send(Context context, int code, Intent intent, 540 OnFinished onFinished, Handler handler) throws CanceledException { 541 send(context, code, intent, onFinished, handler, null); 542 } 543 544 /** 545 * Perform the operation associated with this PendingIntent, allowing the 546 * caller to specify information about the Intent to use and be notified 547 * when the send has completed. 548 * 549 * <p>For the intent parameter, a PendingIntent 550 * often has restrictions on which fields can be supplied here, based on 551 * how the PendingIntent was retrieved in {@link #getActivity}, 552 * {@link #getBroadcast}, or {@link #getService}. 553 * 554 * @param context The Context of the caller. This may be null if 555 * <var>intent</var> is also null. 556 * @param code Result code to supply back to the PendingIntent's target. 557 * @param intent Additional Intent data. See {@link Intent#fillIn 558 * Intent.fillIn()} for information on how this is applied to the 559 * original Intent. Use null to not modify the original Intent. 560 * @param onFinished The object to call back on when the send has 561 * completed, or null for no callback. 562 * @param handler Handler identifying the thread on which the callback 563 * should happen. If null, the callback will happen from the thread 564 * pool of the process. 565 * @param requiredPermission Name of permission that a recipient of the PendingIntent 566 * is required to hold. This is only valid for broadcast intents, and 567 * corresponds to the permission argument in 568 * {@link Context#sendBroadcast(Intent, String) Context.sendOrderedBroadcast(Intent, String)}. 569 * If null, no permission is required. 570 * 571 * @see #send() 572 * @see #send(int) 573 * @see #send(Context, int, Intent) 574 * @see #send(int, android.app.PendingIntent.OnFinished, Handler) 575 * @see #send(Context, int, Intent, OnFinished, Handler) 576 * 577 * @throws CanceledException Throws CanceledException if the PendingIntent 578 * is no longer allowing more intents to be sent through it. 579 */ 580 public void send(Context context, int code, Intent intent, 581 OnFinished onFinished, Handler handler, String requiredPermission) 582 throws CanceledException { 583 try { 584 String resolvedType = intent != null ? 585 intent.resolveTypeIfNeeded(context.getContentResolver()) 586 : null; 587 int res = mTarget.send(code, intent, resolvedType, 588 onFinished != null 589 ? new FinishedDispatcher(this, onFinished, handler) 590 : null, 591 requiredPermission); 592 if (res < 0) { 593 throw new CanceledException(); 594 } 595 } catch (RemoteException e) { 596 throw new CanceledException(e); 597 } 598 } 599 600 /** 601 * Return the package name of the application that created this 602 * PendingIntent, that is the identity under which you will actually be 603 * sending the Intent. The returned string is supplied by the system, so 604 * that an application can not spoof its package. 605 * 606 * @return The package name of the PendingIntent, or null if there is 607 * none associated with it. 608 */ 609 public String getTargetPackage() { 610 try { 611 return ActivityManagerNative.getDefault() 612 .getPackageForIntentSender(mTarget); 613 } catch (RemoteException e) { 614 // Should never happen. 615 return null; 616 } 617 } 618 619 /** 620 * @hide 621 * Check to verify that this PendingIntent targets a specific package. 622 */ 623 public boolean isTargetedToPackage() { 624 try { 625 return ActivityManagerNative.getDefault() 626 .isIntentSenderTargetedToPackage(mTarget); 627 } catch (RemoteException e) { 628 // Should never happen. 629 return false; 630 } 631 } 632 633 /** 634 * @hide 635 * Check whether this PendingIntent will launch an Activity. 636 */ 637 public boolean isActivity() { 638 try { 639 return ActivityManagerNative.getDefault() 640 .isIntentSenderAnActivity(mTarget); 641 } catch (RemoteException e) { 642 // Should never happen. 643 return false; 644 } 645 } 646 647 /** 648 * Comparison operator on two PendingIntent objects, such that true 649 * is returned then they both represent the same operation from the 650 * same package. This allows you to use {@link #getActivity}, 651 * {@link #getBroadcast}, or {@link #getService} multiple times (even 652 * across a process being killed), resulting in different PendingIntent 653 * objects but whose equals() method identifies them as being the same 654 * operation. 655 */ 656 @Override 657 public boolean equals(Object otherObj) { 658 if (otherObj instanceof PendingIntent) { 659 return mTarget.asBinder().equals(((PendingIntent)otherObj) 660 .mTarget.asBinder()); 661 } 662 return false; 663 } 664 665 @Override 666 public int hashCode() { 667 return mTarget.asBinder().hashCode(); 668 } 669 670 @Override 671 public String toString() { 672 StringBuilder sb = new StringBuilder(128); 673 sb.append("PendingIntent{"); 674 sb.append(Integer.toHexString(System.identityHashCode(this))); 675 sb.append(": "); 676 sb.append(mTarget != null ? mTarget.asBinder() : null); 677 sb.append('}'); 678 return sb.toString(); 679 } 680 681 public int describeContents() { 682 return 0; 683 } 684 685 public void writeToParcel(Parcel out, int flags) { 686 out.writeStrongBinder(mTarget.asBinder()); 687 } 688 689 public static final Parcelable.Creator<PendingIntent> CREATOR 690 = new Parcelable.Creator<PendingIntent>() { 691 public PendingIntent createFromParcel(Parcel in) { 692 IBinder target = in.readStrongBinder(); 693 return target != null ? new PendingIntent(target) : null; 694 } 695 696 public PendingIntent[] newArray(int size) { 697 return new PendingIntent[size]; 698 } 699 }; 700 701 /** 702 * Convenience function for writing either a PendingIntent or null pointer to 703 * a Parcel. You must use this with {@link #readPendingIntentOrNullFromParcel} 704 * for later reading it. 705 * 706 * @param sender The PendingIntent to write, or null. 707 * @param out Where to write the PendingIntent. 708 */ 709 public static void writePendingIntentOrNullToParcel(PendingIntent sender, 710 Parcel out) { 711 out.writeStrongBinder(sender != null ? sender.mTarget.asBinder() 712 : null); 713 } 714 715 /** 716 * Convenience function for reading either a Messenger or null pointer from 717 * a Parcel. You must have previously written the Messenger with 718 * {@link #writePendingIntentOrNullToParcel}. 719 * 720 * @param in The Parcel containing the written Messenger. 721 * 722 * @return Returns the Messenger read from the Parcel, or null if null had 723 * been written. 724 */ 725 public static PendingIntent readPendingIntentOrNullFromParcel(Parcel in) { 726 IBinder b = in.readStrongBinder(); 727 return b != null ? new PendingIntent(b) : null; 728 } 729 730 /*package*/ PendingIntent(IIntentSender target) { 731 mTarget = target; 732 } 733 734 /*package*/ PendingIntent(IBinder target) { 735 mTarget = IIntentSender.Stub.asInterface(target); 736 } 737 738 /** @hide */ 739 public IIntentSender getTarget() { 740 return mTarget; 741 } 742} 743