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

Commit 5f38d5ad authored by Daniel Norman's avatar Daniel Norman
Browse files

feat(edt): Create a repository for UiModeManager force invert state

This will be used by Status Bar region-sampled lightness control in
followup changes.

Bug: 379760792
Test: ForceInvertRepositoryTest
Flag: EXEMPT unused in current CL; followup callers are flagged
Change-Id: I418b740c787e3ff28ad4055e92d2a59133cd84a5
parent 56e0b4dd
Loading
Loading
Loading
Loading
+107 −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.uimode.data.repository

import android.app.UiModeManager
import android.app.UiModeManager.FORCE_INVERT_TYPE_DARK
import android.app.UiModeManager.ForceInvertStateChangeListener
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.mock
import org.mockito.kotlin.stub

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

    private val capturedListeners = mutableListOf<ForceInvertStateChangeListener>()

    private val Kosmos.uiModeManager by
        Kosmos.Fixture<UiModeManager> {
            mock {
                on { addForceInvertStateChangeListener(any(), any()) } doAnswer
                    {
                        val listener = it.getArgument<ForceInvertStateChangeListener>(1)
                        capturedListeners.add(listener)
                        Unit
                    }

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

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

    @Test
    fun isForceInvertDark_typeDark_returnsTrue() =
        kosmos.runTest {
            setActiveForceInvertType(FORCE_INVERT_TYPE_DARK)

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

    @Test
    fun isForceInvertDark_typeOff_returnsFalse() =
        kosmos.runTest {
            setActiveForceInvertType(UiModeManager.FORCE_INVERT_TYPE_OFF)

            val isForceInvertDark by collectLastValue(underTest.isForceInvertDark)
            assertThat(isForceInvertDark).isFalse()
        }

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

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

    private fun Kosmos.setActiveForceInvertType(
        @UiModeManager.ForceInvertType forceInvertType: Int
    ) {
        uiModeManager.stub { on { forceInvertState } doReturn forceInvertType }
        capturedListeners.forEach { it.onForceInvertStateChanged(forceInvertType) }
    }
}
+2 −0
Original line number Diff line number Diff line
@@ -167,6 +167,7 @@ import com.android.systemui.topui.TopUiController;
import com.android.systemui.topui.TopUiModule;
import com.android.systemui.touchpad.TouchpadModule;
import com.android.systemui.tuner.dagger.TunerModule;
import com.android.systemui.uimode.data.UiModeModule;
import com.android.systemui.user.UserModule;
import com.android.systemui.user.domain.UserDomainLayerModule;
import com.android.systemui.util.EventLogModule;
@@ -295,6 +296,7 @@ import javax.inject.Named;
        TopUiModule.class,
        TouchpadModule.class,
        TunerModule.class,
        UiModeModule.class,
        UserDomainLayerModule.class,
        UserModule.class,
        UtilModule.class,
+22 −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.uimode.data

import com.android.systemui.uimode.data.repository.ForceInvertRepositoryModule
import dagger.Module

@Module(includes = [ForceInvertRepositoryModule::class]) object UiModeModule
+70 −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.uimode.data.repository

import android.app.UiModeManager
import android.app.UiModeManager.FORCE_INVERT_TYPE_DARK
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.util.kotlin.emitOnStart
import com.android.systemui.utils.coroutines.flow.conflatedCallbackFlow
import dagger.Binds
import dagger.Module
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.asExecutor
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.withContext

/** Exposes state related to the force invert dark theme accessibility feature. */
interface ForceInvertRepository {
    /** Flow is `true` if the user has enabled the dark theme feature, otherwise `false`. */
    val isForceInvertDark: Flow<Boolean>
}

@SysUISingleton
class ForceInvertRepositoryImpl
@Inject
constructor(
    private val uiModeManager: UiModeManager,
    @Background private val bgDispatcher: CoroutineDispatcher,
) : ForceInvertRepository {
    override val isForceInvertDark: Flow<Boolean> =
        conflatedCallbackFlow {
                val listener = UiModeManager.ForceInvertStateChangeListener { _ -> trySend(Unit) }
                uiModeManager.addForceInvertStateChangeListener(bgDispatcher.asExecutor(), listener)
                awaitClose { uiModeManager.removeForceInvertStateChangeListener(listener) }
            }
            .emitOnStart()
            .map { isForceInvertDark() }
            .distinctUntilChanged()
            .flowOn(bgDispatcher)

    private suspend fun isForceInvertDark(): Boolean =
        withContext(bgDispatcher) {
            (uiModeManager.forceInvertState and FORCE_INVERT_TYPE_DARK) == FORCE_INVERT_TYPE_DARK
        }
}

@Module
interface ForceInvertRepositoryModule {
    @Binds fun bindImpl(impl: ForceInvertRepositoryImpl): ForceInvertRepository
}
+33 −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.uimode.data.repository

import com.android.systemui.kosmos.Kosmos
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow

val Kosmos.fakeForceInvertRepository by Kosmos.Fixture { FakeForceInvertRepository() }

class FakeForceInvertRepository : ForceInvertRepository {
    private val _isForceInvertDark = MutableStateFlow(false)
    override val isForceInvertDark: Flow<Boolean> = _isForceInvertDark.asStateFlow()

    fun setForceInvertDark(active: Boolean) {
        _isForceInvertDark.value = active
    }
}