blob: 8691ed43ac814045cba24112e3b21f50d2abff4f [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.content;
18
Mathew Inwood5c0d3542018-08-14 13:54:31 +010019import android.annotation.UnsupportedAppUsage;
Sudheer Shankadc589ac2016-11-10 15:30:17 -080020import android.app.ActivityManager;
Dianne Hackborne829fef2010-10-26 17:44:01 -070021import android.app.ActivityThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.app.IActivityManager;
Dianne Hackborne829fef2010-10-26 17:44:01 -070023import android.app.QueuedWork;
Mathew Inwood8c854f82018-09-14 12:35:36 +010024import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.os.Bundle;
26import android.os.IBinder;
27import android.os.RemoteException;
28import android.util.Log;
Dianne Hackborne829fef2010-10-26 17:44:01 -070029import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
31/**
Mark Lu33ec1062016-12-05 10:57:55 -080032 * Base class for code that receives and handles broadcast intents sent by
33 * {@link android.content.Context#sendBroadcast(Intent)}.
Dianne Hackborn7871bad2011-12-12 15:19:26 -080034 *
35 * <p>You can either dynamically register an instance of this class with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036 * {@link Context#registerReceiver Context.registerReceiver()}
Mark Lu33ec1062016-12-05 10:57:55 -080037 * or statically declare an implementation with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 * {@link android.R.styleable#AndroidManifestReceiver &lt;receiver&gt;}
Dianne Hackborn7871bad2011-12-12 15:19:26 -080039 * tag in your <code>AndroidManifest.xml</code>.
Joe Fernandezb54e7a32011-10-03 15:09:50 -070040 *
41 * <div class="special reference">
42 * <h3>Developer Guides</h3>
Mark Lu33ec1062016-12-05 10:57:55 -080043 * <p>For more information about using BroadcastReceiver, read the
44 * <a href="{@docRoot}guide/components/broadcasts.html">Broadcasts</a> developer guide.</p></div>
Joe Fernandezb54e7a32011-10-03 15:09:50 -070045 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 */
47public abstract class BroadcastReceiver {
Mathew Inwood5c0d3542018-08-14 13:54:31 +010048 @UnsupportedAppUsage
Dianne Hackborne829fef2010-10-26 17:44:01 -070049 private PendingResult mPendingResult;
50 private boolean mDebugUnregister;
Mark Lu33ec1062016-12-05 10:57:55 -080051
Dianne Hackborne829fef2010-10-26 17:44:01 -070052 /**
53 * State for a result that is pending for a broadcast receiver. Returned
54 * by {@link BroadcastReceiver#goAsync() goAsync()}
55 * while in {@link BroadcastReceiver#onReceive BroadcastReceiver.onReceive()}.
Dianne Hackborn327fbd22011-01-17 14:38:50 -080056 * This allows you to return from onReceive() without having the broadcast
57 * terminate; you must call {@link #finish()} once you are done with the
58 * broadcast. This allows you to process the broadcast off of the main
59 * thread of your app.
Mark Lu33ec1062016-12-05 10:57:55 -080060 *
Dianne Hackborn327fbd22011-01-17 14:38:50 -080061 * <p>Note on threading: the state inside of this class is not itself
62 * thread-safe, however you can use it from any thread if you properly
63 * sure that you do not have races. Typically this means you will hand
64 * the entire object to another thread, which will be solely responsible
65 * for setting any results and finally calling {@link #finish()}.
Dianne Hackborne829fef2010-10-26 17:44:01 -070066 */
67 public static class PendingResult {
68 /** @hide */
69 public static final int TYPE_COMPONENT = 0;
70 /** @hide */
71 public static final int TYPE_REGISTERED = 1;
72 /** @hide */
73 public static final int TYPE_UNREGISTERED = 2;
Mark Lu33ec1062016-12-05 10:57:55 -080074
Mathew Inwood8c854f82018-09-14 12:35:36 +010075 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dianne Hackborne829fef2010-10-26 17:44:01 -070076 final int mType;
Mathew Inwood5c0d3542018-08-14 13:54:31 +010077 @UnsupportedAppUsage
Dianne Hackborne829fef2010-10-26 17:44:01 -070078 final boolean mOrderedHint;
Mathew Inwood5c0d3542018-08-14 13:54:31 +010079 @UnsupportedAppUsage
Dianne Hackborne829fef2010-10-26 17:44:01 -070080 final boolean mInitialStickyHint;
Mathew Inwood8c854f82018-09-14 12:35:36 +010081 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dianne Hackborne829fef2010-10-26 17:44:01 -070082 final IBinder mToken;
Mathew Inwood5c0d3542018-08-14 13:54:31 +010083 @UnsupportedAppUsage
Dianne Hackborn20e80982012-08-31 19:00:44 -070084 final int mSendingUser;
Mathew Inwood8c854f82018-09-14 12:35:36 +010085 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
riddle_hsu1f5ac4d2015-01-03 15:38:21 +080086 final int mFlags;
Mark Lu33ec1062016-12-05 10:57:55 -080087
Mathew Inwood8c854f82018-09-14 12:35:36 +010088 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dianne Hackborne829fef2010-10-26 17:44:01 -070089 int mResultCode;
Mathew Inwood8c854f82018-09-14 12:35:36 +010090 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dianne Hackborne829fef2010-10-26 17:44:01 -070091 String mResultData;
Mathew Inwood5c0d3542018-08-14 13:54:31 +010092 @UnsupportedAppUsage
Dianne Hackborne829fef2010-10-26 17:44:01 -070093 Bundle mResultExtras;
Mathew Inwood5c0d3542018-08-14 13:54:31 +010094 @UnsupportedAppUsage
Dianne Hackborne829fef2010-10-26 17:44:01 -070095 boolean mAbortBroadcast;
Mathew Inwood5c0d3542018-08-14 13:54:31 +010096 @UnsupportedAppUsage
Dianne Hackborne829fef2010-10-26 17:44:01 -070097 boolean mFinished;
Dianne Hackborn20e80982012-08-31 19:00:44 -070098
Dianne Hackborne829fef2010-10-26 17:44:01 -070099 /** @hide */
Mathew Inwood8c854f82018-09-14 12:35:36 +0100100 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
riddle_hsu1f5ac4d2015-01-03 15:38:21 +0800101 public PendingResult(int resultCode, String resultData, Bundle resultExtras, int type,
102 boolean ordered, boolean sticky, IBinder token, int userId, int flags) {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700103 mResultCode = resultCode;
104 mResultData = resultData;
105 mResultExtras = resultExtras;
106 mType = type;
107 mOrderedHint = ordered;
108 mInitialStickyHint = sticky;
109 mToken = token;
Dianne Hackborn20e80982012-08-31 19:00:44 -0700110 mSendingUser = userId;
riddle_hsu1f5ac4d2015-01-03 15:38:21 +0800111 mFlags = flags;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700112 }
Mark Lu33ec1062016-12-05 10:57:55 -0800113
Dianne Hackborne829fef2010-10-26 17:44:01 -0700114 /**
115 * Version of {@link BroadcastReceiver#setResultCode(int)
116 * BroadcastReceiver.setResultCode(int)} for
117 * asynchronous broadcast handling.
118 */
119 public final void setResultCode(int code) {
120 checkSynchronousHint();
121 mResultCode = code;
122 }
123
124 /**
125 * Version of {@link BroadcastReceiver#getResultCode()
126 * BroadcastReceiver.getResultCode()} for
127 * asynchronous broadcast handling.
128 */
129 public final int getResultCode() {
130 return mResultCode;
131 }
132
133 /**
134 * Version of {@link BroadcastReceiver#setResultData(String)
135 * BroadcastReceiver.setResultData(String)} for
136 * asynchronous broadcast handling.
137 */
138 public final void setResultData(String data) {
139 checkSynchronousHint();
140 mResultData = data;
141 }
142
143 /**
144 * Version of {@link BroadcastReceiver#getResultData()
145 * BroadcastReceiver.getResultData()} for
146 * asynchronous broadcast handling.
147 */
148 public final String getResultData() {
149 return mResultData;
150 }
151
152 /**
153 * Version of {@link BroadcastReceiver#setResultExtras(Bundle)
154 * BroadcastReceiver.setResultExtras(Bundle)} for
155 * asynchronous broadcast handling.
156 */
157 public final void setResultExtras(Bundle extras) {
158 checkSynchronousHint();
159 mResultExtras = extras;
160 }
161
162 /**
163 * Version of {@link BroadcastReceiver#getResultExtras(boolean)
164 * BroadcastReceiver.getResultExtras(boolean)} for
165 * asynchronous broadcast handling.
166 */
167 public final Bundle getResultExtras(boolean makeMap) {
168 Bundle e = mResultExtras;
169 if (!makeMap) return e;
170 if (e == null) mResultExtras = e = new Bundle();
171 return e;
172 }
173
174 /**
175 * Version of {@link BroadcastReceiver#setResult(int, String, Bundle)
176 * BroadcastReceiver.setResult(int, String, Bundle)} for
177 * asynchronous broadcast handling.
178 */
179 public final void setResult(int code, String data, Bundle extras) {
180 checkSynchronousHint();
181 mResultCode = code;
182 mResultData = data;
183 mResultExtras = extras;
184 }
Mark Lu33ec1062016-12-05 10:57:55 -0800185
Dianne Hackborne829fef2010-10-26 17:44:01 -0700186 /**
187 * Version of {@link BroadcastReceiver#getAbortBroadcast()
188 * BroadcastReceiver.getAbortBroadcast()} for
189 * asynchronous broadcast handling.
190 */
191 public final boolean getAbortBroadcast() {
192 return mAbortBroadcast;
193 }
194
195 /**
196 * Version of {@link BroadcastReceiver#abortBroadcast()
197 * BroadcastReceiver.abortBroadcast()} for
198 * asynchronous broadcast handling.
199 */
200 public final void abortBroadcast() {
201 checkSynchronousHint();
202 mAbortBroadcast = true;
203 }
Mark Lu33ec1062016-12-05 10:57:55 -0800204
Dianne Hackborne829fef2010-10-26 17:44:01 -0700205 /**
206 * Version of {@link BroadcastReceiver#clearAbortBroadcast()
207 * BroadcastReceiver.clearAbortBroadcast()} for
208 * asynchronous broadcast handling.
209 */
210 public final void clearAbortBroadcast() {
211 mAbortBroadcast = false;
212 }
Mark Lu33ec1062016-12-05 10:57:55 -0800213
Dianne Hackborne829fef2010-10-26 17:44:01 -0700214 /**
215 * Finish the broadcast. The current result will be sent and the
216 * next broadcast will proceed.
217 */
218 public final void finish() {
219 if (mType == TYPE_COMPONENT) {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800220 final IActivityManager mgr = ActivityManager.getService();
Dianne Hackborne829fef2010-10-26 17:44:01 -0700221 if (QueuedWork.hasPendingWork()) {
222 // If this is a broadcast component, we need to make sure any
223 // queued work is complete before telling AM we are done, so
224 // we don't have our process killed before that. We now know
225 // there is pending work; put another piece of work at the end
226 // of the list to finish the broadcast, so we don't block this
227 // thread (which may be the main thread) to have it finished.
228 //
Philip P. Moltmann82b90222017-02-14 12:43:57 -0800229 // Note that we don't need to use QueuedWork.addFinisher() with the
Dianne Hackborne829fef2010-10-26 17:44:01 -0700230 // runnable, since we know the AM is waiting for us until the
231 // executor gets to it.
Philip P. Moltmann82b90222017-02-14 12:43:57 -0800232 QueuedWork.queue(new Runnable() {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700233 @Override public void run() {
234 if (ActivityThread.DEBUG_BROADCAST) Slog.i(ActivityThread.TAG,
235 "Finishing broadcast after work to component " + mToken);
236 sendFinished(mgr);
237 }
Philip P. Moltmann82b90222017-02-14 12:43:57 -0800238 }, false);
Dianne Hackborne829fef2010-10-26 17:44:01 -0700239 } else {
240 if (ActivityThread.DEBUG_BROADCAST) Slog.i(ActivityThread.TAG,
241 "Finishing broadcast to component " + mToken);
242 sendFinished(mgr);
243 }
244 } else if (mOrderedHint && mType != TYPE_UNREGISTERED) {
245 if (ActivityThread.DEBUG_BROADCAST) Slog.i(ActivityThread.TAG,
246 "Finishing broadcast to " + mToken);
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800247 final IActivityManager mgr = ActivityManager.getService();
Dianne Hackborne829fef2010-10-26 17:44:01 -0700248 sendFinished(mgr);
249 }
250 }
Mark Lu33ec1062016-12-05 10:57:55 -0800251
Dianne Hackborne829fef2010-10-26 17:44:01 -0700252 /** @hide */
253 public void setExtrasClassLoader(ClassLoader cl) {
254 if (mResultExtras != null) {
255 mResultExtras.setClassLoader(cl);
256 }
257 }
Mark Lu33ec1062016-12-05 10:57:55 -0800258
Dianne Hackborne829fef2010-10-26 17:44:01 -0700259 /** @hide */
260 public void sendFinished(IActivityManager am) {
261 synchronized (this) {
262 if (mFinished) {
263 throw new IllegalStateException("Broadcast already finished");
264 }
265 mFinished = true;
Mark Lu33ec1062016-12-05 10:57:55 -0800266
Dianne Hackborne829fef2010-10-26 17:44:01 -0700267 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400268 if (mResultExtras != null) {
269 mResultExtras.setAllowFds(false);
270 }
Dianne Hackborne829fef2010-10-26 17:44:01 -0700271 if (mOrderedHint) {
272 am.finishReceiver(mToken, mResultCode, mResultData, mResultExtras,
riddle_hsu1f5ac4d2015-01-03 15:38:21 +0800273 mAbortBroadcast, mFlags);
Dianne Hackborne829fef2010-10-26 17:44:01 -0700274 } else {
275 // This broadcast was sent to a component; it is not ordered,
276 // but we still need to tell the activity manager we are done.
riddle_hsu1f5ac4d2015-01-03 15:38:21 +0800277 am.finishReceiver(mToken, 0, null, null, false, mFlags);
Dianne Hackborne829fef2010-10-26 17:44:01 -0700278 }
279 } catch (RemoteException ex) {
280 }
281 }
282 }
Dianne Hackborn20e80982012-08-31 19:00:44 -0700283
284 /** @hide */
285 public int getSendingUserId() {
286 return mSendingUser;
287 }
288
Dianne Hackborne829fef2010-10-26 17:44:01 -0700289 void checkSynchronousHint() {
290 // Note that we don't assert when receiving the initial sticky value,
291 // since that may have come from an ordered broadcast. We'll catch
292 // them later when the real broadcast happens again.
293 if (mOrderedHint || mInitialStickyHint) {
294 return;
295 }
296 RuntimeException e = new RuntimeException(
297 "BroadcastReceiver trying to return result during a non-ordered broadcast");
298 e.fillInStackTrace();
299 Log.e("BroadcastReceiver", e.getMessage(), e);
300 }
301 }
Mark Lu33ec1062016-12-05 10:57:55 -0800302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 public BroadcastReceiver() {
304 }
305
306 /**
307 * This method is called when the BroadcastReceiver is receiving an Intent
308 * broadcast. During this time you can use the other methods on
Nick Pellyde88dba2012-03-02 15:25:31 -0800309 * BroadcastReceiver to view/modify the current result values. This method
310 * is always called within the main thread of its process, unless you
311 * explicitly asked for it to be scheduled on a different thread using
312 * {@link android.content.Context#registerReceiver(BroadcastReceiver,
313 * IntentFilter, String, android.os.Handler)}. When it runs on the main
314 * thread you should
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 * never perform long-running operations in it (there is a timeout of
316 * 10 seconds that the system allows before considering the receiver to
317 * be blocked and a candidate to be killed). You cannot launch a popup dialog
318 * in your implementation of onReceive().
Nick Pellyde88dba2012-03-02 15:25:31 -0800319 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 * <p><b>If this BroadcastReceiver was launched through a &lt;receiver&gt; tag,
321 * then the object is no longer alive after returning from this
Mark Lu33ec1062016-12-05 10:57:55 -0800322 * function.</b> This means you should not perform any operations that
323 * return a result to you asynchronously. If you need to perform any follow up
324 * background work, schedule a {@link android.app.job.JobService} with
325 * {@link android.app.job.JobScheduler}.
326 *
327 * If you wish to interact with a service that is already running and previously
328 * bound using {@link android.content.Context#bindService(Intent, ServiceConnection, int) bindService()},
329 * you can use {@link #peekService}.
330 *
Chris Tatea34df8a22009-04-02 23:15:58 -0700331 * <p>The Intent filters used in {@link android.content.Context#registerReceiver}
332 * and in application manifests are <em>not</em> guaranteed to be exclusive. They
333 * are hints to the operating system about how to find suitable recipients. It is
334 * possible for senders to force delivery to specific recipients, bypassing filter
335 * resolution. For this reason, {@link #onReceive(Context, Intent) onReceive()}
336 * implementations should respond only to known actions, ignoring any unexpected
337 * Intents that they may receive.
Mark Lu33ec1062016-12-05 10:57:55 -0800338 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 * @param context The Context in which the receiver is running.
340 * @param intent The Intent being received.
341 */
342 public abstract void onReceive(Context context, Intent intent);
343
344 /**
Dianne Hackborne829fef2010-10-26 17:44:01 -0700345 * This can be called by an application in {@link #onReceive} to allow
346 * it to keep the broadcast active after returning from that function.
347 * This does <em>not</em> change the expectation of being relatively
Dianne Hackbornbfc23312017-05-11 11:53:24 -0700348 * responsive to the broadcast, but does allow
Dianne Hackborne829fef2010-10-26 17:44:01 -0700349 * the implementation to move work related to it over to another thread
350 * to avoid glitching the main UI thread due to disk IO.
Mark Lu33ec1062016-12-05 10:57:55 -0800351 *
Dianne Hackbornbfc23312017-05-11 11:53:24 -0700352 * <p>As a general rule, broadcast receivers are allowed to run for up to 10 seconds
353 * before they system will consider them non-responsive and ANR the app. Since these usually
354 * execute on the app's main thread, they are already bound by the ~5 second time limit
355 * of various operations that can happen there (not to mention just avoiding UI jank), so
Jeff Sharkey67f9d502017-08-05 13:49:13 -0600356 * the receive limit is generally not of concern. However, once you use {@code goAsync}, though
Dianne Hackbornbfc23312017-05-11 11:53:24 -0700357 * able to be off the main thread, the broadcast execution limit still applies, and that
358 * includes the time spent between calling this method and ultimately
359 * {@link PendingResult#finish() PendingResult.finish()}.</p>
360 *
361 * <p>If you are taking advantage of this method to have more time to execute, it is useful
362 * to know that the available time can be longer in certain situations. In particular, if
363 * the broadcast you are receiving is not a foreground broadcast (that is, the sender has not
364 * used {@link Intent#FLAG_RECEIVER_FOREGROUND}), then more time is allowed for the receivers
365 * to run, allowing them to execute for 30 seconds or even a bit more. This is something that
366 * receivers should rarely take advantage of (long work should be punted to another system
367 * facility such as {@link android.app.job.JobScheduler}, {@link android.app.Service}, or
368 * see especially {@link android.support.v4.app.JobIntentService}), but can be useful in
369 * certain rare cases where it is necessary to do some work as soon as the broadcast is
370 * delivered. Keep in mind that the work you do here will block further broadcasts until
371 * it completes, so taking advantage of this at all excessively can be counter-productive
372 * and cause later events to be received more slowly.</p>
373 *
Dianne Hackborne829fef2010-10-26 17:44:01 -0700374 * @return Returns a {@link PendingResult} representing the result of
375 * the active broadcast. The BroadcastRecord itself is no longer active;
376 * all data and other interaction must go through {@link PendingResult}
377 * APIs. The {@link PendingResult#finish PendingResult.finish()} method
378 * must be called once processing of the broadcast is done.
379 */
380 public final PendingResult goAsync() {
381 PendingResult res = mPendingResult;
382 mPendingResult = null;
383 return res;
384 }
Mark Lu33ec1062016-12-05 10:57:55 -0800385
Dianne Hackborne829fef2010-10-26 17:44:01 -0700386 /**
Mark Lu33ec1062016-12-05 10:57:55 -0800387 * Provide a binder to an already-bound service. This method is synchronous
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 * and will not start the target service if it is not present, so it is safe
389 * to call from {@link #onReceive}.
Mark Lu33ec1062016-12-05 10:57:55 -0800390 *
391 * For peekService() to return a non null {@link android.os.IBinder} interface
392 * the service must have published it before. In other words some component
393 * must have called {@link android.content.Context#bindService(Intent, ServiceConnection, int)} on it.
394 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 * @param myContext The Context that had been passed to {@link #onReceive(Context, Intent)}
Mark Lu33ec1062016-12-05 10:57:55 -0800396 * @param service Identifies the already-bound service you wish to use. See
397 * {@link android.content.Context#bindService(Intent, ServiceConnection, int)}
398 * for more information.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 */
400 public IBinder peekService(Context myContext, Intent service) {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800401 IActivityManager am = ActivityManager.getService();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 IBinder binder = null;
403 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700404 service.prepareToLeaveProcess(myContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 binder = am.peekService(service, service.resolveTypeIfNeeded(
Svet Ganov99b60432015-06-27 13:15:22 -0700406 myContext.getContentResolver()), myContext.getOpPackageName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 } catch (RemoteException e) {
408 }
409 return binder;
410 }
411
412 /**
413 * Change the current result code of this broadcast; only works with
414 * broadcasts sent through
415 * {@link Context#sendOrderedBroadcast(Intent, String)
416 * Context.sendOrderedBroadcast}. Often uses the
417 * Activity {@link android.app.Activity#RESULT_CANCELED} and
418 * {@link android.app.Activity#RESULT_OK} constants, though the
419 * actual meaning of this value is ultimately up to the broadcaster.
Mark Lu33ec1062016-12-05 10:57:55 -0800420 *
Dianne Hackborne829fef2010-10-26 17:44:01 -0700421 * <p class="note">This method does not work with non-ordered broadcasts such
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 * as those sent with {@link Context#sendBroadcast(Intent)
Dianne Hackborne829fef2010-10-26 17:44:01 -0700423 * Context.sendBroadcast}</p>
Mark Lu33ec1062016-12-05 10:57:55 -0800424 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 * @param code The new result code.
Mark Lu33ec1062016-12-05 10:57:55 -0800426 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 * @see #setResult(int, String, Bundle)
428 */
429 public final void setResultCode(int code) {
430 checkSynchronousHint();
Dianne Hackborne829fef2010-10-26 17:44:01 -0700431 mPendingResult.mResultCode = code;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 }
433
434 /**
435 * Retrieve the current result code, as set by the previous receiver.
Mark Lu33ec1062016-12-05 10:57:55 -0800436 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 * @return int The current result code.
438 */
439 public final int getResultCode() {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700440 return mPendingResult != null ? mPendingResult.mResultCode : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 }
442
443 /**
444 * Change the current result data of this broadcast; only works with
445 * broadcasts sent through
446 * {@link Context#sendOrderedBroadcast(Intent, String)
447 * Context.sendOrderedBroadcast}. This is an arbitrary
448 * string whose interpretation is up to the broadcaster.
Mark Lu33ec1062016-12-05 10:57:55 -0800449 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 * <p><strong>This method does not work with non-ordered broadcasts such
451 * as those sent with {@link Context#sendBroadcast(Intent)
452 * Context.sendBroadcast}</strong></p>
Mark Lu33ec1062016-12-05 10:57:55 -0800453 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 * @param data The new result data; may be null.
Mark Lu33ec1062016-12-05 10:57:55 -0800455 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 * @see #setResult(int, String, Bundle)
457 */
458 public final void setResultData(String data) {
459 checkSynchronousHint();
Dianne Hackborne829fef2010-10-26 17:44:01 -0700460 mPendingResult.mResultData = data;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 }
462
463 /**
464 * Retrieve the current result data, as set by the previous receiver.
465 * Often this is null.
Mark Lu33ec1062016-12-05 10:57:55 -0800466 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 * @return String The current result data; may be null.
468 */
469 public final String getResultData() {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700470 return mPendingResult != null ? mPendingResult.mResultData : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 }
472
473 /**
474 * Change the current result extras of this broadcast; only works with
475 * broadcasts sent through
476 * {@link Context#sendOrderedBroadcast(Intent, String)
477 * Context.sendOrderedBroadcast}. This is a Bundle
478 * holding arbitrary data, whose interpretation is up to the
479 * broadcaster. Can be set to null. Calling this method completely
480 * replaces the current map (if any).
Mark Lu33ec1062016-12-05 10:57:55 -0800481 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 * <p><strong>This method does not work with non-ordered broadcasts such
483 * as those sent with {@link Context#sendBroadcast(Intent)
484 * Context.sendBroadcast}</strong></p>
Mark Lu33ec1062016-12-05 10:57:55 -0800485 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 * @param extras The new extra data map; may be null.
Mark Lu33ec1062016-12-05 10:57:55 -0800487 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 * @see #setResult(int, String, Bundle)
489 */
490 public final void setResultExtras(Bundle extras) {
491 checkSynchronousHint();
Dianne Hackborne829fef2010-10-26 17:44:01 -0700492 mPendingResult.mResultExtras = extras;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 }
494
495 /**
496 * Retrieve the current result extra data, as set by the previous receiver.
497 * Any changes you make to the returned Map will be propagated to the next
498 * receiver.
Mark Lu33ec1062016-12-05 10:57:55 -0800499 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 * @param makeMap If true then a new empty Map will be made for you if the
501 * current Map is null; if false you should be prepared to
502 * receive a null Map.
Mark Lu33ec1062016-12-05 10:57:55 -0800503 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 * @return Map The current extras map.
505 */
506 public final Bundle getResultExtras(boolean makeMap) {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700507 if (mPendingResult == null) {
508 return null;
509 }
510 Bundle e = mPendingResult.mResultExtras;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 if (!makeMap) return e;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700512 if (e == null) mPendingResult.mResultExtras = e = new Bundle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 return e;
514 }
515
516 /**
517 * Change all of the result data returned from this broadcasts; only works
518 * with broadcasts sent through
519 * {@link Context#sendOrderedBroadcast(Intent, String)
520 * Context.sendOrderedBroadcast}. All current result data is replaced
521 * by the value given to this method.
Mark Lu33ec1062016-12-05 10:57:55 -0800522 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 * <p><strong>This method does not work with non-ordered broadcasts such
524 * as those sent with {@link Context#sendBroadcast(Intent)
525 * Context.sendBroadcast}</strong></p>
Mark Lu33ec1062016-12-05 10:57:55 -0800526 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 * @param code The new result code. Often uses the
528 * Activity {@link android.app.Activity#RESULT_CANCELED} and
529 * {@link android.app.Activity#RESULT_OK} constants, though the
530 * actual meaning of this value is ultimately up to the broadcaster.
531 * @param data The new result data. This is an arbitrary
532 * string whose interpretation is up to the broadcaster; may be null.
533 * @param extras The new extra data map. This is a Bundle
534 * holding arbitrary data, whose interpretation is up to the
535 * broadcaster. Can be set to null. This completely
536 * replaces the current map (if any).
537 */
538 public final void setResult(int code, String data, Bundle extras) {
539 checkSynchronousHint();
Dianne Hackborne829fef2010-10-26 17:44:01 -0700540 mPendingResult.mResultCode = code;
541 mPendingResult.mResultData = data;
542 mPendingResult.mResultExtras = extras;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 }
Mark Lu33ec1062016-12-05 10:57:55 -0800544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 /**
546 * Returns the flag indicating whether or not this receiver should
547 * abort the current broadcast.
Mark Lu33ec1062016-12-05 10:57:55 -0800548 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 * @return True if the broadcast should be aborted.
550 */
551 public final boolean getAbortBroadcast() {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700552 return mPendingResult != null ? mPendingResult.mAbortBroadcast : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 }
554
555 /**
556 * Sets the flag indicating that this receiver should abort the
557 * current broadcast; only works with broadcasts sent through
558 * {@link Context#sendOrderedBroadcast(Intent, String)
559 * Context.sendOrderedBroadcast}. This will prevent
560 * any other broadcast receivers from receiving the broadcast. It will still
Mark Lu33ec1062016-12-05 10:57:55 -0800561 * call {@link #onReceive} of the BroadcastReceiver that the caller of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 * {@link Context#sendOrderedBroadcast(Intent, String)
563 * Context.sendOrderedBroadcast} passed in.
Mark Lu33ec1062016-12-05 10:57:55 -0800564 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 * <p><strong>This method does not work with non-ordered broadcasts such
566 * as those sent with {@link Context#sendBroadcast(Intent)
567 * Context.sendBroadcast}</strong></p>
568 */
569 public final void abortBroadcast() {
570 checkSynchronousHint();
Dianne Hackborne829fef2010-10-26 17:44:01 -0700571 mPendingResult.mAbortBroadcast = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 }
Mark Lu33ec1062016-12-05 10:57:55 -0800573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 /**
575 * Clears the flag indicating that this receiver should abort the current
576 * broadcast.
577 */
578 public final void clearAbortBroadcast() {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700579 if (mPendingResult != null) {
580 mPendingResult.mAbortBroadcast = false;
581 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 }
Mark Lu33ec1062016-12-05 10:57:55 -0800583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 /**
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700585 * Returns true if the receiver is currently processing an ordered
586 * broadcast.
587 */
588 public final boolean isOrderedBroadcast() {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700589 return mPendingResult != null ? mPendingResult.mOrderedHint : false;
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700590 }
Mark Lu33ec1062016-12-05 10:57:55 -0800591
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700592 /**
593 * Returns true if the receiver is currently processing the initial
594 * value of a sticky broadcast -- that is, the value that was last
595 * broadcast and is currently held in the sticky cache, so this is
596 * not directly the result of a broadcast right now.
597 */
598 public final boolean isInitialStickyBroadcast() {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700599 return mPendingResult != null ? mPendingResult.mInitialStickyHint : false;
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700600 }
Mark Lu33ec1062016-12-05 10:57:55 -0800601
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700602 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 * For internal use, sets the hint about whether this BroadcastReceiver is
604 * running in ordered mode.
605 */
606 public final void setOrderedHint(boolean isOrdered) {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700607 // Accidentally left in the SDK.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 }
Mark Lu33ec1062016-12-05 10:57:55 -0800609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 /**
Dianne Hackborne829fef2010-10-26 17:44:01 -0700611 * For internal use to set the result data that is active. @hide
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700612 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100613 @UnsupportedAppUsage
Dianne Hackborne829fef2010-10-26 17:44:01 -0700614 public final void setPendingResult(PendingResult result) {
615 mPendingResult = result;
616 }
Mark Lu33ec1062016-12-05 10:57:55 -0800617
Dianne Hackborne829fef2010-10-26 17:44:01 -0700618 /**
619 * For internal use to set the result data that is active. @hide
620 */
Mathew Inwood5c0d3542018-08-14 13:54:31 +0100621 @UnsupportedAppUsage
Dianne Hackborne829fef2010-10-26 17:44:01 -0700622 public final PendingResult getPendingResult() {
623 return mPendingResult;
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700624 }
Mark Lu33ec1062016-12-05 10:57:55 -0800625
Dianne Hackborn20e80982012-08-31 19:00:44 -0700626 /** @hide */
627 public int getSendingUserId() {
628 return mPendingResult.mSendingUser;
629 }
630
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700631 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 * Control inclusion of debugging help for mismatched
Jeff Smitha45746e2012-07-19 14:19:24 -0500633 * calls to {@link Context#registerReceiver(BroadcastReceiver, IntentFilter)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 * Context.registerReceiver()}.
635 * If called with true, before given to registerReceiver(), then the
636 * callstack of the following {@link Context#unregisterReceiver(BroadcastReceiver)
637 * Context.unregisterReceiver()} call is retained, to be printed if a later
638 * incorrect unregister call is made. Note that doing this requires retaining
639 * information about the BroadcastReceiver for the lifetime of the app,
640 * resulting in a leak -- this should only be used for debugging.
641 */
642 public final void setDebugUnregister(boolean debug) {
643 mDebugUnregister = debug;
644 }
Mark Lu33ec1062016-12-05 10:57:55 -0800645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 /**
647 * Return the last value given to {@link #setDebugUnregister}.
648 */
649 public final boolean getDebugUnregister() {
650 return mDebugUnregister;
651 }
Mark Lu33ec1062016-12-05 10:57:55 -0800652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 void checkSynchronousHint() {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700654 if (mPendingResult == null) {
655 throw new IllegalStateException("Call while result is not pending");
656 }
Mark Lu33ec1062016-12-05 10:57:55 -0800657
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700658 // Note that we don't assert when receiving the initial sticky value,
659 // since that may have come from an ordered broadcast. We'll catch
660 // them later when the real broadcast happens again.
Dianne Hackborne829fef2010-10-26 17:44:01 -0700661 if (mPendingResult.mOrderedHint || mPendingResult.mInitialStickyHint) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 return;
663 }
664 RuntimeException e = new RuntimeException(
665 "BroadcastReceiver trying to return result during a non-ordered broadcast");
666 e.fillInStackTrace();
667 Log.e("BroadcastReceiver", e.getMessage(), e);
668 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669}
670