pull/1391/head
gedoor 3 years ago
parent efc6c14ef4
commit 358b3e26f4
  1. 22
      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. */ /** This main looper cache avoids synchronization overhead when accessed repeatedly. */
@JvmField @JvmField
val mainLooper: Looper = Looper.getMainLooper()!! val mainLooper: Looper = Looper.getMainLooper()
@JvmField @JvmField
val mainThread: Thread = mainLooper.thread val mainThread: Thread = mainLooper.thread
@ -22,15 +22,17 @@ val isMainThread: Boolean inline get() = mainThread === Thread.currentThread()
internal val currentThread: Any? internal val currentThread: Any?
inline get() = Thread.currentThread() inline get() = Thread.currentThread()
@JvmField val mainHandler: Handler by lazy {
val mainHandler: Handler = if (SDK_INT >= 28) Handler.createAsync(mainLooper) else try { if (SDK_INT >= 28) Handler.createAsync(mainLooper) else try {
Handler::class.java.getDeclaredConstructor( Handler::class.java.getDeclaredConstructor(
Looper::class.java, Looper::class.java,
Handler.Callback::class.java, Handler.Callback::class.java,
Boolean::class.javaPrimitiveType // async Boolean::class.javaPrimitiveType // async
).newInstance(mainLooper, null, true) ).newInstance(mainLooper, null, true)
} catch (ignored: NoSuchMethodException) { } catch (ignored: NoSuchMethodException) {
Handler(mainLooper) // Hidden constructor absent. Fall back to non-async constructor. // Hidden constructor absent. Fall back to non-async constructor.
Handler(mainLooper)
}
} }
fun runOnUI(function: () -> Unit) { fun runOnUI(function: () -> Unit) {

Loading…
Cancel
Save