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 | |
Artur Satayev | bc3d8b9 | 2019-12-10 17:47:53 +0000 | [diff] [blame] | 19 | import android.compat.annotation.UnsupportedAppUsage; |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 20 | import android.util.TimeUtils; |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 21 | import android.util.proto.ProtoOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | |
Marcin Oczeretko | 4427272 | 2018-09-19 11:01:32 +0100 | [diff] [blame] | 23 | import com.android.internal.annotations.VisibleForTesting; |
| 24 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | /** |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 26 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | * 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 P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 29 | * 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] | 30 | * |
| 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 | */ |
| 36 | public final class Message implements Parcelable { |
| 37 | /** |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 38 | * 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] | 39 | * 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 Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 45 | /** |
| 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 P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 50 | public int arg1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 52 | /** |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | public int arg2; |
| 58 | |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 59 | /** |
| 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 P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 65 | * |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 66 | * <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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | public Object obj; |
| 70 | |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 71 | /** |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | */ |
| 76 | public Messenger replyTo; |
Wink Saville | a334e7c | 2010-08-24 10:56:30 -0700 | [diff] [blame] | 77 | |
Dianne Hackborn | cb3ed1d | 2014-06-27 18:37:06 -0700 | [diff] [blame] | 78 | /** |
Marcin Oczeretko | ec75872 | 2018-09-12 12:53:47 +0100 | [diff] [blame] | 79 | * 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 Hackborn | cb3ed1d | 2014-06-27 18:37:06 -0700 | [diff] [blame] | 86 | * 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 Oczeretko | ec75872 | 2018-09-12 12:53:47 +0100 | [diff] [blame] | 90 | 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 Hackborn | cb3ed1d | 2014-06-27 18:37:06 -0700 | [diff] [blame] | 98 | |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 99 | /** 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 Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 107 | /*package*/ static final int FLAG_IN_USE = 1 << 0; |
Wink Saville | a334e7c | 2010-08-24 10:56:30 -0700 | [diff] [blame] | 108 | |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 109 | /** If set message is asynchronous */ |
| 110 | /*package*/ static final int FLAG_ASYNCHRONOUS = 1 << 1; |
Wink Saville | a334e7c | 2010-08-24 10:56:30 -0700 | [diff] [blame] | 111 | |
| 112 | /** Flags to clear in the copyFrom method */ |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 113 | /*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] | 114 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 115 | @UnsupportedAppUsage |
Wink Saville | a334e7c | 2010-08-24 10:56:30 -0700 | [diff] [blame] | 116 | /*package*/ int flags; |
| 117 | |
Marcin Oczeretko | 4427272 | 2018-09-19 11:01:32 +0100 | [diff] [blame] | 118 | /** |
| 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 Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 123 | @UnsupportedAppUsage |
Marcin Oczeretko | 4427272 | 2018-09-19 11:01:32 +0100 | [diff] [blame] | 124 | @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 125 | public long when; |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 126 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | /*package*/ Bundle data; |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 128 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 129 | @UnsupportedAppUsage |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 130 | /*package*/ Handler target; |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 131 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 132 | @UnsupportedAppUsage |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 133 | /*package*/ Runnable callback; |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 134 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | // sometimes we store linked lists of these things |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 136 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | /*package*/ Message next; |
| 138 | |
Eugene Susla | 2f5ee71 | 2017-06-23 17:25:24 -0700 | [diff] [blame] | 139 | |
| 140 | /** @hide */ |
| 141 | public static final Object sPoolSync = new Object(); |
Brad Fitzpatrick | 2405c278 | 2011-01-25 14:48:21 -0800 | [diff] [blame] | 142 | private static Message sPool; |
| 143 | private static int sPoolSize = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | |
Romain Guy | a2ad656 | 2012-05-06 16:10:34 -0700 | [diff] [blame] | 145 | private static final int MAX_POOL_SIZE = 50; |
| 146 | |
Dianne Hackborn | 7895bc2 | 2014-09-05 15:09:03 -0700 | [diff] [blame] | 147 | private static boolean gCheckRecycle = true; |
| 148 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | /** |
| 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 Fitzpatrick | 2405c278 | 2011-01-25 14:48:21 -0800 | [diff] [blame] | 154 | synchronized (sPoolSync) { |
| 155 | if (sPool != null) { |
| 156 | Message m = sPool; |
| 157 | sPool = m.next; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | m.next = null; |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 159 | m.flags = 0; // clear in-use flag |
Brad Fitzpatrick | 2405c278 | 2011-01-25 14:48:21 -0800 | [diff] [blame] | 160 | sPoolSize--; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | 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 Hackborn | cb3ed1d | 2014-06-27 18:37:06 -0700 | [diff] [blame] | 180 | m.sendingUid = orig.sendingUid; |
Marcin Oczeretko | ec75872 | 2018-09-12 12:53:47 +0100 | [diff] [blame] | 181 | m.workSourceUid = orig.workSourceUid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | 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 P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 251 | * 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] | 252 | * <em>arg1</em>, and <em>arg2</em> members. |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 253 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | * @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 P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 271 | * 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] | 272 | * <em>arg1</em>, <em>arg2</em>, and <em>obj</em> members. |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 273 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | * @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 P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 281 | public static Message obtain(Handler h, int what, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | 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 Hackborn | 7895bc2 | 2014-09-05 15:09:03 -0700 | [diff] [blame] | 293 | /** @hide */ |
| 294 | public static void updateCheckRecycle(int targetSdkVersion) { |
Dianne Hackborn | 955d8d6 | 2014-10-07 20:17:19 -0700 | [diff] [blame] | 295 | if (targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) { |
Dianne Hackborn | 7895bc2 | 2014-09-05 15:09:03 -0700 | [diff] [blame] | 296 | gCheckRecycle = false; |
| 297 | } |
| 298 | } |
| 299 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 300 | /** |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 301 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | */ |
| 308 | public void recycle() { |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 309 | if (isInUse()) { |
Dianne Hackborn | 7895bc2 | 2014-09-05 15:09:03 -0700 | [diff] [blame] | 310 | if (gCheckRecycle) { |
| 311 | throw new IllegalStateException("This message cannot be recycled because it " |
| 312 | + "is still in use."); |
| 313 | } |
| 314 | return; |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 315 | } |
| 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 Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 323 | @UnsupportedAppUsage |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 324 | 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 Oczeretko | ec75872 | 2018-09-12 12:53:47 +0100 | [diff] [blame] | 333 | sendingUid = UID_NONE; |
| 334 | workSourceUid = UID_NONE; |
Jeff Brown | 9867ed7 | 2014-02-28 14:00:57 -0800 | [diff] [blame] | 335 | when = 0; |
| 336 | target = null; |
| 337 | callback = null; |
| 338 | data = null; |
Jeff Brown | fc9ff4c | 2011-06-10 17:56:08 -0700 | [diff] [blame] | 339 | |
Brad Fitzpatrick | 2405c278 | 2011-01-25 14:48:21 -0800 | [diff] [blame] | 340 | synchronized (sPoolSync) { |
| 341 | if (sPoolSize < MAX_POOL_SIZE) { |
Brad Fitzpatrick | 2405c278 | 2011-01-25 14:48:21 -0800 | [diff] [blame] | 342 | next = sPool; |
| 343 | sPool = this; |
| 344 | sPoolSize++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 | } |
| 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 Saville | a334e7c | 2010-08-24 10:56:30 -0700 | [diff] [blame] | 355 | 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] | 356 | 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 Hackborn | cb3ed1d | 2014-06-27 18:37:06 -0700 | [diff] [blame] | 361 | this.sendingUid = o.sendingUid; |
Marcin Oczeretko | ec75872 | 2018-09-12 12:53:47 +0100 | [diff] [blame] | 362 | this.workSourceUid = o.workSourceUid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | |
| 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 P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 377 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | public void setTarget(Handler target) { |
| 379 | this.target = target; |
| 380 | } |
| 381 | |
| 382 | /** |
Joshua Baxter | 01c8013 | 2018-06-04 14:27:59 -0700 | [diff] [blame] | 383 | * Retrieve the {@link android.os.Handler Handler} implementation that |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 384 | * 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 Norbye | f8dc2d2 | 2018-10-03 09:28:21 +0200 | [diff] [blame] | 400 | * {@link Handler#handleMessage(Message)}. |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 401 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 402 | public Runnable getCallback() { |
| 403 | return callback; |
| 404 | } |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 405 | |
Eugene Susla | 2f5ee71 | 2017-06-23 17:25:24 -0700 | [diff] [blame] | 406 | /** @hide */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 407 | @UnsupportedAppUsage |
Eugene Susla | 2f5ee71 | 2017-06-23 17:25:24 -0700 | [diff] [blame] | 408 | public Message setCallback(Runnable r) { |
| 409 | callback = r; |
| 410 | return this; |
| 411 | } |
| 412 | |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 413 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | * Obtains a Bundle of arbitrary data associated with this |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 415 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | */ |
| 424 | public Bundle getData() { |
| 425 | if (data == null) { |
| 426 | data = new Bundle(); |
| 427 | } |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 428 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 429 | return data; |
| 430 | } |
| 431 | |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 432 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 433 | * Like getData(), but does not lazily create the Bundle. A null |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 434 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 438 | */ |
| 439 | public Bundle peekData() { |
| 440 | return data; |
| 441 | } |
| 442 | |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 443 | /** |
Denver Coneybeare | d0eb553 | 2014-06-18 15:45:14 -0400 | [diff] [blame] | 444 | * Sets a Bundle of arbitrary data values. Use arg1 and arg2 members |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 445 | * 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] | 446 | * @see #getData() |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 447 | * @see #peekData() |
| 448 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 449 | public void setData(Bundle data) { |
| 450 | this.data = data; |
| 451 | } |
| 452 | |
| 453 | /** |
Eugene Susla | 2f5ee71 | 2017-06-23 17:25:24 -0700 | [diff] [blame] | 454 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | * 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 Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 471 | /** |
Jeff Brown | 9840c07 | 2014-11-11 20:21:21 -0800 | [diff] [blame] | 472 | * Returns true if the message is asynchronous, meaning that it is not |
| 473 | * subject to {@link Looper} synchronization barriers. |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 474 | * |
| 475 | * @return True if the message is asynchronous. |
| 476 | * |
| 477 | * @see #setAsynchronous(boolean) |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 478 | */ |
| 479 | public boolean isAsynchronous() { |
| 480 | return (flags & FLAG_ASYNCHRONOUS) != 0; |
| 481 | } |
| 482 | |
| 483 | /** |
Jeff Brown | 9840c07 | 2014-11-11 20:21:21 -0800 | [diff] [blame] | 484 | * 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 Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 504 | * |
| 505 | * @param async True if the message is asynchronous. |
| 506 | * |
| 507 | * @see #isAsynchronous() |
Jeff Brown | e799cb7 | 2012-02-14 11:53:33 -0800 | [diff] [blame] | 508 | */ |
| 509 | public void setAsynchronous(boolean async) { |
| 510 | if (async) { |
| 511 | flags |= FLAG_ASYNCHRONOUS; |
| 512 | } else { |
| 513 | flags &= ~FLAG_ASYNCHRONOUS; |
| 514 | } |
| 515 | } |
| 516 | |
Wink Saville | a334e7c | 2010-08-24 10:56:30 -0700 | [diff] [blame] | 517 | /*package*/ boolean isInUse() { |
| 518 | return ((flags & FLAG_IN_USE) == FLAG_IN_USE); |
| 519 | } |
| 520 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 521 | @UnsupportedAppUsage |
Wink Saville | a334e7c | 2010-08-24 10:56:30 -0700 | [diff] [blame] | 522 | /*package*/ void markInUse() { |
| 523 | flags |= FLAG_IN_USE; |
| 524 | } |
| 525 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 526 | /** Constructor (but the preferred way to get a Message is to call {@link #obtain() Message.obtain()}). |
| 527 | */ |
| 528 | public Message() { |
| 529 | } |
| 530 | |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 531 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 532 | public String toString() { |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 533 | return toString(SystemClock.uptimeMillis()); |
| 534 | } |
| 535 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 536 | @UnsupportedAppUsage |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 537 | String toString(long now) { |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 538 | StringBuilder b = new StringBuilder(); |
| 539 | b.append("{ when="); |
| 540 | TimeUtils.formatDuration(when - now, b); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 541 | |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 542 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 550 | |
Jeff Brown | 5182c78 | 2013-10-15 20:31:52 -0700 | [diff] [blame] | 551 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 570 | b.append(arg1); |
| 571 | } |
| 572 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 573 | b.append(" }"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 574 | return b.toString(); |
| 575 | } |
| 576 | |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 577 | 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 Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 608 | public static final @android.annotation.NonNull Parcelable.Creator<Message> CREATOR |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 609 | = new Parcelable.Creator<Message>() { |
| 610 | public Message createFromParcel(Parcel source) { |
| 611 | Message msg = Message.obtain(); |
| 612 | msg.readFromParcel(source); |
| 613 | return msg; |
| 614 | } |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 615 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 616 | public Message[] newArray(int size) { |
| 617 | return new Message[size]; |
| 618 | } |
| 619 | }; |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 620 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 621 | public int describeContents() { |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | public void writeToParcel(Parcel dest, int flags) { |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 626 | if (callback != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 627 | throw new RuntimeException( |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 628 | "Can't marshal callbacks across processes."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 629 | } |
| 630 | dest.writeInt(what); |
| 631 | dest.writeInt(arg1); |
| 632 | dest.writeInt(arg2); |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 633 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 645 | dest.writeLong(when); |
| 646 | dest.writeBundle(data); |
| 647 | Messenger.writeMessengerOrNullToParcel(replyTo, dest); |
Dianne Hackborn | cb3ed1d | 2014-06-27 18:37:06 -0700 | [diff] [blame] | 648 | dest.writeInt(sendingUid); |
Marcin Oczeretko | ec75872 | 2018-09-12 12:53:47 +0100 | [diff] [blame] | 649 | dest.writeInt(workSourceUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 650 | } |
| 651 | |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 652 | private void readFromParcel(Parcel source) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 653 | what = source.readInt(); |
| 654 | arg1 = source.readInt(); |
| 655 | arg2 = source.readInt(); |
Dianne Hackborn | 75288fa | 2010-02-16 18:01:18 -0800 | [diff] [blame] | 656 | if (source.readInt() != 0) { |
| 657 | obj = source.readParcelable(getClass().getClassLoader()); |
| 658 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 659 | when = source.readLong(); |
| 660 | data = source.readBundle(); |
| 661 | replyTo = Messenger.readMessengerOrNullFromParcel(source); |
Dianne Hackborn | cb3ed1d | 2014-06-27 18:37:06 -0700 | [diff] [blame] | 662 | sendingUid = source.readInt(); |
Marcin Oczeretko | ec75872 | 2018-09-12 12:53:47 +0100 | [diff] [blame] | 663 | workSourceUid = source.readInt(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 664 | } |
| 665 | } |