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

Commit 747fc448 authored by Bharat Singh's avatar Bharat Singh Committed by Android (Google) Code Review
Browse files

Merge changes from topic "revert-lpp-events" into main

* changes:
  Revert "[SysUI][Floaty] Move power button pressed and long pressed to StateFlow"
  Revert "[SysUI][Floaty] Add flow to detect lpp in sysui"
  Modified revert "[SysUI][Floaty] Send KeyEvent with FLAG_LONG_PRESS on long power press"
parents 52b5be79 0d9b17ae
Loading
Loading
Loading
Loading
+1 −17
Original line number Diff line number Diff line
@@ -41,10 +41,7 @@ class KeyEventInteractorTest : SysuiTestCase() {
    @Before
    fun setup() {
        repository = FakeKeyEventRepository()
        underTest =
            KeyEventInteractor(
                repository,
            )
        underTest = KeyEventInteractor(repository)
    }

    @Test
@@ -57,17 +54,4 @@ class KeyEventInteractorTest : SysuiTestCase() {
            repository.setPowerButtonDown(true)
            assertThat(isPowerDown).isTrue()
        }

    @Test
    fun testPowerButtonBeingLongPressedInteractor() =
        runTest {
            val isPowerButtonLongPressed by collectLastValue(
                underTest.isPowerButtonLongPressed)

            repository.setPowerButtonLongPressed(false)
            assertThat(isPowerButtonLongPressed).isFalse()

            repository.setPowerButtonLongPressed(true)
            assertThat(isPowerButtonLongPressed).isTrue()
        }
}
+4 −80
Original line number Diff line number Diff line
@@ -25,8 +25,6 @@ import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.keyevent.data.repository.KeyEventRepositoryImpl
import com.android.systemui.statusbar.CommandQueue
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
@@ -39,7 +37,6 @@ import org.mockito.Mock
import org.mockito.Mockito.verify
import org.mockito.MockitoAnnotations

@OptIn(ExperimentalCoroutinesApi::class)
@SmallTest
@RunWith(AndroidJUnit4::class)
class KeyEventRepositoryTest : SysuiTestCase() {
@@ -52,10 +49,7 @@ class KeyEventRepositoryTest : SysuiTestCase() {
    fun setUp() {
        MockitoAnnotations.initMocks(this)
        testScope = TestScope()
        underTest = KeyEventRepositoryImpl(
            commandQueue = commandQueue,
            applicationScope = testScope.backgroundScope
        )
        underTest = KeyEventRepositoryImpl(commandQueue)
    }

    @Test
@@ -66,90 +60,20 @@ class KeyEventRepositoryTest : SysuiTestCase() {
            assertThat(isPowerButtonDown).isFalse()
        }

    @Test
    fun isPowerButtonBeingLongPressed_initialValueFalse() =
        testScope.runTest {
            val isPowerButtonLongPressed by collectLastValue(underTest.isPowerButtonLongPressed)
            runCurrent()
            assertThat(isPowerButtonLongPressed).isFalse()
        }

    @Test
    fun isPowerButtonDown_onChange() =
        testScope.runTest {
            underTest.isPowerButtonDown.launchIn(testScope.backgroundScope)

            val isPowerButtonDown by collectLastValue(underTest.isPowerButtonDown)
            runCurrent()

            verify(commandQueue).addCallback(commandQueueCallbacks.capture())

            commandQueueCallbacks.value.handleSystemKey(
                KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_POWER)
            )

            runCurrent()

            assertThat(underTest.isPowerButtonDown.value).isTrue()
            assertThat(isPowerButtonDown).isTrue()

            commandQueueCallbacks.value.handleSystemKey(
                KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_POWER)
            )

            runCurrent()

            assertThat(underTest.isPowerButtonDown.value).isFalse()
        }


    @Test
    fun isPowerButtonBeingLongPressed_onPowerButtonDown() =
        testScope.runTest {
            underTest.isPowerButtonLongPressed.launchIn(testScope.backgroundScope)

            runCurrent()

            verify(commandQueue).addCallback(commandQueueCallbacks.capture())

            val keyEvent = KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_POWER)
            commandQueueCallbacks.value.handleSystemKey(keyEvent)

            runCurrent()

            assertThat(underTest.isPowerButtonLongPressed.value).isFalse()
        }

    @Test
    fun isPowerButtonBeingLongPressed_onPowerButtonUp() =
        testScope.runTest {
            underTest.isPowerButtonLongPressed.launchIn(testScope.backgroundScope)

            runCurrent()

            verify(commandQueue).addCallback(commandQueueCallbacks.capture())

            val keyEvent = KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_POWER)
            commandQueueCallbacks.value.handleSystemKey(keyEvent)

            runCurrent()

            assertThat(underTest.isPowerButtonLongPressed.value).isFalse()
        }

    @Test
    fun isPowerButtonBeingLongPressed_onPowerButtonDown_longPressFlagSet() =
        testScope.runTest {
            underTest.isPowerButtonLongPressed.launchIn(testScope.backgroundScope)

            runCurrent()

            verify(commandQueue).addCallback(commandQueueCallbacks.capture())

            val keyEvent = KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_POWER)
            keyEvent.setFlags(KeyEvent.FLAG_LONG_PRESS)
            commandQueueCallbacks.value.handleSystemKey(keyEvent)

            runCurrent()

            assertThat(underTest.isPowerButtonLongPressed.value).isTrue()
            assertThat(isPowerButtonDown).isFalse()
        }
}
+16 −53
Original line number Diff line number Diff line
@@ -19,39 +19,27 @@ package com.android.systemui.keyevent.data.repository
import android.view.KeyEvent
import com.android.systemui.common.coroutine.ChannelExt.trySendWithFailureLogging
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.statusbar.CommandQueue
import com.android.systemui.utils.coroutines.flow.conflatedCallbackFlow
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.stateIn
import javax.inject.Inject
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow

/** Defines interface for classes that encapsulate application state for key event presses. */
interface KeyEventRepository {
    /** Observable for whether the power button key is pressed/down or not. */
    val isPowerButtonDown: StateFlow<Boolean>

    /** Observable for when the power button is being pressed but till the duration of long press */
    val isPowerButtonLongPressed: StateFlow<Boolean>
    val isPowerButtonDown: Flow<Boolean>
}

@SysUISingleton
class KeyEventRepositoryImpl
@Inject
constructor(
    private val commandQueue: CommandQueue,
    @Application applicationScope: CoroutineScope
) : KeyEventRepository {
    override val isPowerButtonDown =
        conflatedCallbackFlow {
            val callback = object : CommandQueue.Callbacks {
class KeyEventRepositoryImpl @Inject constructor(private val commandQueue: CommandQueue) :
    KeyEventRepository {
    override val isPowerButtonDown: Flow<Boolean> = conflatedCallbackFlow {
        val callback =
            object : CommandQueue.Callbacks {
                override fun handleSystemKey(event: KeyEvent) {
                    if (event.keyCode == KeyEvent.KEYCODE_POWER) {
                            trySendWithFailureLogging(event.action == KeyEvent.ACTION_DOWN,
                                TAG, "updated isPowerButtonDown")
                        trySendWithFailureLogging(event.isDown, TAG, "updated isPowerButtonDown")
                    }
                }
            }
@@ -59,31 +47,6 @@ constructor(
        commandQueue.addCallback(callback)
        awaitClose { commandQueue.removeCallback(callback) }
    }
        .stateIn(
            scope = applicationScope,
            started = SharingStarted.WhileSubscribed(),
            initialValue = false
        )

    override val isPowerButtonLongPressed =
        conflatedCallbackFlow {
            val callback = object : CommandQueue.Callbacks {
                    override fun handleSystemKey(event: KeyEvent) {
                        if (event.keyCode == KeyEvent.KEYCODE_POWER) {
                            trySendWithFailureLogging(event.action == KeyEvent.ACTION_DOWN
                                    && event.isLongPress, TAG, "updated isPowerButtonLongPressed")
                        }
                    }
                }
            trySendWithFailureLogging(false, TAG, "init isPowerButtonLongPressed")
            commandQueue.addCallback(callback)
            awaitClose { commandQueue.removeCallback(callback) }
        }
        .stateIn(
            scope = applicationScope,
            started = SharingStarted.WhileSubscribed(),
            initialValue = false
        )

    companion object {
        private const val TAG = "KeyEventRepositoryImpl"
+1 −6
Original line number Diff line number Diff line
@@ -26,11 +26,6 @@ import javax.inject.Inject
 * For key events that SysUI wants to properly handle, see [SysUIKeyEventHandler].
 */
@SysUISingleton
class KeyEventInteractor
@Inject
constructor(
    repository: KeyEventRepository,
) {
class KeyEventInteractor @Inject constructor(repository: KeyEventRepository) {
    val isPowerButtonDown = repository.isPowerButtonDown
    val isPowerButtonLongPressed = repository.isPowerButtonLongPressed
}
+3 −10
Original line number Diff line number Diff line
@@ -18,25 +18,18 @@ package com.android.systemui.keyevent.data.repository

import dagger.Binds
import dagger.Module
import javax.inject.Inject
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject

class FakeKeyEventRepository @Inject constructor() : KeyEventRepository {
    private val _isPowerButtonDown = MutableStateFlow(false)
    override val isPowerButtonDown: StateFlow<Boolean> = _isPowerButtonDown.asStateFlow()

    private val _isPowerButtonLongPressed = MutableStateFlow(false)
    override val isPowerButtonLongPressed = _isPowerButtonLongPressed.asStateFlow()
    override val isPowerButtonDown: Flow<Boolean> = _isPowerButtonDown.asStateFlow()

    fun setPowerButtonDown(isDown: Boolean) {
        _isPowerButtonDown.value = isDown
    }

    fun setPowerButtonLongPressed(isLongPressed: Boolean) {
        _isPowerButtonLongPressed.value = isLongPressed
    }
}

@Module
Loading