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

Commit 0656b510 authored by Lucas Silva's avatar Lucas Silva
Browse files

Refactor communal hub enablement logic

This introduces the idea of suppressor classes, to encapsulate logic for
suppressing the hub. Each suppressor provides a reason for suppression,
and each reason specifies which features it would like to suppress.

This creates a single source of truth for whether the hub is enabled or
not, and which features are enabled.

We also introduce android auto suppression behind a flag.

Bug: 396460215
Test: WIP
Flag: com.android.systemui.glanceable_hub_v2
Change-Id: I2dc742b79f8344ed427ce75a9e312375c9cafacd
parent 170009ab
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -312,6 +312,9 @@
    <!-- Permission necessary to change car audio volume through CarAudioManager -->
    <uses-permission android:name="android.car.permission.CAR_CONTROL_AUDIO_VOLUME" />

    <!-- To detect when projecting to Android Auto -->
    <uses-permission android:name="android.permission.READ_PROJECTION_STATE" />

    <!-- Permission to control Android Debug Bridge (ADB) -->
    <uses-permission android:name="android.permission.MANAGE_DEBUGGING" />

+4 −4
Original line number Diff line number Diff line
@@ -73,12 +73,12 @@ class CommunalOngoingContentStartableTest : SysuiTestCase() {
            assertThat(fakeCommunalMediaRepository.isListening()).isFalse()
            assertThat(fakeCommunalSmartspaceRepository.isListening()).isFalse()

            kosmos.setCommunalEnabled(true)
            setCommunalEnabled(true)

            assertThat(fakeCommunalMediaRepository.isListening()).isTrue()
            assertThat(fakeCommunalSmartspaceRepository.isListening()).isTrue()

            kosmos.setCommunalEnabled(false)
            setCommunalEnabled(false)

            assertThat(fakeCommunalMediaRepository.isListening()).isFalse()
            assertThat(fakeCommunalSmartspaceRepository.isListening()).isFalse()
@@ -93,13 +93,13 @@ class CommunalOngoingContentStartableTest : SysuiTestCase() {
            assertThat(fakeCommunalMediaRepository.isListening()).isFalse()
            assertThat(fakeCommunalSmartspaceRepository.isListening()).isFalse()

            kosmos.setCommunalEnabled(true)
            setCommunalEnabled(true)

            // Media listening does not start when UMO is disabled.
            assertThat(fakeCommunalMediaRepository.isListening()).isFalse()
            assertThat(fakeCommunalSmartspaceRepository.isListening()).isTrue()

            kosmos.setCommunalEnabled(false)
            setCommunalEnabled(false)

            assertThat(fakeCommunalMediaRepository.isListening()).isFalse()
            assertThat(fakeCommunalSmartspaceRepository.isListening()).isFalse()
+120 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.communal.data.repository

import android.app.UiModeManager
import android.app.UiModeManager.OnProjectionStateChangedListener
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.kosmos.Kosmos
import com.android.systemui.kosmos.backgroundScope
import com.android.systemui.kosmos.collectLastValue
import com.android.systemui.kosmos.runTest
import com.android.systemui.kosmos.testDispatcher
import com.android.systemui.kosmos.useUnconfinedTestDispatcher
import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.flow.launchIn
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.any
import org.mockito.kotlin.doAnswer
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.stub

@SmallTest
@RunWith(AndroidJUnit4::class)
class CarProjectionRepositoryImplTest : SysuiTestCase() {
    private val kosmos = testKosmos().useUnconfinedTestDispatcher()

    private val capturedListeners = mutableListOf<OnProjectionStateChangedListener>()

    private val Kosmos.uiModeManager by
        Kosmos.Fixture<UiModeManager> {
            mock {
                on {
                    addOnProjectionStateChangedListener(
                        eq(UiModeManager.PROJECTION_TYPE_AUTOMOTIVE),
                        any(),
                        any(),
                    )
                } doAnswer
                    {
                        val listener = it.getArgument<OnProjectionStateChangedListener>(2)
                        capturedListeners.add(listener)
                        Unit
                    }

                on { removeOnProjectionStateChangedListener(any()) } doAnswer
                    {
                        val listener = it.getArgument<OnProjectionStateChangedListener>(0)
                        capturedListeners.remove(listener)
                        Unit
                    }

                on { activeProjectionTypes } doReturn UiModeManager.PROJECTION_TYPE_NONE
            }
        }

    private val Kosmos.underTest by
        Kosmos.Fixture {
            CarProjectionRepositoryImpl(
                uiModeManager = uiModeManager,
                bgDispatcher = testDispatcher,
            )
        }

    @Test
    fun testProjectionActiveUpdatesAfterCallback() =
        kosmos.runTest {
            val projectionActive by collectLastValue(underTest.projectionActive)
            assertThat(projectionActive).isFalse()

            setActiveProjectionType(UiModeManager.PROJECTION_TYPE_AUTOMOTIVE)
            assertThat(projectionActive).isTrue()

            setActiveProjectionType(UiModeManager.PROJECTION_TYPE_NONE)
            assertThat(projectionActive).isFalse()
        }

    @Test
    fun testProjectionInitialValueTrue() =
        kosmos.runTest {
            setActiveProjectionType(UiModeManager.PROJECTION_TYPE_AUTOMOTIVE)

            val projectionActive by collectLastValue(underTest.projectionActive)
            assertThat(projectionActive).isTrue()
        }

    @Test
    fun testUnsubscribeWhenCancelled() =
        kosmos.runTest {
            val job = underTest.projectionActive.launchIn(backgroundScope)
            assertThat(capturedListeners).hasSize(1)

            job.cancel()
            assertThat(capturedListeners).isEmpty()
        }

    private fun Kosmos.setActiveProjectionType(@UiModeManager.ProjectionType projectionType: Int) {
        uiModeManager.stub { on { activeProjectionTypes } doReturn projectionType }
        capturedListeners.forEach { it.onProjectionStateChanged(projectionType, emptySet()) }
    }
}
+0 −109
Original line number Diff line number Diff line
@@ -34,9 +34,7 @@ import com.android.systemui.Flags.FLAG_GLANCEABLE_HUB_BLURRED_BACKGROUND
import com.android.systemui.Flags.FLAG_GLANCEABLE_HUB_V2
import com.android.systemui.SysuiTestCase
import com.android.systemui.broadcast.broadcastDispatcher
import com.android.systemui.communal.data.model.DisabledReason
import com.android.systemui.communal.data.repository.CommunalSettingsRepositoryImpl.Companion.GLANCEABLE_HUB_BACKGROUND_SETTING
import com.android.systemui.communal.domain.interactor.setCommunalV2Enabled
import com.android.systemui.communal.shared.model.CommunalBackgroundType
import com.android.systemui.communal.shared.model.WhenToDream
import com.android.systemui.flags.Flags.COMMUNAL_SERVICE_ENABLED
@@ -200,63 +198,6 @@ class CommunalSettingsRepositoryImplTest(flags: FlagsParameterization?) : SysuiT
            assertThat(underTest.getFlagEnabled()).isTrue()
        }

    @EnableFlags(FLAG_COMMUNAL_HUB)
    @Test
    fun secondaryUserIsInvalid() =
        kosmos.runTest {
            val enabledState by collectLastValue(underTest.getEnabledState(SECONDARY_USER))

            assertThat(enabledState?.enabled).isFalse()
            assertThat(enabledState).containsExactly(DisabledReason.DISABLED_REASON_INVALID_USER)
        }

    @EnableFlags(FLAG_COMMUNAL_HUB, FLAG_GLANCEABLE_HUB_V2)
    @Test
    fun classicFlagIsDisabled() =
        kosmos.runTest {
            setCommunalV2Enabled(false)
            val enabledState by collectLastValue(underTest.getEnabledState(PRIMARY_USER))
            assertThat(enabledState?.enabled).isFalse()
            assertThat(enabledState).containsExactly(DisabledReason.DISABLED_REASON_FLAG)
        }

    @DisableFlags(FLAG_COMMUNAL_HUB, FLAG_GLANCEABLE_HUB_V2)
    @Test
    fun communalHubFlagIsDisabled() =
        kosmos.runTest {
            val enabledState by collectLastValue(underTest.getEnabledState(PRIMARY_USER))
            assertThat(enabledState?.enabled).isFalse()
            assertThat(enabledState).containsExactly(DisabledReason.DISABLED_REASON_FLAG)
        }

    @EnableFlags(FLAG_COMMUNAL_HUB)
    @Test
    fun hubIsDisabledByUser() =
        kosmos.runTest {
            fakeSettings.putIntForUser(Settings.Secure.GLANCEABLE_HUB_ENABLED, 0, PRIMARY_USER.id)
            val enabledState by collectLastValue(underTest.getEnabledState(PRIMARY_USER))
            assertThat(enabledState?.enabled).isFalse()
            assertThat(enabledState).containsExactly(DisabledReason.DISABLED_REASON_USER_SETTING)

            fakeSettings.putIntForUser(Settings.Secure.GLANCEABLE_HUB_ENABLED, 1, SECONDARY_USER.id)
            assertThat(enabledState?.enabled).isFalse()

            fakeSettings.putIntForUser(Settings.Secure.GLANCEABLE_HUB_ENABLED, 1, PRIMARY_USER.id)
            assertThat(enabledState?.enabled).isTrue()
        }

    @EnableFlags(FLAG_COMMUNAL_HUB)
    @Test
    fun hubIsDisabledByDevicePolicy() =
        kosmos.runTest {
            val enabledState by collectLastValue(underTest.getEnabledState(PRIMARY_USER))
            assertThat(enabledState?.enabled).isTrue()

            setKeyguardFeaturesDisabled(PRIMARY_USER, KEYGUARD_DISABLE_WIDGETS_ALL)
            assertThat(enabledState?.enabled).isFalse()
            assertThat(enabledState).containsExactly(DisabledReason.DISABLED_REASON_DEVICE_POLICY)
        }

    @EnableFlags(FLAG_COMMUNAL_HUB)
    @Test
    fun widgetsAllowedForWorkProfile_isFalse_whenDisallowedByDevicePolicy() =
@@ -269,36 +210,6 @@ class CommunalSettingsRepositoryImplTest(flags: FlagsParameterization?) : SysuiT
            assertThat(widgetsAllowedForWorkProfile).isFalse()
        }

    @EnableFlags(FLAG_COMMUNAL_HUB)
    @Test
    fun hubIsEnabled_whenDisallowedByDevicePolicyForWorkProfile() =
        kosmos.runTest {
            val enabledStateForPrimaryUser by
                collectLastValue(underTest.getEnabledState(PRIMARY_USER))
            assertThat(enabledStateForPrimaryUser?.enabled).isTrue()

            setKeyguardFeaturesDisabled(WORK_PROFILE, KEYGUARD_DISABLE_WIDGETS_ALL)
            assertThat(enabledStateForPrimaryUser?.enabled).isTrue()
        }

    @EnableFlags(FLAG_COMMUNAL_HUB)
    @Test
    fun hubIsDisabledByUserAndDevicePolicy() =
        kosmos.runTest {
            val enabledState by collectLastValue(underTest.getEnabledState(PRIMARY_USER))
            assertThat(enabledState?.enabled).isTrue()

            fakeSettings.putIntForUser(Settings.Secure.GLANCEABLE_HUB_ENABLED, 0, PRIMARY_USER.id)
            setKeyguardFeaturesDisabled(PRIMARY_USER, KEYGUARD_DISABLE_WIDGETS_ALL)

            assertThat(enabledState?.enabled).isFalse()
            assertThat(enabledState)
                .containsExactly(
                    DisabledReason.DISABLED_REASON_DEVICE_POLICY,
                    DisabledReason.DISABLED_REASON_USER_SETTING,
                )
        }

    @Test
    @DisableFlags(FLAG_GLANCEABLE_HUB_BLURRED_BACKGROUND)
    fun backgroundType_defaultValue() =
@@ -326,26 +237,6 @@ class CommunalSettingsRepositoryImplTest(flags: FlagsParameterization?) : SysuiT
            }
        }

    @Test
    fun screensaverDisabledByUser() =
        kosmos.runTest {
            val enabledState by collectLastValue(underTest.getScreensaverEnabledState(PRIMARY_USER))

            fakeSettings.putIntForUser(Settings.Secure.SCREENSAVER_ENABLED, 0, PRIMARY_USER.id)

            assertThat(enabledState).isFalse()
        }

    @Test
    fun screensaverEnabledByUser() =
        kosmos.runTest {
            val enabledState by collectLastValue(underTest.getScreensaverEnabledState(PRIMARY_USER))

            fakeSettings.putIntForUser(Settings.Secure.SCREENSAVER_ENABLED, 1, PRIMARY_USER.id)

            assertThat(enabledState).isTrue()
        }

    @Test
    fun whenToDream_charging() =
        kosmos.runTest {
+49 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
@@ -12,71 +12,38 @@
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.android.systemui.communal.domain.interactor

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.Flags.FLAG_COMMUNAL_HUB
import com.android.systemui.SysuiTestCase
import com.android.systemui.communal.data.repository.FakeCommunalSceneRepository
import com.android.systemui.communal.data.repository.FakeCommunalWidgetRepository
import com.android.systemui.communal.data.repository.fakeCommunalSceneRepository
import com.android.systemui.communal.data.repository.fakeCommunalWidgetRepository
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.keyguard.data.repository.FakeKeyguardRepository
import com.android.systemui.keyguard.data.repository.fakeKeyguardRepository
import com.android.systemui.kosmos.testScope
import com.android.systemui.communal.data.repository.carProjectionRepository
import com.android.systemui.communal.data.repository.fake
import com.android.systemui.kosmos.Kosmos
import com.android.systemui.kosmos.collectLastValue
import com.android.systemui.kosmos.runTest
import com.android.systemui.kosmos.useUnconfinedTestDispatcher
import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

/**
 * This class is a variation of the [CommunalInteractorTest] for cases where communal is disabled.
 */
@SmallTest
@RunWith(AndroidJUnit4::class)
class CommunalInteractorCommunalDisabledTest : SysuiTestCase() {
    private val kosmos = testKosmos()
    private val testScope = kosmos.testScope

    private lateinit var communalRepository: FakeCommunalSceneRepository
    private lateinit var widgetRepository: FakeCommunalWidgetRepository
    private lateinit var keyguardRepository: FakeKeyguardRepository
class CarProjectionInteractorTest : SysuiTestCase() {
    private val kosmos = testKosmos().useUnconfinedTestDispatcher()

    private lateinit var underTest: CommunalInteractor

    @Before
    fun setUp() {
        communalRepository = kosmos.fakeCommunalSceneRepository
        widgetRepository = kosmos.fakeCommunalWidgetRepository
        keyguardRepository = kosmos.fakeKeyguardRepository

        mSetFlagsRule.disableFlags(FLAG_COMMUNAL_HUB)

        underTest = kosmos.communalInteractor
    }

    @Test
    fun isCommunalEnabled_false() =
        testScope.runTest { assertThat(underTest.isCommunalEnabled.value).isFalse() }
    private val Kosmos.underTest by Kosmos.Fixture { carProjectionInteractor }

    @Test
    fun isCommunalAvailable_whenStorageUnlock_false() =
        testScope.runTest {
            val isCommunalAvailable by collectLastValue(underTest.isCommunalAvailable)

            assertThat(isCommunalAvailable).isFalse()

            keyguardRepository.setIsEncryptedOrLockdown(false)
            runCurrent()
    fun testProjectionActive() =
        kosmos.runTest {
            val projectionActive by collectLastValue(underTest.projectionActive)
            assertThat(projectionActive).isFalse()

            assertThat(isCommunalAvailable).isFalse()
            carProjectionRepository.fake.setProjectionActive(true)
            assertThat(projectionActive).isTrue()
        }
}
Loading