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

Commit e71077fd authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Convert isSafetyCenterEnabled from a state flow to a function" into main

parents 87f1dcab 569616b2
Loading
Loading
Loading
Loading
+0 −45
Original line number Diff line number Diff line
@@ -16,12 +16,10 @@

package com.android.systemui.shade.data.repository

import android.content.Intent
import android.safetycenter.SafetyCenterManager
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.broadcast.broadcastDispatcher
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.kosmos.applicationCoroutineScope
import com.android.systemui.kosmos.testDispatcher
@@ -51,7 +49,6 @@ import org.mockito.MockitoAnnotations.initMocks
class PrivacyChipRepositoryTest : SysuiTestCase() {
    private val kosmos = testKosmos()
    private val testScope = kosmos.testScope
    private val broadcastDispatcher = kosmos.broadcastDispatcher

    @Mock private lateinit var privacyConfig: PrivacyConfig
    @Mock private lateinit var privacyItemController: PrivacyItemController
@@ -65,47 +62,6 @@ class PrivacyChipRepositoryTest : SysuiTestCase() {
        setUpUnderTest()
    }

    @Test
    fun isSafetyCenterEnabled_startEnabled() =
        testScope.runTest {
            setUpUnderTest(true)

            val actual by collectLastValue(underTest.isSafetyCenterEnabled)
            runCurrent()

            assertThat(actual).isTrue()
        }

    @Test
    fun isSafetyCenterEnabled_startDisabled() =
        testScope.runTest {
            setUpUnderTest(false)

            val actual by collectLastValue(underTest.isSafetyCenterEnabled)

            assertThat(actual).isFalse()
        }

    @Test
    fun isSafetyCenterEnabled_updates() =
        testScope.runTest {
            val actual by collectLastValue(underTest.isSafetyCenterEnabled)
            runCurrent()

            assertThat(actual).isFalse()

            whenever(safetyCenterManager.isSafetyCenterEnabled).thenReturn(true)

            broadcastDispatcher.sendIntentToMatchingReceiversOnly(
                context,
                Intent(SafetyCenterManager.ACTION_SAFETY_CENTER_ENABLED_CHANGED),
            )

            runCurrent()

            assertThat(actual).isTrue()
        }

    @Test
    fun privacyItems_updates() =
        testScope.runTest {
@@ -174,7 +130,6 @@ class PrivacyChipRepositoryTest : SysuiTestCase() {
                privacyConfig = privacyConfig,
                privacyItemController = privacyItemController,
                backgroundDispatcher = kosmos.testDispatcher,
                broadcastDispatcher = broadcastDispatcher,
                safetyCenterManager = safetyCenterManager,
            )
    }
+5 −1
Original line number Diff line number Diff line
@@ -28,11 +28,11 @@ import com.android.systemui.privacy.PrivacyType
import com.android.systemui.privacy.privacyDialogController
import com.android.systemui.privacy.privacyDialogControllerV2
import com.android.systemui.shade.data.repository.fakePrivacyChipRepository
import com.android.systemui.shade.data.repository.privacyChipRepository
import com.android.systemui.testKosmos
import com.android.systemui.util.mockito.any
import com.android.systemui.util.mockito.whenever
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Before
@@ -128,23 +128,27 @@ class PrivacyChipInteractorTest : SysuiTestCase() {
            assertThat(actual).isTrue()
        }

    @OptIn(ExperimentalCoroutinesApi::class)
    @Test
    fun onPrivacyChipClicked_safetyCenterEnabled() =
        testScope.runTest {
            privacyChipRepository.setIsSafetyCenterEnabled(true)

            underTest.onPrivacyChipClicked(privacyChip)
            runCurrent()

            verify(privacyDialogControllerV2).showDialog(any(), any())
            verify(privacyDialogController, never()).showDialog(any())
        }

    @OptIn(ExperimentalCoroutinesApi::class)
    @Test
    fun onPrivacyChipClicked_safetyCenterDisabled() =
        testScope.runTest {
            privacyChipRepository.setIsSafetyCenterEnabled(false)

            underTest.onPrivacyChipClicked(privacyChip)
            runCurrent()

            verify(privacyDialogController).showDialog(any())
            verify(privacyDialogControllerV2, never()).showDialog(any(), any())
+10 −27
Original line number Diff line number Diff line
@@ -16,10 +16,7 @@

package com.android.systemui.shade.data.repository

import android.content.IntentFilter
import android.os.UserHandle
import android.safetycenter.SafetyCenterManager
import com.android.systemui.broadcast.BroadcastDispatcher
import com.android.systemui.utils.coroutines.flow.conflatedCallbackFlow
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
@@ -33,14 +30,10 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.withContext

interface PrivacyChipRepository {
    /** Whether or not the Safety Center is enabled. */
    val isSafetyCenterEnabled: StateFlow<Boolean>

    /** The list of PrivacyItems to be displayed by the privacy chip. */
    val privacyItems: StateFlow<List<PrivacyItem>>

@@ -49,6 +42,9 @@ interface PrivacyChipRepository {

    /** Whether or not location indicators are enabled in the device privacy config. */
    val isLocationIndicationEnabled: StateFlow<Boolean>

    /** Whether or not the Safety Center is enabled. */
    suspend fun isSafetyCenterEnabled() : Boolean
}

@SysUISingleton
@@ -59,27 +55,8 @@ constructor(
    private val privacyConfig: PrivacyConfig,
    private val privacyItemController: PrivacyItemController,
    @Background private val backgroundDispatcher: CoroutineDispatcher,
    broadcastDispatcher: BroadcastDispatcher,
    private val safetyCenterManager: SafetyCenterManager,
) : PrivacyChipRepository {
    override val isSafetyCenterEnabled: StateFlow<Boolean> =
        broadcastDispatcher
            .broadcastFlow(
                filter =
                    IntentFilter().apply {
                        addAction(SafetyCenterManager.ACTION_SAFETY_CENTER_ENABLED_CHANGED)
                    },
                user = UserHandle.SYSTEM,
                map = { _, _ -> safetyCenterManager.isSafetyCenterEnabled }
            )
            .onStart { emit(safetyCenterManager.isSafetyCenterEnabled) }
            .flowOn(backgroundDispatcher)
            .stateIn(
                scope = applicationScope,
                started = SharingStarted.WhileSubscribed(),
                initialValue = false,
            )

    override val privacyItems: StateFlow<List<PrivacyItem>> =
        conflatedCallbackFlow {
                val callback =
@@ -130,4 +107,10 @@ constructor(
                started = SharingStarted.Eagerly,
                initialValue = privacyItemController.locationAvailable,
            )

    override suspend fun isSafetyCenterEnabled() : Boolean {
        return withContext(backgroundDispatcher) {
            safetyCenterManager.isSafetyCenterEnabled
        }
    }
}
+11 −5
Original line number Diff line number Diff line
@@ -31,12 +31,13 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch

@SysUISingleton
class PrivacyChipInteractor
@Inject
constructor(
    @Application applicationScope: CoroutineScope,
    @Application val applicationScope: CoroutineScope,
    private val repository: PrivacyChipRepository,
    private val privacyDialogController: PrivacyDialogController,
    private val privacyDialogControllerV2: PrivacyDialogControllerV2,
@@ -80,10 +81,15 @@ constructor(
    fun onPrivacyChipClicked(privacyChip: OngoingPrivacyChip) {
        if (!deviceProvisionedController.isDeviceProvisioned) return

        if (repository.isSafetyCenterEnabled.value) {
            privacyDialogControllerV2.showDialog(shadeDialogContextInteractor.context, privacyChip)
        applicationScope.launch {
            if (repository.isSafetyCenterEnabled()) {
                privacyDialogControllerV2.showDialog(
                    shadeDialogContextInteractor.context,
                    privacyChip
                )
            } else {
                privacyDialogController.showDialog(shadeDialogContextInteractor.context)
            }
        }
    }
}
+4 −4
Original line number Diff line number Diff line
@@ -25,9 +25,6 @@ import kotlinx.coroutines.flow.MutableStateFlow
/** Fake implementation of [PrivacyChipRepository] */
@SysUISingleton
class FakePrivacyChipRepository @Inject constructor() : PrivacyChipRepository {
    private val _isSafetyCenterEnabled = MutableStateFlow(false)
    override val isSafetyCenterEnabled = _isSafetyCenterEnabled

    private val _privacyItems: MutableStateFlow<List<PrivacyItem>> = MutableStateFlow(emptyList())
    override val privacyItems = _privacyItems

@@ -37,8 +34,11 @@ class FakePrivacyChipRepository @Inject constructor() : PrivacyChipRepository {
    private val _isLocationIndicationEnabled = MutableStateFlow(false)
    override val isLocationIndicationEnabled = _isLocationIndicationEnabled

    private var _isSafetyCenterEnabled = false
    override suspend fun isSafetyCenterEnabled() : Boolean = _isSafetyCenterEnabled

    fun setIsSafetyCenterEnabled(value: Boolean) {
        _isSafetyCenterEnabled.value = value
        _isSafetyCenterEnabled = value
    }

    fun setPrivacyItems(value: List<PrivacyItem>) {