@@ -4,8 +4,6 @@ import android.bluetooth.BluetoothDevice
44import android.bluetooth.BluetoothManager
55import android.content.Context
66import android.hardware.input.InputManager
7- import android.os.Handler
8- import android.os.Looper
97import android.view.InputDevice
108import androidx.core.content.getSystemService
119import dagger.hilt.android.qualifiers.ApplicationContext
@@ -37,7 +35,8 @@ class AndroidDevicesAdapter @Inject constructor(
3735 private val bluetoothAdapter : io.github.sds100.keymapper.system.bluetooth.BluetoothAdapter ,
3836 private val permissionAdapter : PermissionAdapter ,
3937 private val coroutineScope : CoroutineScope ,
40- ) : DevicesAdapter {
38+ ) : DevicesAdapter,
39+ InputManager .InputDeviceListener {
4140
4241 private val ctx = context.applicationContext
4342 private val inputManager = ctx.getSystemService<InputManager >()
@@ -69,40 +68,7 @@ class AndroidDevicesAdapter @Inject constructor(
6968 }
7069 }
7170
72- inputManager?.apply {
73- registerInputDeviceListener(
74- object : InputManager .InputDeviceListener {
75- override fun onInputDeviceAdded (deviceId : Int ) {
76- coroutineScope.launch {
77- val device = InputDevice .getDevice(deviceId) ? : return @launch
78- onInputDeviceConnect.emit(
79- InputDeviceUtils .createInputDeviceInfo(device),
80- )
81-
82- updateInputDevices()
83- }
84- }
85-
86- override fun onInputDeviceRemoved (deviceId : Int ) {
87- coroutineScope.launch {
88- connectedInputDevices.value.ifIsData { connectedInputDevices ->
89- val device = connectedInputDevices.find { it.id == deviceId }
90- ? : return @ifIsData
91-
92- onInputDeviceDisconnect.emit(device)
93- }
94-
95- updateInputDevices()
96- }
97- }
98-
99- override fun onInputDeviceChanged (deviceId : Int ) {
100- updateInputDevices()
101- }
102- },
103- Handler (Looper .getMainLooper()),
104- )
105- }
71+ inputManager?.registerInputDeviceListener(this , null )
10672
10773 bluetoothAdapter.onDeviceConnect.onEach { device ->
10874 val currentValue = connectedBluetoothDevices.value
@@ -146,6 +112,34 @@ class AndroidDevicesAdapter @Inject constructor(
146112 return InputDevice .getDevice(deviceId)?.let { InputDeviceUtils .createInputDeviceInfo(it) }
147113 }
148114
115+ override fun onInputDeviceAdded (deviceId : Int ) {
116+ coroutineScope.launch {
117+ val device = InputDevice .getDevice(deviceId) ? : return @launch
118+ onInputDeviceConnect.emit(
119+ InputDeviceUtils .createInputDeviceInfo(device),
120+ )
121+
122+ updateInputDevices()
123+ }
124+ }
125+
126+ override fun onInputDeviceRemoved (deviceId : Int ) {
127+ coroutineScope.launch {
128+ connectedInputDevices.value.ifIsData { connectedInputDevices ->
129+ val device = connectedInputDevices.find { it.id == deviceId }
130+ ? : return @ifIsData
131+
132+ onInputDeviceDisconnect.emit(device)
133+ }
134+
135+ updateInputDevices()
136+ }
137+ }
138+
139+ override fun onInputDeviceChanged (deviceId : Int ) {
140+ updateInputDevices()
141+ }
142+
149143 private fun updateInputDevices () {
150144 val devices = mutableListOf<InputDeviceInfo >()
151145
0 commit comments