The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.os; |
| 18 | |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 19 | import android.util.TimeUtils; |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 20 | import android.util.proto.ProtoOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | |
Marcin Oczeretko | 4427272 | 2018-09-19 11:01:32 +0100 | [diff] [blame] | 22 | import com.android.internal.annotations.VisibleForTesting; |
| 23 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | /** |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 25 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | * 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 P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 28 | * extra object field that allow you to not do allocations in many cases. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | * |
| 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 | */ |
| 35 | public final class Message implements Parcelable { |
| 36 | /** |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 37 | * User-defined message code so that the recipient can identify |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | * 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 Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 44 | /** |
| 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 P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 49 | public int arg1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 51 | /** |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | public int arg2; |
| 57 | |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 58 | /** |
| 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 P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 64 | * |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 65 | * <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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | public Object obj; |
| 69 | |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 70 | /** |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | */ |
| 75 | public Messenger replyTo; |
Wink Saville | a334e7c | 2010-08-24 10:56:30 -0700 | [diff] [blame] | 76 | |
Dianne Hackborn | cb3ed1d | 2014-06-27 18:37:06 -0700 | [diff] [blame] | 77 | /** |
Marcin Oczeretko | ec75872 | 2018-09-12 12:53:47 +0100 | [diff] [blame] | 78 | * 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 Hackborn | cb3ed1d | 2014-06-27 18:37:06 -0700 | [diff] [blame] | 85 | * 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 Oczeretko | ec75872 | 2018-09-12 12:53:47 +0100 | [diff] [blame] | 89 | 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 Hackborn | cb3ed1d | 2014-06-27 18:37:06 -0700 | [diff] [blame] | 97 | |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 98 | /** 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 Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 106 | /*package*/ static final int FLAG_IN_USE = 1 << 0; |
Wink Saville | a334e7c | 2010-08-24 10:56:30 -0700 | [diff] [blame] | 107 | |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 108 | /** If set message is asynchronous */ |
| 109 | /*package*/ static final int FLAG_ASYNCHRONOUS = 1 << 1; |
Wink Saville | a334e7c | 2010-08-24 10:56:30 -0700 | [diff] [blame] | 110 | |
| 111 | /** Flags to clear in the copyFrom method */ |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 112 | /*package*/ static final int FLAGS_TO_CLEAR_ON_COPY_FROM = FLAG_IN_USE; |
Wink Saville | a334e7c | 2010-08-24 10:56:30 -0700 | [diff] [blame] | 113 | |
| 114 | /*package*/ int flags; |
| 115 | |
Marcin Oczeretko | 4427272 | 2018-09-19 11:01:32 +0100 | [diff] [blame] | 116 | /** |
| 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 P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 123 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | /*package*/ Bundle data; |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 125 | |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 126 | /*package*/ Handler target; |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 127 | |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 128 | /*package*/ Runnable callback; |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 129 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | // sometimes we store linked lists of these things |
| 131 | /*package*/ Message next; |
| 132 | |
Eugene Susla | 2f5ee71 | 2017-06-23 17:25:24 -0700 | [diff] [blame] | 133 | |
| 134 | /** @hide */ |
| 135 | public static final Object sPoolSync = new Object(); |
Brad Fitzpatrick | 2405c278 | 2011-01-25 14:48:21 -0800 | [diff] [blame] | 136 | private static Message sPool; |
| 137 | private static int sPoolSize = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | |
Romain Guy | a2ad656 | 2012-05-06 16:10:34 -0700 | [diff] [blame] | 139 | private static final int MAX_POOL_SIZE = 50; |
| 140 | |
Dianne Hackborn | 7895bc2 | 2014-09-05 15:09:03 -0700 | [diff] [blame] | 141 | private static boolean gCheckRecycle = true; |
| 142 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | /** |
| 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 Fitzpatrick | 2405c278 | 2011-01-25 14:48:21 -0800 | [diff] [blame] | 148 | synchronized (sPoolSync) { |
| 149 | if (sPool != null) { |
| 150 | Message m = sPool; |
| 151 | sPool = m.next; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | m.next = null; |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 153 | m.flags = 0; // clear in-use flag |
Brad Fitzpatrick | 2405c278 | 2011-01-25 14:48:21 -0800 | [diff] [blame] | 154 | sPoolSize--; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | 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 Hackborn | cb3ed1d | 2014-06-27 18:37:06 -0700 | [diff] [blame] | 174 | m.sendingUid = orig.sendingUid; |
Marcin Oczeretko | ec75872 | 2018-09-12 12:53:47 +0100 | [diff] [blame] | 175 | m.workSourceUid = orig.workSourceUid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | 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 P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 245 | * Same as {@link #obtain()}, but sets the values of the <em>target</em>, <em>what</em>, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | * <em>arg1</em>, and <em>arg2</em> members. |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 247 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | * @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 P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 265 | * Same as {@link #obtain()}, but sets the values of the <em>target</em>, <em>what</em>, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | * <em>arg1</em>, <em>arg2</em>, and <em>obj</em> members. |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 267 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 | * @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 P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 275 | public static Message obtain(Handler h, int what, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | 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 Hackborn | 7895bc2 | 2014-09-05 15:09:03 -0700 | [diff] [blame] | 287 | /** @hide */ |
| 288 | public static void updateCheckRecycle(int targetSdkVersion) { |
Dianne Hackborn | 955d8d6 | 2014-10-07 20:17:19 -0700 | [diff] [blame] | 289 | if (targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) { |
Dianne Hackborn | 7895bc2 | 2014-09-05 15:09:03 -0700 | [diff] [blame] | 290 | gCheckRecycle = false; |
| 291 | } |
| 292 | } |
| 293 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | /** |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 295 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | */ |
| 302 | public void recycle() { |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 303 | if (isInUse()) { |
Dianne Hackborn | 7895bc2 | 2014-09-05 15:09:03 -0700 | [diff] [blame] | 304 | if (gCheckRecycle) { |
| 305 | throw new IllegalStateException("This message cannot be recycled because it " |
| 306 | + "is still in use."); |
| 307 | } |
| 308 | return; |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 309 | } |
| 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 Oczeretko | ec75872 | 2018-09-12 12:53:47 +0100 | [diff] [blame] | 326 | sendingUid = UID_NONE; |
| 327 | workSourceUid = UID_NONE; |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 328 | when = 0; |
| 329 | target = null; |
| 330 | callback = null; |
| 331 | data = null; |
Jeff Brown | fc9ff4c | 2011-06-10 17:56:08 -0700 | [diff] [blame] | 332 | |
Brad Fitzpatrick | 2405c278 | 2011-01-25 14:48:21 -0800 | [diff] [blame] | 333 | synchronized (sPoolSync) { |
| 334 | if (sPoolSize < MAX_POOL_SIZE) { |
Brad Fitzpatrick | 2405c278 | 2011-01-25 14:48:21 -0800 | [diff] [blame] | 335 | next = sPool; |
| 336 | sPool = this; |
| 337 | sPoolSize++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | } |
| 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 Saville | a334e7c | 2010-08-24 10:56:30 -0700 | [diff] [blame] | 348 | this.flags = o.flags & ~FLAGS_TO_CLEAR_ON_COPY_FROM; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | 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 Hackborn | cb3ed1d | 2014-06-27 18:37:06 -0700 | [diff] [blame] | 354 | this.sendingUid = o.sendingUid; |
Marcin Oczeretko | ec75872 | 2018-09-12 12:53:47 +0100 | [diff] [blame] | 355 | this.workSourceUid = o.workSourceUid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | |
| 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 P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 370 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | public void setTarget(Handler target) { |
| 372 | this.target = target; |
| 373 | } |
| 374 | |
| 375 | /** |
Joshua Baxter | 01c8013 | 2018-06-04 14:27:59 -0700 | [diff] [blame] | 376 | * Retrieve the {@link android.os.Handler Handler} implementation that |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | * 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 Norbye | f8dc2d2 | 2018-10-03 09:28:21 +0200 | [diff] [blame] | 393 | * {@link Handler#handleMessage(Message)}. |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 394 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | public Runnable getCallback() { |
| 396 | return callback; |
| 397 | } |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 398 | |
Eugene Susla | 2f5ee71 | 2017-06-23 17:25:24 -0700 | [diff] [blame] | 399 | /** @hide */ |
| 400 | public Message setCallback(Runnable r) { |
| 401 | callback = r; |
| 402 | return this; |
| 403 | } |
| 404 | |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 405 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | * Obtains a Bundle of arbitrary data associated with this |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 407 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 415 | */ |
| 416 | public Bundle getData() { |
| 417 | if (data == null) { |
| 418 | data = new Bundle(); |
| 419 | } |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 420 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 421 | return data; |
| 422 | } |
| 423 | |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 424 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 425 | * Like getData(), but does not lazily create the Bundle. A null |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 426 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | */ |
| 431 | public Bundle peekData() { |
| 432 | return data; |
| 433 | } |
| 434 | |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 435 | /** |
Denver Coneybeare | d0eb553 | 2014-06-18 15:45:14 -0400 | [diff] [blame] | 436 | * Sets a Bundle of arbitrary data values. Use arg1 and arg2 members |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 437 | * as a lower cost way to send a few simple integer values, if you can. |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 438 | * @see #getData() |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 439 | * @see #peekData() |
| 440 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 441 | public void setData(Bundle data) { |
| 442 | this.data = data; |
| 443 | } |
| 444 | |
| 445 | /** |
Eugene Susla | 2f5ee71 | 2017-06-23 17:25:24 -0700 | [diff] [blame] | 446 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | * 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 Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 463 | /** |
Jeff Brown | 9840c07 | 2014-11-11 20:21:21 -0800 | [diff] [blame] | 464 | * Returns true if the message is asynchronous, meaning that it is not |
| 465 | * subject to {@link Looper} synchronization barriers. |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 466 | * |
| 467 | * @return True if the message is asynchronous. |
| 468 | * |
| 469 | * @see #setAsynchronous(boolean) |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 470 | */ |
| 471 | public boolean isAsynchronous() { |
| 472 | return (flags & FLAG_ASYNCHRONOUS) != 0; |
| 473 | } |
| 474 | |
| 475 | /** |
Jeff Brown | 9840c07 | 2014-11-11 20:21:21 -0800 | [diff] [blame] | 476 | * 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 Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 496 | * |
| 497 | * @param async True if the message is asynchronous. |
| 498 | * |
| 499 | * @see #isAsynchronous() |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 500 | */ |
| 501 | public void setAsynchronous(boolean async) { |
| 502 | if (async) { |
| 503 | flags |= FLAG_ASYNCHRONOUS; |
| 504 | } else { |
| 505 | flags &= ~FLAG_ASYNCHRONOUS; |
| 506 | } |
| 507 | } |
| 508 | |
Wink Saville | a334e7c | 2010-08-24 10:56:30 -0700 | [diff] [blame] | 509 | /*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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | /** Constructor (but the preferred way to get a Message is to call {@link #obtain() Message.obtain()}). |
| 518 | */ |
| 519 | public Message() { |
| 520 | } |
| 521 | |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 522 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 523 | public String toString() { |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 524 | return toString(SystemClock.uptimeMillis()); |
| 525 | } |
| 526 | |
| 527 | String toString(long now) { |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 528 | StringBuilder b = new StringBuilder(); |
| 529 | b.append("{ when="); |
| 530 | TimeUtils.formatDuration(when - now, b); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 531 | |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 532 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 540 | |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 541 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 560 | b.append(arg1); |
| 561 | } |
| 562 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 563 | b.append(" }"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 564 | return b.toString(); |
| 565 | } |
| 566 | |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 567 | 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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | public static final Parcelable.Creator<Message> CREATOR |
| 599 | = new Parcelable.Creator<Message>() { |
| 600 | public Message createFromParcel(Parcel source) { |
| 601 | Message msg = Message.obtain(); |
| 602 | msg.readFromParcel(source); |
| 603 | return msg; |
| 604 | } |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 605 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 606 | public Message[] newArray(int size) { |
| 607 | return new Message[size]; |
| 608 | } |
| 609 | }; |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 610 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | public int describeContents() { |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | public void writeToParcel(Parcel dest, int flags) { |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 616 | if (callback != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 617 | throw new RuntimeException( |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 618 | "Can't marshal callbacks across processes."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | } |
| 620 | dest.writeInt(what); |
| 621 | dest.writeInt(arg1); |
| 622 | dest.writeInt(arg2); |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 623 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 635 | dest.writeLong(when); |
| 636 | dest.writeBundle(data); |
| 637 | Messenger.writeMessengerOrNullToParcel(replyTo, dest); |
Dianne Hackborn | cb3ed1d | 2014-06-27 18:37:06 -0700 | [diff] [blame] | 638 | dest.writeInt(sendingUid); |
Marcin Oczeretko | ec75872 | 2018-09-12 12:53:47 +0100 | [diff] [blame] | 639 | dest.writeInt(workSourceUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 640 | } |
| 641 | |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 642 | private void readFromParcel(Parcel source) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 643 | what = source.readInt(); |
| 644 | arg1 = source.readInt(); |
| 645 | arg2 = source.readInt(); |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 646 | if (source.readInt() != 0) { |
| 647 | obj = source.readParcelable(getClass().getClassLoader()); |
| 648 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 649 | when = source.readLong(); |
| 650 | data = source.readBundle(); |
| 651 | replyTo = Messenger.readMessengerOrNullFromParcel(source); |
Dianne Hackborn | cb3ed1d | 2014-06-27 18:37:06 -0700 | [diff] [blame] | 652 | sendingUid = source.readInt(); |
Marcin Oczeretko | ec75872 | 2018-09-12 12:53:47 +0100 | [diff] [blame] | 653 | workSourceUid = source.readInt(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 654 | } |
| 655 | } |