From d887c7d175fc409753271db89ae7d9bd90a5c339 Mon Sep 17 00:00:00 2001 From: Michal Brzezinski Date: Tue, 18 Apr 2023 18:33:18 +0100 Subject: [PATCH] Handling case when device returned from InputManager is null getInputDevice is @Nullable so we should handle that case and indicate the device is not valid keyboard Fixes: 278065638 Test: emitsDisconnected_whenDeviceWithIdDoesNotExist Change-Id: I7e267cc6809b12457de22173bf6450b355b5ab63 --- .../keyboard/data/repository/KeyboardRepository.kt | 2 +- .../data/repository/KeyboardRepositoryTest.kt | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/SystemUI/src/com/android/systemui/keyboard/data/repository/KeyboardRepository.kt b/packages/SystemUI/src/com/android/systemui/keyboard/data/repository/KeyboardRepository.kt index b86083abad211..1f1329111ce7e 100644 --- a/packages/SystemUI/src/com/android/systemui/keyboard/data/repository/KeyboardRepository.kt +++ b/packages/SystemUI/src/com/android/systemui/keyboard/data/repository/KeyboardRepository.kt @@ -106,7 +106,7 @@ constructor( } private fun isPhysicalFullKeyboard(deviceId: Int): Boolean { - val device = inputManager.getInputDevice(deviceId) + val device = inputManager.getInputDevice(deviceId) ?: return false return !device.isVirtual && device.isFullKeyboard } diff --git a/packages/SystemUI/tests/src/com/android/systemui/keyboard/data/repository/KeyboardRepositoryTest.kt b/packages/SystemUI/tests/src/com/android/systemui/keyboard/data/repository/KeyboardRepositoryTest.kt index f6ff4b214035e..6f9dedf9dda82 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/keyboard/data/repository/KeyboardRepositoryTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/keyboard/data/repository/KeyboardRepositoryTest.kt @@ -95,6 +95,16 @@ class KeyboardRepositoryTest : SysuiTestCase() { assertThat(isKeyboardConnected).isTrue() } + @Test + fun emitsDisconnected_whenDeviceWithIdDoesNotExist() = + testScope.runTest { + val deviceListener = captureDeviceListener() + val isKeyboardConnected by collectLastValue(underTest.keyboardConnected) + + deviceListener.onInputDeviceAdded(NULL_DEVICE_ID) + assertThat(isKeyboardConnected).isFalse() + } + @Test fun emitsDisconnected_whenKeyboardDisconnects() = testScope.runTest { @@ -172,6 +182,7 @@ class KeyboardRepositoryTest : SysuiTestCase() { private const val VIRTUAL_FULL_KEYBOARD_ID = 2 private const val PHYSICAL_NOT_FULL_KEYBOARD_ID = 3 private const val ANOTHER_PHYSICAL_FULL_KEYBOARD_ID = 4 + private const val NULL_DEVICE_ID = 5 private val INPUT_DEVICES_MAP: Map = mapOf(