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

Commit 3de7dff8 authored by Behnam Heydarshahi's avatar Behnam Heydarshahi
Browse files

Share color a11y repository flows

Fixes: 317385746
Flag: aconfig com.android.systemui.qs_new_tiles DEVELOPMENT
Test: atest ColorInversionRepositoryImplTest ColorCorrectionRepositoryImplTest
Test: atest SystemUiRoboTests
Change-Id: I5adbbdfdddd80fa9b0374dafeb01d2d19a9c188d
parent 4881667c
Loading
Loading
Loading
Loading
+43 −40
Original line number Diff line number Diff line
@@ -17,14 +17,15 @@
package com.android.systemui.accessibility.data.repository

import android.os.UserHandle
import android.provider.Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.coroutines.collectValues
import com.android.systemui.util.settings.FakeSettings
import com.google.common.truth.Truth
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runCurrent
@@ -51,6 +52,7 @@ class ColorCorrectionRepositoryImplTest : SysuiTestCase() {
        underTest =
            ColorCorrectionRepositoryImpl(
                testDispatcher,
                scope.backgroundScope,
                settings,
            )
    }
@@ -58,83 +60,78 @@ class ColorCorrectionRepositoryImplTest : SysuiTestCase() {
    @Test
    fun isEnabled_initiallyGetsSettingsValue() =
        scope.runTest {
            val actualValue by collectLastValue(underTest.isEnabled(testUser1))

            settings.putIntForUser(
                ColorCorrectionRepositoryImpl.SETTING_NAME,
                1,
                SETTING_NAME,
                ENABLED,
                testUser1.identifier
            )

            underTest =
                ColorCorrectionRepositoryImpl(
                    testDispatcher,
                    settings,
                )

            underTest.isEnabled(testUser1).launchIn(backgroundScope)
            runCurrent()

            val actualValue: Boolean = underTest.isEnabled(testUser1).first()
            Truth.assertThat(actualValue).isTrue()
        }

    @Test
    fun isEnabled_settingUpdated_valueUpdated() =
        scope.runTest {
            underTest.isEnabled(testUser1).launchIn(backgroundScope)
            val flowValues: List<Boolean> by collectValues(underTest.isEnabled(testUser1))

            settings.putIntForUser(
                ColorCorrectionRepositoryImpl.SETTING_NAME,
                ColorCorrectionRepositoryImpl.DISABLED,
                SETTING_NAME,
                DISABLED,
                testUser1.identifier
            )
            runCurrent()
            Truth.assertThat(underTest.isEnabled(testUser1).first()).isFalse()

            settings.putIntForUser(
                ColorCorrectionRepositoryImpl.SETTING_NAME,
                ColorCorrectionRepositoryImpl.ENABLED,
                SETTING_NAME,
                ENABLED,
                testUser1.identifier
            )
            runCurrent()
            Truth.assertThat(underTest.isEnabled(testUser1).first()).isTrue()

            settings.putIntForUser(
                ColorCorrectionRepositoryImpl.SETTING_NAME,
                ColorCorrectionRepositoryImpl.DISABLED,
                SETTING_NAME,
                DISABLED,
                testUser1.identifier
            )
            runCurrent()
            Truth.assertThat(underTest.isEnabled(testUser1).first()).isFalse()

            Truth.assertThat(flowValues.size).isEqualTo(3)
            Truth.assertThat(flowValues).containsExactly(false, true, false).inOrder()
        }

    @Test
    fun isEnabled_settingForUserOneOnly_valueUpdatedForUserOneOnly() =
        scope.runTest {
            underTest.isEnabled(testUser1).launchIn(backgroundScope)
            val lastValueUser1 by collectLastValue(underTest.isEnabled(testUser1))
            val lastValueUser2 by collectLastValue(underTest.isEnabled(testUser2))

            settings.putIntForUser(
                ColorCorrectionRepositoryImpl.SETTING_NAME,
                ColorCorrectionRepositoryImpl.DISABLED,
                SETTING_NAME,
                DISABLED,
                testUser1.identifier
            )
            underTest.isEnabled(testUser2).launchIn(backgroundScope)
            settings.putIntForUser(
                ColorCorrectionRepositoryImpl.SETTING_NAME,
                ColorCorrectionRepositoryImpl.DISABLED,
                SETTING_NAME,
                DISABLED,
                testUser2.identifier
            )

            runCurrent()
            Truth.assertThat(underTest.isEnabled(testUser1).first()).isFalse()
            Truth.assertThat(underTest.isEnabled(testUser2).first()).isFalse()

            Truth.assertThat(lastValueUser1).isFalse()
            Truth.assertThat(lastValueUser2).isFalse()

            settings.putIntForUser(
                ColorCorrectionRepositoryImpl.SETTING_NAME,
                ColorCorrectionRepositoryImpl.ENABLED,
                SETTING_NAME,
                ENABLED,
                testUser1.identifier
            )
            runCurrent()
            Truth.assertThat(underTest.isEnabled(testUser1).first()).isTrue()
            Truth.assertThat(underTest.isEnabled(testUser2).first()).isFalse()

            Truth.assertThat(lastValueUser1).isTrue()
            Truth.assertThat(lastValueUser2).isFalse()
        }

    @Test
@@ -146,10 +143,10 @@ class ColorCorrectionRepositoryImplTest : SysuiTestCase() {

            val actualValue =
                settings.getIntForUser(
                    ColorCorrectionRepositoryImpl.SETTING_NAME,
                    SETTING_NAME,
                    testUser1.identifier
                )
            Truth.assertThat(actualValue).isEqualTo(ColorCorrectionRepositoryImpl.ENABLED)
            Truth.assertThat(actualValue).isEqualTo(ENABLED)
        }

    @Test
@@ -161,9 +158,15 @@ class ColorCorrectionRepositoryImplTest : SysuiTestCase() {

            val actualValue =
                settings.getIntForUser(
                    ColorCorrectionRepositoryImpl.SETTING_NAME,
                    SETTING_NAME,
                    testUser1.identifier
                )
            Truth.assertThat(actualValue).isEqualTo(ColorCorrectionRepositoryImpl.DISABLED)
            Truth.assertThat(actualValue).isEqualTo(DISABLED)
        }

    companion object {
        private const val SETTING_NAME = ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED
        private const val DISABLED = 0
        private const val ENABLED = 1
    }
}
+17 −24
Original line number Diff line number Diff line
@@ -21,11 +21,11 @@ import android.provider.Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.coroutines.collectValues
import com.android.systemui.util.settings.FakeSettings
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runCurrent
@@ -52,6 +52,7 @@ class ColorInversionRepositoryImplTest : SysuiTestCase() {
        underTest =
            ColorInversionRepositoryImpl(
                testDispatcher,
                scope.backgroundScope,
                settings,
            )
    }
@@ -59,55 +60,47 @@ class ColorInversionRepositoryImplTest : SysuiTestCase() {
    @Test
    fun isEnabled_initiallyGetsSettingsValue() =
        scope.runTest {
            settings.putIntForUser(SETTING_NAME, 1, testUser1.identifier)
            val actualValue by collectLastValue(underTest.isEnabled(testUser1))

            underTest =
                ColorInversionRepositoryImpl(
                    testDispatcher,
                    settings,
                )

            underTest.isEnabled(testUser1).launchIn(backgroundScope)
            settings.putIntForUser(SETTING_NAME, ENABLED, testUser1.identifier)
            runCurrent()

            val actualValue: Boolean = underTest.isEnabled(testUser1).first()
            assertThat(actualValue).isTrue()
        }

    @Test
    fun isEnabled_settingUpdated_valueUpdated() =
        scope.runTest {
            underTest.isEnabled(testUser1).launchIn(backgroundScope)
            val flowValues: List<Boolean> by
                collectValues(underTest.isEnabled(testUser1))

            settings.putIntForUser(SETTING_NAME, DISABLED, testUser1.identifier)
            runCurrent()
            assertThat(underTest.isEnabled(testUser1).first()).isFalse()

            settings.putIntForUser(SETTING_NAME, ENABLED, testUser1.identifier)
            runCurrent()
            assertThat(underTest.isEnabled(testUser1).first()).isTrue()

            settings.putIntForUser(SETTING_NAME, DISABLED, testUser1.identifier)
            runCurrent()
            assertThat(underTest.isEnabled(testUser1).first()).isFalse()

            assertThat(flowValues.size).isEqualTo(3)
            assertThat(flowValues).containsExactly(false, true, false).inOrder()
        }

    @Test
    fun isEnabled_settingForUserOneOnly_valueUpdatedForUserOneOnly() =
        scope.runTest {
            underTest.isEnabled(testUser1).launchIn(backgroundScope)
            val lastValueUser1 by collectLastValue(underTest.isEnabled(testUser1))
            val lastValueUser2 by collectLastValue(underTest.isEnabled(testUser2))

            settings.putIntForUser(SETTING_NAME, DISABLED, testUser1.identifier)
            underTest.isEnabled(testUser2).launchIn(backgroundScope)
            settings.putIntForUser(SETTING_NAME, DISABLED, testUser2.identifier)

            runCurrent()
            assertThat(underTest.isEnabled(testUser1).first()).isFalse()
            assertThat(underTest.isEnabled(testUser2).first()).isFalse()
            assertThat(lastValueUser1).isFalse()
            assertThat(lastValueUser2).isFalse()

            settings.putIntForUser(SETTING_NAME, ENABLED, testUser1.identifier)
            runCurrent()
            assertThat(underTest.isEnabled(testUser1).first()).isTrue()
            assertThat(underTest.isEnabled(testUser2).first()).isFalse()
            assertThat(lastValueUser1).isTrue()
            assertThat(lastValueUser2).isFalse()
        }

    @Test
+23 −11
Original line number Diff line number Diff line
@@ -19,16 +19,20 @@ package com.android.systemui.accessibility.data.repository
import android.os.UserHandle
import android.provider.Settings.Secure
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.util.settings.SecureSettings
import com.android.systemui.util.settings.SettingsProxyExt.observerFlow
import javax.inject.Inject
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.withContext

/** Provides data related to color correction. */
@@ -45,22 +49,24 @@ class ColorCorrectionRepositoryImpl
@Inject
constructor(
    @Background private val bgCoroutineContext: CoroutineContext,
    @Application private val scope: CoroutineScope,
    private val secureSettings: SecureSettings,
) : ColorCorrectionRepository {

    companion object {
        const val SETTING_NAME = Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED
        const val DISABLED = 0
        const val ENABLED = 1
    }
    private val userMap = mutableMapOf<Int, Flow<Boolean>>()

    override fun isEnabled(userHandle: UserHandle): Flow<Boolean> =
        userMap.getOrPut(userHandle.identifier) {
            secureSettings
                .observerFlow(userHandle.identifier, SETTING_NAME)
                .onStart { emit(Unit) }
            .map { secureSettings.getIntForUser(SETTING_NAME, userHandle.identifier) == ENABLED }
                .map {
                    secureSettings.getIntForUser(SETTING_NAME, userHandle.identifier) == ENABLED
                }
                .distinctUntilChanged()
                .flowOn(bgCoroutineContext)
                .shareIn(scope, SharingStarted.WhileSubscribed(), replay = 1)
        }

    override suspend fun setIsEnabled(isEnabled: Boolean, userHandle: UserHandle): Boolean =
        withContext(bgCoroutineContext) {
@@ -70,4 +76,10 @@ constructor(
                userHandle.identifier
            )
        }

    companion object {
        private const val SETTING_NAME = Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED
        private const val DISABLED = 0
        private const val ENABLED = 1
    }
}
+18 −6
Original line number Diff line number Diff line
@@ -19,16 +19,20 @@ package com.android.systemui.accessibility.data.repository
import android.os.UserHandle
import android.provider.Settings.Secure
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.util.settings.SecureSettings
import com.android.systemui.util.settings.SettingsProxyExt.observerFlow
import javax.inject.Inject
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.withContext

/** Provides data related to color inversion. */
@@ -45,16 +49,24 @@ class ColorInversionRepositoryImpl
@Inject
constructor(
    @Background private val bgCoroutineContext: CoroutineContext,
    @Application private val scope: CoroutineScope,
    private val secureSettings: SecureSettings,
) : ColorInversionRepository {

    private val userMap = mutableMapOf<Int, Flow<Boolean>>()

    override fun isEnabled(userHandle: UserHandle): Flow<Boolean> =
        userMap.getOrPut(userHandle.identifier) {
            secureSettings
                .observerFlow(userHandle.identifier, SETTING_NAME)
                .onStart { emit(Unit) }
            .map { secureSettings.getIntForUser(SETTING_NAME, userHandle.identifier) == ENABLED }
                .map {
                    secureSettings.getIntForUser(SETTING_NAME, userHandle.identifier) == ENABLED
                }
                .distinctUntilChanged()
                .flowOn(bgCoroutineContext)
                .shareIn(scope, SharingStarted.WhileSubscribed(), replay = 1)
        }

    override suspend fun setIsEnabled(isEnabled: Boolean, userHandle: UserHandle): Boolean =
        withContext(bgCoroutineContext) {