1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
|
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.app;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UnsupportedAppUsage;
import android.content.Context;
import android.content.IIntentReceiver;
import android.content.IIntentSender;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.RemoteException;
import android.os.UserHandle;
import android.util.AndroidException;
import android.util.ArraySet;
import android.util.proto.ProtoOutputStream;
import com.android.internal.os.IResultReceiver;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* A description of an Intent and target action to perform with it. Instances
* of this class are created with {@link #getActivity}, {@link #getActivities},
* {@link #getBroadcast}, and {@link #getService}; the returned object can be
* handed to other applications so that they can perform the action you
* described on your behalf at a later time.
*
* <p>By giving a PendingIntent to another application,
* you are granting it the right to perform the operation you have specified
* as if the other application was yourself (with the same permissions and
* identity). As such, you should be careful about how you build the PendingIntent:
* almost always, for example, the base Intent you supply should have the component
* name explicitly set to one of your own components, to ensure it is ultimately
* sent there and nowhere else.
*
* <p>A PendingIntent itself is simply a reference to a token maintained by
* the system describing the original data used to retrieve it. This means
* that, even if its owning application's process is killed, the
* PendingIntent itself will remain usable from other processes that
* have been given it. If the creating application later re-retrieves the
* same kind of PendingIntent (same operation, same Intent action, data,
* categories, and components, and same flags), it will receive a PendingIntent
* representing the same token if that is still valid, and can thus call
* {@link #cancel} to remove it.
*
* <p>Because of this behavior, it is important to know when two Intents
* are considered to be the same for purposes of retrieving a PendingIntent.
* A common mistake people make is to create multiple PendingIntent objects
* with Intents that only vary in their "extra" contents, expecting to get
* a different PendingIntent each time. This does <em>not</em> happen. The
* parts of the Intent that are used for matching are the same ones defined
* by {@link Intent#filterEquals(Intent) Intent.filterEquals}. If you use two
* Intent objects that are equivalent as per
* {@link Intent#filterEquals(Intent) Intent.filterEquals}, then you will get
* the same PendingIntent for both of them.
*
* <p>There are two typical ways to deal with this.
*
* <p>If you truly need multiple distinct PendingIntent objects active at
* the same time (such as to use as two notifications that are both shown
* at the same time), then you will need to ensure there is something that
* is different about them to associate them with different PendingIntents.
* This may be any of the Intent attributes considered by
* {@link Intent#filterEquals(Intent) Intent.filterEquals}, or different
* request code integers supplied to {@link #getActivity}, {@link #getActivities},
* {@link #getBroadcast}, or {@link #getService}.
*
* <p>If you only need one PendingIntent active at a time for any of the
* Intents you will use, then you can alternatively use the flags
* {@link #FLAG_CANCEL_CURRENT} or {@link #FLAG_UPDATE_CURRENT} to either
* cancel or modify whatever current PendingIntent is associated with the
* Intent you are supplying.
*/
public final class PendingIntent implements Parcelable {
private final IIntentSender mTarget;
private IResultReceiver mCancelReceiver;
private IBinder mWhitelistToken;
private ArraySet<CancelListener> mCancelListeners;
/** @hide */
@IntDef(flag = true,
value = {
FLAG_ONE_SHOT,
FLAG_NO_CREATE,
FLAG_CANCEL_CURRENT,
FLAG_UPDATE_CURRENT,
FLAG_IMMUTABLE,
Intent.FILL_IN_ACTION,
Intent.FILL_IN_DATA,
Intent.FILL_IN_CATEGORIES,
Intent.FILL_IN_COMPONENT,
Intent.FILL_IN_PACKAGE,
Intent.FILL_IN_SOURCE_BOUNDS,
Intent.FILL_IN_SELECTOR,
Intent.FILL_IN_CLIP_DATA
})
@Retention(RetentionPolicy.SOURCE)
public @interface Flags {}
/**
* Flag indicating that this PendingIntent can be used only once.
* For use with {@link #getActivity}, {@link #getBroadcast}, and
* {@link #getService}. <p>If set, after
* {@link #send()} is called on it, it will be automatically
* canceled for you and any future attempt to send through it will fail.
*/
public static final int FLAG_ONE_SHOT = 1<<30;
/**
* Flag indicating that if the described PendingIntent does not
* already exist, then simply return null instead of creating it.
* For use with {@link #getActivity}, {@link #getBroadcast}, and
* {@link #getService}.
*/
public static final int FLAG_NO_CREATE = 1<<29;
/**
* Flag indicating that if the described PendingIntent already exists,
* the current one should be canceled before generating a new one.
* For use with {@link #getActivity}, {@link #getBroadcast}, and
* {@link #getService}. <p>You can use
* this to retrieve a new PendingIntent when you are only changing the
* extra data in the Intent; by canceling the previous pending intent,
* this ensures that only entities given the new data will be able to
* launch it. If this assurance is not an issue, consider
* {@link #FLAG_UPDATE_CURRENT}.
*/
public static final int FLAG_CANCEL_CURRENT = 1<<28;
/**
* Flag indicating that if the described PendingIntent already exists,
* then keep it but replace its extra data with what is in this new
* Intent. For use with {@link #getActivity}, {@link #getBroadcast}, and
* {@link #getService}. <p>This can be used if you are creating intents where only the
* extras change, and don't care that any entities that received your
* previous PendingIntent will be able to launch it with your new
* extras even if they are not explicitly given to it.
*/
public static final int FLAG_UPDATE_CURRENT = 1<<27;
/**
* Flag indicating that the created PendingIntent should be immutable.
* This means that the additional intent argument passed to the send
* methods to fill in unpopulated properties of this intent will be
* ignored.
*/
public static final int FLAG_IMMUTABLE = 1<<26;
/**
* Exception thrown when trying to send through a PendingIntent that
* has been canceled or is otherwise no longer able to execute the request.
*/
public static class CanceledException extends AndroidException {
public CanceledException() {
}
public CanceledException(String name) {
super(name);
}
public CanceledException(Exception cause) {
super(cause);
}
}
/**
* Callback interface for discovering when a send operation has
* completed. Primarily for use with a PendingIntent that is
* performing a broadcast, this provides the same information as
* calling {@link Context#sendOrderedBroadcast(Intent, String,
* android.content.BroadcastReceiver, Handler, int, String, Bundle)
* Context.sendBroadcast()} with a final BroadcastReceiver.
*/
public interface OnFinished {
/**
* Called when a send operation as completed.
*
* @param pendingIntent The PendingIntent this operation was sent through.
* @param intent The original Intent that was sent.
* @param resultCode The final result code determined by the send.
* @param resultData The final data collected by a broadcast.
* @param resultExtras The final extras collected by a broadcast.
*/
void onSendFinished(PendingIntent pendingIntent, Intent intent,
int resultCode, String resultData, Bundle resultExtras);
}
private static class FinishedDispatcher extends IIntentReceiver.Stub
implements Runnable {
private final PendingIntent mPendingIntent;
private final OnFinished mWho;
private final Handler mHandler;
private Intent mIntent;
private int mResultCode;
private String mResultData;
private Bundle mResultExtras;
private static Handler sDefaultSystemHandler;
FinishedDispatcher(PendingIntent pi, OnFinished who, Handler handler) {
mPendingIntent = pi;
mWho = who;
if (handler == null && ActivityThread.isSystem()) {
// We assign a default handler for the system process to avoid deadlocks when
// processing receivers in various components that hold global service locks.
if (sDefaultSystemHandler == null) {
sDefaultSystemHandler = new Handler(Looper.getMainLooper());
}
mHandler = sDefaultSystemHandler;
} else {
mHandler = handler;
}
}
public void performReceive(Intent intent, int resultCode, String data,
Bundle extras, boolean serialized, boolean sticky, int sendingUser) {
mIntent = intent;
mResultCode = resultCode;
mResultData = data;
mResultExtras = extras;
if (mHandler == null) {
run();
} else {
mHandler.post(this);
}
}
public void run() {
mWho.onSendFinished(mPendingIntent, mIntent, mResultCode,
mResultData, mResultExtras);
}
}
/**
* Listener for observing when pending intents are written to a parcel.
*
* @hide
*/
public interface OnMarshaledListener {
/**
* Called when a pending intent is written to a parcel.
*
* @param intent The pending intent.
* @param parcel The parcel to which it was written.
* @param flags The parcel flags when it was written.
*/
void onMarshaled(PendingIntent intent, Parcel parcel, int flags);
}
private static final ThreadLocal<OnMarshaledListener> sOnMarshaledListener
= new ThreadLocal<>();
/**
* Registers an listener for pending intents being written to a parcel.
*
* @param listener The listener, null to clear.
*
* @hide
*/
@UnsupportedAppUsage
public static void setOnMarshaledListener(OnMarshaledListener listener) {
sOnMarshaledListener.set(listener);
}
/**
* Retrieve a PendingIntent that will start a new activity, like calling
* {@link Context#startActivity(Intent) Context.startActivity(Intent)}.
* Note that the activity will be started outside of the context of an
* existing activity, so you must use the {@link Intent#FLAG_ACTIVITY_NEW_TASK
* Intent.FLAG_ACTIVITY_NEW_TASK} launch flag in the Intent.
*
* <p class="note">For security reasons, the {@link android.content.Intent}
* you supply here should almost always be an <em>explicit intent</em>,
* that is specify an explicit component to be delivered to through
* {@link Intent#setClass(android.content.Context, Class) Intent.setClass}</p>
*
* @param context The Context in which this PendingIntent should start
* the activity.
* @param requestCode Private request code for the sender
* @param intent Intent of the activity to be launched.
* @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE},
* {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT},
* or any of the flags as supported by
* {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
* of the intent that can be supplied when the actual send happens.
*
* @return Returns an existing or new PendingIntent matching the given
* parameters. May return null only if {@link #FLAG_NO_CREATE} has been
* supplied.
*/
public static PendingIntent getActivity(Context context, int requestCode,
Intent intent, @Flags int flags) {
return getActivity(context, requestCode, intent, flags, null);
}
/**
* Retrieve a PendingIntent that will start a new activity, like calling
* {@link Context#startActivity(Intent) Context.startActivity(Intent)}.
* Note that the activity will be started outside of the context of an
* existing activity, so you must use the {@link Intent#FLAG_ACTIVITY_NEW_TASK
* Intent.FLAG_ACTIVITY_NEW_TASK} launch flag in the Intent.
*
* <p class="note">For security reasons, the {@link android.content.Intent}
* you supply here should almost always be an <em>explicit intent</em>,
* that is specify an explicit component to be delivered to through
* {@link Intent#setClass(android.content.Context, Class) Intent.setClass}</p>
*
* @param context The Context in which this PendingIntent should start
* the activity.
* @param requestCode Private request code for the sender
* @param intent Intent of the activity to be launched.
* @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE},
* {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT},
* or any of the flags as supported by
* {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
* of the intent that can be supplied when the actual send happens.
* @param options Additional options for how the Activity should be started.
* May be null if there are no options.
*
* @return Returns an existing or new PendingIntent matching the given
* parameters. May return null only if {@link #FLAG_NO_CREATE} has been
* supplied.
*/
public static PendingIntent getActivity(Context context, int requestCode,
@NonNull Intent intent, @Flags int flags, @Nullable Bundle options) {
String packageName = context.getPackageName();
String resolvedType = intent != null ? intent.resolveTypeIfNeeded(
context.getContentResolver()) : null;
try {
intent.migrateExtraStreamToClipData();
intent.prepareToLeaveProcess(context);
IIntentSender target =
ActivityManager.getService().getIntentSender(
ActivityManager.INTENT_SENDER_ACTIVITY, packageName,
null, null, requestCode, new Intent[] { intent },
resolvedType != null ? new String[] { resolvedType } : null,
flags, options, context.getUserId());
return target != null ? new PendingIntent(target) : null;
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* @hide
* Note that UserHandle.CURRENT will be interpreted at the time the
* activity is started, not when the pending intent is created.
*/
@UnsupportedAppUsage
public static PendingIntent getActivityAsUser(Context context, int requestCode,
@NonNull Intent intent, int flags, Bundle options, UserHandle user) {
String packageName = context.getPackageName();
String resolvedType = intent != null ? intent.resolveTypeIfNeeded(
context.getContentResolver()) : null;
try {
intent.migrateExtraStreamToClipData();
intent.prepareToLeaveProcess(context);
IIntentSender target =
ActivityManager.getService().getIntentSender(
ActivityManager.INTENT_SENDER_ACTIVITY, packageName,
null, null, requestCode, new Intent[] { intent },
resolvedType != null ? new String[] { resolvedType } : null,
flags, options, user.getIdentifier());
return target != null ? new PendingIntent(target) : null;
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Like {@link #getActivity(Context, int, Intent, int)}, but allows an
* array of Intents to be supplied. The last Intent in the array is
* taken as the primary key for the PendingIntent, like the single Intent
* given to {@link #getActivity(Context, int, Intent, int)}. Upon sending
* the resulting PendingIntent, all of the Intents are started in the same
* way as they would be by passing them to {@link Context#startActivities(Intent[])}.
*
* <p class="note">
* The <em>first</em> intent in the array will be started outside of the context of an
* existing activity, so you must use the {@link Intent#FLAG_ACTIVITY_NEW_TASK
* Intent.FLAG_ACTIVITY_NEW_TASK} launch flag in the Intent. (Activities after
* the first in the array are started in the context of the previous activity
* in the array, so FLAG_ACTIVITY_NEW_TASK is not needed nor desired for them.)
* </p>
*
* <p class="note">
* The <em>last</em> intent in the array represents the key for the
* PendingIntent. In other words, it is the significant element for matching
* (as done with the single intent given to {@link #getActivity(Context, int, Intent, int)},
* its content will be the subject of replacement by
* {@link #send(Context, int, Intent)} and {@link #FLAG_UPDATE_CURRENT}, etc.
* This is because it is the most specific of the supplied intents, and the
* UI the user actually sees when the intents are started.
* </p>
*
* <p class="note">For security reasons, the {@link android.content.Intent} objects
* you supply here should almost always be <em>explicit intents</em>,
* that is specify an explicit component to be delivered to through
* {@link Intent#setClass(android.content.Context, Class) Intent.setClass}</p>
*
* @param context The Context in which this PendingIntent should start
* the activity.
* @param requestCode Private request code for the sender
* @param intents Array of Intents of the activities to be launched.
* @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE},
* {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT},
* or any of the flags as supported by
* {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
* of the intent that can be supplied when the actual send happens.
*
* @return Returns an existing or new PendingIntent matching the given
* parameters. May return null only if {@link #FLAG_NO_CREATE} has been
* supplied.
*/
public static PendingIntent getActivities(Context context, int requestCode,
@NonNull Intent[] intents, @Flags int flags) {
return getActivities(context, requestCode, intents, flags, null);
}
/**
* Like {@link #getActivity(Context, int, Intent, int)}, but allows an
* array of Intents to be supplied. The last Intent in the array is
* taken as the primary key for the PendingIntent, like the single Intent
* given to {@link #getActivity(Context, int, Intent, int)}. Upon sending
* the resulting PendingIntent, all of the Intents are started in the same
* way as they would be by passing them to {@link Context#startActivities(Intent[])}.
*
* <p class="note">
* The <em>first</em> intent in the array will be started outside of the context of an
* existing activity, so you must use the {@link Intent#FLAG_ACTIVITY_NEW_TASK
* Intent.FLAG_ACTIVITY_NEW_TASK} launch flag in the Intent. (Activities after
* the first in the array are started in the context of the previous activity
* in the array, so FLAG_ACTIVITY_NEW_TASK is not needed nor desired for them.)
* </p>
*
* <p class="note">
* The <em>last</em> intent in the array represents the key for the
* PendingIntent. In other words, it is the significant element for matching
* (as done with the single intent given to {@link #getActivity(Context, int, Intent, int)},
* its content will be the subject of replacement by
* {@link #send(Context, int, Intent)} and {@link #FLAG_UPDATE_CURRENT}, etc.
* This is because it is the most specific of the supplied intents, and the
* UI the user actually sees when the intents are started.
* </p>
*
* <p class="note">For security reasons, the {@link android.content.Intent} objects
* you supply here should almost always be <em>explicit intents</em>,
* that is specify an explicit component to be delivered to through
* {@link Intent#setClass(android.content.Context, Class) Intent.setClass}</p>
*
* @param context The Context in which this PendingIntent should start
* the activity.
* @param requestCode Private request code for the sender
* @param intents Array of Intents of the activities to be launched.
* @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE},
* {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT},
* {@link #FLAG_IMMUTABLE} or any of the flags as supported by
* {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
* of the intent that can be supplied when the actual send happens.
*
* @return Returns an existing or new PendingIntent matching the given
* parameters. May return null only if {@link #FLAG_NO_CREATE} has been
* supplied.
*/
public static PendingIntent getActivities(Context context, int requestCode,
@NonNull Intent[] intents, @Flags int flags, @Nullable Bundle options) {
String packageName = context.getPackageName();
String[] resolvedTypes = new String[intents.length];
for (int i=0; i<intents.length; i++) {
intents[i].migrateExtraStreamToClipData();
intents[i].prepareToLeaveProcess(context);
resolvedTypes[i] = intents[i].resolveTypeIfNeeded(context.getContentResolver());
}
try {
IIntentSender target =
ActivityManager.getService().getIntentSender(
ActivityManager.INTENT_SENDER_ACTIVITY, packageName,
null, null, requestCode, intents, resolvedTypes, flags, options,
context.getUserId());
return target != null ? new PendingIntent(target) : null;
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* @hide
* Note that UserHandle.CURRENT will be interpreted at the time the
* activity is started, not when the pending intent is created.
*/
public static PendingIntent getActivitiesAsUser(Context context, int requestCode,
@NonNull Intent[] intents, int flags, Bundle options, UserHandle user) {
String packageName = context.getPackageName();
String[] resolvedTypes = new String[intents.length];
for (int i=0; i<intents.length; i++) {
intents[i].migrateExtraStreamToClipData();
intents[i].prepareToLeaveProcess(context);
resolvedTypes[i] = intents[i].resolveTypeIfNeeded(context.getContentResolver());
}
try {
IIntentSender target =
ActivityManager.getService().getIntentSender(
ActivityManager.INTENT_SENDER_ACTIVITY, packageName,
null, null, requestCode, intents, resolvedTypes,
flags, options, user.getIdentifier());
return target != null ? new PendingIntent(target) : null;
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Retrieve a PendingIntent that will perform a broadcast, like calling
* {@link Context#sendBroadcast(Intent) Context.sendBroadcast()}.
*
* <p class="note">For security reasons, the {@link android.content.Intent}
* you supply here should almost always be an <em>explicit intent</em>,
* that is specify an explicit component to be delivered to through
* {@link Intent#setClass(android.content.Context, Class) Intent.setClass}</p>
*
* @param context The Context in which this PendingIntent should perform
* the broadcast.
* @param requestCode Private request code for the sender
* @param intent The Intent to be broadcast.
* @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE},
* {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT},
* {@link #FLAG_IMMUTABLE} or any of the flags as supported by
* {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
* of the intent that can be supplied when the actual send happens.
*
* @return Returns an existing or new PendingIntent matching the given
* parameters. May return null only if {@link #FLAG_NO_CREATE} has been
* supplied.
*/
public static PendingIntent getBroadcast(Context context, int requestCode,
Intent intent, @Flags int flags) {
return getBroadcastAsUser(context, requestCode, intent, flags, context.getUser());
}
/**
* @hide
* Note that UserHandle.CURRENT will be interpreted at the time the
* broadcast is sent, not when the pending intent is created.
*/
@UnsupportedAppUsage
public static PendingIntent getBroadcastAsUser(Context context, int requestCode,
Intent intent, int flags, UserHandle userHandle) {
String packageName = context.getPackageName();
String resolvedType = intent != null ? intent.resolveTypeIfNeeded(
context.getContentResolver()) : null;
try {
intent.prepareToLeaveProcess(context);
IIntentSender target =
ActivityManager.getService().getIntentSender(
ActivityManager.INTENT_SENDER_BROADCAST, packageName,
null, null, requestCode, new Intent[] { intent },
resolvedType != null ? new String[] { resolvedType } : null,
flags, null, userHandle.getIdentifier());
return target != null ? new PendingIntent(target) : null;
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Retrieve a PendingIntent that will start a service, like calling
* {@link Context#startService Context.startService()}. The start
* arguments given to the service will come from the extras of the Intent.
*
* <p class="note">For security reasons, the {@link android.content.Intent}
* you supply here should almost always be an <em>explicit intent</em>,
* that is specify an explicit component to be delivered to through
* {@link Intent#setClass(android.content.Context, Class) Intent.setClass}</p>
*
* @param context The Context in which this PendingIntent should start
* the service.
* @param requestCode Private request code for the sender
* @param intent An Intent describing the service to be started.
* @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE},
* {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT},
* {@link #FLAG_IMMUTABLE} or any of the flags as supported by
* {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
* of the intent that can be supplied when the actual send happens.
*
* @return Returns an existing or new PendingIntent matching the given
* parameters. May return null only if {@link #FLAG_NO_CREATE} has been
* supplied.
*/
public static PendingIntent getService(Context context, int requestCode,
@NonNull Intent intent, @Flags int flags) {
return buildServicePendingIntent(context, requestCode, intent, flags,
ActivityManager.INTENT_SENDER_SERVICE);
}
/**
* Retrieve a PendingIntent that will start a foreground service, like calling
* {@link Context#startForegroundService Context.startForegroundService()}. The start
* arguments given to the service will come from the extras of the Intent.
*
* <p class="note">For security reasons, the {@link android.content.Intent}
* you supply here should almost always be an <em>explicit intent</em>,
* that is specify an explicit component to be delivered to through
* {@link Intent#setClass(android.content.Context, Class) Intent.setClass}</p>
*
* @param context The Context in which this PendingIntent should start
* the service.
* @param requestCode Private request code for the sender
* @param intent An Intent describing the service to be started.
* @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE},
* {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT},
* {@link #FLAG_IMMUTABLE} or any of the flags as supported by
* {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
* of the intent that can be supplied when the actual send happens.
*
* @return Returns an existing or new PendingIntent matching the given
* parameters. May return null only if {@link #FLAG_NO_CREATE} has been
* supplied.
*/
public static PendingIntent getForegroundService(Context context, int requestCode,
@NonNull Intent intent, @Flags int flags) {
return buildServicePendingIntent(context, requestCode, intent, flags,
ActivityManager.INTENT_SENDER_FOREGROUND_SERVICE);
}
private static PendingIntent buildServicePendingIntent(Context context, int requestCode,
Intent intent, int flags, int serviceKind) {
String packageName = context.getPackageName();
String resolvedType = intent != null ? intent.resolveTypeIfNeeded(
context.getContentResolver()) : null;
try {
intent.prepareToLeaveProcess(context);
IIntentSender target =
ActivityManager.getService().getIntentSender(
serviceKind, packageName,
null, null, requestCode, new Intent[] { intent },
resolvedType != null ? new String[] { resolvedType } : null,
flags, null, context.getUserId());
return target != null ? new PendingIntent(target) : null;
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Retrieve a IntentSender object that wraps the existing sender of the PendingIntent
*
* @return Returns a IntentSender object that wraps the sender of PendingIntent
*
*/
public IntentSender getIntentSender() {
return new IntentSender(mTarget, mWhitelistToken);
}
/**
* Cancel a currently active PendingIntent. Only the original application
* owning a PendingIntent can cancel it.
*/
public void cancel() {
try {
ActivityManager.getService().cancelIntentSender(mTarget);
} catch (RemoteException e) {
}
}
/**
* Perform the operation associated with this PendingIntent.
*
* @see #send(Context, int, Intent, android.app.PendingIntent.OnFinished, Handler)
*
* @throws CanceledException Throws CanceledException if the PendingIntent
* is no longer allowing more intents to be sent through it.
*/
public void send() throws CanceledException {
send(null, 0, null, null, null, null, null);
}
/**
* Perform the operation associated with this PendingIntent.
*
* @param code Result code to supply back to the PendingIntent's target.
*
* @see #send(Context, int, Intent, android.app.PendingIntent.OnFinished, Handler)
*
* @throws CanceledException Throws CanceledException if the PendingIntent
* is no longer allowing more intents to be sent through it.
*/
public void send(int code) throws CanceledException {
send(null, code, null, null, null, null, null);
}
/**
* Perform the operation associated with this PendingIntent, allowing the
* caller to specify information about the Intent to use.
*
* @param context The Context of the caller.
* @param code Result code to supply back to the PendingIntent's target.
* @param intent Additional Intent data. See {@link Intent#fillIn
* Intent.fillIn()} for information on how this is applied to the
* original Intent. If flag {@link #FLAG_IMMUTABLE} was set when this
* pending intent was created, this argument will be ignored.
*
* @see #send(Context, int, Intent, android.app.PendingIntent.OnFinished, Handler)
*
* @throws CanceledException Throws CanceledException if the PendingIntent
* is no longer allowing more intents to be sent through it.
*/
public void send(Context context, int code, @Nullable Intent intent)
throws CanceledException {
send(context, code, intent, null, null, null, null);
}
/**
* Perform the operation associated with this PendingIntent, allowing the
* caller to be notified when the send has completed.
*
* @param code Result code to supply back to the PendingIntent's target.
* @param onFinished The object to call back on when the send has
* completed, or null for no callback.
* @param handler Handler identifying the thread on which the callback
* should happen. If null, the callback will happen from the thread
* pool of the process.
*
* @see #send(Context, int, Intent, android.app.PendingIntent.OnFinished, Handler)
*
* @throws CanceledException Throws CanceledException if the PendingIntent
* is no longer allowing more intents to be sent through it.
*/
public void send(int code, @Nullable OnFinished onFinished, @Nullable Handler handler)
throws CanceledException {
send(null, code, null, onFinished, handler, null, null);
}
/**
* Perform the operation associated with this PendingIntent, allowing the
* caller to specify information about the Intent to use and be notified
* when the send has completed.
*
* <p>For the intent parameter, a PendingIntent
* often has restrictions on which fields can be supplied here, based on
* how the PendingIntent was retrieved in {@link #getActivity},
* {@link #getBroadcast}, or {@link #getService}.
*
* @param context The Context of the caller. This may be null if
* <var>intent</var> is also null.
* @param code Result code to supply back to the PendingIntent's target.
* @param intent Additional Intent data. See {@link Intent#fillIn
* Intent.fillIn()} for information on how this is applied to the
* original Intent. Use null to not modify the original Intent.
* If flag {@link #FLAG_IMMUTABLE} was set when this pending intent was
* created, this argument will be ignored.
* @param onFinished The object to call back on when the send has
* completed, or null for no callback.
* @param handler Handler identifying the thread on which the callback
* should happen. If null, the callback will happen from the thread
* pool of the process.
*
* @see #send()
* @see #send(int)
* @see #send(Context, int, Intent)
* @see #send(int, android.app.PendingIntent.OnFinished, Handler)
* @see #send(Context, int, Intent, OnFinished, Handler, String)
*
* @throws CanceledException Throws CanceledException if the PendingIntent
* is no longer allowing more intents to be sent through it.
*/
public void send(Context context, int code, @Nullable Intent intent,
@Nullable OnFinished onFinished, @Nullable Handler handler) throws CanceledException {
send(context, code, intent, onFinished, handler, null, null);
}
/**
* Perform the operation associated with this PendingIntent, allowing the
* caller to specify information about the Intent to use and be notified
* when the send has completed.
*
* <p>For the intent parameter, a PendingIntent
* often has restrictions on which fields can be supplied here, based on
* how the PendingIntent was retrieved in {@link #getActivity},
* {@link #getBroadcast}, or {@link #getService}.
*
* @param context The Context of the caller. This may be null if
* <var>intent</var> is also null.
* @param code Result code to supply back to the PendingIntent's target.
* @param intent Additional Intent data. See {@link Intent#fillIn
* Intent.fillIn()} for information on how this is applied to the
* original Intent. Use null to not modify the original Intent.
* If flag {@link #FLAG_IMMUTABLE} was set when this pending intent was
* created, this argument will be ignored.
* @param onFinished The object to call back on when the send has
* completed, or null for no callback.
* @param handler Handler identifying the thread on which the callback
* should happen. If null, the callback will happen from the thread
* pool of the process.
* @param requiredPermission Name of permission that a recipient of the PendingIntent
* is required to hold. This is only valid for broadcast intents, and
* corresponds to the permission argument in
* {@link Context#sendBroadcast(Intent, String) Context.sendOrderedBroadcast(Intent, String)}.
* If null, no permission is required.
*
* @see #send()
* @see #send(int)
* @see #send(Context, int, Intent)
* @see #send(int, android.app.PendingIntent.OnFinished, Handler)
* @see #send(Context, int, Intent, OnFinished, Handler)
*
* @throws CanceledException Throws CanceledException if the PendingIntent
* is no longer allowing more intents to be sent through it.
*/
public void send(Context context, int code, @Nullable Intent intent,
@Nullable OnFinished onFinished, @Nullable Handler handler,
@Nullable String requiredPermission)
throws CanceledException {
send(context, code, intent, onFinished, handler, requiredPermission, null);
}
/**
* Perform the operation associated with this PendingIntent, allowing the
* caller to specify information about the Intent to use and be notified
* when the send has completed.
*
* <p>For the intent parameter, a PendingIntent
* often has restrictions on which fields can be supplied here, based on
* how the PendingIntent was retrieved in {@link #getActivity},
* {@link #getBroadcast}, or {@link #getService}.
*
* @param context The Context of the caller. This may be null if
* <var>intent</var> is also null.
* @param code Result code to supply back to the PendingIntent's target.
* @param intent Additional Intent data. See {@link Intent#fillIn
* Intent.fillIn()} for information on how this is applied to the
* original Intent. Use null to not modify the original Intent.
* If flag {@link #FLAG_IMMUTABLE} was set when this pending intent was
* created, this argument will be ignored.
* @param onFinished The object to call back on when the send has
* completed, or null for no callback.
* @param handler Handler identifying the thread on which the callback
* should happen. If null, the callback will happen from the thread
* pool of the process.
* @param requiredPermission Name of permission that a recipient of the PendingIntent
* is required to hold. This is only valid for broadcast intents, and
* corresponds to the permission argument in
* {@link Context#sendBroadcast(Intent, String) Context.sendOrderedBroadcast(Intent, String)}.
* If null, no permission is required.
* @param options Additional options the caller would like to provide to modify the sending
* behavior. May be built from an {@link ActivityOptions} to apply to an activity start.
*
* @see #send()
* @see #send(int)
* @see #send(Context, int, Intent)
* @see #send(int, android.app.PendingIntent.OnFinished, Handler)
* @see #send(Context, int, Intent, OnFinished, Handler)
*
* @throws CanceledException Throws CanceledException if the PendingIntent
* is no longer allowing more intents to be sent through it.
*/
public void send(Context context, int code, @Nullable Intent intent,
@Nullable OnFinished onFinished, @Nullable Handler handler,
@Nullable String requiredPermission, @Nullable Bundle options)
throws CanceledException {
if (sendAndReturnResult(context, code, intent, onFinished, handler, requiredPermission,
options) < 0) {
throw new CanceledException();
}
}
/**
* Like {@link #send}, but returns the result
* @hide
*/
public int sendAndReturnResult(Context context, int code, @Nullable Intent intent,
@Nullable OnFinished onFinished, @Nullable Handler handler,
@Nullable String requiredPermission, @Nullable Bundle options)
throws CanceledException {
try {
String resolvedType = intent != null ?
intent.resolveTypeIfNeeded(context.getContentResolver())
: null;
return ActivityManager.getService().sendIntentSender(
mTarget, mWhitelistToken, code, intent, resolvedType,
onFinished != null
? new FinishedDispatcher(this, onFinished, handler)
: null,
requiredPermission, options);
} catch (RemoteException e) {
throw new CanceledException(e);
}
}
/**
* @deprecated Renamed to {@link #getCreatorPackage()}.
*/
@Deprecated
public String getTargetPackage() {
try {
return ActivityManager.getService()
.getPackageForIntentSender(mTarget);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Return the package name of the application that created this
* PendingIntent, that is the identity under which you will actually be
* sending the Intent. The returned string is supplied by the system, so
* that an application can not spoof its package.
*
* <p class="note">Be careful about how you use this. All this tells you is
* who created the PendingIntent. It does <strong>not</strong> tell you who
* handed the PendingIntent to you: that is, PendingIntent objects are intended to be
* passed between applications, so the PendingIntent you receive from an application
* could actually be one it received from another application, meaning the result
* you get here will identify the original application. Because of this, you should
* only use this information to identify who you expect to be interacting with
* through a {@link #send} call, not who gave you the PendingIntent.</p>
*
* @return The package name of the PendingIntent, or null if there is
* none associated with it.
*/
@Nullable
public String getCreatorPackage() {
try {
return ActivityManager.getService()
.getPackageForIntentSender(mTarget);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Return the uid of the application that created this
* PendingIntent, that is the identity under which you will actually be
* sending the Intent. The returned integer is supplied by the system, so
* that an application can not spoof its uid.
*
* <p class="note">Be careful about how you use this. All this tells you is
* who created the PendingIntent. It does <strong>not</strong> tell you who
* handed the PendingIntent to you: that is, PendingIntent objects are intended to be
* passed between applications, so the PendingIntent you receive from an application
* could actually be one it received from another application, meaning the result
* you get here will identify the original application. Because of this, you should
* only use this information to identify who you expect to be interacting with
* through a {@link #send} call, not who gave you the PendingIntent.</p>
*
* @return The uid of the PendingIntent, or -1 if there is
* none associated with it.
*/
public int getCreatorUid() {
try {
return ActivityManager.getService()
.getUidForIntentSender(mTarget);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Register a listener to when this pendingIntent is cancelled. There are no guarantees on which
* thread a listener will be called and it's up to the caller to synchronize. This may
* trigger a synchronous binder call so should therefore usually be called on a background
* thread.
*
* @hide
*/
public void registerCancelListener(CancelListener cancelListener) {
synchronized (this) {
if (mCancelReceiver == null) {
mCancelReceiver = new IResultReceiver.Stub() {
@Override
public void send(int resultCode, Bundle resultData) throws RemoteException {
notifyCancelListeners();
}
};
}
if (mCancelListeners == null) {
mCancelListeners = new ArraySet<>();
}
boolean wasEmpty = mCancelListeners.isEmpty();
mCancelListeners.add(cancelListener);
if (wasEmpty) {
try {
ActivityManager.getService().registerIntentSenderCancelListener(mTarget,
mCancelReceiver);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
}
}
private void notifyCancelListeners() {
ArraySet<CancelListener> cancelListeners;
synchronized (this) {
cancelListeners = new ArraySet<>(mCancelListeners);
}
int size = cancelListeners.size();
for (int i = 0; i < size; i++) {
cancelListeners.valueAt(i).onCancelled(this);
}
}
/**
* Un-register a listener to when this pendingIntent is cancelled.
*
* @hide
*/
public void unregisterCancelListener(CancelListener cancelListener) {
synchronized (this) {
if (mCancelListeners == null) {
return;
}
boolean wasEmpty = mCancelListeners.isEmpty();
mCancelListeners.remove(cancelListener);
if (mCancelListeners.isEmpty() && !wasEmpty) {
try {
ActivityManager.getService().unregisterIntentSenderCancelListener(mTarget,
mCancelReceiver);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
}
}
/**
* Return the user handle of the application that created this
* PendingIntent, that is the user under which you will actually be
* sending the Intent. The returned UserHandle is supplied by the system, so
* that an application can not spoof its user. See
* {@link android.os.Process#myUserHandle() Process.myUserHandle()} for
* more explanation of user handles.
*
* <p class="note">Be careful about how you use this. All this tells you is
* who created the PendingIntent. It does <strong>not</strong> tell you who
* handed the PendingIntent to you: that is, PendingIntent objects are intended to be
* passed between applications, so the PendingIntent you receive from an application
* could actually be one it received from another application, meaning the result
* you get here will identify the original application. Because of this, you should
* only use this information to identify who you expect to be interacting with
* through a {@link #send} call, not who gave you the PendingIntent.</p>
*
* @return The user handle of the PendingIntent, or null if there is
* none associated with it.
*/
@Nullable
public UserHandle getCreatorUserHandle() {
try {
int uid = ActivityManager.getService()
.getUidForIntentSender(mTarget);
return uid > 0 ? new UserHandle(UserHandle.getUserId(uid)) : null;
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* @hide
* Check to verify that this PendingIntent targets a specific package.
*/
public boolean isTargetedToPackage() {
try {
return ActivityManager.getService()
.isIntentSenderTargetedToPackage(mTarget);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* @hide
* Check whether this PendingIntent will launch an Activity.
*/
@UnsupportedAppUsage
public boolean isActivity() {
try {
return ActivityManager.getService()
.isIntentSenderAnActivity(mTarget);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* @hide
* Check whether this PendingIntent will launch a foreground service
*/
public boolean isForegroundService() {
try {
return ActivityManager.getService()
.isIntentSenderAForegroundService(mTarget);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* @hide
* Check whether this PendingIntent will launch an Activity.
*/
public boolean isBroadcast() {
try {
return ActivityManager.getService()
.isIntentSenderABroadcast(mTarget);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* @hide
* Return the Intent of this PendingIntent.
*/
@UnsupportedAppUsage
public Intent getIntent() {
try {
return ActivityManager.getService()
.getIntentForIntentSender(mTarget);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* @hide
* Return descriptive tag for this PendingIntent.
*/
@UnsupportedAppUsage
public String getTag(String prefix) {
try {
return ActivityManager.getService()
.getTagForIntentSender(mTarget, prefix);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Comparison operator on two PendingIntent objects, such that true
* is returned then they both represent the same operation from the
* same package. This allows you to use {@link #getActivity},
* {@link #getBroadcast}, or {@link #getService} multiple times (even
* across a process being killed), resulting in different PendingIntent
* objects but whose equals() method identifies them as being the same
* operation.
*/
@Override
public boolean equals(Object otherObj) {
if (otherObj instanceof PendingIntent) {
return mTarget.asBinder().equals(((PendingIntent)otherObj)
.mTarget.asBinder());
}
return false;
}
@Override
public int hashCode() {
return mTarget.asBinder().hashCode();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(128);
sb.append("PendingIntent{");
sb.append(Integer.toHexString(System.identityHashCode(this)));
sb.append(": ");
sb.append(mTarget != null ? mTarget.asBinder() : null);
sb.append('}');
return sb.toString();
}
/** @hide */
public void writeToProto(ProtoOutputStream proto, long fieldId) {
final long token = proto.start(fieldId);
if (mTarget != null) {
proto.write(PendingIntentProto.TARGET, mTarget.asBinder().toString());
}
proto.end(token);
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeStrongBinder(mTarget.asBinder());
OnMarshaledListener listener = sOnMarshaledListener.get();
if (listener != null) {
listener.onMarshaled(this, out, flags);
}
}
public static final @android.annotation.NonNull Parcelable.Creator<PendingIntent> CREATOR
= new Parcelable.Creator<PendingIntent>() {
public PendingIntent createFromParcel(Parcel in) {
IBinder target = in.readStrongBinder();
return target != null
? new PendingIntent(target, in.getClassCookie(PendingIntent.class))
: null;
}
public PendingIntent[] newArray(int size) {
return new PendingIntent[size];
}
};
/**
* Convenience function for writing either a PendingIntent or null pointer to
* a Parcel. You must use this with {@link #readPendingIntentOrNullFromParcel}
* for later reading it.
*
* @param sender The PendingIntent to write, or null.
* @param out Where to write the PendingIntent.
*/
public static void writePendingIntentOrNullToParcel(@Nullable PendingIntent sender,
@NonNull Parcel out) {
out.writeStrongBinder(sender != null ? sender.mTarget.asBinder() : null);
if (sender != null) {
OnMarshaledListener listener = sOnMarshaledListener.get();
if (listener != null) {
listener.onMarshaled(sender, out, 0 /* flags */);
}
}
}
/**
* Convenience function for reading either a PendingIntent or null pointer from
* a Parcel. You must have previously written the PendingIntent with
* {@link #writePendingIntentOrNullToParcel}.
*
* @param in The Parcel containing the written PendingIntent.
*
* @return Returns the PendingIntent read from the Parcel, or null if null had
* been written.
*/
@Nullable
public static PendingIntent readPendingIntentOrNullFromParcel(@NonNull Parcel in) {
IBinder b = in.readStrongBinder();
return b != null ? new PendingIntent(b, in.getClassCookie(PendingIntent.class)) : null;
}
/**
* Creates a PendingIntent with the given target.
* @param target the backing IIntentSender
* @hide
*/
public PendingIntent(IIntentSender target) {
mTarget = target;
}
/*package*/ PendingIntent(IBinder target, Object cookie) {
mTarget = IIntentSender.Stub.asInterface(target);
if (cookie != null) {
mWhitelistToken = (IBinder)cookie;
}
}
/** @hide */
public IIntentSender getTarget() {
return mTarget;
}
/** @hide */
public IBinder getWhitelistToken() {
return mWhitelistToken;
}
/**
* A listener to when a pending intent is cancelled
*
* @hide
*/
public interface CancelListener {
/**
* Called when a Pending Intent is cancelled.
*
* @param intent The intent that was cancelled.
*/
void onCancelled(PendingIntent intent);
}
}
|