Roman Elizarov | 23f864e | 2017-03-03 19:57:47 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | 23f864e | 2017-03-03 19:57:47 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | package kotlinx.coroutines.experimental.android |
| 6 | |
| 7 | import android.os.Handler |
| 8 | import android.os.Looper |
Roman Elizarov | d1f50a3 | 2017-11-09 12:09:06 +0300 | [diff] [blame] | 9 | import android.view.Choreographer |
Roman Elizarov | 30dd5c1 | 2017-04-11 13:50:03 +0300 | [diff] [blame] | 10 | import kotlinx.coroutines.experimental.* |
Roman Elizarov | 23f864e | 2017-03-03 19:57:47 +0300 | [diff] [blame] | 11 | import java.util.concurrent.TimeUnit |
| 12 | import kotlin.coroutines.experimental.CoroutineContext |
| 13 | |
| 14 | /** |
| 15 | * Dispatches execution onto Android main UI thread and provides native [delay][Delay.delay] support. |
| 16 | */ |
| 17 | val UI = HandlerContext(Handler(Looper.getMainLooper()), "UI") |
| 18 | |
| 19 | /** |
| 20 | * Represents an arbitrary [Handler] as a implementation of [CoroutineDispatcher]. |
| 21 | */ |
| 22 | fun Handler.asCoroutineDispatcher() = HandlerContext(this) |
| 23 | |
Roman Elizarov | 7c507aa | 2017-11-10 14:34:06 +0300 | [diff] [blame] | 24 | private const val MAX_DELAY = Long.MAX_VALUE / 2 // cannot delay for too long on Android |
| 25 | |
Roman Elizarov | 23f864e | 2017-03-03 19:57:47 +0300 | [diff] [blame] | 26 | /** |
| 27 | * Implements [CoroutineDispatcher] on top of an arbitrary Android [Handler]. |
| 28 | * @param handler a handler. |
| 29 | * @param name an optional name for debugging. |
| 30 | */ |
| 31 | public class HandlerContext( |
| 32 | private val handler: Handler, |
| 33 | private val name: String? = null |
| 34 | ) : CoroutineDispatcher(), Delay { |
Roman Elizarov | d1f50a3 | 2017-11-09 12:09:06 +0300 | [diff] [blame] | 35 | @Volatile |
| 36 | private var _choreographer: Choreographer? = null |
| 37 | |
Roman Elizarov | 23f864e | 2017-03-03 19:57:47 +0300 | [diff] [blame] | 38 | override fun dispatch(context: CoroutineContext, block: Runnable) { |
| 39 | handler.post(block) |
| 40 | } |
| 41 | |
| 42 | override fun scheduleResumeAfterDelay(time: Long, unit: TimeUnit, continuation: CancellableContinuation<Unit>) { |
| 43 | handler.postDelayed({ |
| 44 | with(continuation) { resumeUndispatched(Unit) } |
Roman Elizarov | 7c507aa | 2017-11-10 14:34:06 +0300 | [diff] [blame] | 45 | }, unit.toMillis(time).coerceAtMost(MAX_DELAY)) |
Roman Elizarov | 23f864e | 2017-03-03 19:57:47 +0300 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | override fun invokeOnTimeout(time: Long, unit: TimeUnit, block: Runnable): DisposableHandle { |
Roman Elizarov | 7c507aa | 2017-11-10 14:34:06 +0300 | [diff] [blame] | 49 | handler.postDelayed(block, unit.toMillis(time).coerceAtMost(MAX_DELAY)) |
Roman Elizarov | 23f864e | 2017-03-03 19:57:47 +0300 | [diff] [blame] | 50 | return object : DisposableHandle { |
| 51 | override fun dispose() { |
| 52 | handler.removeCallbacks(block) |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
Roman Elizarov | d1f50a3 | 2017-11-09 12:09:06 +0300 | [diff] [blame] | 57 | /** |
| 58 | * Awaits the next animation frame and returns frame time in nanoseconds. |
| 59 | */ |
| 60 | public suspend fun awaitFrame(): Long { |
| 61 | // fast path when choreographer is already known |
| 62 | val choreographer = _choreographer |
| 63 | if (choreographer != null) { |
| 64 | return suspendCancellableCoroutine { cont -> |
| 65 | postFrameCallback(choreographer, cont) |
| 66 | } |
| 67 | } |
| 68 | // post into looper thread thread to figure it out |
| 69 | return suspendCancellableCoroutine { cont -> |
| 70 | handler.post { |
| 71 | updateChoreographerAndPostFrameCallback(cont) |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | private fun updateChoreographerAndPostFrameCallback(cont: CancellableContinuation<Long>) { |
| 77 | val choreographer = _choreographer ?: |
| 78 | Choreographer.getInstance()!!.also { _choreographer = it } |
| 79 | postFrameCallback(choreographer, cont) |
| 80 | } |
| 81 | |
| 82 | private fun postFrameCallback(choreographer: Choreographer, cont: CancellableContinuation<Long>) { |
| 83 | choreographer.postFrameCallback { nanos -> |
| 84 | with(cont) { resumeUndispatched(nanos) } |
| 85 | } |
| 86 | } |
| 87 | |
Roman Elizarov | 23f864e | 2017-03-03 19:57:47 +0300 | [diff] [blame] | 88 | override fun toString() = name ?: handler.toString() |
Roman Elizarov | 30dd5c1 | 2017-04-11 13:50:03 +0300 | [diff] [blame] | 89 | override fun equals(other: Any?): Boolean = other is HandlerContext && other.handler === handler |
| 90 | override fun hashCode(): Int = System.identityHashCode(handler) |
Roman Elizarov | 23f864e | 2017-03-03 19:57:47 +0300 | [diff] [blame] | 91 | } |