• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.content;
18 
19 import android.annotation.Nullable;
20 import android.app.ActivityManager;
21 import android.app.ActivityManager.PendingIntentInfo;
22 import android.compat.annotation.UnsupportedAppUsage;
23 import android.os.Bundle;
24 import android.os.Handler;
25 import android.os.IBinder;
26 import android.os.Parcel;
27 import android.os.Parcelable;
28 import android.os.RemoteException;
29 import android.os.UserHandle;
30 import android.util.AndroidException;
31 
32 /**
33  * A description of an Intent and target action to perform with it.
34  * The returned object can be
35  * handed to other applications so that they can perform the action you
36  * described on your behalf at a later time.
37  *
38  * <p>By giving a IntentSender to another application,
39  * you are granting it the right to perform the operation you have specified
40  * as if the other application was yourself (with the same permissions and
41  * identity).  As such, you should be careful about how you build the IntentSender:
42  * often, for example, the base Intent you supply will have the component
43  * name explicitly set to one of your own components, to ensure it is ultimately
44  * sent there and nowhere else.
45  *
46  * <p>A IntentSender itself is simply a reference to a token maintained by
47  * the system describing the original data used to retrieve it.  This means
48  * that, even if its owning application's process is killed, the
49  * IntentSender itself will remain usable from other processes that
50  * have been given it.  If the creating application later re-retrieves the
51  * same kind of IntentSender (same operation, same Intent action, data,
52  * categories, and components, and same flags), it will receive a IntentSender
53  * representing the same token if that is still valid.
54  *
55  * <p>Instances of this class can not be made directly, but rather must be
56  * created from an existing {@link android.app.PendingIntent} with
57  * {@link android.app.PendingIntent#getIntentSender() PendingIntent.getIntentSender()}.
58  */
59 public class IntentSender implements Parcelable {
60     @UnsupportedAppUsage
61     private final IIntentSender mTarget;
62     IBinder mWhitelistToken;
63 
64     // cached pending intent information
65     private @Nullable PendingIntentInfo mCachedInfo;
66 
67     /**
68      * Exception thrown when trying to send through a PendingIntent that
69      * has been canceled or is otherwise no longer able to execute the request.
70      */
71     public static class SendIntentException extends AndroidException {
SendIntentException()72         public SendIntentException() {
73         }
74 
SendIntentException(String name)75         public SendIntentException(String name) {
76             super(name);
77         }
78 
SendIntentException(Exception cause)79         public SendIntentException(Exception cause) {
80             super(cause);
81         }
82     }
83 
84     /**
85      * Callback interface for discovering when a send operation has
86      * completed.  Primarily for use with a IntentSender that is
87      * performing a broadcast, this provides the same information as
88      * calling {@link Context#sendOrderedBroadcast(Intent, String,
89      * android.content.BroadcastReceiver, Handler, int, String, Bundle)
90      * Context.sendBroadcast()} with a final BroadcastReceiver.
91      */
92     public interface OnFinished {
93         /**
94          * Called when a send operation as completed.
95          *
96          * @param IntentSender The IntentSender this operation was sent through.
97          * @param intent The original Intent that was sent.
98          * @param resultCode The final result code determined by the send.
99          * @param resultData The final data collected by a broadcast.
100          * @param resultExtras The final extras collected by a broadcast.
101          */
onSendFinished(IntentSender IntentSender, Intent intent, int resultCode, String resultData, Bundle resultExtras)102         void onSendFinished(IntentSender IntentSender, Intent intent,
103                 int resultCode, String resultData, Bundle resultExtras);
104     }
105 
106     private static class FinishedDispatcher extends IIntentReceiver.Stub
107             implements Runnable {
108         private final IntentSender mIntentSender;
109         private final OnFinished mWho;
110         private final Handler mHandler;
111         private Intent mIntent;
112         private int mResultCode;
113         private String mResultData;
114         private Bundle mResultExtras;
FinishedDispatcher(IntentSender pi, OnFinished who, Handler handler)115         FinishedDispatcher(IntentSender pi, OnFinished who, Handler handler) {
116             mIntentSender = pi;
117             mWho = who;
118             mHandler = handler;
119         }
performReceive(Intent intent, int resultCode, String data, Bundle extras, boolean serialized, boolean sticky, int sendingUser)120         public void performReceive(Intent intent, int resultCode, String data,
121                 Bundle extras, boolean serialized, boolean sticky, int sendingUser) {
122             mIntent = intent;
123             mResultCode = resultCode;
124             mResultData = data;
125             mResultExtras = extras;
126             if (mHandler == null) {
127                 run();
128             } else {
129                 mHandler.post(this);
130             }
131         }
run()132         public void run() {
133             mWho.onSendFinished(mIntentSender, mIntent, mResultCode,
134                     mResultData, mResultExtras);
135         }
136     }
137 
138     /**
139      * Perform the operation associated with this IntentSender, allowing the
140      * caller to specify information about the Intent to use and be notified
141      * when the send has completed.
142      *
143      * @param context The Context of the caller.  This may be null if
144      * <var>intent</var> is also null.
145      * @param code Result code to supply back to the IntentSender's target.
146      * @param intent Additional Intent data.  See {@link Intent#fillIn
147      * Intent.fillIn()} for information on how this is applied to the
148      * original Intent.  Use null to not modify the original Intent.
149      * @param onFinished The object to call back on when the send has
150      * completed, or null for no callback.
151      * @param handler Handler identifying the thread on which the callback
152      * should happen.  If null, the callback will happen from the thread
153      * pool of the process.
154      *
155      *
156      * @throws SendIntentException Throws CanceledIntentException if the IntentSender
157      * is no longer allowing more intents to be sent through it.
158      */
sendIntent(Context context, int code, Intent intent, OnFinished onFinished, Handler handler)159     public void sendIntent(Context context, int code, Intent intent,
160             OnFinished onFinished, Handler handler) throws SendIntentException {
161         sendIntent(context, code, intent, onFinished, handler, null);
162     }
163 
164     /**
165      * Perform the operation associated with this IntentSender, allowing the
166      * caller to specify information about the Intent to use and be notified
167      * when the send has completed.
168      *
169      * @param context The Context of the caller.  This may be null if
170      * <var>intent</var> is also null.
171      * @param code Result code to supply back to the IntentSender's target.
172      * @param intent Additional Intent data.  See {@link Intent#fillIn
173      * Intent.fillIn()} for information on how this is applied to the
174      * original Intent.  Use null to not modify the original Intent.
175      * @param onFinished The object to call back on when the send has
176      * completed, or null for no callback.
177      * @param handler Handler identifying the thread on which the callback
178      * should happen.  If null, the callback will happen from the thread
179      * pool of the process.
180      * @param requiredPermission Name of permission that a recipient of the PendingIntent
181      * is required to hold.  This is only valid for broadcast intents, and
182      * corresponds to the permission argument in
183      * {@link Context#sendBroadcast(Intent, String) Context.sendOrderedBroadcast(Intent, String)}.
184      * If null, no permission is required.
185      *
186      *
187      * @throws SendIntentException Throws CanceledIntentException if the IntentSender
188      * is no longer allowing more intents to be sent through it.
189      */
sendIntent(Context context, int code, Intent intent, OnFinished onFinished, Handler handler, String requiredPermission)190     public void sendIntent(Context context, int code, Intent intent,
191             OnFinished onFinished, Handler handler, String requiredPermission)
192             throws SendIntentException {
193         try {
194             String resolvedType = intent != null ?
195                     intent.resolveTypeIfNeeded(context.getContentResolver())
196                     : null;
197             int res = ActivityManager.getService().sendIntentSender(mTarget, mWhitelistToken,
198                     code, intent, resolvedType,
199                     onFinished != null
200                             ? new FinishedDispatcher(this, onFinished, handler)
201                             : null,
202                     requiredPermission, null);
203             if (res < 0) {
204                 throw new SendIntentException();
205             }
206         } catch (RemoteException e) {
207             throw new SendIntentException();
208         }
209     }
210 
211     /**
212      * @deprecated Renamed to {@link #getCreatorPackage()}.
213      */
214     @Deprecated
getTargetPackage()215     public String getTargetPackage() {
216         return getCreatorPackage();
217     }
218 
219     /**
220      * Return the package name of the application that created this
221      * IntentSender, that is the identity under which you will actually be
222      * sending the Intent.  The returned string is supplied by the system, so
223      * that an application can not spoof its package.
224      *
225      * @return The package name of the PendingIntent, or null if there is
226      * none associated with it.
227      */
getCreatorPackage()228     public String getCreatorPackage() {
229         return getCachedInfo().getCreatorPackage();
230     }
231 
232     /**
233      * Return the uid of the application that created this
234      * PendingIntent, that is the identity under which you will actually be
235      * sending the Intent.  The returned integer is supplied by the system, so
236      * that an application can not spoof its uid.
237      *
238      * @return The uid of the PendingIntent, or -1 if there is
239      * none associated with it.
240      */
getCreatorUid()241     public int getCreatorUid() {
242         return getCachedInfo().getCreatorUid();
243     }
244 
245     /**
246      * Return the user handle of the application that created this
247      * PendingIntent, that is the user under which you will actually be
248      * sending the Intent.  The returned UserHandle is supplied by the system, so
249      * that an application can not spoof its user.  See
250      * {@link android.os.Process#myUserHandle() Process.myUserHandle()} for
251      * more explanation of user handles.
252      *
253      * @return The user handle of the PendingIntent, or null if there is
254      * none associated with it.
255      */
getCreatorUserHandle()256     public UserHandle getCreatorUserHandle() {
257         int uid = getCachedInfo().getCreatorUid();
258         return uid > 0 ? new UserHandle(UserHandle.getUserId(uid)) : null;
259     }
260 
261     /**
262      * Comparison operator on two IntentSender objects, such that true
263      * is returned then they both represent the same operation from the
264      * same package.
265      */
266     @Override
equals(@ullable Object otherObj)267     public boolean equals(@Nullable Object otherObj) {
268         if (otherObj instanceof IntentSender) {
269             return mTarget.asBinder().equals(((IntentSender)otherObj)
270                     .mTarget.asBinder());
271         }
272         return false;
273     }
274 
275     @Override
hashCode()276     public int hashCode() {
277         return mTarget.asBinder().hashCode();
278     }
279 
280     @Override
toString()281     public String toString() {
282         StringBuilder sb = new StringBuilder(128);
283         sb.append("IntentSender{");
284         sb.append(Integer.toHexString(System.identityHashCode(this)));
285         sb.append(": ");
286         sb.append(mTarget != null ? mTarget.asBinder() : null);
287         sb.append('}');
288         return sb.toString();
289     }
290 
describeContents()291     public int describeContents() {
292         return 0;
293     }
294 
writeToParcel(Parcel out, int flags)295     public void writeToParcel(Parcel out, int flags) {
296         out.writeStrongBinder(mTarget.asBinder());
297     }
298 
299     public static final @android.annotation.NonNull Parcelable.Creator<IntentSender> CREATOR
300             = new Parcelable.Creator<IntentSender>() {
301         public IntentSender createFromParcel(Parcel in) {
302             IBinder target = in.readStrongBinder();
303             return target != null ? new IntentSender(target) : null;
304         }
305 
306         public IntentSender[] newArray(int size) {
307             return new IntentSender[size];
308         }
309     };
310 
311     /**
312      * Convenience function for writing either a IntentSender or null pointer to
313      * a Parcel.  You must use this with {@link #readIntentSenderOrNullFromParcel}
314      * for later reading it.
315      *
316      * @param sender The IntentSender to write, or null.
317      * @param out Where to write the IntentSender.
318      */
writeIntentSenderOrNullToParcel(IntentSender sender, Parcel out)319     public static void writeIntentSenderOrNullToParcel(IntentSender sender,
320             Parcel out) {
321         out.writeStrongBinder(sender != null ? sender.mTarget.asBinder()
322                 : null);
323     }
324 
325     /**
326      * Convenience function for reading either a Messenger or null pointer from
327      * a Parcel.  You must have previously written the Messenger with
328      * {@link #writeIntentSenderOrNullToParcel}.
329      *
330      * @param in The Parcel containing the written Messenger.
331      *
332      * @return Returns the Messenger read from the Parcel, or null if null had
333      * been written.
334      */
readIntentSenderOrNullFromParcel(Parcel in)335     public static IntentSender readIntentSenderOrNullFromParcel(Parcel in) {
336         IBinder b = in.readStrongBinder();
337         return b != null ? new IntentSender(b) : null;
338     }
339 
340     /** @hide */
341     @UnsupportedAppUsage
getTarget()342     public IIntentSender getTarget() {
343         return mTarget;
344     }
345 
346     /** @hide */
getWhitelistToken()347     public IBinder getWhitelistToken() {
348         return mWhitelistToken;
349     }
350 
351     /** @hide */
352     @UnsupportedAppUsage
IntentSender(IIntentSender target)353     public IntentSender(IIntentSender target) {
354         mTarget = target;
355     }
356 
357     /** @hide */
IntentSender(IIntentSender target, IBinder whitelistToken)358     public IntentSender(IIntentSender target, IBinder whitelistToken) {
359         mTarget = target;
360         mWhitelistToken = whitelistToken;
361     }
362 
363     /** @hide */
IntentSender(IBinder target)364     public IntentSender(IBinder target) {
365         mTarget = IIntentSender.Stub.asInterface(target);
366     }
367 
getCachedInfo()368     private PendingIntentInfo getCachedInfo() {
369         if (mCachedInfo == null) {
370             try {
371                 mCachedInfo = ActivityManager.getService().getInfoForIntentSender(mTarget);
372             } catch (RemoteException e) {
373                 throw e.rethrowFromSystemServer();
374             }
375         }
376 
377         return mCachedInfo;
378     }
379 }
380