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

Commit ec802b49 authored by Behnam Heydarshahi's avatar Behnam Heydarshahi
Browse files

Use default val in night display repo for auto mode

When NightDisplayRepository reads automode setting, it used to
throw an exception if the setting was missing. Instead of that,
now the repo provides a default value, preventing the exception.

Flag: ACONFIG com.android.systemui.qs_new_tiles_future DEVELOPMENT
Fixes: 341125737
Test: atest NightDisplayRepositoryTest
Change-Id: I83f17182e8cd07df6438bf8e0f3115bc91790b1c
parent 325f6d65
Loading
Loading
Loading
Loading
+22 −6
Original line number Diff line number Diff line
@@ -42,7 +42,6 @@ import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers
@@ -90,11 +89,6 @@ class NightDisplayRepositoryTest : SysuiTestCase() {
            locationController,
        )

    @Before
    fun setup() {
        enrollInForcedNightDisplayAutoMode(INITIALLY_FORCE_AUTO_MODE, testUser)
    }

    @Test
    fun nightDisplayState_matchesAutoMode() =
        scope.runTest {
@@ -126,6 +120,8 @@ class NightDisplayRepositoryTest : SysuiTestCase() {
    @Test
    fun nightDisplayState_matchesIsNightDisplayActivated() =
        scope.runTest {
            enrollInForcedNightDisplayAutoMode(INITIALLY_FORCE_AUTO_MODE, testUser)

            val callbackCaptor = argumentCaptor<NightDisplayListener.Callback>()

            val lastState by collectLastValue(underTest.nightDisplayState(testUser))
@@ -148,6 +144,7 @@ class NightDisplayRepositoryTest : SysuiTestCase() {
        scope.runTest {
            whenever(colorDisplayManager.nightDisplayAutoMode)
                .thenReturn(ColorDisplayManager.AUTO_MODE_CUSTOM_TIME)
            enrollInForcedNightDisplayAutoMode(INITIALLY_FORCE_AUTO_MODE, testUser)

            val lastState by collectLastValue(underTest.nightDisplayState(testUser))
            runCurrent()
@@ -160,6 +157,7 @@ class NightDisplayRepositoryTest : SysuiTestCase() {
        scope.runTest {
            whenever(colorDisplayManager.nightDisplayAutoMode)
                .thenReturn(ColorDisplayManager.AUTO_MODE_TWILIGHT)
            enrollInForcedNightDisplayAutoMode(INITIALLY_FORCE_AUTO_MODE, testUser)

            val lastState by collectLastValue(underTest.nightDisplayState(testUser))
            runCurrent()
@@ -167,6 +165,24 @@ class NightDisplayRepositoryTest : SysuiTestCase() {
            assertThat(lastState!!.autoMode).isEqualTo(ColorDisplayManager.AUTO_MODE_TWILIGHT)
        }

    /**
     * When the value of the raw auto mode is missing the call to nightDisplayState should not crash
     */
    @Test
    fun nightDisplayState_whenAutoModeSettingIsNotInitialized_loadsDataWithoutException() =
        scope.runTest {
            // only auto mode_available is set, and the raw auto_mode has nothing set
            globalSettings.putString(
                Settings.Global.NIGHT_DISPLAY_FORCED_AUTO_MODE_AVAILABLE,
                NIGHT_DISPLAY_FORCED_AUTO_MODE_AVAILABLE
            )

            val lastState by collectLastValue(underTest.nightDisplayState(testUser))
            runCurrent()

            assertThat(lastState!!.shouldForceAutoMode).isTrue()
        }

    @Test
    fun nightDisplayState_matchesForceAutoMode() =
        scope.runTest {
+10 −8
Original line number Diff line number Diff line
@@ -149,12 +149,7 @@ constructor(
                secureSettings
                    .observerFlow(userHandle.identifier, DISPLAY_AUTO_MODE_RAW_SETTING_NAME)
                    .onStart { emit(Unit) }
                    .map {
                        secureSettings.getIntForUser(
                            DISPLAY_AUTO_MODE_RAW_SETTING_NAME,
                            userHandle.identifier
                        ) == NIGHT_DISPLAY_AUTO_MODE_RAW_NOT_SET
                    }
                    .map { isNightDisplayAutoModeRawSettingNotSet(userHandle.identifier) }
            }
            .distinctUntilChanged()

@@ -179,12 +174,19 @@ constructor(
            colorDisplayManager.nightDisplayCustomEndTime,
            globalSettings.getString(IS_FORCE_AUTO_MODE_AVAILABLE_SETTING_NAME) ==
                NIGHT_DISPLAY_FORCED_AUTO_MODE_AVAILABLE &&
                secureSettings.getIntForUser(DISPLAY_AUTO_MODE_RAW_SETTING_NAME, user.identifier) ==
                    NIGHT_DISPLAY_AUTO_MODE_RAW_NOT_SET,
                isNightDisplayAutoModeRawSettingNotSet(user.identifier),
            locationController.isLocationEnabled,
        )
    }

    private fun isNightDisplayAutoModeRawSettingNotSet(userId: Int): Boolean {
        return secureSettings.getIntForUser(
            DISPLAY_AUTO_MODE_RAW_SETTING_NAME,
            NIGHT_DISPLAY_AUTO_MODE_RAW_NOT_SET,
            userId
        ) == NIGHT_DISPLAY_AUTO_MODE_RAW_NOT_SET
    }

    private companion object {
        const val NIGHT_DISPLAY_AUTO_MODE_RAW_NOT_SET = -1
        const val NIGHT_DISPLAY_FORCED_AUTO_MODE_AVAILABLE = "1"