mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-06 04:55:44 +00:00
Fix Air Triggers on RedMagic phones with unnamed input devices (#3755)
Recognize RedMagic triggers merged into an unnamed keyboard
This commit is contained in:
1 parent
b63a72affb
commit
0b7d7aa634
2 files changed
+47
-11
No files matched your search
@@ -3,6 +3,7 @@ package com.droiddeck.launcher.input
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.Handler
|
||||
import android.os.IBinder
|
||||
import android.os.Looper
|
||||
@@ -23,12 +24,20 @@ enum class AirTrigger(val keyCode: Int, val control: InputControl, val pressLigh
|
||||
LEFT(KeyEvent.KEYCODE_F7, InputControl.LT, 106, 107), RIGHT(KeyEvent.KEYCODE_F8, InputControl.RT, 108, 109);
|
||||
|
||||
companion object {
|
||||
// InputReader merges both trigger nodes into one device named after the left one.
|
||||
private const val DEVICE_PREFIX = "nubia_tgk"
|
||||
|
||||
fun isTriggerDevice(name: String?) = name?.startsWith(DEVICE_PREFIX) == true
|
||||
fun of(keyCode: Int, deviceName: String?): AirTrigger? =
|
||||
if (isTriggerDevice(deviceName)) entries.firstOrNull { it.keyCode == keyCode } else null
|
||||
internal fun isTriggerDevice(device: InputDevice?): Boolean {
|
||||
if (device == null) return false
|
||||
if (device.name.startsWith(DEVICE_PREFIX)) return true
|
||||
// Some RedMagic firmware merges the trigger sensors with an unnamed built-in keyboard.
|
||||
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q &&
|
||||
device.name.isEmpty() && !device.isVirtual && !device.isExternal &&
|
||||
device.supportsSource(InputDevice.SOURCE_KEYBOARD) &&
|
||||
device.hasKeys(LEFT.keyCode, RIGHT.keyCode).all { it }
|
||||
}
|
||||
|
||||
internal fun of(keyCode: Int, device: InputDevice?): AirTrigger? =
|
||||
entries.firstOrNull { it.keyCode == keyCode }?.takeIf { isTriggerDevice(device) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,7 +127,7 @@ object RedMagicAirTriggers {
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
fun supported(context: Context) = context.packageManager.resolveContentProvider(AUTHORITY, 0) != null &&
|
||||
InputDevice.getDeviceIds().any { AirTrigger.isTriggerDevice(InputDevice.getDevice(it)?.name) }
|
||||
InputDevice.getDeviceIds().any { AirTrigger.isTriggerDevice(InputDevice.getDevice(it)) }
|
||||
|
||||
/** Call on the main thread. */
|
||||
fun open(context: Context) {
|
||||
@@ -240,8 +249,8 @@ class AirTriggerInput(
|
||||
}
|
||||
|
||||
fun onKey(event: KeyEvent, enabled: () -> Boolean): Boolean {
|
||||
if (AirTrigger.entries.none { it.keyCode == event.keyCode }) return false
|
||||
val trigger = AirTrigger.of(event.keyCode, event.device?.name) ?: return false
|
||||
if (event.keyCode != AirTrigger.LEFT.keyCode && event.keyCode != AirTrigger.RIGHT.keyCode) return false
|
||||
val trigger = AirTrigger.of(event.keyCode, event.device) ?: return false
|
||||
return onKey(trigger, event.action, event.repeatCount, event.action == KeyEvent.ACTION_DOWN && enabled())
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import android.hardware.input.InputManager
|
||||
import android.os.Looper
|
||||
import android.provider.Settings
|
||||
import android.view.InputDevice
|
||||
import android.view.KeyCharacterMap
|
||||
import android.view.KeyEvent
|
||||
import java.io.File
|
||||
import java.time.Duration
|
||||
@@ -15,6 +18,7 @@ import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.shadows.InputDeviceBuilder
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [34], manifest = Config.NONE, shadows = [JoyConPadTest.RecordingWriter::class])
|
||||
@@ -49,12 +53,35 @@ class RedMagicAirTriggersTest {
|
||||
private fun up(trigger: AirTrigger) = input.onKey(trigger, KeyEvent.ACTION_UP, 0, true)
|
||||
private fun idle(ms: Long) = shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(ms))
|
||||
|
||||
private fun keyboard(id: Int, name: String, vararg keys: Int, external: Boolean = false): InputDevice {
|
||||
val device = InputDeviceBuilder.newBuilder().setId(id).setName(name).setDescriptor("keyboard-$id")
|
||||
.setExternal(external).setSources(InputDevice.SOURCE_KEYBOARD)
|
||||
.setKeyCharacterMap(KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD)).build()
|
||||
val manager = shadowOf(context.getSystemService(InputManager::class.java))
|
||||
manager.addInputDevice(device)
|
||||
manager.addInputDeviceKeys(id, keys)
|
||||
return device
|
||||
}
|
||||
|
||||
@Test fun onlyTheTriggerDeviceKeysAreAirTriggers() {
|
||||
assertEquals(AirTrigger.LEFT, AirTrigger.of(KeyEvent.KEYCODE_F7, "nubia_tgk_aw_sar0_ch0"))
|
||||
assertEquals(AirTrigger.RIGHT, AirTrigger.of(KeyEvent.KEYCODE_F8, "nubia_tgk_aw_sar0_ch0"))
|
||||
assertNull(AirTrigger.of(KeyEvent.KEYCODE_F7, "Keychron K2"))
|
||||
val triggers = keyboard(10, "nubia_tgk_aw_sar0_ch0")
|
||||
val externalKeyboard = keyboard(11, "Keychron K2", KeyEvent.KEYCODE_F7, KeyEvent.KEYCODE_F8, external = true)
|
||||
assertEquals(AirTrigger.LEFT, AirTrigger.of(KeyEvent.KEYCODE_F7, triggers))
|
||||
assertEquals(AirTrigger.RIGHT, AirTrigger.of(KeyEvent.KEYCODE_F8, triggers))
|
||||
assertNull(AirTrigger.of(KeyEvent.KEYCODE_F7, externalKeyboard))
|
||||
assertNull(AirTrigger.of(KeyEvent.KEYCODE_F8, null))
|
||||
assertNull(AirTrigger.of(KeyEvent.KEYCODE_VOLUME_DOWN, "nubia_tgk_aw_sar0_ch0"))
|
||||
assertNull(AirTrigger.of(KeyEvent.KEYCODE_VOLUME_DOWN, triggers))
|
||||
}
|
||||
|
||||
@Test fun theUnnamedInternalKeyboardCanContainBothTriggers() {
|
||||
val left = KeyEvent.KEYCODE_F7
|
||||
val right = KeyEvent.KEYCODE_F8
|
||||
val merged = keyboard(10, "", left, right, KeyEvent.KEYCODE_POWER)
|
||||
assertEquals(AirTrigger.LEFT, AirTrigger.of(left, merged))
|
||||
assertEquals(AirTrigger.RIGHT, AirTrigger.of(right, merged))
|
||||
assertNull(AirTrigger.of(left, keyboard(11, "", left, right, external = true)))
|
||||
assertNull(AirTrigger.of(left, keyboard(12, "", left)))
|
||||
assertNull(AirTrigger.of(left, keyboard(-1, "", left, right)))
|
||||
}
|
||||
|
||||
@Test fun theTriggerLightUsesTheCodeRedMagicsSettingsGiveTheChosenEffect() {
|
||||
|
||||
Reference in new issue
Block a user