| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.os; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.compat.annotation.UnsupportedAppUsage; |
| 22 |
import android.util.Log; |
| 23 |
import android.util.Printer; |
| 24 |
import android.util.Slog; |
| 25 |
import android.util.proto.ProtoOutputStream; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public final class Looper { |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
private static final String TAG = "Looper"; |
| 69 |
|
| 70 |
|
| 71 |
@UnsupportedAppUsage |
| 72 |
static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>(); |
| 73 |
@UnsupportedAppUsage |
| 74 |
private static Looper sMainLooper; |
| 75 |
private static Observer sObserver; |
| 76 |
|
| 77 |
@UnsupportedAppUsage |
| 78 |
final MessageQueue mQueue; |
| 79 |
final Thread mThread; |
| 80 |
private boolean mInLoop; |
| 81 |
|
| 82 |
@UnsupportedAppUsage |
| 83 |
private Printer mLogging; |
| 84 |
private long mTraceTag; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
private long mSlowDispatchThresholdMs; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
private long mSlowDeliveryThresholdMs; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
private boolean mSlowDeliveryDetected; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public static void prepare() { |
| 109 |
prepare(true); |
| 110 |
} |
| 111 |
|
| 112 |
private static void prepare(boolean quitAllowed) { |
| 113 |
if (sThreadLocal.get() != null) { |
| 114 |
throw new RuntimeException("Only one Looper may be created per thread"); |
| 115 |
} |
| 116 |
sThreadLocal.set(new Looper(quitAllowed)); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
@Deprecated |
| 127 |
public static void prepareMainLooper() { |
| 128 |
prepare(false); |
| 129 |
synchronized (Looper.class) { |
| 130 |
if (sMainLooper != null) { |
| 131 |
throw new IllegalStateException("The main Looper has already been prepared."); |
| 132 |
} |
| 133 |
sMainLooper = myLooper(); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public static Looper getMainLooper() { |
| 141 |
synchronized (Looper.class) { |
| 142 |
return sMainLooper; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public static void setObserver(@Nullable Observer observer) { |
| 152 |
sObserver = observer; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
@SuppressWarnings("AndroidFrameworkBinderIdentity") |
| 159 |
private static boolean loopOnce(final Looper me, |
| 160 |
final long ident, final int thresholdOverride) { |
| 161 |
Message msg = me.mQueue.next(); |
| 162 |
if (msg == null) { |
| 163 |
|
| 164 |
return false; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
final Printer logging = me.mLogging; |
| 169 |
if (logging != null) { |
| 170 |
logging.println(">>>>> Dispatching to " + msg.target + " " |
| 171 |
+ msg.callback + ": " + msg.what); |
| 172 |
} |
| 173 |
|
| 174 |
final Observer observer = sObserver; |
| 175 |
|
| 176 |
final long traceTag = me.mTraceTag; |
| 177 |
long slowDispatchThresholdMs = me.mSlowDispatchThresholdMs; |
| 178 |
long slowDeliveryThresholdMs = me.mSlowDeliveryThresholdMs; |
| 179 |
if (thresholdOverride > 0) { |
| 180 |
slowDispatchThresholdMs = thresholdOverride; |
| 181 |
slowDeliveryThresholdMs = thresholdOverride; |
| 182 |
} |
| 183 |
final boolean logSlowDelivery = (slowDeliveryThresholdMs > 0) && (msg.when > 0); |
| 184 |
final boolean logSlowDispatch = (slowDispatchThresholdMs > 0); |
| 185 |
|
| 186 |
final boolean needStartTime = logSlowDelivery || logSlowDispatch; |
| 187 |
final boolean needEndTime = logSlowDispatch; |
| 188 |
|
| 189 |
if (traceTag != 0 && Trace.isTagEnabled(traceTag)) { |
| 190 |
Trace.traceBegin(traceTag, msg.target.getTraceName(msg)); |
| 191 |
} |
| 192 |
|
| 193 |
final long dispatchStart = needStartTime ? SystemClock.uptimeMillis() : 0; |
| 194 |
final long dispatchEnd; |
| 195 |
Object token = null; |
| 196 |
if (observer != null) { |
| 197 |
token = observer.messageDispatchStarting(); |
| 198 |
} |
| 199 |
long origWorkSource = ThreadLocalWorkSource.setUid(msg.workSourceUid); |
| 200 |
try { |
| 201 |
msg.target.dispatchMessage(msg); |
| 202 |
if (observer != null) { |
| 203 |
observer.messageDispatched(token, msg); |
| 204 |
} |
| 205 |
dispatchEnd = needEndTime ? SystemClock.uptimeMillis() : 0; |
| 206 |
} catch (Exception exception) { |
| 207 |
if (observer != null) { |
| 208 |
observer.dispatchingThrewException(token, msg, exception); |
| 209 |
} |
| 210 |
throw exception; |
| 211 |
} finally { |
| 212 |
ThreadLocalWorkSource.restore(origWorkSource); |
| 213 |
if (traceTag != 0) { |
| 214 |
Trace.traceEnd(traceTag); |
| 215 |
} |
| 216 |
} |
| 217 |
if (logSlowDelivery) { |
| 218 |
if (me.mSlowDeliveryDetected) { |
| 219 |
if ((dispatchStart - msg.when) <= 10) { |
| 220 |
Slog.w(TAG, "Drained"); |
| 221 |
me.mSlowDeliveryDetected = false; |
| 222 |
} |
| 223 |
} else { |
| 224 |
if (showSlowLog(slowDeliveryThresholdMs, msg.when, dispatchStart, "delivery", |
| 225 |
msg)) { |
| 226 |
|
| 227 |
me.mSlowDeliveryDetected = true; |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
if (logSlowDispatch) { |
| 232 |
showSlowLog(slowDispatchThresholdMs, dispatchStart, dispatchEnd, "dispatch", msg); |
| 233 |
} |
| 234 |
|
| 235 |
if (logging != null) { |
| 236 |
logging.println("<<<<< Finished to " + msg.target + " " + msg.callback); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
final long newIdent = Binder.clearCallingIdentity(); |
| 242 |
if (ident != newIdent) { |
| 243 |
Log.wtf(TAG, "Thread identity changed from 0x" |
| 244 |
+ Long.toHexString(ident) + " to 0x" |
| 245 |
+ Long.toHexString(newIdent) + " while dispatching to " |
| 246 |
+ msg.target.getClass().getName() + " " |
| 247 |
+ msg.callback + " what=" + msg.what); |
| 248 |
} |
| 249 |
|
| 250 |
msg.recycleUnchecked(); |
| 251 |
|
| 252 |
return true; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
@SuppressWarnings("AndroidFrameworkBinderIdentity") |
| 260 |
public static void loop() { |
| 261 |
final Looper me = myLooper(); |
| 262 |
if (me == null) { |
| 263 |
throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread."); |
| 264 |
} |
| 265 |
if (me.mInLoop) { |
| 266 |
Slog.w(TAG, "Loop again would have the queued messages be executed" |
| 267 |
+ " before this one completed."); |
| 268 |
} |
| 269 |
|
| 270 |
me.mInLoop = true; |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
Binder.clearCallingIdentity(); |
| 275 |
final long ident = Binder.clearCallingIdentity(); |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
final int thresholdOverride = |
| 280 |
SystemProperties.getInt("log.looper." |
| 281 |
+ Process.myUid() + "." |
| 282 |
+ Thread.currentThread().getName() |
| 283 |
+ ".slow", 0); |
| 284 |
|
| 285 |
me.mSlowDeliveryDetected = false; |
| 286 |
|
| 287 |
for (;;) { |
| 288 |
if (!loopOnce(me, ident, thresholdOverride)) { |
| 289 |
return; |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
private static boolean showSlowLog(long threshold, long measureStart, long measureEnd, |
| 295 |
String what, Message msg) { |
| 296 |
final long actualTime = measureEnd - measureStart; |
| 297 |
if (actualTime < threshold) { |
| 298 |
return false; |
| 299 |
} |
| 300 |
|
| 301 |
Slog.w(TAG, "Slow " + what + " took " + actualTime + "ms " |
| 302 |
+ Thread.currentThread().getName() + " h=" |
| 303 |
+ msg.target.getClass().getName() + " c=" + msg.callback + " m=" + msg.what); |
| 304 |
return true; |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
public static @Nullable Looper myLooper() { |
| 312 |
return sThreadLocal.get(); |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
public static @NonNull MessageQueue myQueue() { |
| 321 |
return myLooper().mQueue; |
| 322 |
} |
| 323 |
|
| 324 |
private Looper(boolean quitAllowed) { |
| 325 |
mQueue = new MessageQueue(quitAllowed); |
| 326 |
mThread = Thread.currentThread(); |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
public boolean isCurrentThread() { |
| 333 |
return Thread.currentThread() == mThread; |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
public void setMessageLogging(@Nullable Printer printer) { |
| 346 |
mLogging = printer; |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
@UnsupportedAppUsage |
| 351 |
public void setTraceTag(long traceTag) { |
| 352 |
mTraceTag = traceTag; |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
public void setSlowLogThresholdMs(long slowDispatchThresholdMs, long slowDeliveryThresholdMs) { |
| 360 |
mSlowDispatchThresholdMs = slowDispatchThresholdMs; |
| 361 |
mSlowDeliveryThresholdMs = slowDeliveryThresholdMs; |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
public void quit() { |
| 381 |
mQueue.quit(false); |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
public void quitSafely() { |
| 397 |
mQueue.quit(true); |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
public @NonNull Thread getThread() { |
| 406 |
return mThread; |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
public @NonNull MessageQueue getQueue() { |
| 415 |
return mQueue; |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
public void dump(@NonNull Printer pw, @NonNull String prefix) { |
| 425 |
pw.println(prefix + toString()); |
| 426 |
mQueue.dump(pw, prefix + " ", null); |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
public void dump(@NonNull Printer pw, @NonNull String prefix, Handler handler) { |
| 438 |
pw.println(prefix + toString()); |
| 439 |
mQueue.dump(pw, prefix + " ", handler); |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
public void dumpDebug(ProtoOutputStream proto, long fieldId) { |
| 444 |
final long looperToken = proto.start(fieldId); |
| 445 |
proto.write(LooperProto.THREAD_NAME, mThread.getName()); |
| 446 |
proto.write(LooperProto.THREAD_ID, mThread.getId()); |
| 447 |
if (mQueue != null) { |
| 448 |
mQueue.dumpDebug(proto, LooperProto.QUEUE); |
| 449 |
} |
| 450 |
proto.end(looperToken); |
| 451 |
} |
| 452 |
|
| 453 |
@Override |
| 454 |
public String toString() { |
| 455 |
return "Looper (" + mThread.getName() + ", tid " + mThread.getId() |
| 456 |
+ ") {" + Integer.toHexString(System.identityHashCode(this)) + "}"; |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
public interface Observer { |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
Object messageDispatchStarting(); |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
void messageDispatched(Object token, Message msg); |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
void dispatchingThrewException(Object token, Message msg, Exception exception); |
| 492 |
} |
| 493 |
} |