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