Implementing real KeyboardBacklightListener in KeyboardRepository

Also adding MONITOR_KEYBOARD_BACKLIGHT permission which is required for
that listener

Flag: KEYBOARD_BACKLIGHT_INDICATOR
Fixes: 268645734
Fixes: 268638509
Test: soon
Change-Id: I95f224ac6d46fccdbd27eae0b49ff63f9f9e070f
This commit is contained in:
Michal Brzezinski
2023-02-28 11:46:07 +00:00
parent 3521df06db
commit 6879c2c9cf
2 changed files with 26 additions and 8 deletions

View File

@@ -341,6 +341,8 @@
<uses-permission android:name="android.permission.SET_UNRESTRICTED_KEEP_CLEAR_AREAS" />
<uses-permission android:name="android.permission.MONITOR_KEYBOARD_BACKLIGHT" />
<protected-broadcast android:name="com.android.settingslib.action.REGISTER_SLICE_RECEIVER" />
<protected-broadcast android:name="com.android.settingslib.action.UNREGISTER_SLICE_RECEIVER" />
<protected-broadcast android:name="com.android.settings.flashlight.action.FLASHLIGHT_CHANGED" />

View File

@@ -18,15 +18,19 @@
package com.android.systemui.keyboard.data.repository
import android.hardware.input.InputManager
import android.hardware.input.InputManager.KeyboardBacklightListener
import android.hardware.input.KeyboardBacklightState
import com.android.systemui.common.coroutine.ChannelExt.trySendWithFailureLogging
import com.android.systemui.common.coroutine.ConflatedCallbackFlow.conflatedCallbackFlow
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.keyboard.data.model.BacklightModel
import java.util.concurrent.Executor
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
@@ -51,24 +55,22 @@ constructor(
private val connectedDeviceIds: Flow<Set<Int>> =
conflatedCallbackFlow {
fun send(element: Set<Int>) = trySendWithFailureLogging(element, TAG)
var connectedKeyboards = inputManager.inputDeviceIds.toSet()
val listener =
object : InputManager.InputDeviceListener {
override fun onInputDeviceAdded(deviceId: Int) {
connectedKeyboards = connectedKeyboards + deviceId
send(connectedKeyboards)
sendWithLogging(connectedKeyboards)
}
override fun onInputDeviceChanged(deviceId: Int) = Unit
override fun onInputDeviceRemoved(deviceId: Int) {
connectedKeyboards = connectedKeyboards - deviceId
send(connectedKeyboards)
sendWithLogging(connectedKeyboards)
}
}
send(connectedKeyboards)
sendWithLogging(connectedKeyboards)
inputManager.registerInputDeviceListener(listener, /* handler= */ null)
awaitClose { inputManager.unregisterInputDeviceListener(listener) }
}
@@ -78,6 +80,16 @@ constructor(
replay = 1,
)
private val backlightStateListener: Flow<KeyboardBacklightState> = conflatedCallbackFlow {
val listener = KeyboardBacklightListener { _, state, isTriggeredByKeyPress ->
if (isTriggeredByKeyPress) {
sendWithLogging(state)
}
}
inputManager.registerKeyboardBacklightListener(Executor(Runnable::run), listener)
awaitClose { inputManager.unregisterKeyboardBacklightListener(listener) }
}
override val keyboardConnected: Flow<Boolean> =
connectedDeviceIds
.map { it.any { deviceId -> isPhysicalFullKeyboard(deviceId) } }
@@ -85,9 +97,13 @@ constructor(
.flowOn(backgroundDispatcher)
override val backlight: Flow<BacklightModel> =
conflatedCallbackFlow {
// TODO(b/268645734) register BacklightListener
}
backlightStateListener
.map { BacklightModel(it.brightnessLevel, it.maxBrightnessLevel) }
.flowOn(backgroundDispatcher)
private fun <T> SendChannel<T>.sendWithLogging(element: T) {
trySendWithFailureLogging(element, TAG)
}
private fun isPhysicalFullKeyboard(deviceId: Int): Boolean {
val device = inputManager.getInputDevice(deviceId)