Merge "Implementing real KeyboardBacklightListener in KeyboardRepository" into udc-dev

This commit is contained in:
Christian Göllner
2023-03-03 15:00:30 +00:00
committed by Android (Google) Code Review
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.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.REGISTER_SLICE_RECEIVER" />
<protected-broadcast android:name="com.android.settingslib.action.UNREGISTER_SLICE_RECEIVER" /> <protected-broadcast android:name="com.android.settingslib.action.UNREGISTER_SLICE_RECEIVER" />
<protected-broadcast android:name="com.android.settings.flashlight.action.FLASHLIGHT_CHANGED" /> <protected-broadcast android:name="com.android.settings.flashlight.action.FLASHLIGHT_CHANGED" />

View File

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