• 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.app.ActivityManagerNative;
20 import android.app.ActivityThread;
21 import android.app.IActivityManager;
22 import android.app.QueuedWork;
23 import android.os.Bundle;
24 import android.os.IBinder;
25 import android.os.RemoteException;
26 import android.util.Log;
27 import android.util.Slog;
28 
29 /**
30  * Base class for code that receives and handles broadcast intents sent by
31  * {@link android.content.Context#sendBroadcast(Intent)}.
32  *
33  * <p>You can either dynamically register an instance of this class with
34  * {@link Context#registerReceiver Context.registerReceiver()}
35  * or statically declare an implementation with the
36  * {@link android.R.styleable#AndroidManifestReceiver &lt;receiver&gt;}
37  * tag in your <code>AndroidManifest.xml</code>.
38  *
39  * <div class="special reference">
40  * <h3>Developer Guides</h3>
41  * <p>For more information about using BroadcastReceiver, read the
42  * <a href="{@docRoot}guide/components/broadcasts.html">Broadcasts</a> developer guide.</p></div>
43  *
44  */
45 public abstract class BroadcastReceiver {
46     private PendingResult mPendingResult;
47     private boolean mDebugUnregister;
48 
49     /**
50      * State for a result that is pending for a broadcast receiver.  Returned
51      * by {@link BroadcastReceiver#goAsync() goAsync()}
52      * while in {@link BroadcastReceiver#onReceive BroadcastReceiver.onReceive()}.
53      * This allows you to return from onReceive() without having the broadcast
54      * terminate; you must call {@link #finish()} once you are done with the
55      * broadcast.  This allows you to process the broadcast off of the main
56      * thread of your app.
57      *
58      * <p>Note on threading: the state inside of this class is not itself
59      * thread-safe, however you can use it from any thread if you properly
60      * sure that you do not have races.  Typically this means you will hand
61      * the entire object to another thread, which will be solely responsible
62      * for setting any results and finally calling {@link #finish()}.
63      */
64     public static class PendingResult {
65         /** @hide */
66         public static final int TYPE_COMPONENT = 0;
67         /** @hide */
68         public static final int TYPE_REGISTERED = 1;
69         /** @hide */
70         public static final int TYPE_UNREGISTERED = 2;
71 
72         final int mType;
73         final boolean mOrderedHint;
74         final boolean mInitialStickyHint;
75         final IBinder mToken;
76         final int mSendingUser;
77         final int mFlags;
78 
79         int mResultCode;
80         String mResultData;
81         Bundle mResultExtras;
82         boolean mAbortBroadcast;
83         boolean mFinished;
84 
85         /** @hide */
PendingResult(int resultCode, String resultData, Bundle resultExtras, int type, boolean ordered, boolean sticky, IBinder token, int userId, int flags)86         public PendingResult(int resultCode, String resultData, Bundle resultExtras, int type,
87                 boolean ordered, boolean sticky, IBinder token, int userId, int flags) {
88             mResultCode = resultCode;
89             mResultData = resultData;
90             mResultExtras = resultExtras;
91             mType = type;
92             mOrderedHint = ordered;
93             mInitialStickyHint = sticky;
94             mToken = token;
95             mSendingUser = userId;
96             mFlags = flags;
97         }
98 
99         /**
100          * Version of {@link BroadcastReceiver#setResultCode(int)
101          * BroadcastReceiver.setResultCode(int)} for
102          * asynchronous broadcast handling.
103          */
setResultCode(int code)104         public final void setResultCode(int code) {
105             checkSynchronousHint();
106             mResultCode = code;
107         }
108 
109         /**
110          * Version of {@link BroadcastReceiver#getResultCode()
111          * BroadcastReceiver.getResultCode()} for
112          * asynchronous broadcast handling.
113          */
getResultCode()114         public final int getResultCode() {
115             return mResultCode;
116         }
117 
118         /**
119          * Version of {@link BroadcastReceiver#setResultData(String)
120          * BroadcastReceiver.setResultData(String)} for
121          * asynchronous broadcast handling.
122          */
setResultData(String data)123         public final void setResultData(String data) {
124             checkSynchronousHint();
125             mResultData = data;
126         }
127 
128         /**
129          * Version of {@link BroadcastReceiver#getResultData()
130          * BroadcastReceiver.getResultData()} for
131          * asynchronous broadcast handling.
132          */
getResultData()133         public final String getResultData() {
134             return mResultData;
135         }
136 
137         /**
138          * Version of {@link BroadcastReceiver#setResultExtras(Bundle)
139          * BroadcastReceiver.setResultExtras(Bundle)} for
140          * asynchronous broadcast handling.
141          */
setResultExtras(Bundle extras)142         public final void setResultExtras(Bundle extras) {
143             checkSynchronousHint();
144             mResultExtras = extras;
145         }
146 
147         /**
148          * Version of {@link BroadcastReceiver#getResultExtras(boolean)
149          * BroadcastReceiver.getResultExtras(boolean)} for
150          * asynchronous broadcast handling.
151          */
getResultExtras(boolean makeMap)152         public final Bundle getResultExtras(boolean makeMap) {
153             Bundle e = mResultExtras;
154             if (!makeMap) return e;
155             if (e == null) mResultExtras = e = new Bundle();
156             return e;
157         }
158 
159         /**
160          * Version of {@link BroadcastReceiver#setResult(int, String, Bundle)
161          * BroadcastReceiver.setResult(int, String, Bundle)} for
162          * asynchronous broadcast handling.
163          */
setResult(int code, String data, Bundle extras)164         public final void setResult(int code, String data, Bundle extras) {
165             checkSynchronousHint();
166             mResultCode = code;
167             mResultData = data;
168             mResultExtras = extras;
169         }
170 
171         /**
172          * Version of {@link BroadcastReceiver#getAbortBroadcast()
173          * BroadcastReceiver.getAbortBroadcast()} for
174          * asynchronous broadcast handling.
175          */
getAbortBroadcast()176         public final boolean getAbortBroadcast() {
177             return mAbortBroadcast;
178         }
179 
180         /**
181          * Version of {@link BroadcastReceiver#abortBroadcast()
182          * BroadcastReceiver.abortBroadcast()} for
183          * asynchronous broadcast handling.
184          */
abortBroadcast()185         public final void abortBroadcast() {
186             checkSynchronousHint();
187             mAbortBroadcast = true;
188         }
189 
190         /**
191          * Version of {@link BroadcastReceiver#clearAbortBroadcast()
192          * BroadcastReceiver.clearAbortBroadcast()} for
193          * asynchronous broadcast handling.
194          */
clearAbortBroadcast()195         public final void clearAbortBroadcast() {
196             mAbortBroadcast = false;
197         }
198 
199         /**
200          * Finish the broadcast.  The current result will be sent and the
201          * next broadcast will proceed.
202          */
finish()203         public final void finish() {
204             if (mType == TYPE_COMPONENT) {
205                 final IActivityManager mgr = ActivityManagerNative.getDefault();
206                 if (QueuedWork.hasPendingWork()) {
207                     // If this is a broadcast component, we need to make sure any
208                     // queued work is complete before telling AM we are done, so
209                     // we don't have our process killed before that.  We now know
210                     // there is pending work; put another piece of work at the end
211                     // of the list to finish the broadcast, so we don't block this
212                     // thread (which may be the main thread) to have it finished.
213                     //
214                     // Note that we don't need to use QueuedWork.add() with the
215                     // runnable, since we know the AM is waiting for us until the
216                     // executor gets to it.
217                     QueuedWork.singleThreadExecutor().execute( new Runnable() {
218                         @Override public void run() {
219                             if (ActivityThread.DEBUG_BROADCAST) Slog.i(ActivityThread.TAG,
220                                     "Finishing broadcast after work to component " + mToken);
221                             sendFinished(mgr);
222                         }
223                     });
224                 } else {
225                     if (ActivityThread.DEBUG_BROADCAST) Slog.i(ActivityThread.TAG,
226                             "Finishing broadcast to component " + mToken);
227                     sendFinished(mgr);
228                 }
229             } else if (mOrderedHint && mType != TYPE_UNREGISTERED) {
230                 if (ActivityThread.DEBUG_BROADCAST) Slog.i(ActivityThread.TAG,
231                         "Finishing broadcast to " + mToken);
232                 final IActivityManager mgr = ActivityManagerNative.getDefault();
233                 sendFinished(mgr);
234             }
235         }
236 
237         /** @hide */
setExtrasClassLoader(ClassLoader cl)238         public void setExtrasClassLoader(ClassLoader cl) {
239             if (mResultExtras != null) {
240                 mResultExtras.setClassLoader(cl);
241             }
242         }
243 
244         /** @hide */
sendFinished(IActivityManager am)245         public void sendFinished(IActivityManager am) {
246             synchronized (this) {
247                 if (mFinished) {
248                     throw new IllegalStateException("Broadcast already finished");
249                 }
250                 mFinished = true;
251 
252                 try {
253                     if (mResultExtras != null) {
254                         mResultExtras.setAllowFds(false);
255                     }
256                     if (mOrderedHint) {
257                         am.finishReceiver(mToken, mResultCode, mResultData, mResultExtras,
258                                 mAbortBroadcast, mFlags);
259                     } else {
260                         // This broadcast was sent to a component; it is not ordered,
261                         // but we still need to tell the activity manager we are done.
262                         am.finishReceiver(mToken, 0, null, null, false, mFlags);
263                     }
264                 } catch (RemoteException ex) {
265                 }
266             }
267         }
268 
269         /** @hide */
getSendingUserId()270         public int getSendingUserId() {
271             return mSendingUser;
272         }
273 
checkSynchronousHint()274         void checkSynchronousHint() {
275             // Note that we don't assert when receiving the initial sticky value,
276             // since that may have come from an ordered broadcast.  We'll catch
277             // them later when the real broadcast happens again.
278             if (mOrderedHint || mInitialStickyHint) {
279                 return;
280             }
281             RuntimeException e = new RuntimeException(
282                     "BroadcastReceiver trying to return result during a non-ordered broadcast");
283             e.fillInStackTrace();
284             Log.e("BroadcastReceiver", e.getMessage(), e);
285         }
286     }
287 
BroadcastReceiver()288     public BroadcastReceiver() {
289     }
290 
291     /**
292      * This method is called when the BroadcastReceiver is receiving an Intent
293      * broadcast.  During this time you can use the other methods on
294      * BroadcastReceiver to view/modify the current result values.  This method
295      * is always called within the main thread of its process, unless you
296      * explicitly asked for it to be scheduled on a different thread using
297      * {@link android.content.Context#registerReceiver(BroadcastReceiver,
298      * IntentFilter, String, android.os.Handler)}. When it runs on the main
299      * thread you should
300      * never perform long-running operations in it (there is a timeout of
301      * 10 seconds that the system allows before considering the receiver to
302      * be blocked and a candidate to be killed). You cannot launch a popup dialog
303      * in your implementation of onReceive().
304      *
305      * <p><b>If this BroadcastReceiver was launched through a &lt;receiver&gt; tag,
306      * then the object is no longer alive after returning from this
307      * function.</b> This means you should not perform any operations that
308      * return a result to you asynchronously. If you need to perform any follow up
309      * background work, schedule a {@link android.app.job.JobService} with
310      * {@link android.app.job.JobScheduler}.
311      *
312      * If you wish to interact with a service that is already running and previously
313      * bound using {@link android.content.Context#bindService(Intent, ServiceConnection, int) bindService()},
314      * you can use {@link #peekService}.
315      *
316      * <p>The Intent filters used in {@link android.content.Context#registerReceiver}
317      * and in application manifests are <em>not</em> guaranteed to be exclusive. They
318      * are hints to the operating system about how to find suitable recipients. It is
319      * possible for senders to force delivery to specific recipients, bypassing filter
320      * resolution.  For this reason, {@link #onReceive(Context, Intent) onReceive()}
321      * implementations should respond only to known actions, ignoring any unexpected
322      * Intents that they may receive.
323      *
324      * @param context The Context in which the receiver is running.
325      * @param intent The Intent being received.
326      */
onReceive(Context context, Intent intent)327     public abstract void onReceive(Context context, Intent intent);
328 
329     /**
330      * This can be called by an application in {@link #onReceive} to allow
331      * it to keep the broadcast active after returning from that function.
332      * This does <em>not</em> change the expectation of being relatively
333      * responsive to the broadcast (finishing it within 10s), but does allow
334      * the implementation to move work related to it over to another thread
335      * to avoid glitching the main UI thread due to disk IO.
336      *
337      * @return Returns a {@link PendingResult} representing the result of
338      * the active broadcast.  The BroadcastRecord itself is no longer active;
339      * all data and other interaction must go through {@link PendingResult}
340      * APIs.  The {@link PendingResult#finish PendingResult.finish()} method
341      * must be called once processing of the broadcast is done.
342      */
goAsync()343     public final PendingResult goAsync() {
344         PendingResult res = mPendingResult;
345         mPendingResult = null;
346         return res;
347     }
348 
349     /**
350      * Provide a binder to an already-bound service.  This method is synchronous
351      * and will not start the target service if it is not present, so it is safe
352      * to call from {@link #onReceive}.
353      *
354      * For peekService() to return a non null {@link android.os.IBinder} interface
355      * the service must have published it before. In other words some component
356      * must have called {@link android.content.Context#bindService(Intent, ServiceConnection, int)} on it.
357      *
358      * @param myContext The Context that had been passed to {@link #onReceive(Context, Intent)}
359      * @param service Identifies the already-bound service you wish to use. See
360      * {@link android.content.Context#bindService(Intent, ServiceConnection, int)}
361      * for more information.
362      */
peekService(Context myContext, Intent service)363     public IBinder peekService(Context myContext, Intent service) {
364         IActivityManager am = ActivityManagerNative.getDefault();
365         IBinder binder = null;
366         try {
367             service.prepareToLeaveProcess(myContext);
368             binder = am.peekService(service, service.resolveTypeIfNeeded(
369                     myContext.getContentResolver()), myContext.getOpPackageName());
370         } catch (RemoteException e) {
371         }
372         return binder;
373     }
374 
375     /**
376      * Change the current result code of this broadcast; only works with
377      * broadcasts sent through
378      * {@link Context#sendOrderedBroadcast(Intent, String)
379      * Context.sendOrderedBroadcast}.  Often uses the
380      * Activity {@link android.app.Activity#RESULT_CANCELED} and
381      * {@link android.app.Activity#RESULT_OK} constants, though the
382      * actual meaning of this value is ultimately up to the broadcaster.
383      *
384      * <p class="note">This method does not work with non-ordered broadcasts such
385      * as those sent with {@link Context#sendBroadcast(Intent)
386      * Context.sendBroadcast}</p>
387      *
388      * @param code The new result code.
389      *
390      * @see #setResult(int, String, Bundle)
391      */
setResultCode(int code)392     public final void setResultCode(int code) {
393         checkSynchronousHint();
394         mPendingResult.mResultCode = code;
395     }
396 
397     /**
398      * Retrieve the current result code, as set by the previous receiver.
399      *
400      * @return int The current result code.
401      */
getResultCode()402     public final int getResultCode() {
403         return mPendingResult != null ? mPendingResult.mResultCode : 0;
404     }
405 
406     /**
407      * Change the current result data of this broadcast; only works with
408      * broadcasts sent through
409      * {@link Context#sendOrderedBroadcast(Intent, String)
410      * Context.sendOrderedBroadcast}.  This is an arbitrary
411      * string whose interpretation is up to the broadcaster.
412      *
413      * <p><strong>This method does not work with non-ordered broadcasts such
414      * as those sent with {@link Context#sendBroadcast(Intent)
415      * Context.sendBroadcast}</strong></p>
416      *
417      * @param data The new result data; may be null.
418      *
419      * @see #setResult(int, String, Bundle)
420      */
setResultData(String data)421     public final void setResultData(String data) {
422         checkSynchronousHint();
423         mPendingResult.mResultData = data;
424     }
425 
426     /**
427      * Retrieve the current result data, as set by the previous receiver.
428      * Often this is null.
429      *
430      * @return String The current result data; may be null.
431      */
getResultData()432     public final String getResultData() {
433         return mPendingResult != null ? mPendingResult.mResultData : null;
434     }
435 
436     /**
437      * Change the current result extras of this broadcast; only works with
438      * broadcasts sent through
439      * {@link Context#sendOrderedBroadcast(Intent, String)
440      * Context.sendOrderedBroadcast}.  This is a Bundle
441      * holding arbitrary data, whose interpretation is up to the
442      * broadcaster.  Can be set to null.  Calling this method completely
443      * replaces the current map (if any).
444      *
445      * <p><strong>This method does not work with non-ordered broadcasts such
446      * as those sent with {@link Context#sendBroadcast(Intent)
447      * Context.sendBroadcast}</strong></p>
448      *
449      * @param extras The new extra data map; may be null.
450      *
451      * @see #setResult(int, String, Bundle)
452      */
setResultExtras(Bundle extras)453     public final void setResultExtras(Bundle extras) {
454         checkSynchronousHint();
455         mPendingResult.mResultExtras = extras;
456     }
457 
458     /**
459      * Retrieve the current result extra data, as set by the previous receiver.
460      * Any changes you make to the returned Map will be propagated to the next
461      * receiver.
462      *
463      * @param makeMap If true then a new empty Map will be made for you if the
464      *                current Map is null; if false you should be prepared to
465      *                receive a null Map.
466      *
467      * @return Map The current extras map.
468      */
getResultExtras(boolean makeMap)469     public final Bundle getResultExtras(boolean makeMap) {
470         if (mPendingResult == null) {
471             return null;
472         }
473         Bundle e = mPendingResult.mResultExtras;
474         if (!makeMap) return e;
475         if (e == null) mPendingResult.mResultExtras = e = new Bundle();
476         return e;
477     }
478 
479     /**
480      * Change all of the result data returned from this broadcasts; only works
481      * with broadcasts sent through
482      * {@link Context#sendOrderedBroadcast(Intent, String)
483      * Context.sendOrderedBroadcast}.  All current result data is replaced
484      * by the value given to this method.
485      *
486      * <p><strong>This method does not work with non-ordered broadcasts such
487      * as those sent with {@link Context#sendBroadcast(Intent)
488      * Context.sendBroadcast}</strong></p>
489      *
490      * @param code The new result code.  Often uses the
491      * Activity {@link android.app.Activity#RESULT_CANCELED} and
492      * {@link android.app.Activity#RESULT_OK} constants, though the
493      * actual meaning of this value is ultimately up to the broadcaster.
494      * @param data The new result data.  This is an arbitrary
495      * string whose interpretation is up to the broadcaster; may be null.
496      * @param extras The new extra data map.  This is a Bundle
497      * holding arbitrary data, whose interpretation is up to the
498      * broadcaster.  Can be set to null.  This completely
499      * replaces the current map (if any).
500      */
setResult(int code, String data, Bundle extras)501     public final void setResult(int code, String data, Bundle extras) {
502         checkSynchronousHint();
503         mPendingResult.mResultCode = code;
504         mPendingResult.mResultData = data;
505         mPendingResult.mResultExtras = extras;
506     }
507 
508     /**
509      * Returns the flag indicating whether or not this receiver should
510      * abort the current broadcast.
511      *
512      * @return True if the broadcast should be aborted.
513      */
getAbortBroadcast()514     public final boolean getAbortBroadcast() {
515         return mPendingResult != null ? mPendingResult.mAbortBroadcast : false;
516     }
517 
518     /**
519      * Sets the flag indicating that this receiver should abort the
520      * current broadcast; only works with broadcasts sent through
521      * {@link Context#sendOrderedBroadcast(Intent, String)
522      * Context.sendOrderedBroadcast}.  This will prevent
523      * any other broadcast receivers from receiving the broadcast. It will still
524      * call {@link #onReceive} of the BroadcastReceiver that the caller of
525      * {@link Context#sendOrderedBroadcast(Intent, String)
526      * Context.sendOrderedBroadcast} passed in.
527      *
528      * <p><strong>This method does not work with non-ordered broadcasts such
529      * as those sent with {@link Context#sendBroadcast(Intent)
530      * Context.sendBroadcast}</strong></p>
531      */
abortBroadcast()532     public final void abortBroadcast() {
533         checkSynchronousHint();
534         mPendingResult.mAbortBroadcast = true;
535     }
536 
537     /**
538      * Clears the flag indicating that this receiver should abort the current
539      * broadcast.
540      */
clearAbortBroadcast()541     public final void clearAbortBroadcast() {
542         if (mPendingResult != null) {
543             mPendingResult.mAbortBroadcast = false;
544         }
545     }
546 
547     /**
548      * Returns true if the receiver is currently processing an ordered
549      * broadcast.
550      */
isOrderedBroadcast()551     public final boolean isOrderedBroadcast() {
552         return mPendingResult != null ? mPendingResult.mOrderedHint : false;
553     }
554 
555     /**
556      * Returns true if the receiver is currently processing the initial
557      * value of a sticky broadcast -- that is, the value that was last
558      * broadcast and is currently held in the sticky cache, so this is
559      * not directly the result of a broadcast right now.
560      */
isInitialStickyBroadcast()561     public final boolean isInitialStickyBroadcast() {
562         return mPendingResult != null ? mPendingResult.mInitialStickyHint : false;
563     }
564 
565     /**
566      * For internal use, sets the hint about whether this BroadcastReceiver is
567      * running in ordered mode.
568      */
setOrderedHint(boolean isOrdered)569     public final void setOrderedHint(boolean isOrdered) {
570         // Accidentally left in the SDK.
571     }
572 
573     /**
574      * For internal use to set the result data that is active. @hide
575      */
setPendingResult(PendingResult result)576     public final void setPendingResult(PendingResult result) {
577         mPendingResult = result;
578     }
579 
580     /**
581      * For internal use to set the result data that is active. @hide
582      */
getPendingResult()583     public final PendingResult getPendingResult() {
584         return mPendingResult;
585     }
586 
587     /** @hide */
getSendingUserId()588     public int getSendingUserId() {
589         return mPendingResult.mSendingUser;
590     }
591 
592     /**
593      * Control inclusion of debugging help for mismatched
594      * calls to {@link Context#registerReceiver(BroadcastReceiver, IntentFilter)
595      * Context.registerReceiver()}.
596      * If called with true, before given to registerReceiver(), then the
597      * callstack of the following {@link Context#unregisterReceiver(BroadcastReceiver)
598      * Context.unregisterReceiver()} call is retained, to be printed if a later
599      * incorrect unregister call is made.  Note that doing this requires retaining
600      * information about the BroadcastReceiver for the lifetime of the app,
601      * resulting in a leak -- this should only be used for debugging.
602      */
setDebugUnregister(boolean debug)603     public final void setDebugUnregister(boolean debug) {
604         mDebugUnregister = debug;
605     }
606 
607     /**
608      * Return the last value given to {@link #setDebugUnregister}.
609      */
getDebugUnregister()610     public final boolean getDebugUnregister() {
611         return mDebugUnregister;
612     }
613 
checkSynchronousHint()614     void checkSynchronousHint() {
615         if (mPendingResult == null) {
616             throw new IllegalStateException("Call while result is not pending");
617         }
618 
619         // Note that we don't assert when receiving the initial sticky value,
620         // since that may have come from an ordered broadcast.  We'll catch
621         // them later when the real broadcast happens again.
622         if (mPendingResult.mOrderedHint || mPendingResult.mInitialStickyHint) {
623             return;
624         }
625         RuntimeException e = new RuntimeException(
626                 "BroadcastReceiver trying to return result during a non-ordered broadcast");
627         e.fillInStackTrace();
628         Log.e("BroadcastReceiver", e.getMessage(), e);
629     }
630 }
631 
632