mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-24 23:36:04 +00:00
fix(input): normalize hardware Escape before Back handlers (#2976)
This commit is contained in:
3 files changed
+67
No files matched your search
@@ -3552,6 +3552,15 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
|
||||
|
||||
|
||||
override fun dispatchKeyEvent(event: KeyEvent): Boolean {
|
||||
val normalized = com.droiddeck.launcher.input.GuestKeys.normalizeKeyboardEscape(event)
|
||||
if (normalized === event) return dispatchSessionKeyEvent(event)
|
||||
if (event.action == KeyEvent.ACTION_DOWN && event.repeatCount == 0) {
|
||||
Log.i(TAG, "hardware Esc normalized from Back device=${event.deviceId} scan=${event.scanCode}")
|
||||
}
|
||||
return dispatchSessionKeyEvent(normalized)
|
||||
}
|
||||
|
||||
private fun dispatchSessionKeyEvent(event: KeyEvent): Boolean {
|
||||
if (com.droiddeck.launcher.ui.AgentPet.handleBack(this, event)) return true
|
||||
when (event.keyCode) {
|
||||
KeyEvent.KEYCODE_VOLUME_UP, KeyEvent.KEYCODE_VOLUME_DOWN, KeyEvent.KEYCODE_VOLUME_MUTE -> {
|
||||
|
||||
@@ -3,6 +3,12 @@ package com.droiddeck.launcher.input
|
||||
import android.view.KeyEvent
|
||||
|
||||
object GuestKeys {
|
||||
fun normalizeKeyboardEscape(event: KeyEvent): KeyEvent {
|
||||
if (!isKeyboardEscape(event.keyCode, event.scanCode)) return event
|
||||
return KeyEvent(event.downTime, event.eventTime, event.action, KeyEvent.KEYCODE_ESCAPE,
|
||||
event.repeatCount, event.metaState, event.deviceId, event.scanCode, event.flags, event.source)
|
||||
}
|
||||
|
||||
/** Sends [event] as evdev presses, with Shift around characters typed by a keyboard that has no Shift key; false when it has no evdev code. */
|
||||
fun send(event: KeyEvent, sendKey: (code: Int, down: Boolean) -> Unit): Boolean {
|
||||
val down = event.action == KeyEvent.ACTION_DOWN
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import android.view.InputDevice
|
||||
import android.view.KeyEvent
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [34], manifest = Config.NONE)
|
||||
class GuestKeysTest {
|
||||
@Test fun hardwareEscapeBypassesBackMenuAndReachesGuestWithMatchingRelease() {
|
||||
val taps = mutableListOf<Boolean>()
|
||||
val menu = BackQamInput({}, taps::add)
|
||||
val guest = mutableListOf<Pair<Int, Boolean>>()
|
||||
for (action in listOf(KeyEvent.ACTION_DOWN, KeyEvent.ACTION_UP)) {
|
||||
val event = KeyEvent(10, 20, action, KeyEvent.KEYCODE_BACK, 0,
|
||||
KeyEvent.META_SHIFT_ON, 43, 1, 0, InputDevice.SOURCE_KEYBOARD)
|
||||
val normalized = GuestKeys.normalizeKeyboardEscape(event)
|
||||
assertEquals(KeyEvent.KEYCODE_ESCAPE, normalized.keyCode)
|
||||
assertEquals(event.action, normalized.action)
|
||||
assertEquals(event.metaState, normalized.metaState)
|
||||
assertEquals(event.deviceId, normalized.deviceId)
|
||||
assertEquals(event.scanCode, normalized.scanCode)
|
||||
assertEquals(event.source, normalized.source)
|
||||
assertFalse(menu.onKeyEvent(normalized, true))
|
||||
assertTrue(GuestKeys.send(normalized) { code, down -> guest += code to down })
|
||||
}
|
||||
assertEquals(listOf(1 to true, 1 to false), guest)
|
||||
assertTrue(taps.isEmpty())
|
||||
}
|
||||
|
||||
@Test fun navigationBackAndOtherPhysicalBackKeysAreUnchanged() {
|
||||
for (scan in listOf(0, 158)) {
|
||||
val event = KeyEvent(10, 20, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK,
|
||||
0, 0, 43, scan, 0, InputDevice.SOURCE_KEYBOARD)
|
||||
assertSame(event, GuestKeys.normalizeKeyboardEscape(event))
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun normalizedEscapePreservesRepeatAndCancellation() {
|
||||
val event = KeyEvent(10, 20, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK,
|
||||
3, 0, 43, 1, KeyEvent.FLAG_CANCELED, InputDevice.SOURCE_KEYBOARD)
|
||||
val normalized = GuestKeys.normalizeKeyboardEscape(event)
|
||||
assertEquals(event.repeatCount, normalized.repeatCount)
|
||||
assertEquals(event.flags, normalized.flags)
|
||||
assertEquals(event.downTime, normalized.downTime)
|
||||
assertEquals(event.eventTime, normalized.eventTime)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user