blob: 317579e78ba87d22058a3fc0649edcfec0e79b51 [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
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070019import android.util.TimeUtils;
Netta P958d0a52017-02-07 11:20:55 -080020import android.util.proto.ProtoOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021
Marcin Oczeretko44272722018-09-19 11:01:32 +010022import com.android.internal.annotations.VisibleForTesting;
23
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024/**
Netta P958d0a52017-02-07 11:20:55 -080025 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026 * Defines a message containing a description and arbitrary data object that can be
27 * sent to a {@link Handler}. This object contains two extra int fields and an
Netta P958d0a52017-02-07 11:20:55 -080028 * extra object field that allow you to not do allocations in many cases.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029 *
30 * <p class="note">While the constructor of Message is public, the best way to get
31 * one of these is to call {@link #obtain Message.obtain()} or one of the
32 * {@link Handler#obtainMessage Handler.obtainMessage()} methods, which will pull
33 * them from a pool of recycled objects.</p>
34 */
35public final class Message implements Parcelable {
36 /**
Netta P958d0a52017-02-07 11:20:55 -080037 * User-defined message code so that the recipient can identify
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 * what this message is about. Each {@link Handler} has its own name-space
39 * for message codes, so you do not need to worry about yours conflicting
40 * with other handlers.
41 */
42 public int what;
43
Dianne Hackborn75288fa2010-02-16 18:01:18 -080044 /**
45 * arg1 and arg2 are lower-cost alternatives to using
46 * {@link #setData(Bundle) setData()} if you only need to store a
47 * few integer values.
48 */
Netta P958d0a52017-02-07 11:20:55 -080049 public int arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050
Dianne Hackborn75288fa2010-02-16 18:01:18 -080051 /**
52 * arg1 and arg2 are lower-cost alternatives to using
53 * {@link #setData(Bundle) setData()} if you only need to store a
54 * few integer values.
55 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 public int arg2;
57
Dianne Hackborn75288fa2010-02-16 18:01:18 -080058 /**
59 * An arbitrary object to send to the recipient. When using
60 * {@link Messenger} to send the message across processes this can only
61 * be non-null if it contains a Parcelable of a framework class (not one
62 * implemented by the application). For other data transfer use
63 * {@link #setData}.
Netta P958d0a52017-02-07 11:20:55 -080064 *
Dianne Hackborn75288fa2010-02-16 18:01:18 -080065 * <p>Note that Parcelable objects here are not supported prior to
66 * the {@link android.os.Build.VERSION_CODES#FROYO} release.
67 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 public Object obj;
69
Dianne Hackborn75288fa2010-02-16 18:01:18 -080070 /**
71 * Optional Messenger where replies to this message can be sent. The
72 * semantics of exactly how this is used are up to the sender and
73 * receiver.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 */
75 public Messenger replyTo;
Wink Savillea334e7c2010-08-24 10:56:30 -070076
Dianne Hackborncb3ed1d2014-06-27 18:37:06 -070077 /**
Marcin Oczeretkoec758722018-09-12 12:53:47 +010078 * Indicates that the uid is not set;
79 *
80 * @hide Only for use within the system server.
81 */
82 public static final int UID_NONE = -1;
83
84 /**
Dianne Hackborncb3ed1d2014-06-27 18:37:06 -070085 * Optional field indicating the uid that sent the message. This is
86 * only valid for messages posted by a {@link Messenger}; otherwise,
87 * it will be -1.
88 */
Marcin Oczeretkoec758722018-09-12 12:53:47 +010089 public int sendingUid = UID_NONE;
90
91 /**
92 * Optional field indicating the uid that caused this message to be enqueued.
93 *
94 * @hide Only for use within the system server.
95 */
96 public int workSourceUid = UID_NONE;
Dianne Hackborncb3ed1d2014-06-27 18:37:06 -070097
Jeff Brown9867ed72014-02-28 14:00:57 -080098 /** If set message is in use.
99 * This flag is set when the message is enqueued and remains set while it
100 * is delivered and afterwards when it is recycled. The flag is only cleared
101 * when a new message is created or obtained since that is the only time that
102 * applications are allowed to modify the contents of the message.
103 *
104 * It is an error to attempt to enqueue or recycle a message that is already in use.
105 */
Jeff Browne799cb72012-02-14 11:53:33 -0800106 /*package*/ static final int FLAG_IN_USE = 1 << 0;
Wink Savillea334e7c2010-08-24 10:56:30 -0700107
Jeff Browne799cb72012-02-14 11:53:33 -0800108 /** If set message is asynchronous */
109 /*package*/ static final int FLAG_ASYNCHRONOUS = 1 << 1;
Wink Savillea334e7c2010-08-24 10:56:30 -0700110
111 /** Flags to clear in the copyFrom method */
Jeff Browne799cb72012-02-14 11:53:33 -0800112 /*package*/ static final int FLAGS_TO_CLEAR_ON_COPY_FROM = FLAG_IN_USE;
Wink Savillea334e7c2010-08-24 10:56:30 -0700113
114 /*package*/ int flags;
115
Marcin Oczeretko44272722018-09-19 11:01:32 +0100116 /**
117 * The targeted delivery time of this message. The time-base is
118 * {@link SystemClock#uptimeMillis}.
119 * @hide Only for use within the tests.
120 */
121 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
122 public long when;
Netta P958d0a52017-02-07 11:20:55 -0800123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 /*package*/ Bundle data;
Netta P958d0a52017-02-07 11:20:55 -0800125
Jeff Brown9867ed72014-02-28 14:00:57 -0800126 /*package*/ Handler target;
Netta P958d0a52017-02-07 11:20:55 -0800127
Jeff Brown9867ed72014-02-28 14:00:57 -0800128 /*package*/ Runnable callback;
Netta P958d0a52017-02-07 11:20:55 -0800129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 // sometimes we store linked lists of these things
131 /*package*/ Message next;
132
Eugene Susla2f5ee712017-06-23 17:25:24 -0700133
134 /** @hide */
135 public static final Object sPoolSync = new Object();
Brad Fitzpatrick2405c2782011-01-25 14:48:21 -0800136 private static Message sPool;
137 private static int sPoolSize = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138
Romain Guya2ad6562012-05-06 16:10:34 -0700139 private static final int MAX_POOL_SIZE = 50;
140
Dianne Hackborn7895bc22014-09-05 15:09:03 -0700141 private static boolean gCheckRecycle = true;
142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 /**
144 * Return a new Message instance from the global pool. Allows us to
145 * avoid allocating new objects in many cases.
146 */
147 public static Message obtain() {
Brad Fitzpatrick2405c2782011-01-25 14:48:21 -0800148 synchronized (sPoolSync) {
149 if (sPool != null) {
150 Message m = sPool;
151 sPool = m.next;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 m.next = null;
Jeff Brown9867ed72014-02-28 14:00:57 -0800153 m.flags = 0; // clear in-use flag
Brad Fitzpatrick2405c2782011-01-25 14:48:21 -0800154 sPoolSize--;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 return m;
156 }
157 }
158 return new Message();
159 }
160
161 /**
162 * Same as {@link #obtain()}, but copies the values of an existing
163 * message (including its target) into the new one.
164 * @param orig Original message to copy.
165 * @return A Message object from the global pool.
166 */
167 public static Message obtain(Message orig) {
168 Message m = obtain();
169 m.what = orig.what;
170 m.arg1 = orig.arg1;
171 m.arg2 = orig.arg2;
172 m.obj = orig.obj;
173 m.replyTo = orig.replyTo;
Dianne Hackborncb3ed1d2014-06-27 18:37:06 -0700174 m.sendingUid = orig.sendingUid;
Marcin Oczeretkoec758722018-09-12 12:53:47 +0100175 m.workSourceUid = orig.workSourceUid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 if (orig.data != null) {
177 m.data = new Bundle(orig.data);
178 }
179 m.target = orig.target;
180 m.callback = orig.callback;
181
182 return m;
183 }
184
185 /**
186 * Same as {@link #obtain()}, but sets the value for the <em>target</em> member on the Message returned.
187 * @param h Handler to assign to the returned Message object's <em>target</em> member.
188 * @return A Message object from the global pool.
189 */
190 public static Message obtain(Handler h) {
191 Message m = obtain();
192 m.target = h;
193
194 return m;
195 }
196
197 /**
198 * Same as {@link #obtain(Handler)}, but assigns a callback Runnable on
199 * the Message that is returned.
200 * @param h Handler to assign to the returned Message object's <em>target</em> member.
201 * @param callback Runnable that will execute when the message is handled.
202 * @return A Message object from the global pool.
203 */
204 public static Message obtain(Handler h, Runnable callback) {
205 Message m = obtain();
206 m.target = h;
207 m.callback = callback;
208
209 return m;
210 }
211
212 /**
213 * Same as {@link #obtain()}, but sets the values for both <em>target</em> and
214 * <em>what</em> members on the Message.
215 * @param h Value to assign to the <em>target</em> member.
216 * @param what Value to assign to the <em>what</em> member.
217 * @return A Message object from the global pool.
218 */
219 public static Message obtain(Handler h, int what) {
220 Message m = obtain();
221 m.target = h;
222 m.what = what;
223
224 return m;
225 }
226
227 /**
228 * Same as {@link #obtain()}, but sets the values of the <em>target</em>, <em>what</em>, and <em>obj</em>
229 * members.
230 * @param h The <em>target</em> value to set.
231 * @param what The <em>what</em> value to set.
232 * @param obj The <em>object</em> method to set.
233 * @return A Message object from the global pool.
234 */
235 public static Message obtain(Handler h, int what, Object obj) {
236 Message m = obtain();
237 m.target = h;
238 m.what = what;
239 m.obj = obj;
240
241 return m;
242 }
243
244 /**
Netta P958d0a52017-02-07 11:20:55 -0800245 * 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 -0800246 * <em>arg1</em>, and <em>arg2</em> members.
Netta P958d0a52017-02-07 11:20:55 -0800247 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 * @param h The <em>target</em> value to set.
249 * @param what The <em>what</em> value to set.
250 * @param arg1 The <em>arg1</em> value to set.
251 * @param arg2 The <em>arg2</em> value to set.
252 * @return A Message object from the global pool.
253 */
254 public static Message obtain(Handler h, int what, int arg1, int arg2) {
255 Message m = obtain();
256 m.target = h;
257 m.what = what;
258 m.arg1 = arg1;
259 m.arg2 = arg2;
260
261 return m;
262 }
263
264 /**
Netta P958d0a52017-02-07 11:20:55 -0800265 * 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 -0800266 * <em>arg1</em>, <em>arg2</em>, and <em>obj</em> members.
Netta P958d0a52017-02-07 11:20:55 -0800267 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 * @param h The <em>target</em> value to set.
269 * @param what The <em>what</em> value to set.
270 * @param arg1 The <em>arg1</em> value to set.
271 * @param arg2 The <em>arg2</em> value to set.
272 * @param obj The <em>obj</em> value to set.
273 * @return A Message object from the global pool.
274 */
Netta P958d0a52017-02-07 11:20:55 -0800275 public static Message obtain(Handler h, int what,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 int arg1, int arg2, Object obj) {
277 Message m = obtain();
278 m.target = h;
279 m.what = what;
280 m.arg1 = arg1;
281 m.arg2 = arg2;
282 m.obj = obj;
283
284 return m;
285 }
286
Dianne Hackborn7895bc22014-09-05 15:09:03 -0700287 /** @hide */
288 public static void updateCheckRecycle(int targetSdkVersion) {
Dianne Hackborn955d8d62014-10-07 20:17:19 -0700289 if (targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) {
Dianne Hackborn7895bc22014-09-05 15:09:03 -0700290 gCheckRecycle = false;
291 }
292 }
293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 /**
Jeff Brown9867ed72014-02-28 14:00:57 -0800295 * Return a Message instance to the global pool.
296 * <p>
297 * You MUST NOT touch the Message after calling this function because it has
298 * effectively been freed. It is an error to recycle a message that is currently
299 * enqueued or that is in the process of being delivered to a Handler.
300 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 */
302 public void recycle() {
Jeff Brown9867ed72014-02-28 14:00:57 -0800303 if (isInUse()) {
Dianne Hackborn7895bc22014-09-05 15:09:03 -0700304 if (gCheckRecycle) {
305 throw new IllegalStateException("This message cannot be recycled because it "
306 + "is still in use.");
307 }
308 return;
Jeff Brown9867ed72014-02-28 14:00:57 -0800309 }
310 recycleUnchecked();
311 }
312
313 /**
314 * Recycles a Message that may be in-use.
315 * Used internally by the MessageQueue and Looper when disposing of queued Messages.
316 */
317 void recycleUnchecked() {
318 // Mark the message as in use while it remains in the recycled object pool.
319 // Clear out all other details.
320 flags = FLAG_IN_USE;
321 what = 0;
322 arg1 = 0;
323 arg2 = 0;
324 obj = null;
325 replyTo = null;
Marcin Oczeretkoec758722018-09-12 12:53:47 +0100326 sendingUid = UID_NONE;
327 workSourceUid = UID_NONE;
Jeff Brown9867ed72014-02-28 14:00:57 -0800328 when = 0;
329 target = null;
330 callback = null;
331 data = null;
Jeff Brownfc9ff4c2011-06-10 17:56:08 -0700332
Brad Fitzpatrick2405c2782011-01-25 14:48:21 -0800333 synchronized (sPoolSync) {
334 if (sPoolSize < MAX_POOL_SIZE) {
Brad Fitzpatrick2405c2782011-01-25 14:48:21 -0800335 next = sPool;
336 sPool = this;
337 sPoolSize++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 }
339 }
340 }
341
342 /**
343 * Make this message like o. Performs a shallow copy of the data field.
344 * Does not copy the linked list fields, nor the timestamp or
345 * target/callback of the original message.
346 */
347 public void copyFrom(Message o) {
Wink Savillea334e7c2010-08-24 10:56:30 -0700348 this.flags = o.flags & ~FLAGS_TO_CLEAR_ON_COPY_FROM;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 this.what = o.what;
350 this.arg1 = o.arg1;
351 this.arg2 = o.arg2;
352 this.obj = o.obj;
353 this.replyTo = o.replyTo;
Dianne Hackborncb3ed1d2014-06-27 18:37:06 -0700354 this.sendingUid = o.sendingUid;
Marcin Oczeretkoec758722018-09-12 12:53:47 +0100355 this.workSourceUid = o.workSourceUid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356
357 if (o.data != null) {
358 this.data = (Bundle) o.data.clone();
359 } else {
360 this.data = null;
361 }
362 }
363
364 /**
365 * Return the targeted delivery time of this message, in milliseconds.
366 */
367 public long getWhen() {
368 return when;
369 }
Netta P958d0a52017-02-07 11:20:55 -0800370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 public void setTarget(Handler target) {
372 this.target = target;
373 }
374
375 /**
Joshua Baxter01c80132018-06-04 14:27:59 -0700376 * Retrieve the {@link android.os.Handler Handler} implementation that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 * will receive this message. The object must implement
378 * {@link android.os.Handler#handleMessage(android.os.Message)
379 * Handler.handleMessage()}. Each Handler has its own name-space for
380 * message codes, so you do not need to
381 * worry about yours conflicting with other handlers.
382 */
383 public Handler getTarget() {
384 return target;
385 }
386
387 /**
388 * Retrieve callback object that will execute when this message is handled.
389 * This object must implement Runnable. This is called by
390 * the <em>target</em> {@link Handler} that is receiving this Message to
391 * dispatch it. If
392 * not set, the message will be dispatched to the receiving Handler's
Tor Norbyef8dc2d22018-10-03 09:28:21 +0200393 * {@link Handler#handleMessage(Message)}.
Dianne Hackborn75288fa2010-02-16 18:01:18 -0800394 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 public Runnable getCallback() {
396 return callback;
397 }
Netta P958d0a52017-02-07 11:20:55 -0800398
Eugene Susla2f5ee712017-06-23 17:25:24 -0700399 /** @hide */
400 public Message setCallback(Runnable r) {
401 callback = r;
402 return this;
403 }
404
Netta P958d0a52017-02-07 11:20:55 -0800405 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 * Obtains a Bundle of arbitrary data associated with this
Dianne Hackborn75288fa2010-02-16 18:01:18 -0800407 * event, lazily creating it if necessary. Set this value by calling
408 * {@link #setData(Bundle)}. Note that when transferring data across
409 * processes via {@link Messenger}, you will need to set your ClassLoader
410 * on the Bundle via {@link Bundle#setClassLoader(ClassLoader)
411 * Bundle.setClassLoader()} so that it can instantiate your objects when
412 * you retrieve them.
413 * @see #peekData()
414 * @see #setData(Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 */
416 public Bundle getData() {
417 if (data == null) {
418 data = new Bundle();
419 }
Netta P958d0a52017-02-07 11:20:55 -0800420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 return data;
422 }
423
Netta P958d0a52017-02-07 11:20:55 -0800424 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 * Like getData(), but does not lazily create the Bundle. A null
Dianne Hackborn75288fa2010-02-16 18:01:18 -0800426 * is returned if the Bundle does not already exist. See
427 * {@link #getData} for further information on this.
428 * @see #getData()
429 * @see #setData(Bundle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 */
431 public Bundle peekData() {
432 return data;
433 }
434
Dianne Hackborn75288fa2010-02-16 18:01:18 -0800435 /**
Denver Coneybeared0eb5532014-06-18 15:45:14 -0400436 * Sets a Bundle of arbitrary data values. Use arg1 and arg2 members
Dianne Hackborn75288fa2010-02-16 18:01:18 -0800437 * as a lower cost way to send a few simple integer values, if you can.
Netta P958d0a52017-02-07 11:20:55 -0800438 * @see #getData()
Dianne Hackborn75288fa2010-02-16 18:01:18 -0800439 * @see #peekData()
440 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 public void setData(Bundle data) {
442 this.data = data;
443 }
444
445 /**
Eugene Susla2f5ee712017-06-23 17:25:24 -0700446 * Chainable setter for {@link #what}
447 *
448 * @hide
449 */
450 public Message setWhat(int what) {
451 this.what = what;
452 return this;
453 }
454
455 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 * Sends this Message to the Handler specified by {@link #getTarget}.
457 * Throws a null pointer exception if this field has not been set.
458 */
459 public void sendToTarget() {
460 target.sendMessage(this);
461 }
462
Jeff Browne799cb72012-02-14 11:53:33 -0800463 /**
Jeff Brown9840c072014-11-11 20:21:21 -0800464 * Returns true if the message is asynchronous, meaning that it is not
465 * subject to {@link Looper} synchronization barriers.
Jeff Browne799cb72012-02-14 11:53:33 -0800466 *
467 * @return True if the message is asynchronous.
468 *
469 * @see #setAsynchronous(boolean)
Jeff Browne799cb72012-02-14 11:53:33 -0800470 */
471 public boolean isAsynchronous() {
472 return (flags & FLAG_ASYNCHRONOUS) != 0;
473 }
474
475 /**
Jeff Brown9840c072014-11-11 20:21:21 -0800476 * Sets whether the message is asynchronous, meaning that it is not
477 * subject to {@link Looper} synchronization barriers.
478 * <p>
479 * Certain operations, such as view invalidation, may introduce synchronization
480 * barriers into the {@link Looper}'s message queue to prevent subsequent messages
481 * from being delivered until some condition is met. In the case of view invalidation,
482 * messages which are posted after a call to {@link android.view.View#invalidate}
483 * are suspended by means of a synchronization barrier until the next frame is
484 * ready to be drawn. The synchronization barrier ensures that the invalidation
485 * request is completely handled before resuming.
486 * </p><p>
487 * Asynchronous messages are exempt from synchronization barriers. They typically
488 * represent interrupts, input events, and other signals that must be handled independently
489 * even while other work has been suspended.
490 * </p><p>
491 * Note that asynchronous messages may be delivered out of order with respect to
492 * synchronous messages although they are always delivered in order among themselves.
493 * If the relative order of these messages matters then they probably should not be
494 * asynchronous in the first place. Use with caution.
495 * </p>
Jeff Browne799cb72012-02-14 11:53:33 -0800496 *
497 * @param async True if the message is asynchronous.
498 *
499 * @see #isAsynchronous()
Jeff Browne799cb72012-02-14 11:53:33 -0800500 */
501 public void setAsynchronous(boolean async) {
502 if (async) {
503 flags |= FLAG_ASYNCHRONOUS;
504 } else {
505 flags &= ~FLAG_ASYNCHRONOUS;
506 }
507 }
508
Wink Savillea334e7c2010-08-24 10:56:30 -0700509 /*package*/ boolean isInUse() {
510 return ((flags & FLAG_IN_USE) == FLAG_IN_USE);
511 }
512
513 /*package*/ void markInUse() {
514 flags |= FLAG_IN_USE;
515 }
516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 /** Constructor (but the preferred way to get a Message is to call {@link #obtain() Message.obtain()}).
518 */
519 public Message() {
520 }
521
Jeff Brown5182c782013-10-15 20:31:52 -0700522 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 public String toString() {
Dianne Hackborn1ebccf52010-08-15 13:04:34 -0700524 return toString(SystemClock.uptimeMillis());
525 }
526
527 String toString(long now) {
Jeff Brown5182c782013-10-15 20:31:52 -0700528 StringBuilder b = new StringBuilder();
529 b.append("{ when=");
530 TimeUtils.formatDuration(when - now, b);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531
Jeff Brown5182c782013-10-15 20:31:52 -0700532 if (target != null) {
533 if (callback != null) {
534 b.append(" callback=");
535 b.append(callback.getClass().getName());
536 } else {
537 b.append(" what=");
538 b.append(what);
539 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540
Jeff Brown5182c782013-10-15 20:31:52 -0700541 if (arg1 != 0) {
542 b.append(" arg1=");
543 b.append(arg1);
544 }
545
546 if (arg2 != 0) {
547 b.append(" arg2=");
548 b.append(arg2);
549 }
550
551 if (obj != null) {
552 b.append(" obj=");
553 b.append(obj);
554 }
555
556 b.append(" target=");
557 b.append(target.getClass().getName());
558 } else {
559 b.append(" barrier=");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 b.append(arg1);
561 }
562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 b.append(" }");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 return b.toString();
565 }
566
Netta P958d0a52017-02-07 11:20:55 -0800567 void writeToProto(ProtoOutputStream proto, long fieldId) {
568 final long messageToken = proto.start(fieldId);
569 proto.write(MessageProto.WHEN, when);
570
571 if (target != null) {
572 if (callback != null) {
573 proto.write(MessageProto.CALLBACK, callback.getClass().getName());
574 } else {
575 proto.write(MessageProto.WHAT, what);
576 }
577
578 if (arg1 != 0) {
579 proto.write(MessageProto.ARG1, arg1);
580 }
581
582 if (arg2 != 0) {
583 proto.write(MessageProto.ARG2, arg2);
584 }
585
586 if (obj != null) {
587 proto.write(MessageProto.OBJ, obj.toString());
588 }
589
590 proto.write(MessageProto.TARGET, target.getClass().getName());
591 } else {
592 proto.write(MessageProto.BARRIER, arg1);
593 }
594
595 proto.end(messageToken);
596 }
597
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700598 public static final @android.annotation.NonNull Parcelable.Creator<Message> CREATOR
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 = new Parcelable.Creator<Message>() {
600 public Message createFromParcel(Parcel source) {
601 Message msg = Message.obtain();
602 msg.readFromParcel(source);
603 return msg;
604 }
Netta P958d0a52017-02-07 11:20:55 -0800605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 public Message[] newArray(int size) {
607 return new Message[size];
608 }
609 };
Netta P958d0a52017-02-07 11:20:55 -0800610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 public int describeContents() {
612 return 0;
613 }
614
615 public void writeToParcel(Parcel dest, int flags) {
Dianne Hackborn75288fa2010-02-16 18:01:18 -0800616 if (callback != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 throw new RuntimeException(
Dianne Hackborn75288fa2010-02-16 18:01:18 -0800618 "Can't marshal callbacks across processes.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 }
620 dest.writeInt(what);
621 dest.writeInt(arg1);
622 dest.writeInt(arg2);
Dianne Hackborn75288fa2010-02-16 18:01:18 -0800623 if (obj != null) {
624 try {
625 Parcelable p = (Parcelable)obj;
626 dest.writeInt(1);
627 dest.writeParcelable(p, flags);
628 } catch (ClassCastException e) {
629 throw new RuntimeException(
630 "Can't marshal non-Parcelable objects across processes.");
631 }
632 } else {
633 dest.writeInt(0);
634 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 dest.writeLong(when);
636 dest.writeBundle(data);
637 Messenger.writeMessengerOrNullToParcel(replyTo, dest);
Dianne Hackborncb3ed1d2014-06-27 18:37:06 -0700638 dest.writeInt(sendingUid);
Marcin Oczeretkoec758722018-09-12 12:53:47 +0100639 dest.writeInt(workSourceUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 }
641
Romain Guy8f3b8e32012-03-27 16:33:45 -0700642 private void readFromParcel(Parcel source) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 what = source.readInt();
644 arg1 = source.readInt();
645 arg2 = source.readInt();
Dianne Hackborn75288fa2010-02-16 18:01:18 -0800646 if (source.readInt() != 0) {
647 obj = source.readParcelable(getClass().getClassLoader());
648 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 when = source.readLong();
650 data = source.readBundle();
651 replyTo = Messenger.readMessengerOrNullFromParcel(source);
Dianne Hackborncb3ed1d2014-06-27 18:37:06 -0700652 sendingUid = source.readInt();
Marcin Oczeretkoec758722018-09-12 12:53:47 +0100653 workSourceUid = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 }
655}