From e7387143402a8bdde5e84664fa9bf8a1b1866967 Mon Sep 17 00:00:00 2001 From: Alejandro Nijamkin Date: Sun, 7 Aug 2022 18:28:46 -0700 Subject: [PATCH] Take home controls setting into account Fixes bug where the home controls quick affordance configuration fails to actually listen for changes in the settings for showing home controls on the lock-screen. Fix: 241693714 Test: Updated unit tests. Manually verified that the quick affordance for home controls shows up on the lock-screen only when Settings > Display > Lock screen > Show device controls is enabled. Change-Id: Id316e61539469c44fe16884852a9b8dd342dcc07 --- ...meControlsKeyguardQuickAffordanceConfig.kt | 9 +++- ...kAffordanceConfigParameterizedStateTest.kt | 43 +++++++++++-------- ...ntrolsKeyguardQuickAffordanceConfigTest.kt | 22 +++++++++- 3 files changed, 53 insertions(+), 21 deletions(-) rename packages/SystemUI/tests/src/com/android/systemui/keyguard/data/{repository => quickaffordance}/HomeControlsKeyguardQuickAffordanceConfigParameterizedStateTest.kt (81%) diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/data/quickaffordance/HomeControlsKeyguardQuickAffordanceConfig.kt b/packages/SystemUI/src/com/android/systemui/keyguard/data/quickaffordance/HomeControlsKeyguardQuickAffordanceConfig.kt index 3202ecb9a2870..df44957ec5914 100644 --- a/packages/SystemUI/src/com/android/systemui/keyguard/data/quickaffordance/HomeControlsKeyguardQuickAffordanceConfig.kt +++ b/packages/SystemUI/src/com/android/systemui/keyguard/data/quickaffordance/HomeControlsKeyguardQuickAffordanceConfig.kt @@ -36,6 +36,7 @@ import com.android.systemui.util.kotlin.getOrNull import javax.inject.Inject import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.flowOf /** Home controls quick affordance data source. */ @@ -50,7 +51,13 @@ constructor( private val appContext = context.applicationContext override val state: Flow = - stateInternal(component.getControlsListingController().getOrNull()) + component.canShowWhileLockedSetting.flatMapLatest { canShowWhileLocked -> + if (canShowWhileLocked) { + stateInternal(component.getControlsListingController().getOrNull()) + } else { + flowOf(KeyguardQuickAffordanceConfig.State.Hidden) + } + } override fun onQuickAffordanceClicked( animationController: ActivityLaunchAnimator.Controller?, diff --git a/packages/SystemUI/tests/src/com/android/systemui/keyguard/data/repository/HomeControlsKeyguardQuickAffordanceConfigParameterizedStateTest.kt b/packages/SystemUI/tests/src/com/android/systemui/keyguard/data/quickaffordance/HomeControlsKeyguardQuickAffordanceConfigParameterizedStateTest.kt similarity index 81% rename from packages/SystemUI/tests/src/com/android/systemui/keyguard/data/repository/HomeControlsKeyguardQuickAffordanceConfigParameterizedStateTest.kt rename to packages/SystemUI/tests/src/com/android/systemui/keyguard/data/quickaffordance/HomeControlsKeyguardQuickAffordanceConfigParameterizedStateTest.kt index bcc76abc89ba2..810c6dc4776d3 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/keyguard/data/repository/HomeControlsKeyguardQuickAffordanceConfigParameterizedStateTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/keyguard/data/quickaffordance/HomeControlsKeyguardQuickAffordanceConfigParameterizedStateTest.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.android.systemui.keyguard.data.repository +package com.android.systemui.keyguard.data.quickaffordance import androidx.test.filters.SmallTest import com.android.systemui.R @@ -22,11 +22,10 @@ import com.android.systemui.SysuiTestCase import com.android.systemui.controls.controller.ControlsController import com.android.systemui.controls.dagger.ControlsComponent import com.android.systemui.controls.management.ControlsListingController -import com.android.systemui.keyguard.data.quickaffordance.HomeControlsKeyguardQuickAffordanceConfig -import com.android.systemui.keyguard.data.quickaffordance.KeyguardQuickAffordanceConfig import com.android.systemui.util.mockito.mock import com.google.common.truth.Truth.assertThat import java.util.Optional +import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.test.runBlockingTest @@ -50,18 +49,19 @@ class HomeControlsKeyguardQuickAffordanceConfigParameterizedStateTest : SysuiTes companion object { @Parameters( name = - "feature enabled = {0}, has favorites = {1}, has service infos = {2} - expected" + - " visible = {3}" + "feature enabled = {0}, has favorites = {1}, has service infos = {2}, can show" + + " while locked = {3} - expected visible = {4}" ) @JvmStatic fun data() = - (0 until 8) + (0 until 16) .map { combination -> arrayOf( - /* isFeatureEnabled= */ combination and 0b100 != 0, - /* hasFavorites= */ combination and 0b010 != 0, - /* hasServiceInfos= */ combination and 0b001 != 0, - /* isVisible= */ combination == 0b111, + /* isFeatureEnabled= */ combination and 0b1000 != 0, + /* hasFavorites= */ combination and 0b0100 != 0, + /* hasServiceInfos= */ combination and 0b0010 != 0, + /* canShowWhileLocked= */ combination and 0b0001 != 0, + /* isVisible= */ combination == 0b1111, ) } .toList() @@ -79,7 +79,8 @@ class HomeControlsKeyguardQuickAffordanceConfigParameterizedStateTest : SysuiTes @JvmField @Parameter(0) var isFeatureEnabled: Boolean = false @JvmField @Parameter(1) var hasFavorites: Boolean = false @JvmField @Parameter(2) var hasServiceInfos: Boolean = false - @JvmField @Parameter(3) var isVisible: Boolean = false + @JvmField @Parameter(3) var canShowWhileLocked: Boolean = false + @JvmField @Parameter(4) var isVisible: Boolean = false @Before fun setUp() { @@ -89,6 +90,8 @@ class HomeControlsKeyguardQuickAffordanceConfigParameterizedStateTest : SysuiTes whenever(component.getControlsController()).thenReturn(Optional.of(controlsController)) whenever(component.getControlsListingController()) .thenReturn(Optional.of(controlsListingController)) + whenever(component.canShowWhileLockedSetting) + .thenReturn(MutableStateFlow(canShowWhileLocked)) underTest = HomeControlsKeyguardQuickAffordanceConfig( @@ -111,14 +114,16 @@ class HomeControlsKeyguardQuickAffordanceConfigParameterizedStateTest : SysuiTes val values = mutableListOf() val job = underTest.state.onEach(values::add).launchIn(this) - verify(controlsListingController).addCallback(callbackCaptor.capture()) - callbackCaptor.value.onServicesUpdated( - if (hasServiceInfos) { - listOf(mock()) - } else { - emptyList() - } - ) + if (canShowWhileLocked) { + verify(controlsListingController).addCallback(callbackCaptor.capture()) + callbackCaptor.value.onServicesUpdated( + if (hasServiceInfos) { + listOf(mock()) + } else { + emptyList() + } + ) + } assertThat(values.last()) .isInstanceOf( diff --git a/packages/SystemUI/tests/src/com/android/systemui/keyguard/data/quickaffordance/HomeControlsKeyguardQuickAffordanceConfigTest.kt b/packages/SystemUI/tests/src/com/android/systemui/keyguard/data/quickaffordance/HomeControlsKeyguardQuickAffordanceConfigTest.kt index 592e80b9e7d96..ef588f5ce255c 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/keyguard/data/quickaffordance/HomeControlsKeyguardQuickAffordanceConfigTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/keyguard/data/quickaffordance/HomeControlsKeyguardQuickAffordanceConfigTest.kt @@ -51,6 +51,7 @@ class HomeControlsKeyguardQuickAffordanceConfigTest : SysuiTestCase() { @Before fun setUp() { MockitoAnnotations.initMocks(this) + whenever(component.canShowWhileLockedSetting).thenReturn(MutableStateFlow(true)) underTest = HomeControlsKeyguardQuickAffordanceConfig( @@ -60,7 +61,26 @@ class HomeControlsKeyguardQuickAffordanceConfigTest : SysuiTestCase() { } @Test - fun `state - when listing controller is missing - returns None`() = runBlockingTest { + fun `state - when cannot show while locked - returns Hidden`() = runBlockingTest { + whenever(component.canShowWhileLockedSetting).thenReturn(MutableStateFlow(false)) + whenever(component.isEnabled()).thenReturn(true) + whenever(component.getTileImageId()).thenReturn(R.drawable.controls_icon) + whenever(component.getTileTitleId()).thenReturn(R.string.quick_controls_title) + val controlsController = mock() + whenever(component.getControlsController()).thenReturn(Optional.of(controlsController)) + whenever(component.getControlsListingController()).thenReturn(Optional.empty()) + whenever(controlsController.getFavorites()).thenReturn(listOf(mock())) + + val values = mutableListOf() + val job = underTest.state.onEach(values::add).launchIn(this) + + assertThat(values.last()) + .isInstanceOf(KeyguardQuickAffordanceConfig.State.Hidden::class.java) + job.cancel() + } + + @Test + fun `state - when listing controller is missing - returns Hidden`() = runBlockingTest { whenever(component.isEnabled()).thenReturn(true) whenever(component.getTileImageId()).thenReturn(R.drawable.controls_icon) whenever(component.getTileTitleId()).thenReturn(R.string.quick_controls_title)