From 7b65e61cadd8da36993bb00ce767e06638ad5804 Mon Sep 17 00:00:00 2001 From: Goooler Date: Tue, 4 Jan 2022 16:48:58 +0800 Subject: [PATCH] Add `isMainThread` extension to check thread --- .../src/main/java/leakcanary/internal/Handlers.kt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/leakcanary-android-utils/src/main/java/leakcanary/internal/Handlers.kt b/leakcanary-android-utils/src/main/java/leakcanary/internal/Handlers.kt index 1f2a09e747..9e6b6a3136 100644 --- a/leakcanary-android-utils/src/main/java/leakcanary/internal/Handlers.kt +++ b/leakcanary-android-utils/src/main/java/leakcanary/internal/Handlers.kt @@ -5,14 +5,16 @@ import android.os.Looper internal val mainHandler by lazy { Handler(Looper.getMainLooper()) } +internal val isMainThread: Boolean get() = Looper.getMainLooper().thread === Thread.currentThread() + internal fun checkMainThread() { - check(Looper.getMainLooper().thread === Thread.currentThread()) { + check(isMainThread) { "Should be called from the main thread, not ${Thread.currentThread()}" } } internal fun checkNotMainThread() { - check(Looper.getMainLooper().thread !== Thread.currentThread()) { + check(!isMainThread) { "Should not be called from the main thread" } }