Skip to content

Commit

Permalink
Use Dispatchers.Default for interactive input to improve responsivene…
Browse files Browse the repository at this point in the history
…ss of input events.
  • Loading branch information
iiordanov committed Jan 20, 2024
1 parent d7c97ab commit 86cf13e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import android.view.MotionEvent
import android.view.View
import com.iiordanov.bVNC.Constants
import com.iiordanov.util.NetworkUtils
import kotlinx.coroutines.Dispatchers
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors

Expand All @@ -53,17 +54,17 @@ class RemoteClientsInputListener(
}
try {
if (evt.action == KeyEvent.ACTION_DOWN || evt.action == KeyEvent.ACTION_MULTIPLE) {
consumed = NetworkUtils.tryRunningCoroutineWithTimeout {
consumed = NetworkUtils.tryRunningCoroutineWithTimeout({
keyInputHandler?.onKeyDownEvent(
keyCode, evt
)
}
}, Dispatchers.Default)
} else if (evt.action == KeyEvent.ACTION_UP) {
consumed = NetworkUtils.tryRunningCoroutineWithTimeout {
consumed = NetworkUtils.tryRunningCoroutineWithTimeout({
keyInputHandler?.onKeyUpEvent(
keyCode, evt
)
}
}, Dispatchers.Default)
}
resetOnScreenKeys(keyCode)
} catch (e: NullPointerException) {
Expand Down Expand Up @@ -110,11 +111,11 @@ class RemoteClientsInputListener(
fun onTrackballEvent(event: MotionEvent?): Boolean {
try {
// If we are using the Dpad as arrow keys, don't send the event to the inputHandler.
return if (useDpadAsArrows) false else NetworkUtils.tryRunningCoroutineWithTimeout {
return if (useDpadAsArrows) false else NetworkUtils.tryRunningCoroutineWithTimeout({
this.touchInputHandler?.onTouchEvent(
event
)
}
}, Dispatchers.Default)
} catch (e: NullPointerException) {
Log.e(tag, "NullPointerException ignored")
}
Expand All @@ -124,11 +125,11 @@ class RemoteClientsInputListener(
// Send touch events or mouse events like button clicks to be handled.
fun onTouchEvent(event: MotionEvent?): Boolean {
try {
return NetworkUtils.tryRunningCoroutineWithTimeout {
return NetworkUtils.tryRunningCoroutineWithTimeout({
this.touchInputHandler?.onTouchEvent(
event
)
}
}, Dispatchers.Default)
} catch (e: NullPointerException) {
Log.e(tag, "NullPointerException ignored")
}
Expand All @@ -149,11 +150,11 @@ class RemoteClientsInputListener(
val isHoverEventFromFingerOnTouchscreen =
(a == MotionEvent.ACTION_HOVER_MOVE && event.source == InputDevice.SOURCE_TOUCHSCREEN && toolTypeFinger)
if (!(isHoverEnter || isHoverExit || isHoverEventFromFingerOnTouchscreen)) {
return NetworkUtils.tryRunningCoroutineWithTimeout {
return NetworkUtils.tryRunningCoroutineWithTimeout({
this.touchInputHandler?.onTouchEvent(
event
)
}
}, Dispatchers.Default)
}
return false
}
Expand Down
18 changes: 11 additions & 7 deletions bVNC/src/main/java/com/iiordanov/util/NetworkUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.iiordanov.util

import android.util.Log
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
Expand All @@ -32,25 +33,28 @@ object NetworkUtils {
private const val tag: String = "NetworkUtils"

fun isValidIpv6Address(address: String?): Boolean {
return tryRunningCoroutineWithTimeout {
InetAddress.getByName(address) is Inet6Address
}
return tryRunningCoroutineWithTimeout(
{
InetAddress.getByName(address) is Inet6Address
},
Dispatchers.IO
)
}

fun tryRunningCoroutineWithTimeout(
block: (CoroutineScope) -> Boolean?
block: (CoroutineScope) -> Boolean?, dispatcher: CoroutineDispatcher
): Boolean {
return runBlocking {
this.tryRunningWithTimeoutAsync(block, false, 1000L)
this.tryRunningWithTimeoutAsync(block, false, 1000L, dispatcher)
} ?: false
}

private suspend inline fun <T, R> T.tryRunningWithTimeoutAsync(
crossinline block: T.() -> R, default: R, timeout: Long
crossinline block: T.() -> R, default: R, timeout: Long, dispatcher: CoroutineDispatcher
): R {
return try {
withTimeout(timeout) {
withContext(Dispatchers.IO) {
withContext(dispatcher) {
block()
}
}
Expand Down

0 comments on commit 86cf13e

Please sign in to comment.