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

Commit 4ff5ee1c authored by Bharat Singh's avatar Bharat Singh
Browse files

[SysUI][Floaty] Add flow to detect lpp in sysui

* False when power button not pressed
* True when power button pressed beyond long press threshold
* False when power button pressed within long press duration

Bug: 399263897
Flag: com.android.systemui.shared.enable_lpp_squeeze_effect
Change-Id: I61a480c2ee1ba9fd7b2771d1974e0bbdf5ff9030
parent b5c868e7
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -57,4 +57,17 @@ class KeyEventInteractorTest : SysuiTestCase() {
            repository.setPowerButtonDown(true)
            assertThat(isPowerDown).isTrue()
        }

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

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

            repository.setPowerButtonBeingLongPressed(true)
            assertThat(isPowerButtonLongPressed).isTrue()
        }
}
+61 −1
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ 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.test.TestScope
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
@@ -34,10 +35,10 @@ import org.junit.runner.RunWith
import org.mockito.ArgumentCaptor
import org.mockito.Captor
import org.mockito.Mock
import org.mockito.Mockito.times
import org.mockito.Mockito.verify
import org.mockito.MockitoAnnotations

@OptIn(ExperimentalCoroutinesApi::class)
@SmallTest
@RunWith(AndroidJUnit4::class)
class KeyEventRepositoryTest : SysuiTestCase() {
@@ -61,6 +62,15 @@ 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 {
@@ -77,4 +87,54 @@ class KeyEventRepositoryTest : SysuiTestCase() {
            )
            assertThat(isPowerButtonDown).isFalse()
        }


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

            runCurrent()

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

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

            assertThat(isPowerButtonLongPressed).isFalse()
        }

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

            runCurrent()

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

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

            assertThat(isPowerButtonLongPressed).isFalse()
        }

    @Test
    fun isPowerButtonBeingLongPressed_onPowerButtonDown_longPressFlagSet() =
        testScope.runTest {
            val isPowerButtonBeingLongPressed by collectLastValue(
                underTest.isPowerButtonLongPressed)

            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)

            assertThat(isPowerButtonBeingLongPressed).isTrue()
        }
}
+19 −1
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@ package com.android.systemui.keyevent.data.repository

import android.view.KeyEvent
import com.android.systemui.common.coroutine.ChannelExt.trySendWithFailureLogging
import com.android.systemui.common.coroutine.ConflatedCallbackFlow.conflatedCallbackFlow
import com.android.systemui.utils.coroutines.flow.conflatedCallbackFlow
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.statusbar.CommandQueue
import javax.inject.Inject
@@ -29,6 +29,9 @@ import kotlinx.coroutines.flow.Flow
interface KeyEventRepository {
    /** Observable for whether the power button key is pressed/down or not. */
    val isPowerButtonDown: Flow<Boolean>

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

@SysUISingleton
@@ -51,6 +54,21 @@ constructor(
        awaitClose { commandQueue.removeCallback(callback) }
    }

    override val isPowerButtonLongPressed: 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
                                && event.isLongPress, TAG, "updated isPowerButtonLongPressed")
                    }
                }
            }
        trySendWithFailureLogging(false, TAG, "init isPowerButtonLongPressed")
        commandQueue.addCallback(callback)
        awaitClose { commandQueue.removeCallback(callback) }
    }

    companion object {
        private const val TAG = "KeyEventRepositoryImpl"
    }
+1 −0
Original line number Diff line number Diff line
@@ -32,4 +32,5 @@ constructor(
    repository: KeyEventRepository,
) {
    val isPowerButtonDown = repository.isPowerButtonDown
    val isPowerButtonLongPressed = repository.isPowerButtonLongPressed
}
+7 −0
Original line number Diff line number Diff line
@@ -27,9 +27,16 @@ class FakeKeyEventRepository @Inject constructor() : KeyEventRepository {
    private val _isPowerButtonDown = MutableStateFlow(false)
    override val isPowerButtonDown: Flow<Boolean> = _isPowerButtonDown.asStateFlow()

    private val _isPowerButtonLongPressed = MutableStateFlow(false)
    override val isPowerButtonLongPressed = _isPowerButtonLongPressed.asStateFlow()

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

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

@Module