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 | |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 19 | import android.annotation.IntDef; |
| 20 | import android.annotation.NonNull; |
| 21 | import android.annotation.Nullable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 24 | import android.content.IIntentReceiver; |
| 25 | import android.content.IIntentSender; |
| 26 | import android.content.IntentSender; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.os.Bundle; |
| 28 | import android.os.RemoteException; |
| 29 | import android.os.Handler; |
| 30 | import android.os.IBinder; |
| 31 | import android.os.Parcel; |
| 32 | import android.os.Parcelable; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 33 | import android.os.UserHandle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.util.AndroidException; |
| 35 | |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 36 | import java.lang.annotation.Retention; |
| 37 | import java.lang.annotation.RetentionPolicy; |
| 38 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | /** |
| 40 | * A description of an Intent and target action to perform with it. Instances |
Dianne Hackborn | 8832c18 | 2012-09-17 17:20:24 -0700 | [diff] [blame] | 41 | * of this class are created with {@link #getActivity}, {@link #getActivities}, |
| 42 | * {@link #getBroadcast}, and {@link #getService}; the returned object can be |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | * handed to other applications so that they can perform the action you |
| 44 | * described on your behalf at a later time. |
| 45 | * |
| 46 | * <p>By giving a PendingIntent to another application, |
| 47 | * you are granting it the right to perform the operation you have specified |
| 48 | * as if the other application was yourself (with the same permissions and |
| 49 | * identity). As such, you should be careful about how you build the PendingIntent: |
Dianne Hackborn | a53ee35 | 2013-02-20 12:47:02 -0800 | [diff] [blame] | 50 | * almost always, for example, the base Intent you supply should have the component |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | * name explicitly set to one of your own components, to ensure it is ultimately |
| 52 | * sent there and nowhere else. |
| 53 | * |
| 54 | * <p>A PendingIntent itself is simply a reference to a token maintained by |
| 55 | * the system describing the original data used to retrieve it. This means |
| 56 | * that, even if its owning application's process is killed, the |
| 57 | * PendingIntent itself will remain usable from other processes that |
| 58 | * have been given it. If the creating application later re-retrieves the |
| 59 | * same kind of PendingIntent (same operation, same Intent action, data, |
| 60 | * categories, and components, and same flags), it will receive a PendingIntent |
| 61 | * representing the same token if that is still valid, and can thus call |
| 62 | * {@link #cancel} to remove it. |
Dianne Hackborn | 8832c18 | 2012-09-17 17:20:24 -0700 | [diff] [blame] | 63 | * |
| 64 | * <p>Because of this behavior, it is important to know when two Intents |
| 65 | * are considered to be the same for purposes of retrieving a PendingIntent. |
| 66 | * A common mistake people make is to create multiple PendingIntent objects |
| 67 | * with Intents that only vary in their "extra" contents, expecting to get |
| 68 | * a different PendingIntent each time. This does <em>not</em> happen. The |
| 69 | * parts of the Intent that are used for matching are the same ones defined |
| 70 | * by {@link Intent#filterEquals(Intent) Intent.filterEquals}. If you use two |
| 71 | * Intent objects that are equivalent as per |
| 72 | * {@link Intent#filterEquals(Intent) Intent.filterEquals}, then you will get |
| 73 | * the same PendingIntent for both of them. |
| 74 | * |
| 75 | * <p>There are two typical ways to deal with this. |
| 76 | * |
| 77 | * <p>If you truly need multiple distinct PendingIntent objects active at |
| 78 | * the same time (such as to use as two notifications that are both shown |
| 79 | * at the same time), then you will need to ensure there is something that |
| 80 | * is different about them to associate them with different PendingIntents. |
| 81 | * This may be any of the Intent attributes considered by |
| 82 | * {@link Intent#filterEquals(Intent) Intent.filterEquals}, or different |
| 83 | * request code integers supplied to {@link #getActivity}, {@link #getActivities}, |
| 84 | * {@link #getBroadcast}, or {@link #getService}. |
| 85 | * |
| 86 | * <p>If you only need one PendingIntent active at a time for any of the |
| 87 | * Intents you will use, then you can alternatively use the flags |
| 88 | * {@link #FLAG_CANCEL_CURRENT} or {@link #FLAG_UPDATE_CURRENT} to either |
| 89 | * cancel or modify whatever current PendingIntent is associated with the |
| 90 | * Intent you are supplying. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | */ |
| 92 | public final class PendingIntent implements Parcelable { |
| 93 | private final IIntentSender mTarget; |
| 94 | |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 95 | /** @hide */ |
| 96 | @IntDef(flag = true, |
| 97 | value = { |
| 98 | FLAG_ONE_SHOT, |
| 99 | FLAG_NO_CREATE, |
| 100 | FLAG_CANCEL_CURRENT, |
| 101 | FLAG_UPDATE_CURRENT, |
| 102 | |
| 103 | Intent.FILL_IN_ACTION, |
| 104 | Intent.FILL_IN_DATA, |
| 105 | Intent.FILL_IN_CATEGORIES, |
| 106 | Intent.FILL_IN_COMPONENT, |
| 107 | Intent.FILL_IN_PACKAGE, |
| 108 | Intent.FILL_IN_SOURCE_BOUNDS, |
| 109 | Intent.FILL_IN_SELECTOR, |
| 110 | Intent.FILL_IN_CLIP_DATA |
| 111 | }) |
| 112 | @Retention(RetentionPolicy.SOURCE) |
| 113 | public @interface Flags {} |
| 114 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | /** |
Scott Main | df75bdc | 2013-01-15 15:12:13 -0800 | [diff] [blame] | 116 | * Flag indicating that this PendingIntent can be used only once. |
| 117 | * For use with {@link #getActivity}, {@link #getBroadcast}, and |
| 118 | * {@link #getService}. <p>If set, after |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | * {@link #send()} is called on it, it will be automatically |
| 120 | * canceled for you and any future attempt to send through it will fail. |
| 121 | */ |
| 122 | public static final int FLAG_ONE_SHOT = 1<<30; |
| 123 | /** |
Scott Main | df75bdc | 2013-01-15 15:12:13 -0800 | [diff] [blame] | 124 | * Flag indicating that if the described PendingIntent already |
| 125 | * exists, then simply return null instead of creating it. |
| 126 | * For use with {@link #getActivity}, {@link #getBroadcast}, and |
| 127 | * {@link #getService}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | */ |
| 129 | public static final int FLAG_NO_CREATE = 1<<29; |
| 130 | /** |
Scott Main | df75bdc | 2013-01-15 15:12:13 -0800 | [diff] [blame] | 131 | * Flag indicating that if the described PendingIntent already exists, |
| 132 | * the current one should be canceled before generating a new one. |
| 133 | * For use with {@link #getActivity}, {@link #getBroadcast}, and |
| 134 | * {@link #getService}. <p>You can use |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | * this to retrieve a new PendingIntent when you are only changing the |
| 136 | * extra data in the Intent; by canceling the previous pending intent, |
| 137 | * this ensures that only entities given the new data will be able to |
| 138 | * launch it. If this assurance is not an issue, consider |
| 139 | * {@link #FLAG_UPDATE_CURRENT}. |
| 140 | */ |
| 141 | public static final int FLAG_CANCEL_CURRENT = 1<<28; |
| 142 | /** |
Scott Main | df75bdc | 2013-01-15 15:12:13 -0800 | [diff] [blame] | 143 | * Flag indicating that if the described PendingIntent already exists, |
| 144 | * then keep it but replace its extra data with what is in this new |
| 145 | * Intent. For use with {@link #getActivity}, {@link #getBroadcast}, and |
| 146 | * {@link #getService}. <p>This can be used if you are creating intents where only the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | * extras change, and don't care that any entities that received your |
| 148 | * previous PendingIntent will be able to launch it with your new |
| 149 | * extras even if they are not explicitly given to it. |
| 150 | */ |
| 151 | public static final int FLAG_UPDATE_CURRENT = 1<<27; |
| 152 | |
| 153 | /** |
| 154 | * Exception thrown when trying to send through a PendingIntent that |
| 155 | * has been canceled or is otherwise no longer able to execute the request. |
| 156 | */ |
| 157 | public static class CanceledException extends AndroidException { |
| 158 | public CanceledException() { |
| 159 | } |
| 160 | |
| 161 | public CanceledException(String name) { |
| 162 | super(name); |
| 163 | } |
| 164 | |
| 165 | public CanceledException(Exception cause) { |
| 166 | super(cause); |
| 167 | } |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 168 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | |
| 170 | /** |
| 171 | * Callback interface for discovering when a send operation has |
| 172 | * completed. Primarily for use with a PendingIntent that is |
| 173 | * performing a broadcast, this provides the same information as |
| 174 | * calling {@link Context#sendOrderedBroadcast(Intent, String, |
| 175 | * android.content.BroadcastReceiver, Handler, int, String, Bundle) |
| 176 | * Context.sendBroadcast()} with a final BroadcastReceiver. |
| 177 | */ |
| 178 | public interface OnFinished { |
| 179 | /** |
| 180 | * Called when a send operation as completed. |
| 181 | * |
| 182 | * @param pendingIntent The PendingIntent this operation was sent through. |
| 183 | * @param intent The original Intent that was sent. |
| 184 | * @param resultCode The final result code determined by the send. |
| 185 | * @param resultData The final data collected by a broadcast. |
| 186 | * @param resultExtras The final extras collected by a broadcast. |
| 187 | */ |
| 188 | void onSendFinished(PendingIntent pendingIntent, Intent intent, |
| 189 | int resultCode, String resultData, Bundle resultExtras); |
| 190 | } |
| 191 | |
| 192 | private static class FinishedDispatcher extends IIntentReceiver.Stub |
| 193 | implements Runnable { |
| 194 | private final PendingIntent mPendingIntent; |
| 195 | private final OnFinished mWho; |
| 196 | private final Handler mHandler; |
| 197 | private Intent mIntent; |
| 198 | private int mResultCode; |
| 199 | private String mResultData; |
| 200 | private Bundle mResultExtras; |
| 201 | FinishedDispatcher(PendingIntent pi, OnFinished who, Handler handler) { |
| 202 | mPendingIntent = pi; |
| 203 | mWho = who; |
| 204 | mHandler = handler; |
| 205 | } |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 206 | public void performReceive(Intent intent, int resultCode, String data, |
| 207 | Bundle extras, boolean serialized, boolean sticky, int sendingUser) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | mIntent = intent; |
| 209 | mResultCode = resultCode; |
| 210 | mResultData = data; |
| 211 | mResultExtras = extras; |
| 212 | if (mHandler == null) { |
| 213 | run(); |
| 214 | } else { |
| 215 | mHandler.post(this); |
| 216 | } |
| 217 | } |
| 218 | public void run() { |
| 219 | mWho.onSendFinished(mPendingIntent, mIntent, mResultCode, |
| 220 | mResultData, mResultExtras); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Retrieve a PendingIntent that will start a new activity, like calling |
| 226 | * {@link Context#startActivity(Intent) Context.startActivity(Intent)}. |
| 227 | * Note that the activity will be started outside of the context of an |
| 228 | * existing activity, so you must use the {@link Intent#FLAG_ACTIVITY_NEW_TASK |
| 229 | * Intent.FLAG_ACTIVITY_NEW_TASK} launch flag in the Intent. |
| 230 | * |
Dianne Hackborn | a53ee35 | 2013-02-20 12:47:02 -0800 | [diff] [blame] | 231 | * <p class="note">For security reasons, the {@link android.content.Intent} |
| 232 | * you supply here should almost always be an <em>explicit intent</em>, |
| 233 | * that is specify an explicit component to be delivered to through |
| 234 | * {@link Intent#setClass(android.content.Context, Class)} Intent.setClass</p> |
| 235 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | * @param context The Context in which this PendingIntent should start |
| 237 | * the activity. |
Danny Baumann | f15a419 | 2013-04-05 13:42:57 +0200 | [diff] [blame] | 238 | * @param requestCode Private request code for the sender |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | * @param intent Intent of the activity to be launched. |
| 240 | * @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE}, |
| 241 | * {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT}, |
| 242 | * or any of the flags as supported by |
| 243 | * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts |
| 244 | * of the intent that can be supplied when the actual send happens. |
| 245 | * |
| 246 | * @return Returns an existing or new PendingIntent matching the given |
| 247 | * parameters. May return null only if {@link #FLAG_NO_CREATE} has been |
| 248 | * supplied. |
| 249 | */ |
| 250 | public static PendingIntent getActivity(Context context, int requestCode, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 251 | Intent intent, @Flags int flags) { |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 252 | return getActivity(context, requestCode, intent, flags, null); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Retrieve a PendingIntent that will start a new activity, like calling |
| 257 | * {@link Context#startActivity(Intent) Context.startActivity(Intent)}. |
| 258 | * Note that the activity will be started outside of the context of an |
| 259 | * existing activity, so you must use the {@link Intent#FLAG_ACTIVITY_NEW_TASK |
| 260 | * Intent.FLAG_ACTIVITY_NEW_TASK} launch flag in the Intent. |
| 261 | * |
Dianne Hackborn | a53ee35 | 2013-02-20 12:47:02 -0800 | [diff] [blame] | 262 | * <p class="note">For security reasons, the {@link android.content.Intent} |
| 263 | * you supply here should almost always be an <em>explicit intent</em>, |
| 264 | * that is specify an explicit component to be delivered to through |
| 265 | * {@link Intent#setClass(android.content.Context, Class)} Intent.setClass</p> |
| 266 | * |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 267 | * @param context The Context in which this PendingIntent should start |
| 268 | * the activity. |
Danny Baumann | f15a419 | 2013-04-05 13:42:57 +0200 | [diff] [blame] | 269 | * @param requestCode Private request code for the sender |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 270 | * @param intent Intent of the activity to be launched. |
| 271 | * @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE}, |
| 272 | * {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT}, |
| 273 | * or any of the flags as supported by |
| 274 | * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts |
| 275 | * of the intent that can be supplied when the actual send happens. |
| 276 | * @param options Additional options for how the Activity should be started. |
| 277 | * May be null if there are no options. |
| 278 | * |
| 279 | * @return Returns an existing or new PendingIntent matching the given |
| 280 | * parameters. May return null only if {@link #FLAG_NO_CREATE} has been |
| 281 | * supplied. |
| 282 | */ |
| 283 | public static PendingIntent getActivity(Context context, int requestCode, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 284 | @NonNull Intent intent, @Flags int flags, @Nullable Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | String packageName = context.getPackageName(); |
| 286 | String resolvedType = intent != null ? intent.resolveTypeIfNeeded( |
| 287 | context.getContentResolver()) : null; |
| 288 | try { |
Jeff Sharkey | 02ffba9 | 2013-03-08 16:13:15 -0800 | [diff] [blame] | 289 | intent.migrateExtraStreamToClipData(); |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 290 | intent.prepareToLeaveProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 291 | IIntentSender target = |
| 292 | ActivityManagerNative.getDefault().getIntentSender( |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 293 | ActivityManager.INTENT_SENDER_ACTIVITY, packageName, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 294 | null, null, requestCode, new Intent[] { intent }, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 295 | resolvedType != null ? new String[] { resolvedType } : null, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 296 | flags, options, UserHandle.myUserId()); |
| 297 | return target != null ? new PendingIntent(target) : null; |
| 298 | } catch (RemoteException e) { |
| 299 | } |
| 300 | return null; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * @hide |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 305 | * Note that UserHandle.CURRENT will be interpreted at the time the |
| 306 | * activity is started, not when the pending intent is created. |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 307 | */ |
| 308 | public static PendingIntent getActivityAsUser(Context context, int requestCode, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 309 | @NonNull Intent intent, int flags, Bundle options, UserHandle user) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 310 | String packageName = context.getPackageName(); |
| 311 | String resolvedType = intent != null ? intent.resolveTypeIfNeeded( |
| 312 | context.getContentResolver()) : null; |
| 313 | try { |
Jeff Sharkey | 02ffba9 | 2013-03-08 16:13:15 -0800 | [diff] [blame] | 314 | intent.migrateExtraStreamToClipData(); |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 315 | intent.prepareToLeaveProcess(); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 316 | IIntentSender target = |
| 317 | ActivityManagerNative.getDefault().getIntentSender( |
| 318 | ActivityManager.INTENT_SENDER_ACTIVITY, packageName, |
| 319 | null, null, requestCode, new Intent[] { intent }, |
| 320 | resolvedType != null ? new String[] { resolvedType } : null, |
| 321 | flags, options, user.getIdentifier()); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 322 | return target != null ? new PendingIntent(target) : null; |
| 323 | } catch (RemoteException e) { |
| 324 | } |
| 325 | return null; |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Like {@link #getActivity(Context, int, Intent, int)}, but allows an |
Tim Hutt | 5313c9f | 2012-12-10 12:34:19 +0000 | [diff] [blame] | 330 | * array of Intents to be supplied. The last Intent in the array is |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 331 | * taken as the primary key for the PendingIntent, like the single Intent |
| 332 | * given to {@link #getActivity(Context, int, Intent, int)}. Upon sending |
| 333 | * the resulting PendingIntent, all of the Intents are started in the same |
| 334 | * way as they would be by passing them to {@link Context#startActivities(Intent[])}. |
| 335 | * |
| 336 | * <p class="note"> |
| 337 | * The <em>first</em> intent in the array will be started outside of the context of an |
| 338 | * existing activity, so you must use the {@link Intent#FLAG_ACTIVITY_NEW_TASK |
| 339 | * Intent.FLAG_ACTIVITY_NEW_TASK} launch flag in the Intent. (Activities after |
| 340 | * the first in the array are started in the context of the previous activity |
| 341 | * in the array, so FLAG_ACTIVITY_NEW_TASK is not needed nor desired for them.) |
| 342 | * </p> |
| 343 | * |
| 344 | * <p class="note"> |
| 345 | * The <em>last</em> intent in the array represents the key for the |
| 346 | * PendingIntent. In other words, it is the significant element for matching |
| 347 | * (as done with the single intent given to {@link #getActivity(Context, int, Intent, int)}, |
| 348 | * its content will be the subject of replacement by |
| 349 | * {@link #send(Context, int, Intent)} and {@link #FLAG_UPDATE_CURRENT}, etc. |
| 350 | * This is because it is the most specific of the supplied intents, and the |
| 351 | * UI the user actually sees when the intents are started. |
| 352 | * </p> |
| 353 | * |
Dianne Hackborn | a53ee35 | 2013-02-20 12:47:02 -0800 | [diff] [blame] | 354 | * <p class="note">For security reasons, the {@link android.content.Intent} objects |
| 355 | * you supply here should almost always be <em>explicit intents</em>, |
| 356 | * that is specify an explicit component to be delivered to through |
| 357 | * {@link Intent#setClass(android.content.Context, Class)} Intent.setClass</p> |
| 358 | * |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 359 | * @param context The Context in which this PendingIntent should start |
| 360 | * the activity. |
Danny Baumann | f15a419 | 2013-04-05 13:42:57 +0200 | [diff] [blame] | 361 | * @param requestCode Private request code for the sender |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 362 | * @param intents Array of Intents of the activities to be launched. |
| 363 | * @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE}, |
| 364 | * {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT}, |
| 365 | * or any of the flags as supported by |
| 366 | * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts |
| 367 | * of the intent that can be supplied when the actual send happens. |
| 368 | * |
| 369 | * @return Returns an existing or new PendingIntent matching the given |
| 370 | * parameters. May return null only if {@link #FLAG_NO_CREATE} has been |
| 371 | * supplied. |
| 372 | */ |
| 373 | public static PendingIntent getActivities(Context context, int requestCode, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 374 | @NonNull Intent[] intents, @Flags int flags) { |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 375 | return getActivities(context, requestCode, intents, flags, null); |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Like {@link #getActivity(Context, int, Intent, int)}, but allows an |
Tim Hutt | 5313c9f | 2012-12-10 12:34:19 +0000 | [diff] [blame] | 380 | * array of Intents to be supplied. The last Intent in the array is |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 381 | * taken as the primary key for the PendingIntent, like the single Intent |
| 382 | * given to {@link #getActivity(Context, int, Intent, int)}. Upon sending |
| 383 | * the resulting PendingIntent, all of the Intents are started in the same |
| 384 | * way as they would be by passing them to {@link Context#startActivities(Intent[])}. |
| 385 | * |
| 386 | * <p class="note"> |
| 387 | * The <em>first</em> intent in the array will be started outside of the context of an |
| 388 | * existing activity, so you must use the {@link Intent#FLAG_ACTIVITY_NEW_TASK |
| 389 | * Intent.FLAG_ACTIVITY_NEW_TASK} launch flag in the Intent. (Activities after |
| 390 | * the first in the array are started in the context of the previous activity |
| 391 | * in the array, so FLAG_ACTIVITY_NEW_TASK is not needed nor desired for them.) |
| 392 | * </p> |
| 393 | * |
| 394 | * <p class="note"> |
| 395 | * The <em>last</em> intent in the array represents the key for the |
| 396 | * PendingIntent. In other words, it is the significant element for matching |
| 397 | * (as done with the single intent given to {@link #getActivity(Context, int, Intent, int)}, |
| 398 | * its content will be the subject of replacement by |
| 399 | * {@link #send(Context, int, Intent)} and {@link #FLAG_UPDATE_CURRENT}, etc. |
| 400 | * This is because it is the most specific of the supplied intents, and the |
| 401 | * UI the user actually sees when the intents are started. |
| 402 | * </p> |
| 403 | * |
Dianne Hackborn | a53ee35 | 2013-02-20 12:47:02 -0800 | [diff] [blame] | 404 | * <p class="note">For security reasons, the {@link android.content.Intent} objects |
| 405 | * you supply here should almost always be <em>explicit intents</em>, |
| 406 | * that is specify an explicit component to be delivered to through |
| 407 | * {@link Intent#setClass(android.content.Context, Class)} Intent.setClass</p> |
| 408 | * |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 409 | * @param context The Context in which this PendingIntent should start |
| 410 | * the activity. |
Danny Baumann | f15a419 | 2013-04-05 13:42:57 +0200 | [diff] [blame] | 411 | * @param requestCode Private request code for the sender |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 412 | * @param intents Array of Intents of the activities to be launched. |
| 413 | * @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE}, |
| 414 | * {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT}, |
| 415 | * or any of the flags as supported by |
| 416 | * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts |
| 417 | * of the intent that can be supplied when the actual send happens. |
| 418 | * |
| 419 | * @return Returns an existing or new PendingIntent matching the given |
| 420 | * parameters. May return null only if {@link #FLAG_NO_CREATE} has been |
| 421 | * supplied. |
| 422 | */ |
| 423 | public static PendingIntent getActivities(Context context, int requestCode, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 424 | @NonNull Intent[] intents, @Flags int flags, @Nullable Bundle options) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 425 | String packageName = context.getPackageName(); |
| 426 | String[] resolvedTypes = new String[intents.length]; |
| 427 | for (int i=0; i<intents.length; i++) { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 428 | intents[i].migrateExtraStreamToClipData(); |
| 429 | intents[i].prepareToLeaveProcess(); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 430 | resolvedTypes[i] = intents[i].resolveTypeIfNeeded(context.getContentResolver()); |
| 431 | } |
| 432 | try { |
| 433 | IIntentSender target = |
| 434 | ActivityManagerNative.getDefault().getIntentSender( |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 435 | ActivityManager.INTENT_SENDER_ACTIVITY, packageName, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 436 | null, null, requestCode, intents, resolvedTypes, flags, options, |
| 437 | UserHandle.myUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 438 | return target != null ? new PendingIntent(target) : null; |
| 439 | } catch (RemoteException e) { |
| 440 | } |
| 441 | return null; |
| 442 | } |
| 443 | |
| 444 | /** |
Adam Powell | d56b4d1 | 2012-09-30 18:27:31 -0700 | [diff] [blame] | 445 | * @hide |
| 446 | * Note that UserHandle.CURRENT will be interpreted at the time the |
| 447 | * activity is started, not when the pending intent is created. |
| 448 | */ |
| 449 | public static PendingIntent getActivitiesAsUser(Context context, int requestCode, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 450 | @NonNull Intent[] intents, int flags, Bundle options, UserHandle user) { |
Adam Powell | d56b4d1 | 2012-09-30 18:27:31 -0700 | [diff] [blame] | 451 | String packageName = context.getPackageName(); |
| 452 | String[] resolvedTypes = new String[intents.length]; |
| 453 | for (int i=0; i<intents.length; i++) { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 454 | intents[i].migrateExtraStreamToClipData(); |
| 455 | intents[i].prepareToLeaveProcess(); |
Adam Powell | d56b4d1 | 2012-09-30 18:27:31 -0700 | [diff] [blame] | 456 | resolvedTypes[i] = intents[i].resolveTypeIfNeeded(context.getContentResolver()); |
| 457 | } |
| 458 | try { |
| 459 | IIntentSender target = |
| 460 | ActivityManagerNative.getDefault().getIntentSender( |
| 461 | ActivityManager.INTENT_SENDER_ACTIVITY, packageName, |
| 462 | null, null, requestCode, intents, resolvedTypes, |
| 463 | flags, options, user.getIdentifier()); |
| 464 | return target != null ? new PendingIntent(target) : null; |
| 465 | } catch (RemoteException e) { |
| 466 | } |
| 467 | return null; |
| 468 | } |
| 469 | |
| 470 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | * Retrieve a PendingIntent that will perform a broadcast, like calling |
| 472 | * {@link Context#sendBroadcast(Intent) Context.sendBroadcast()}. |
| 473 | * |
Dianne Hackborn | a53ee35 | 2013-02-20 12:47:02 -0800 | [diff] [blame] | 474 | * <p class="note">For security reasons, the {@link android.content.Intent} |
| 475 | * you supply here should almost always be an <em>explicit intent</em>, |
| 476 | * that is specify an explicit component to be delivered to through |
| 477 | * {@link Intent#setClass(android.content.Context, Class)} Intent.setClass</p> |
| 478 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 479 | * @param context The Context in which this PendingIntent should perform |
| 480 | * the broadcast. |
Danny Baumann | f15a419 | 2013-04-05 13:42:57 +0200 | [diff] [blame] | 481 | * @param requestCode Private request code for the sender |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 482 | * @param intent The Intent to be broadcast. |
| 483 | * @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE}, |
| 484 | * {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT}, |
| 485 | * or any of the flags as supported by |
| 486 | * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts |
| 487 | * of the intent that can be supplied when the actual send happens. |
| 488 | * |
| 489 | * @return Returns an existing or new PendingIntent matching the given |
| 490 | * parameters. May return null only if {@link #FLAG_NO_CREATE} has been |
| 491 | * supplied. |
| 492 | */ |
| 493 | public static PendingIntent getBroadcast(Context context, int requestCode, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 494 | Intent intent, @Flags int flags) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 495 | return getBroadcastAsUser(context, requestCode, intent, flags, |
| 496 | new UserHandle(UserHandle.myUserId())); |
| 497 | } |
| 498 | |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 499 | /** |
| 500 | * @hide |
| 501 | * Note that UserHandle.CURRENT will be interpreted at the time the |
| 502 | * broadcast is sent, not when the pending intent is created. |
| 503 | */ |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 504 | public static PendingIntent getBroadcastAsUser(Context context, int requestCode, |
| 505 | Intent intent, int flags, UserHandle userHandle) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | String packageName = context.getPackageName(); |
| 507 | String resolvedType = intent != null ? intent.resolveTypeIfNeeded( |
| 508 | context.getContentResolver()) : null; |
| 509 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 510 | intent.prepareToLeaveProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 511 | IIntentSender target = |
| 512 | ActivityManagerNative.getDefault().getIntentSender( |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 513 | ActivityManager.INTENT_SENDER_BROADCAST, packageName, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 514 | null, null, requestCode, new Intent[] { intent }, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 515 | resolvedType != null ? new String[] { resolvedType } : null, |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 516 | flags, null, userHandle.getIdentifier()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | return target != null ? new PendingIntent(target) : null; |
| 518 | } catch (RemoteException e) { |
| 519 | } |
| 520 | return null; |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Retrieve a PendingIntent that will start a service, like calling |
| 525 | * {@link Context#startService Context.startService()}. The start |
| 526 | * arguments given to the service will come from the extras of the Intent. |
| 527 | * |
Dianne Hackborn | a53ee35 | 2013-02-20 12:47:02 -0800 | [diff] [blame] | 528 | * <p class="note">For security reasons, the {@link android.content.Intent} |
| 529 | * you supply here should almost always be an <em>explicit intent</em>, |
| 530 | * that is specify an explicit component to be delivered to through |
| 531 | * {@link Intent#setClass(android.content.Context, Class)} Intent.setClass</p> |
| 532 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 533 | * @param context The Context in which this PendingIntent should start |
| 534 | * the service. |
Danny Baumann | f15a419 | 2013-04-05 13:42:57 +0200 | [diff] [blame] | 535 | * @param requestCode Private request code for the sender |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 536 | * @param intent An Intent describing the service to be started. |
| 537 | * @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE}, |
| 538 | * {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT}, |
| 539 | * or any of the flags as supported by |
| 540 | * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts |
| 541 | * of the intent that can be supplied when the actual send happens. |
| 542 | * |
| 543 | * @return Returns an existing or new PendingIntent matching the given |
| 544 | * parameters. May return null only if {@link #FLAG_NO_CREATE} has been |
| 545 | * supplied. |
| 546 | */ |
| 547 | public static PendingIntent getService(Context context, int requestCode, |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 548 | @NonNull Intent intent, @Flags int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | String packageName = context.getPackageName(); |
| 550 | String resolvedType = intent != null ? intent.resolveTypeIfNeeded( |
| 551 | context.getContentResolver()) : null; |
| 552 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 553 | intent.prepareToLeaveProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 554 | IIntentSender target = |
| 555 | ActivityManagerNative.getDefault().getIntentSender( |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 556 | ActivityManager.INTENT_SENDER_SERVICE, packageName, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 557 | null, null, requestCode, new Intent[] { intent }, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 558 | resolvedType != null ? new String[] { resolvedType } : null, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 559 | flags, null, UserHandle.myUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 560 | return target != null ? new PendingIntent(target) : null; |
| 561 | } catch (RemoteException e) { |
| 562 | } |
| 563 | return null; |
| 564 | } |
| 565 | |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 566 | /** |
| 567 | * Retrieve a IntentSender object that wraps the existing sender of the PendingIntent |
| 568 | * |
| 569 | * @return Returns a IntentSender object that wraps the sender of PendingIntent |
| 570 | * |
| 571 | */ |
| 572 | public IntentSender getIntentSender() { |
Dianne Hackborn | 7f20543 | 2009-07-28 00:13:47 -0700 | [diff] [blame] | 573 | return new IntentSender(mTarget); |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 574 | } |
| 575 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 576 | /** |
| 577 | * Cancel a currently active PendingIntent. Only the original application |
Ken Wakasa | f76a50c | 2012-03-09 19:56:35 +0900 | [diff] [blame] | 578 | * owning a PendingIntent can cancel it. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 579 | */ |
| 580 | public void cancel() { |
| 581 | try { |
| 582 | ActivityManagerNative.getDefault().cancelIntentSender(mTarget); |
| 583 | } catch (RemoteException e) { |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * Perform the operation associated with this PendingIntent. |
| 589 | * |
| 590 | * @see #send(Context, int, Intent, android.app.PendingIntent.OnFinished, Handler) |
| 591 | * |
| 592 | * @throws CanceledException Throws CanceledException if the PendingIntent |
| 593 | * is no longer allowing more intents to be sent through it. |
| 594 | */ |
| 595 | public void send() throws CanceledException { |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 596 | send(null, 0, null, null, null, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | /** |
| 600 | * Perform the operation associated with this PendingIntent. |
| 601 | * |
| 602 | * @param code Result code to supply back to the PendingIntent's target. |
| 603 | * |
| 604 | * @see #send(Context, int, Intent, android.app.PendingIntent.OnFinished, Handler) |
| 605 | * |
| 606 | * @throws CanceledException Throws CanceledException if the PendingIntent |
| 607 | * is no longer allowing more intents to be sent through it. |
| 608 | */ |
| 609 | public void send(int code) throws CanceledException { |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 610 | send(null, code, null, null, null, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | /** |
| 614 | * Perform the operation associated with this PendingIntent, allowing the |
| 615 | * caller to specify information about the Intent to use. |
| 616 | * |
| 617 | * @param context The Context of the caller. |
| 618 | * @param code Result code to supply back to the PendingIntent's target. |
| 619 | * @param intent Additional Intent data. See {@link Intent#fillIn |
| 620 | * Intent.fillIn()} for information on how this is applied to the |
| 621 | * original Intent. |
| 622 | * |
| 623 | * @see #send(Context, int, Intent, android.app.PendingIntent.OnFinished, Handler) |
| 624 | * |
| 625 | * @throws CanceledException Throws CanceledException if the PendingIntent |
| 626 | * is no longer allowing more intents to be sent through it. |
| 627 | */ |
| 628 | public void send(Context context, int code, Intent intent) |
| 629 | throws CanceledException { |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 630 | send(context, code, intent, null, null, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /** |
| 634 | * Perform the operation associated with this PendingIntent, allowing the |
| 635 | * caller to be notified when the send has completed. |
| 636 | * |
| 637 | * @param code Result code to supply back to the PendingIntent's target. |
| 638 | * @param onFinished The object to call back on when the send has |
| 639 | * completed, or null for no callback. |
| 640 | * @param handler Handler identifying the thread on which the callback |
| 641 | * should happen. If null, the callback will happen from the thread |
| 642 | * pool of the process. |
| 643 | * |
| 644 | * @see #send(Context, int, Intent, android.app.PendingIntent.OnFinished, Handler) |
| 645 | * |
| 646 | * @throws CanceledException Throws CanceledException if the PendingIntent |
| 647 | * is no longer allowing more intents to be sent through it. |
| 648 | */ |
| 649 | public void send(int code, OnFinished onFinished, Handler handler) |
| 650 | throws CanceledException { |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 651 | send(null, code, null, onFinished, handler, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | /** |
| 655 | * Perform the operation associated with this PendingIntent, allowing the |
| 656 | * caller to specify information about the Intent to use and be notified |
| 657 | * when the send has completed. |
| 658 | * |
| 659 | * <p>For the intent parameter, a PendingIntent |
| 660 | * often has restrictions on which fields can be supplied here, based on |
| 661 | * how the PendingIntent was retrieved in {@link #getActivity}, |
| 662 | * {@link #getBroadcast}, or {@link #getService}. |
| 663 | * |
| 664 | * @param context The Context of the caller. This may be null if |
| 665 | * <var>intent</var> is also null. |
| 666 | * @param code Result code to supply back to the PendingIntent's target. |
| 667 | * @param intent Additional Intent data. See {@link Intent#fillIn |
| 668 | * Intent.fillIn()} for information on how this is applied to the |
| 669 | * original Intent. Use null to not modify the original Intent. |
| 670 | * @param onFinished The object to call back on when the send has |
| 671 | * completed, or null for no callback. |
| 672 | * @param handler Handler identifying the thread on which the callback |
| 673 | * should happen. If null, the callback will happen from the thread |
| 674 | * pool of the process. |
| 675 | * |
| 676 | * @see #send() |
| 677 | * @see #send(int) |
| 678 | * @see #send(Context, int, Intent) |
| 679 | * @see #send(int, android.app.PendingIntent.OnFinished, Handler) |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 680 | * @see #send(Context, int, Intent, OnFinished, Handler, String) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 681 | * |
| 682 | * @throws CanceledException Throws CanceledException if the PendingIntent |
| 683 | * is no longer allowing more intents to be sent through it. |
| 684 | */ |
| 685 | public void send(Context context, int code, Intent intent, |
| 686 | OnFinished onFinished, Handler handler) throws CanceledException { |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 687 | send(context, code, intent, onFinished, handler, null); |
| 688 | } |
| 689 | |
| 690 | /** |
| 691 | * Perform the operation associated with this PendingIntent, allowing the |
| 692 | * caller to specify information about the Intent to use and be notified |
| 693 | * when the send has completed. |
| 694 | * |
| 695 | * <p>For the intent parameter, a PendingIntent |
| 696 | * often has restrictions on which fields can be supplied here, based on |
| 697 | * how the PendingIntent was retrieved in {@link #getActivity}, |
| 698 | * {@link #getBroadcast}, or {@link #getService}. |
| 699 | * |
| 700 | * @param context The Context of the caller. This may be null if |
| 701 | * <var>intent</var> is also null. |
| 702 | * @param code Result code to supply back to the PendingIntent's target. |
| 703 | * @param intent Additional Intent data. See {@link Intent#fillIn |
| 704 | * Intent.fillIn()} for information on how this is applied to the |
| 705 | * original Intent. Use null to not modify the original Intent. |
| 706 | * @param onFinished The object to call back on when the send has |
| 707 | * completed, or null for no callback. |
| 708 | * @param handler Handler identifying the thread on which the callback |
| 709 | * should happen. If null, the callback will happen from the thread |
| 710 | * pool of the process. |
| 711 | * @param requiredPermission Name of permission that a recipient of the PendingIntent |
| 712 | * is required to hold. This is only valid for broadcast intents, and |
| 713 | * corresponds to the permission argument in |
| 714 | * {@link Context#sendBroadcast(Intent, String) Context.sendOrderedBroadcast(Intent, String)}. |
| 715 | * If null, no permission is required. |
| 716 | * |
| 717 | * @see #send() |
| 718 | * @see #send(int) |
| 719 | * @see #send(Context, int, Intent) |
| 720 | * @see #send(int, android.app.PendingIntent.OnFinished, Handler) |
| 721 | * @see #send(Context, int, Intent, OnFinished, Handler) |
| 722 | * |
| 723 | * @throws CanceledException Throws CanceledException if the PendingIntent |
| 724 | * is no longer allowing more intents to be sent through it. |
| 725 | */ |
| 726 | public void send(Context context, int code, Intent intent, |
| 727 | OnFinished onFinished, Handler handler, String requiredPermission) |
| 728 | throws CanceledException { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 729 | try { |
| 730 | String resolvedType = intent != null ? |
| 731 | intent.resolveTypeIfNeeded(context.getContentResolver()) |
| 732 | : null; |
| 733 | int res = mTarget.send(code, intent, resolvedType, |
| 734 | onFinished != null |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 735 | ? new FinishedDispatcher(this, onFinished, handler) |
| 736 | : null, |
| 737 | requiredPermission); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 738 | if (res < 0) { |
| 739 | throw new CanceledException(); |
| 740 | } |
| 741 | } catch (RemoteException e) { |
| 742 | throw new CanceledException(e); |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | /** |
Dianne Hackborn | 8832c18 | 2012-09-17 17:20:24 -0700 | [diff] [blame] | 747 | * @deprecated Renamed to {@link #getCreatorPackage()}. |
| 748 | */ |
| 749 | @Deprecated |
| 750 | public String getTargetPackage() { |
| 751 | try { |
| 752 | return ActivityManagerNative.getDefault() |
| 753 | .getPackageForIntentSender(mTarget); |
| 754 | } catch (RemoteException e) { |
| 755 | // Should never happen. |
| 756 | return null; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 761 | * Return the package name of the application that created this |
| 762 | * PendingIntent, that is the identity under which you will actually be |
| 763 | * sending the Intent. The returned string is supplied by the system, so |
| 764 | * that an application can not spoof its package. |
| 765 | * |
Dianne Hackborn | a53ee35 | 2013-02-20 12:47:02 -0800 | [diff] [blame] | 766 | * <p class="note">Be careful about how you use this. All this tells you is |
| 767 | * who created the PendingIntent. It does <strong>not</strong> tell you who |
| 768 | * handed the PendingIntent to you: that is, PendingIntent objects are intended to be |
| 769 | * passed between applications, so the PendingIntent you receive from an application |
| 770 | * could actually be one it received from another application, meaning the result |
| 771 | * you get here will identify the original application. Because of this, you should |
| 772 | * only use this information to identify who you expect to be interacting with |
| 773 | * through a {@link #send} call, not who gave you the PendingIntent.</p> |
| 774 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 775 | * @return The package name of the PendingIntent, or null if there is |
| 776 | * none associated with it. |
| 777 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 778 | @Nullable |
Dianne Hackborn | 8832c18 | 2012-09-17 17:20:24 -0700 | [diff] [blame] | 779 | public String getCreatorPackage() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 780 | try { |
| 781 | return ActivityManagerNative.getDefault() |
| 782 | .getPackageForIntentSender(mTarget); |
| 783 | } catch (RemoteException e) { |
| 784 | // Should never happen. |
| 785 | return null; |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | /** |
Dianne Hackborn | c750127 | 2012-08-14 18:05:05 -0700 | [diff] [blame] | 790 | * Return the uid of the application that created this |
| 791 | * PendingIntent, that is the identity under which you will actually be |
| 792 | * sending the Intent. The returned integer is supplied by the system, so |
| 793 | * that an application can not spoof its uid. |
| 794 | * |
Dianne Hackborn | a53ee35 | 2013-02-20 12:47:02 -0800 | [diff] [blame] | 795 | * <p class="note">Be careful about how you use this. All this tells you is |
| 796 | * who created the PendingIntent. It does <strong>not</strong> tell you who |
| 797 | * handed the PendingIntent to you: that is, PendingIntent objects are intended to be |
| 798 | * passed between applications, so the PendingIntent you receive from an application |
| 799 | * could actually be one it received from another application, meaning the result |
| 800 | * you get here will identify the original application. Because of this, you should |
| 801 | * only use this information to identify who you expect to be interacting with |
| 802 | * through a {@link #send} call, not who gave you the PendingIntent.</p> |
| 803 | * |
Dianne Hackborn | c750127 | 2012-08-14 18:05:05 -0700 | [diff] [blame] | 804 | * @return The uid of the PendingIntent, or -1 if there is |
| 805 | * none associated with it. |
| 806 | */ |
Dianne Hackborn | 8832c18 | 2012-09-17 17:20:24 -0700 | [diff] [blame] | 807 | public int getCreatorUid() { |
Dianne Hackborn | c750127 | 2012-08-14 18:05:05 -0700 | [diff] [blame] | 808 | try { |
| 809 | return ActivityManagerNative.getDefault() |
| 810 | .getUidForIntentSender(mTarget); |
| 811 | } catch (RemoteException e) { |
| 812 | // Should never happen. |
| 813 | return -1; |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | /** |
| 818 | * Return the user handle of the application that created this |
| 819 | * PendingIntent, that is the user under which you will actually be |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 820 | * sending the Intent. The returned UserHandle is supplied by the system, so |
Dianne Hackborn | c750127 | 2012-08-14 18:05:05 -0700 | [diff] [blame] | 821 | * that an application can not spoof its user. See |
| 822 | * {@link android.os.Process#myUserHandle() Process.myUserHandle()} for |
| 823 | * more explanation of user handles. |
| 824 | * |
Dianne Hackborn | a53ee35 | 2013-02-20 12:47:02 -0800 | [diff] [blame] | 825 | * <p class="note">Be careful about how you use this. All this tells you is |
| 826 | * who created the PendingIntent. It does <strong>not</strong> tell you who |
| 827 | * handed the PendingIntent to you: that is, PendingIntent objects are intended to be |
| 828 | * passed between applications, so the PendingIntent you receive from an application |
| 829 | * could actually be one it received from another application, meaning the result |
| 830 | * you get here will identify the original application. Because of this, you should |
| 831 | * only use this information to identify who you expect to be interacting with |
| 832 | * through a {@link #send} call, not who gave you the PendingIntent.</p> |
| 833 | * |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 834 | * @return The user handle of the PendingIntent, or null if there is |
Dianne Hackborn | c750127 | 2012-08-14 18:05:05 -0700 | [diff] [blame] | 835 | * none associated with it. |
| 836 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 837 | @Nullable |
Dianne Hackborn | 8832c18 | 2012-09-17 17:20:24 -0700 | [diff] [blame] | 838 | public UserHandle getCreatorUserHandle() { |
Dianne Hackborn | c750127 | 2012-08-14 18:05:05 -0700 | [diff] [blame] | 839 | try { |
| 840 | int uid = ActivityManagerNative.getDefault() |
| 841 | .getUidForIntentSender(mTarget); |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 842 | return uid > 0 ? new UserHandle(UserHandle.getUserId(uid)) : null; |
Dianne Hackborn | c750127 | 2012-08-14 18:05:05 -0700 | [diff] [blame] | 843 | } catch (RemoteException e) { |
| 844 | // Should never happen. |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 845 | return null; |
Dianne Hackborn | c750127 | 2012-08-14 18:05:05 -0700 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | |
| 849 | /** |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 850 | * @hide |
| 851 | * Check to verify that this PendingIntent targets a specific package. |
| 852 | */ |
| 853 | public boolean isTargetedToPackage() { |
| 854 | try { |
| 855 | return ActivityManagerNative.getDefault() |
| 856 | .isIntentSenderTargetedToPackage(mTarget); |
| 857 | } catch (RemoteException e) { |
| 858 | // Should never happen. |
| 859 | return false; |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | /** |
Dianne Hackborn | 1927ae8 | 2012-06-22 15:21:36 -0700 | [diff] [blame] | 864 | * @hide |
| 865 | * Check whether this PendingIntent will launch an Activity. |
| 866 | */ |
| 867 | public boolean isActivity() { |
| 868 | try { |
| 869 | return ActivityManagerNative.getDefault() |
| 870 | .isIntentSenderAnActivity(mTarget); |
| 871 | } catch (RemoteException e) { |
| 872 | // Should never happen. |
| 873 | return false; |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | /** |
Dianne Hackborn | 8103890 | 2012-11-26 17:04:09 -0800 | [diff] [blame] | 878 | * @hide |
| 879 | * Return the Intent of this PendingIntent. |
| 880 | */ |
| 881 | public Intent getIntent() { |
| 882 | try { |
| 883 | return ActivityManagerNative.getDefault() |
| 884 | .getIntentForIntentSender(mTarget); |
| 885 | } catch (RemoteException e) { |
| 886 | // Should never happen. |
| 887 | return null; |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 892 | * Comparison operator on two PendingIntent objects, such that true |
| 893 | * is returned then they both represent the same operation from the |
| 894 | * same package. This allows you to use {@link #getActivity}, |
| 895 | * {@link #getBroadcast}, or {@link #getService} multiple times (even |
| 896 | * across a process being killed), resulting in different PendingIntent |
| 897 | * objects but whose equals() method identifies them as being the same |
| 898 | * operation. |
| 899 | */ |
| 900 | @Override |
| 901 | public boolean equals(Object otherObj) { |
| 902 | if (otherObj instanceof PendingIntent) { |
| 903 | return mTarget.asBinder().equals(((PendingIntent)otherObj) |
| 904 | .mTarget.asBinder()); |
| 905 | } |
| 906 | return false; |
| 907 | } |
| 908 | |
| 909 | @Override |
| 910 | public int hashCode() { |
| 911 | return mTarget.asBinder().hashCode(); |
| 912 | } |
| 913 | |
| 914 | @Override |
| 915 | public String toString() { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 916 | StringBuilder sb = new StringBuilder(128); |
| 917 | sb.append("PendingIntent{"); |
| 918 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 919 | sb.append(": "); |
| 920 | sb.append(mTarget != null ? mTarget.asBinder() : null); |
| 921 | sb.append('}'); |
| 922 | return sb.toString(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | public int describeContents() { |
| 926 | return 0; |
| 927 | } |
| 928 | |
| 929 | public void writeToParcel(Parcel out, int flags) { |
| 930 | out.writeStrongBinder(mTarget.asBinder()); |
| 931 | } |
| 932 | |
| 933 | public static final Parcelable.Creator<PendingIntent> CREATOR |
| 934 | = new Parcelable.Creator<PendingIntent>() { |
| 935 | public PendingIntent createFromParcel(Parcel in) { |
| 936 | IBinder target = in.readStrongBinder(); |
| 937 | return target != null ? new PendingIntent(target) : null; |
| 938 | } |
| 939 | |
| 940 | public PendingIntent[] newArray(int size) { |
| 941 | return new PendingIntent[size]; |
| 942 | } |
| 943 | }; |
| 944 | |
| 945 | /** |
| 946 | * Convenience function for writing either a PendingIntent or null pointer to |
| 947 | * a Parcel. You must use this with {@link #readPendingIntentOrNullFromParcel} |
| 948 | * for later reading it. |
| 949 | * |
| 950 | * @param sender The PendingIntent to write, or null. |
| 951 | * @param out Where to write the PendingIntent. |
| 952 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 953 | public static void writePendingIntentOrNullToParcel(@Nullable PendingIntent sender, |
| 954 | @NonNull Parcel out) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 955 | out.writeStrongBinder(sender != null ? sender.mTarget.asBinder() |
| 956 | : null); |
| 957 | } |
| 958 | |
| 959 | /** |
| 960 | * Convenience function for reading either a Messenger or null pointer from |
| 961 | * a Parcel. You must have previously written the Messenger with |
| 962 | * {@link #writePendingIntentOrNullToParcel}. |
| 963 | * |
| 964 | * @param in The Parcel containing the written Messenger. |
| 965 | * |
| 966 | * @return Returns the Messenger read from the Parcel, or null if null had |
| 967 | * been written. |
| 968 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 969 | @Nullable |
| 970 | public static PendingIntent readPendingIntentOrNullFromParcel(@NonNull Parcel in) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 971 | IBinder b = in.readStrongBinder(); |
| 972 | return b != null ? new PendingIntent(b) : null; |
| 973 | } |
| 974 | |
| 975 | /*package*/ PendingIntent(IIntentSender target) { |
| 976 | mTarget = target; |
| 977 | } |
| 978 | |
| 979 | /*package*/ PendingIntent(IBinder target) { |
| 980 | mTarget = IIntentSender.Stub.asInterface(target); |
| 981 | } |
| 982 | |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 983 | /** @hide */ |
| 984 | public IIntentSender getTarget() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 985 | return mTarget; |
| 986 | } |
| 987 | } |