pull/1391/head
gedoor 3 years ago
parent efc6c14ef4
commit 358b3e26f4
  1. 12
      app/src/main/java/io/legado/app/utils/HandlerUtils.kt

@ -11,7 +11,7 @@ import kotlinx.coroutines.launch
/** This main looper cache avoids synchronization overhead when accessed repeatedly. */
@JvmField
val mainLooper: Looper = Looper.getMainLooper()!!
val mainLooper: Looper = Looper.getMainLooper()
@JvmField
val mainThread: Thread = mainLooper.thread
@ -22,15 +22,17 @@ val isMainThread: Boolean inline get() = mainThread === Thread.currentThread()
internal val currentThread: Any?
inline get() = Thread.currentThread()
@JvmField
val mainHandler: Handler = if (SDK_INT >= 28) Handler.createAsync(mainLooper) else try {
val mainHandler: Handler by lazy {
if (SDK_INT >= 28) Handler.createAsync(mainLooper) else try {
Handler::class.java.getDeclaredConstructor(
Looper::class.java,
Handler.Callback::class.java,
Boolean::class.javaPrimitiveType // async
).newInstance(mainLooper, null, true)
} catch (ignored: NoSuchMethodException) {
Handler(mainLooper) // Hidden constructor absent. Fall back to non-async constructor.
} catch (ignored: NoSuchMethodException) {
// Hidden constructor absent. Fall back to non-async constructor.
Handler(mainLooper)
}
}
fun runOnUI(function: () -> Unit) {

Loading…
Cancel
Save