/* * Copyright (C) 2006 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.app; import android.content.Context; import android.content.Intent; import android.content.IIntentReceiver; import android.content.IIntentSender; import android.content.IntentSender; import android.os.Bundle; import android.os.RemoteException; import android.os.Handler; import android.os.IBinder; import android.os.Parcel; import android.os.Parcelable; import android.util.AndroidException; /** * A description of an Intent and target action to perform with it. Instances * of this class are created with {@link #getActivity}, * {@link #getBroadcast}, {@link #getService}; the returned object can be * handed to other applications so that they can perform the action you * described on your behalf at a later time. * *
By giving a PendingIntent to another application, * you are granting it the right to perform the operation you have specified * as if the other application was yourself (with the same permissions and * identity). As such, you should be careful about how you build the PendingIntent: * often, for example, the base Intent you supply will have the component * name explicitly set to one of your own components, to ensure it is ultimately * sent there and nowhere else. * *
A PendingIntent itself is simply a reference to a token maintained by * the system describing the original data used to retrieve it. This means * that, even if its owning application's process is killed, the * PendingIntent itself will remain usable from other processes that * have been given it. If the creating application later re-retrieves the * same kind of PendingIntent (same operation, same Intent action, data, * categories, and components, and same flags), it will receive a PendingIntent * representing the same token if that is still valid, and can thus call * {@link #cancel} to remove it. */ public final class PendingIntent implements Parcelable { private final IIntentSender mTarget; /** * Flag for use with {@link #getActivity}, {@link #getBroadcast}, and * {@link #getService}: this * PendingIntent can only be used once. If set, after * {@link #send()} is called on it, it will be automatically * canceled for you and any future attempt to send through it will fail. */ public static final int FLAG_ONE_SHOT = 1<<30; /** * Flag for use with {@link #getActivity}, {@link #getBroadcast}, and * {@link #getService}: if the described PendingIntent does not already * exist, then simply return null instead of creating it. */ public static final int FLAG_NO_CREATE = 1<<29; /** * Flag for use with {@link #getActivity}, {@link #getBroadcast}, and * {@link #getService}: if the described PendingIntent already exists, * the current one is canceled before generating a new one. You can use * this to retrieve a new PendingIntent when you are only changing the * extra data in the Intent; by canceling the previous pending intent, * this ensures that only entities given the new data will be able to * launch it. If this assurance is not an issue, consider * {@link #FLAG_UPDATE_CURRENT}. */ public static final int FLAG_CANCEL_CURRENT = 1<<28; /** * Flag for use with {@link #getActivity}, {@link #getBroadcast}, and * {@link #getService}: if the described PendingIntent already exists, * then keep it but its replace its extra data with what is in this new * Intent. This can be used if you are creating intents where only the * extras change, and don't care that any entities that received your * previous PendingIntent will be able to launch it with your new * extras even if they are not explicitly given to it. */ public static final int FLAG_UPDATE_CURRENT = 1<<27; /** * Exception thrown when trying to send through a PendingIntent that * has been canceled or is otherwise no longer able to execute the request. */ public static class CanceledException extends AndroidException { public CanceledException() { } public CanceledException(String name) { super(name); } public CanceledException(Exception cause) { super(cause); } } /** * Callback interface for discovering when a send operation has * completed. Primarily for use with a PendingIntent that is * performing a broadcast, this provides the same information as * calling {@link Context#sendOrderedBroadcast(Intent, String, * android.content.BroadcastReceiver, Handler, int, String, Bundle) * Context.sendBroadcast()} with a final BroadcastReceiver. */ public interface OnFinished { /** * Called when a send operation as completed. * * @param pendingIntent The PendingIntent this operation was sent through. * @param intent The original Intent that was sent. * @param resultCode The final result code determined by the send. * @param resultData The final data collected by a broadcast. * @param resultExtras The final extras collected by a broadcast. */ void onSendFinished(PendingIntent pendingIntent, Intent intent, int resultCode, String resultData, Bundle resultExtras); } private static class FinishedDispatcher extends IIntentReceiver.Stub implements Runnable { private final PendingIntent mPendingIntent; private final OnFinished mWho; private final Handler mHandler; private Intent mIntent; private int mResultCode; private String mResultData; private Bundle mResultExtras; FinishedDispatcher(PendingIntent pi, OnFinished who, Handler handler) { mPendingIntent = pi; mWho = who; mHandler = handler; } public void performReceive(Intent intent, int resultCode, String data, Bundle extras, boolean serialized, boolean sticky) { mIntent = intent; mResultCode = resultCode; mResultData = data; mResultExtras = extras; if (mHandler == null) { run(); } else { mHandler.post(this); } } public void run() { mWho.onSendFinished(mPendingIntent, mIntent, mResultCode, mResultData, mResultExtras); } } /** * Retrieve a PendingIntent that will start a new activity, like calling * {@link Context#startActivity(Intent) Context.startActivity(Intent)}. * Note that the activity will be started outside of the context of an * existing activity, so you must use the {@link Intent#FLAG_ACTIVITY_NEW_TASK * Intent.FLAG_ACTIVITY_NEW_TASK} launch flag in the Intent. * * @param context The Context in which this PendingIntent should start * the activity. * @param requestCode Private request code for the sender (currently * not used). * @param intent Intent of the activity to be launched. * @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE}, * {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT}, * or any of the flags as supported by * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts * of the intent that can be supplied when the actual send happens. * * @return Returns an existing or new PendingIntent matching the given * parameters. May return null only if {@link #FLAG_NO_CREATE} has been * supplied. */ public static PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags) { String packageName = context.getPackageName(); String resolvedType = intent != null ? intent.resolveTypeIfNeeded( context.getContentResolver()) : null; try { IIntentSender target = ActivityManagerNative.getDefault().getIntentSender( IActivityManager.INTENT_SENDER_ACTIVITY, packageName, null, null, requestCode, new Intent[] { intent }, resolvedType != null ? new String[] { resolvedType } : null, flags); return target != null ? new PendingIntent(target) : null; } catch (RemoteException e) { } return null; } /** * Like {@link #getActivity(Context, int, Intent, int)}, but allows an * array of Intents to be supplied. The first Intent in the array is * taken as the primary key for the PendingIntent, like the single Intent * given to {@link #getActivity(Context, int, Intent, int)}. Upon sending * the resulting PendingIntent, all of the Intents are started in the same * way as they would be by passing them to {@link Context#startActivities(Intent[])}. * *
* The first intent in the array will be started outside of the context of an * existing activity, so you must use the {@link Intent#FLAG_ACTIVITY_NEW_TASK * Intent.FLAG_ACTIVITY_NEW_TASK} launch flag in the Intent. (Activities after * the first in the array are started in the context of the previous activity * in the array, so FLAG_ACTIVITY_NEW_TASK is not needed nor desired for them.) *
* ** The last intent in the array represents the key for the * PendingIntent. In other words, it is the significant element for matching * (as done with the single intent given to {@link #getActivity(Context, int, Intent, int)}, * its content will be the subject of replacement by * {@link #send(Context, int, Intent)} and {@link #FLAG_UPDATE_CURRENT}, etc. * This is because it is the most specific of the supplied intents, and the * UI the user actually sees when the intents are started. *
* * @param context The Context in which this PendingIntent should start * the activity. * @param requestCode Private request code for the sender (currently * not used). * @param intents Array of Intents of the activities to be launched. * @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE}, * {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT}, * or any of the flags as supported by * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts * of the intent that can be supplied when the actual send happens. * * @return Returns an existing or new PendingIntent matching the given * parameters. May return null only if {@link #FLAG_NO_CREATE} has been * supplied. */ public static PendingIntent getActivities(Context context, int requestCode, Intent[] intents, int flags) { String packageName = context.getPackageName(); String[] resolvedTypes = new String[intents.length]; for (int i=0; i