@@ -7,7 +7,6 @@ import android.view.*
77import androidx.annotation.*
88import kotlinx.coroutines.*
99import kotlinx.coroutines.internal.*
10- import java.lang.reflect.*
1110import kotlin.coroutines.*
1211
1312/* *
@@ -77,27 +76,16 @@ public fun Handler.asCoroutineDispatcher(name: String? = null): HandlerDispatche
7776private const val MAX_DELAY = Long .MAX_VALUE / 2 // cannot delay for too long on Android
7877
7978@VisibleForTesting
80- internal fun Looper.asHandler (async : Boolean ): Handler {
79+ internal fun Looper.asHandler (async : Boolean ): Handler = when {
8180 // Async support was added in API 16.
82- if (! async || Build .VERSION .SDK_INT < 16 ) {
83- return Handler (this )
84- }
85-
86- if (Build .VERSION .SDK_INT >= 28 ) {
87- // TODO compile against API 28 so this can be invoked without reflection.
88- val factoryMethod = Handler ::class .java.getDeclaredMethod(" createAsync" , Looper ::class .java)
89- return factoryMethod.invoke(null , this ) as Handler
90- }
91-
92- val constructor : Constructor <Handler >
93- try {
94- constructor = Handler ::class .java.getDeclaredConstructor(Looper ::class .java,
95- Handler .Callback ::class .java, Boolean ::class .javaPrimitiveType)
96- } catch (ignored: NoSuchMethodException ) {
81+ ! async || Build .VERSION .SDK_INT < 16 -> Handler (this )
82+ Build .VERSION .SDK_INT >= 28 -> Handler .createAsync(this )
83+ else -> try {
84+ Handler (this , null , true )
85+ } catch (ignored: NoSuchMethodError ) {
9786 // Hidden constructor absent. Fall back to non-async constructor.
98- return Handler (this )
87+ Handler (this )
9988 }
100- return constructor .newInstance(this , null , true )
10189}
10290
10391@JvmField
0 commit comments