Skip to content

Commit 07c588a

Browse files
committed
#1461 fix: crash on startup due to getting MotionEvent device
1 parent 1e8b21a commit 07c588a

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

app/src/main/java/io/github/sds100/keymapper/mappings/keymaps/detection/DpadMotionEventTracker.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.sds100.keymapper.mappings.keymaps.detection
22

33
import android.view.KeyEvent
4+
import io.github.sds100.keymapper.system.devices.InputDeviceInfo
45
import io.github.sds100.keymapper.system.inputevents.InputEventUtils
56
import io.github.sds100.keymapper.system.inputevents.MyKeyEvent
67
import io.github.sds100.keymapper.system.inputevents.MyMotionEvent
@@ -62,11 +63,11 @@ class DpadMotionEventTracker {
6263
* @return An array of key events. Empty if no DPAD buttons changed.
6364
*/
6465
fun convertMotionEvent(event: MyMotionEvent): List<MyKeyEvent> {
65-
val oldState = dpadState[event.device.descriptor] ?: 0
66+
val oldState = dpadState[event.device.getDescriptor()] ?: 0
6667
val newState = eventToDpadState(event)
6768
val diff = oldState xor newState
6869

69-
dpadState[event.device.descriptor] = newState
70+
dpadState[event.device.getDescriptor()] = newState
7071

7172
// If no dpad keys changed then return null
7273
if (diff == 0) {
@@ -114,6 +115,10 @@ class DpadMotionEventTracker {
114115
dpadState.clear()
115116
}
116117

118+
private fun InputDeviceInfo?.getDescriptor(): String {
119+
return this?.descriptor ?: ""
120+
}
121+
117122
private fun eventToDpadState(event: MyMotionEvent): Int {
118123
var state = 0
119124

app/src/main/java/io/github/sds100/keymapper/system/accessibility/MyAccessibilityService.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,7 @@ class MyAccessibilityService :
111111
override fun onKeyEvent(event: KeyEvent?): Boolean {
112112
event ?: return false
113113

114-
val device = if (event.device == null) {
115-
null
116-
} else {
117-
InputDeviceUtils.createInputDeviceInfo(event.device)
118-
}
114+
val device = event.device?.let { InputDeviceUtils.createInputDeviceInfo(it) }
119115

120116
if (controller != null) {
121117
return controller!!.onKeyEventFromIme(

app/src/main/java/io/github/sds100/keymapper/system/inputevents/MyMotionEvent.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import io.github.sds100.keymapper.system.devices.InputDeviceUtils
1010
*/
1111
data class MyMotionEvent(
1212
val metaState: Int,
13-
val device: InputDeviceInfo,
13+
val device: InputDeviceInfo?,
1414
val axisHatX: Float,
1515
val axisHatY: Float,
1616
val isDpad: Boolean,
@@ -19,7 +19,7 @@ data class MyMotionEvent(
1919
fun fromMotionEvent(event: MotionEvent): MyMotionEvent {
2020
return MyMotionEvent(
2121
metaState = event.metaState,
22-
device = InputDeviceUtils.createInputDeviceInfo(event.device),
22+
device = event.device?.let { InputDeviceUtils.createInputDeviceInfo(it) },
2323
axisHatX = event.getAxisValue(MotionEvent.AXIS_HAT_X),
2424
axisHatY = event.getAxisValue(MotionEvent.AXIS_HAT_Y),
2525
isDpad = InputEventUtils.isDpadDevice(event),

0 commit comments

Comments
 (0)