feat: add volume keys for page turning, closes #471 (#982)

This commit is contained in:
Huang Xin 2025-04-28 18:36:24 +08:00 committed by GitHub
parent a424ae8b15
commit 4275508ccd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
45 changed files with 692 additions and 174 deletions

View file

@ -6,6 +6,7 @@ import android.net.Uri
import android.util.Log
import android.os.Build
import android.view.View
import android.view.KeyEvent
import android.view.WindowInsets
import android.view.WindowManager
import android.view.WindowInsetsController
@ -49,6 +50,17 @@ class SetSystemUIVisibilityRequestArgs {
var darkMode: Boolean? = false
}
@InvokeArg
class InterceptKeysRequestArgs {
var volumeKeys: Boolean? = null
var backKey: Boolean? = null
}
interface KeyDownInterceptor {
fun interceptVolumeKeys(enabled: Boolean)
fun interceptBackKey(enabled: Boolean)
}
@TauriPlugin
class NativeBridgePlugin(private val activity: Activity): Plugin(activity) {
private val implementation = NativeBridge()
@ -289,4 +301,24 @@ class NativeBridgePlugin(private val activity: Activity): Plugin(activity) {
}
invoke.resolve(ret)
}
@Command
fun intercept_keys(invoke: Invoke) {
val args = invoke.parseArgs(InterceptKeysRequestArgs::class.java)
if (activity is KeyDownInterceptor) {
when (args.backKey) {
true -> (activity as KeyDownInterceptor).interceptBackKey(true)
false -> (activity as KeyDownInterceptor).interceptBackKey(false)
else -> {}
}
when (args.volumeKeys) {
true -> (activity as KeyDownInterceptor).interceptVolumeKeys(true)
false -> (activity as KeyDownInterceptor).interceptVolumeKeys(false)
else -> {}
}
} else {
Log.e("NativeBridgePlugin", "Activity does not implement KeyDownInterceptor")
}
invoke.resolve()
}
}