diff options
5 files changed, 101 insertions, 2 deletions
diff --git a/packages/SystemUI/multivalentTests/src/com/android/systemui/keyevent/domain/interactor/KeyEventInteractorTest.kt b/packages/SystemUI/multivalentTests/src/com/android/systemui/keyevent/domain/interactor/KeyEventInteractorTest.kt index f37306276848..684af6fc8040 100644 --- a/packages/SystemUI/multivalentTests/src/com/android/systemui/keyevent/domain/interactor/KeyEventInteractorTest.kt +++ b/packages/SystemUI/multivalentTests/src/com/android/systemui/keyevent/domain/interactor/KeyEventInteractorTest.kt @@ -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() + } } diff --git a/packages/SystemUI/multivalentTests/src/com/android/systemui/keyguard/data/repository/KeyEventRepositoryTest.kt b/packages/SystemUI/multivalentTests/src/com/android/systemui/keyguard/data/repository/KeyEventRepositoryTest.kt index c7f1525e2946..9ab5f8948ecc 100644 --- a/packages/SystemUI/multivalentTests/src/com/android/systemui/keyguard/data/repository/KeyEventRepositoryTest.kt +++ b/packages/SystemUI/multivalentTests/src/com/android/systemui/keyguard/data/repository/KeyEventRepositoryTest.kt @@ -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() { @@ -62,6 +63,15 @@ class KeyEventRepositoryTest : SysuiTestCase() { } @Test + fun isPowerButtonBeingLongPressed_initialValueFalse() = + testScope.runTest { + val isPowerButtonLongPressed by collectLastValue( + underTest.isPowerButtonLongPressed) + runCurrent() + assertThat(isPowerButtonLongPressed).isFalse() + } + + @Test fun isPowerButtonDown_onChange() = testScope.runTest { val isPowerButtonDown by collectLastValue(underTest.isPowerButtonDown) @@ -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() + } } diff --git a/packages/SystemUI/src/com/android/systemui/keyevent/data/repository/KeyEventRepository.kt b/packages/SystemUI/src/com/android/systemui/keyevent/data/repository/KeyEventRepository.kt index 9da9a7328659..32257df40d02 100644 --- a/packages/SystemUI/src/com/android/systemui/keyevent/data/repository/KeyEventRepository.kt +++ b/packages/SystemUI/src/com/android/systemui/keyevent/data/repository/KeyEventRepository.kt @@ -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" } diff --git a/packages/SystemUI/src/com/android/systemui/keyevent/domain/interactor/KeyEventInteractor.kt b/packages/SystemUI/src/com/android/systemui/keyevent/domain/interactor/KeyEventInteractor.kt index 9949fa589cd5..ec9bbfb1bc77 100644 --- a/packages/SystemUI/src/com/android/systemui/keyevent/domain/interactor/KeyEventInteractor.kt +++ b/packages/SystemUI/src/com/android/systemui/keyevent/domain/interactor/KeyEventInteractor.kt @@ -32,4 +32,5 @@ constructor( repository: KeyEventRepository, ) { val isPowerButtonDown = repository.isPowerButtonDown + val isPowerButtonLongPressed = repository.isPowerButtonLongPressed } diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/keyevent/data/repository/FakeKeyEventRepository.kt b/packages/SystemUI/tests/utils/src/com/android/systemui/keyevent/data/repository/FakeKeyEventRepository.kt index 97dab4987f6c..807bc82cbf4e 100644 --- a/packages/SystemUI/tests/utils/src/com/android/systemui/keyevent/data/repository/FakeKeyEventRepository.kt +++ b/packages/SystemUI/tests/utils/src/com/android/systemui/keyevent/data/repository/FakeKeyEventRepository.kt @@ -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 |