Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 64cb3540 authored by Christian Göllner's avatar Christian Göllner Committed by Automerger Merge Worker
Browse files

Merge "Implementing real KeyboardBacklightListener in KeyboardRepository" into...

Merge "Implementing real KeyboardBacklightListener in KeyboardRepository" into udc-dev am: 50fdc970

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/21611245



Change-Id: Ib53399f928b74114bcdd6e78eacd797bd587a0e9
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 9a497217 50fdc970
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -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" />
+24 −8
Original line number Diff line number Diff line
@@ -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,8 +97,12 @@ 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 {