blob: 6055befa15d50b0d54ceca7e76fd45ff715a8cc1 [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.os;
18
Andrei Onea24ec3212019-03-15 17:35:05 +000019import android.annotation.UnsupportedAppUsage;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070020import android.util.TimeUtils;
Netta P958d0a52017-02-07 11:20:55 -080021import android.util.proto.ProtoOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022
Marcin Oczeretko44272722018-09-19 11:01:32 +010023import com.android.internal.annotations.VisibleForTesting;
24
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025/**
Netta P958d0a52017-02-07 11:20:55 -080026 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027 * Defines a message containing a description and arbitrary data object that can be
28 * sent to a {@link Handler}. This object contains two extra int fields and an
Netta P958d0a52017-02-07 11:20:55 -080029 * extra object field that allow you to not do allocations in many cases.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030 *
31 * <p class="note">While the constructor of Message is public, the best way to get
32 * one of these is to call {@link #obtain Message.obtain()} or one of the
33 * {@link Handler#obtainMessage Handler.obtainMessage()} methods, which will pull
34 * them from a pool of recycled objects.</p>
35 */
36public final class Message implements Parcelable {
37 /**
Netta P958d0a52017-02-07 11:20:55 -080038 * User-defined message code so that the recipient can identify
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039 * what this message is about. Each {@link Handler} has its own name-space
40 * for message codes, so you do not need to worry about yours conflicting
41 * with other handlers.
42 */
43 public int what;
44
Dianne Hackborn75288fa2010-02-16 18:01:18 -080045 /**
46 * arg1 and arg2 are lower-cost alternatives to using
47 * {@link #setData(Bundle) setData()} if you only need to store a
48 * few integer values.
49 */
Netta P958d0a52017-02-07 11:20:55 -080050 public int arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
Dianne Hackborn75288fa2010-02-16 18:01:18 -080052 /**
53 * arg1 and arg2 are lower-cost alternatives to using
54 * {@link #setData(Bundle) setData()} if you only need to store a
55 * few integer values.
56 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 public int arg2;
58
Dianne Hackborn75288fa2010-02-16 18:01:18 -080059 /**
60 * An arbitrary object to send to the recipient. When using
61 * {@link Messenger} to send the message across processes this can only
62 * be non-null if it contains a Parcelable of a framework class (not one
63 * implemented by the application). For other data transfer use
64 * {@link #setData}.
Netta P958d0a52017-02-07 11:20:55 -080065 *
Dianne Hackborn75288fa2010-02-16 18:01:18 -080066 * <p>Note that Parcelable objects here are not supported prior to
67 * the {@link android.os.Build.VERSION_CODES#FROYO} release.
68 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 public Object obj;
70
Dianne Hackborn75288fa2010-02-16 18:01:18 -080071 /**
72 * Optional Messenger where replies to this message can be sent. The
73 * semantics of exactly how this is used are up to the sender and
74 * receiver.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 */
76 public Messenger replyTo;
Wink Savillea334e7c2010-08-24 10:56:30 -070077
Dianne Hackborncb3ed1d2014-06-27 18:37:06 -070078 /**
Marcin Oczeretkoec758722018-09-12 12:53:47 +010079 * Indicates that the uid is not set;
80 *
81 * @hide Only for use within the system server.
82 */
83 public static final int UID_NONE = -1;
84
85 /**
Dianne Hackborncb3ed1d2014-06-27 18:37:06 -070086 * Optional field indicating the uid that sent the message. This is
87 * only valid for messages posted by a {@link Messenger}; otherwise,
88 * it will be -1.
89 */
Marcin Oczeretkoec758722018-09-12 12:53:47 +010090 public int sendingUid = UID_NONE;
91
92 /**
93 * Optional field indicating the uid that caused this message to be enqueued.
94 *
95 * @hide Only for use within the system server.
96 */
97 public int workSourceUid = UID_NONE;
Dianne Hackborncb3ed1d2014-06-27 18:37:06 -070098
Jeff Brown9867ed72014-02-28 14:00:57 -080099 /** If set message is in use.
100 * This flag is set when the message is enqueued and remains set while it
101 * is delivered and afterwards when it is recycled. The flag is only cleared
102 * when a new message is created or obtained since that is the only time that
103 * applications are allowed to modify the contents of the message.
104 *
105 * It is an error to attempt to enqueue or recycle a message that is already in use.
106 */
Jeff Browne799cb72012-02-14 11:53:33 -0800107 /*package*/ static final int FLAG_IN_USE = 1 << 0;
Wink Savillea334e7c2010-08-24 10:56:30 -0700108
Jeff Browne799cb72012-02-14 11:53:33 -0800109 /** If set message is asynchronous */
110 /*package*/ static final int FLAG_ASYNCHRONOUS = 1 << 1;
Wink Savillea334e7c2010-08-24 10:56:30 -0700111
112 /** Flags to clear in the copyFrom method */
Jeff Browne799cb72012-02-14 11:53:33 -0800113 /*package*/ static final int FLAGS_TO_CLEAR_ON_COPY_FROM = FLAG_IN_USE;
Wink Savillea334e7c2010-08-24 10:56:30 -0700114
Andrei Onea24ec3212019-03-15 17:35:05 +0000115 @UnsupportedAppUsage
Wink Savillea334e7c2010-08-24 10:56:30 -0700116 /*package*/ int flags;
117
Marcin Oczeretko44272722018-09-19 11:01:32 +0100118 /**
119 * The targeted delivery time of this message. The time-base is
120 * {@link SystemClock#uptimeMillis}.
121 * @hide Only for use within the tests.
122 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000123 @UnsupportedAppUsage
Marcin Oczeretko44272722018-09-19 11:01:32 +0100124 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
125 public long when;
Netta P958d0a52017-02-07 11:20:55 -0800126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 /*package*/ Bundle data;
Netta P958d0a52017-02-07 11:20:55 -0800128
Andrei Onea24ec3212019-03-15 17:35:05 +0000129 @UnsupportedAppUsage
Jeff Brown9867ed72014-02-28 14:00:57 -0800130 /*package*/ Handler target;
Netta P958d0a52017-02-07 11:20:55 -0800131
Andrei Onea24ec3212019-03-15 17:35:05 +0000132 @UnsupportedAppUsage
Jeff Brown9867ed72014-02-28 14:00:57 -0800133 /*package*/ Runnable callback;
Netta P958d0a52017-02-07 11:20:55 -0800134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 // sometimes we store linked lists of these things
Andrei Onea24ec3212019-03-15 17:35:05 +0000136 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 /*package*/ Message next;
138
Eugene Susla2f5ee712017-06-23 17:25:24 -0700139
140 /** @hide */
141 public static final Object sPoolSync = new Object();
Brad Fitzpatrick2405c2782011-01-25 14:48:21 -0800142 private static Message sPool;
143 private static int sPoolSize = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144
Romain Guya2ad6562012-05-06 16:10:34 -0700145 private static final int MAX_POOL_SIZE = 50;
146
Dianne Hackborn7895bc22014-09-05 15:09:03 -0700147 private static boolean gCheckRecycle = true;
148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 /**
150 * Return a new Message instance from the global pool. Allows us to
151 * avoid allocating new objects in many cases.
152 */
153 public static Message obtain() {
Brad Fitzpatrick2405c2782011-01-25 14:48:21 -0800154 synchronized (sPoolSync) {
155 if (sPool != null) {
156 Message m = sPool;
157 sPool = m.next;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 m.next = null;
Jeff Brown9867ed72014-02-28 14:00:57 -0800159 m.flags = 0; // clear in-use flag
Brad Fitzpatrick2405c2782011-01-25 14:48:21 -0800160 sPoolSize--;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 return m;
162 }
163 }
164 return new Message();
165 }
166
167 /**
168 * Same as {@link #obtain()}, but copies the values of an existing
169 * message (including its target) into the new one.
170 * @param orig Original message to copy.
171 * @return A Message object from the global pool.
172 */
173 public static Message obtain(Message orig) {
174 Message m = obtain();
175 m.what = orig.what;
176 m.arg1 = orig.arg1;
177 m.arg2 = orig.arg2;
178 m.obj = orig.obj;
179 m.replyTo = orig.replyTo;
Dianne Hackborncb3ed1d2014-06-27 18:37:06 -0700180 m.sendingUid = orig.sendingUid;
Marcin Oczeretkoec758722018-09-12 12:53:47 +0100181 m.workSourceUid = orig.workSourceUid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 if (orig.data != null) {
183 m.data = new Bundle(orig.data);
184 }
185 m.target = orig.target;
186 m.callback = orig.callback;
187
188 return m;
189 }
190
191 /**
192 * Same as {@link #obtain()}, but sets the value for the <em>target</em> member on the Message returned.
193 * @param h Handler to assign to the returned Message object's <em>target</em> member.
194 * @return A Message object from the global pool.
195 */
196 public static Message obtain(Handler h) {
197 Message m = obtain();
198 m.target = h;
199
200 return m;
201 }
202
203 /**
204 * Same as {@link #obtain(Handler)}, but assigns a callback Runnable on
205 * the Message that is returned.
206 * @param h Handler to assign to the returned Message object's <em>target</em> member.
207 * @param callback Runnable that will execute when the message is handled.
208 * @return A Message object from the global pool.
209 */
210 public static Message obtain(Handler h, Runnable callback) {
211 Message m = obtain();
212 m.target = h;
213 m.callback = callback;
214
215 return m;
216 }
217
218 /**
219 * Same as {@link #obtain()}, but sets the values for both <em>target</em> and
220 * <em>what</em> members on the Message.
221 * @param h Value to assign to the <em>target</em> member.
222 * @param what Value to assign to the <em>what</em> member.
223 * @return A Message object from the global pool.
224 */
225 public static Message obtain(Handler h, int what) {
226 Message m = obtain();
227 m.target = h;
228 m.what = what;
229
230 return m;
231 }
232
233 /**
234 * Same as {@link #obtain()}, but sets the values of the <em>target</em>, <em>what</em>, and <em>obj</em>
235 * members.
236 * @param h The <em>target</em> value to set.
237 * @param what The <em>what</em> value to set.
238 * @param obj The <em>object</em> method to set.
239 * @return A Message object from the global pool.
240 */
241 public static Message obtain(Handler h, int what, Object obj) {
242 Message m = obtain();
243 m.target = h;
244 m.what = what;
245 m.obj = obj;
246
247 return m;
248 }
249
250 /**
Netta P958d0a52017-02-07 11:20:55 -0800251 * Same as {@link #obtain()}, but sets the values of the <em>target</em>, <em>what</em>,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 * <em>arg1</em>, and <em>arg2</em> members.
Netta P958d0a52017-02-07 11:20:55 -0800253 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 * @param h The <em>target</em> value to set.
255 * @param what The <em>what</em> value to set.
256 * @param arg1 The <em>arg1</em> value to set.
257 * @param arg2 The <em>arg2</em> value to set.
258 * @return A Message object from the global pool.
259 */
260 public static Message obtain(Handler h, int what, int arg1, int arg2) {
261 Message m = obtain();
262 m.target = h;
263 m.what = what;
264 m.arg1 = arg1;
265 m.arg2 = arg2;
266
267 return m;
268 }
269
270 /**
Netta P958d0a52017-02-07 11:20:55 -0800271 * Same as {@link #obtain()}, but sets the values of the <em>target</em>, <em>what</em>,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 * <em>arg1</em>, <em>arg2</em>, and <em>obj</em> members.
Netta P958d0a52017-02-07 11:20:55 -0800273 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 * @param h The <em>target</em> value to set.
275 * @param what The <em>what</em> value to set.
276 * @param arg1 The <em>arg1</em> value to set.
277 * @param arg2 The <em>arg2</em> value to set.
278 * @param obj The <em>obj</em> value to set.
279 * @return A Message object from the global pool.
280 */
Netta P958d0a52017-02-07 11:20:55 -0800281 public static Message obtain(Handler h, int what,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 int arg1, int arg2, Object obj) {
283 Message m = obtain();
284 m.target = h;
285 m.what = what;
286 m.arg1 = arg1;
287 m.arg2 = arg2;
288 m.obj = obj;
289
290 return m;
291 }
292
Dianne Hackborn7895bc22014-09-05 15:09:03 -0700293 /** @hide */
294 public static void updateCheckRecycle(int targetSdkVersion) {
Dianne Hackborn955d8d62014-10-07 20:17:19 -0700295 if (targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) {
Dianne Hackborn7895bc22014-09-05 15:09:03 -0700296 gCheckRecycle = false;
297 }
298 }
299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 /**
Jeff Brown9867ed72014-02-28 14:00:57 -0800301 * Return a Message instance to the global pool.
302 * <p>
303 * You MUST NOT touch the Message after calling this function because it has
304 * effectively been freed. It is an error to recycle a message that is currently
305 * enqueued or that is in the process of being delivered to a Handler.
306 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 */
308 public void recycle() {
Jeff Brown9867ed72014-02-28 14:00:57 -0800309 if (isInUse()) {
Dianne Hackborn7895bc22014-09-05 15:09:03 -0700310 if (gCheckRecycle) {
311 throw new IllegalStateException("This message cannot be recycled because it "
312 + "is still in use.");
313 }
314 return;
Jeff Brown9867ed72014-02-28 14:00:57 -0800315 }
316 recycleUnchecked();
317 }
318
319 /**
320 * Recycles a Message that may be in-use.
321 * Used internally by the MessageQueue and Looper when disposing of queued Messages.
322 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000323 @UnsupportedAppUsage
Jeff Brown9867ed72014-02-28 14:00:57 -0800324 void recycleUnchecked() {
325 // Mark the message as in use while it remains in the recycled object pool.
326 // Clear out all other details.
327 flags = FLAG_IN_USE;
328 what = 0;
329 arg1 = 0;
330 arg2 = 0;
331 obj = null;
332 replyTo = null;
Marcin Oczeretkoec758722018-09-12 12:53:47 +0100333 sendingUid = UID_NONE;
334 workSourceUid = UID_NONE;
Jeff Brown9867ed72014-02-28 14:00:57 -0800335 when = 0;
336 target = null;
337 callback = null;
338 data = null;
Jeff Brownfc9ff4c2011-06-10 17:56:08 -0700339
Brad Fitzpatrick2405c2782011-01-25 14:48:21 -0800340 synchronized (sPoolSync) {
341 if (sPoolSize < MAX_POOL_SIZE) {
Brad Fitzpatrick2405c2782011-01-25 14:48:21 -0800342 next = sPool;
343 sPool = this;
344 sPoolSize++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 }
346 }
347 }
348
349 /**
350 * Make this message like o. Performs a shallow copy of the data field.
351 * Does not copy the linked list fields, nor the timestamp or
352 * target/callback of the original message.
353 */
354 public void copyFrom(Message o) {
Wink Savillea334e7c2010-08-24 10:56:30 -0700355 this.flags = o.flags & ~FLAGS_TO_CLEAR_ON_COPY_FROM;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 this.what = o.what;
357 this.arg1 = o.arg1;
358 this.arg2 = o.arg2;
359 this.obj = o.obj;
360 this.replyTo = o.replyTo;
Dianne Hackborncb3ed1d2014-06-27 18:37:06 -0700361 this.sendingUid = o.sendingUid;
Marcin Oczeretkoec758722018-09-12 12:53:47 +0100362 this.workSourceUid = o.workSourceUid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363
364 if (o.data != null) {
365 this.data = (Bundle) o.data.clone();
366 } else {
367 this.data = null;
368 }
369 }
370
371 /**
372 * Return the targeted delivery time of this message, in milliseconds.
373 */
374 public long getWhen() {
375 return when;
376 }
Netta P958d0a52017-02-07 11:20:55 -0800377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 public void setTarget(Handler target) {
379 this.target = target;
380 }
381
382 /**
Joshua Baxter01c80132018-06-04 14:27:59 -0700383 * Retrieve the {@link android.os.Handler Handler} implementation that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 * will receive this message. The object must implement
385 * {@link android.os.Handler#handleMessage(android.os.Message)
386 * Handler.handleMessage()}. Each Handler has its own name-space for
387 * message codes, so you do not need to
388 * worry about yours conflicting with other handlers.
389 */
390 public Handler getTarget() {
391 return target;
392 }
393
394 /**
395 * Retrieve callback object that will execute when this message is handled.
396 * This object must implement Runnable. This is called by
397 * the <em>target</em> {@link Handler} that is receiving this Message to
398 * dispatch it. If
399 * not set, the message will be dispatched to the receiving Handler's
Tor Norbyef8dc2d22018-10-03 09:28:21 +0200400 * {@link Handler#handleMessage(Message)}.
Dianne Hackborn75288fa2010-02-16 18:01:18 -0800401 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 public Runnable getCallback() {
403 return callback;
404 }
Netta P958d0a52017-02-07 11:20:55 -0800405
Eugene Susla2f5ee712017-06-23 17:25:24 -0700406 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000407 @UnsupportedAppUsage
Eugene Susla2f5ee712017-06-23 17:25:24 -0700408 public Message setCallback(Runnable r) {
409 callback = r;
410 return this;
411 }
412
Netta P958d0a52017-02-07 11:20:55 -0800413 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 * Obtains a Bundle of arbitrary data associated with this
Dianne Hackborn75288fa2010-02-16 18:01:18 -0800415 * event, lazily creating it if necessary. Set this value by calling
416 * {@link #setData(Bundle)}. Note that when transferring data across
417 * processes via {@link Messenger}, you will need to set your ClassLoader
418 * on the Bundle via {@link Bundle#setClassLoader(ClassLoader)
419 * Bundle.setClassLoader()} so that it can instantiate your objects when
420 * you retrieve them.
421 * @see #peekData()
422 * @see #setData(Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 */
424 public Bundle getData() {
425 if (data == null) {
426 data = new Bundle();
427 }
Netta P958d0a52017-02-07 11:20:55 -0800428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 return data;
430 }
431
Netta P958d0a52017-02-07 11:20:55 -0800432 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 * Like getData(), but does not lazily create the Bundle. A null
Dianne Hackborn75288fa2010-02-16 18:01:18 -0800434 * is returned if the Bundle does not already exist. See
435 * {@link #getData} for further information on this.
436 * @see #getData()
437 * @see #setData(Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 */
439 public Bundle peekData() {
440 return data;
441 }
442
Dianne Hackborn75288fa2010-02-16 18:01:18 -0800443 /**
Denver Coneybeared0eb5532014-06-18 15:45:14 -0400444 * Sets a Bundle of arbitrary data values. Use arg1 and arg2 members
Dianne Hackborn75288fa2010-02-16 18:01:18 -0800445 * as a lower cost way to send a few simple integer values, if you can.
Netta P958d0a52017-02-07 11:20:55 -0800446 * @see #getData()
Dianne Hackborn75288fa2010-02-16 18:01:18 -0800447 * @see #peekData()
448 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 public void setData(Bundle data) {
450 this.data = data;
451 }
452
453 /**
Eugene Susla2f5ee712017-06-23 17:25:24 -0700454 * Chainable setter for {@link #what}
455 *
456 * @hide
457 */
458 public Message setWhat(int what) {
459 this.what = what;
460 return this;
461 }
462
463 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 * Sends this Message to the Handler specified by {@link #getTarget}.
465 * Throws a null pointer exception if this field has not been set.
466 */
467 public void sendToTarget() {
468 target.sendMessage(this);
469 }
470
Jeff Browne799cb72012-02-14 11:53:33 -0800471 /**
Jeff Brown9840c072014-11-11 20:21:21 -0800472 * Returns true if the message is asynchronous, meaning that it is not
473 * subject to {@link Looper} synchronization barriers.
Jeff Browne799cb72012-02-14 11:53:33 -0800474 *
475 * @return True if the message is asynchronous.
476 *
477 * @see #setAsynchronous(boolean)
Jeff Browne799cb72012-02-14 11:53:33 -0800478 */
479 public boolean isAsynchronous() {
480 return (flags & FLAG_ASYNCHRONOUS) != 0;
481 }
482
483 /**
Jeff Brown9840c072014-11-11 20:21:21 -0800484 * Sets whether the message is asynchronous, meaning that it is not
485 * subject to {@link Looper} synchronization barriers.
486 * <p>
487 * Certain operations, such as view invalidation, may introduce synchronization
488 * barriers into the {@link Looper}'s message queue to prevent subsequent messages
489 * from being delivered until some condition is met. In the case of view invalidation,
490 * messages which are posted after a call to {@link android.view.View#invalidate}
491 * are suspended by means of a synchronization barrier until the next frame is
492 * ready to be drawn. The synchronization barrier ensures that the invalidation
493 * request is completely handled before resuming.
494 * </p><p>
495 * Asynchronous messages are exempt from synchronization barriers. They typically
496 * represent interrupts, input events, and other signals that must be handled independently
497 * even while other work has been suspended.
498 * </p><p>
499 * Note that asynchronous messages may be delivered out of order with respect to
500 * synchronous messages although they are always delivered in order among themselves.
501 * If the relative order of these messages matters then they probably should not be
502 * asynchronous in the first place. Use with caution.
503 * </p>
Jeff Browne799cb72012-02-14 11:53:33 -0800504 *
505 * @param async True if the message is asynchronous.
506 *
507 * @see #isAsynchronous()
Jeff Browne799cb72012-02-14 11:53:33 -0800508 */
509 public void setAsynchronous(boolean async) {
510 if (async) {
511 flags |= FLAG_ASYNCHRONOUS;
512 } else {
513 flags &= ~FLAG_ASYNCHRONOUS;
514 }
515 }
516
Wink Savillea334e7c2010-08-24 10:56:30 -0700517 /*package*/ boolean isInUse() {
518 return ((flags & FLAG_IN_USE) == FLAG_IN_USE);
519 }
520
Andrei Onea24ec3212019-03-15 17:35:05 +0000521 @UnsupportedAppUsage
Wink Savillea334e7c2010-08-24 10:56:30 -0700522 /*package*/ void markInUse() {
523 flags |= FLAG_IN_USE;
524 }
525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 /** Constructor (but the preferred way to get a Message is to call {@link #obtain() Message.obtain()}).
527 */
528 public Message() {
529 }
530
Jeff Brown5182c782013-10-15 20:31:52 -0700531 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 public String toString() {
Dianne Hackborn1ebccf52010-08-15 13:04:34 -0700533 return toString(SystemClock.uptimeMillis());
534 }
535
Andrei Onea24ec3212019-03-15 17:35:05 +0000536 @UnsupportedAppUsage
Dianne Hackborn1ebccf52010-08-15 13:04:34 -0700537 String toString(long now) {
Jeff Brown5182c782013-10-15 20:31:52 -0700538 StringBuilder b = new StringBuilder();
539 b.append("{ when=");
540 TimeUtils.formatDuration(when - now, b);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541
Jeff Brown5182c782013-10-15 20:31:52 -0700542 if (target != null) {
543 if (callback != null) {
544 b.append(" callback=");
545 b.append(callback.getClass().getName());
546 } else {
547 b.append(" what=");
548 b.append(what);
549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550
Jeff Brown5182c782013-10-15 20:31:52 -0700551 if (arg1 != 0) {
552 b.append(" arg1=");
553 b.append(arg1);
554 }
555
556 if (arg2 != 0) {
557 b.append(" arg2=");
558 b.append(arg2);
559 }
560
561 if (obj != null) {
562 b.append(" obj=");
563 b.append(obj);
564 }
565
566 b.append(" target=");
567 b.append(target.getClass().getName());
568 } else {
569 b.append(" barrier=");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 b.append(arg1);
571 }
572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 b.append(" }");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 return b.toString();
575 }
576
Netta P958d0a52017-02-07 11:20:55 -0800577 void writeToProto(ProtoOutputStream proto, long fieldId) {
578 final long messageToken = proto.start(fieldId);
579 proto.write(MessageProto.WHEN, when);
580
581 if (target != null) {
582 if (callback != null) {
583 proto.write(MessageProto.CALLBACK, callback.getClass().getName());
584 } else {
585 proto.write(MessageProto.WHAT, what);
586 }
587
588 if (arg1 != 0) {
589 proto.write(MessageProto.ARG1, arg1);
590 }
591
592 if (arg2 != 0) {
593 proto.write(MessageProto.ARG2, arg2);
594 }
595
596 if (obj != null) {
597 proto.write(MessageProto.OBJ, obj.toString());
598 }
599
600 proto.write(MessageProto.TARGET, target.getClass().getName());
601 } else {
602 proto.write(MessageProto.BARRIER, arg1);
603 }
604
605 proto.end(messageToken);
606 }
607
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700608 public static final @android.annotation.NonNull Parcelable.Creator<Message> CREATOR
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 = new Parcelable.Creator<Message>() {
610 public Message createFromParcel(Parcel source) {
611 Message msg = Message.obtain();
612 msg.readFromParcel(source);
613 return msg;
614 }
Netta P958d0a52017-02-07 11:20:55 -0800615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 public Message[] newArray(int size) {
617 return new Message[size];
618 }
619 };
Netta P958d0a52017-02-07 11:20:55 -0800620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 public int describeContents() {
622 return 0;
623 }
624
625 public void writeToParcel(Parcel dest, int flags) {
Dianne Hackborn75288fa2010-02-16 18:01:18 -0800626 if (callback != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 throw new RuntimeException(
Dianne Hackborn75288fa2010-02-16 18:01:18 -0800628 "Can't marshal callbacks across processes.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 }
630 dest.writeInt(what);
631 dest.writeInt(arg1);
632 dest.writeInt(arg2);
Dianne Hackborn75288fa2010-02-16 18:01:18 -0800633 if (obj != null) {
634 try {
635 Parcelable p = (Parcelable)obj;
636 dest.writeInt(1);
637 dest.writeParcelable(p, flags);
638 } catch (ClassCastException e) {
639 throw new RuntimeException(
640 "Can't marshal non-Parcelable objects across processes.");
641 }
642 } else {
643 dest.writeInt(0);
644 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 dest.writeLong(when);
646 dest.writeBundle(data);
647 Messenger.writeMessengerOrNullToParcel(replyTo, dest);
Dianne Hackborncb3ed1d2014-06-27 18:37:06 -0700648 dest.writeInt(sendingUid);
Marcin Oczeretkoec758722018-09-12 12:53:47 +0100649 dest.writeInt(workSourceUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 }
651
Romain Guy8f3b8e32012-03-27 16:33:45 -0700652 private void readFromParcel(Parcel source) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 what = source.readInt();
654 arg1 = source.readInt();
655 arg2 = source.readInt();
Dianne Hackborn75288fa2010-02-16 18:01:18 -0800656 if (source.readInt() != 0) {
657 obj = source.readParcelable(getClass().getClassLoader());
658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 when = source.readLong();
660 data = source.readBundle();
661 replyTo = Messenger.readMessengerOrNullFromParcel(source);
Dianne Hackborncb3ed1d2014-06-27 18:37:06 -0700662 sendingUid = source.readInt();
Marcin Oczeretkoec758722018-09-12 12:53:47 +0100663 workSourceUid = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 }
665}