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

Commit dac5670a authored by Alejandro Nijamkin's avatar Alejandro Nijamkin
Browse files

[flexiglass] DeviceConfig stack.

Adds DeviceConfig repository and interactor.

Bug: 343297387
Flag: NONE there are no callers so this code does nothing
Test: manually verified with CLs on the chain
Test: unit tests included
Change-Id: I0fde1a5aea76188ac965af0e8cd48cd5d652ae90
parent b6325f3a
Loading
Loading
Loading
Loading
+59 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.
 */

@file:OptIn(ExperimentalCoroutinesApi::class)

package com.android.systemui.deviceconfig.data.repository

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.concurrency.fakeExecutor
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.kosmos.testScope
import com.android.systemui.testKosmos
import com.android.systemui.util.fakeDeviceConfigProxy
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith

@SmallTest
@RunWith(AndroidJUnit4::class)
class DeviceConfigRepositoryTest : SysuiTestCase() {

    private val kosmos = testKosmos()
    private val testScope = kosmos.testScope
    private val dataSource = kosmos.fakeDeviceConfigProxy

    private val underTest = kosmos.deviceConfigRepository

    @Test
    fun booleanProperty() =
        testScope.runTest {
            val property by collectLastValue(underTest.property("namespace", "name", false))
            assertThat(property).isFalse()

            dataSource.setProperty("namespace", "name", "true", /* makeDefault= */ false)
            kosmos.fakeExecutor.runAllReady()
            assertThat(property).isTrue()

            dataSource.setProperty("namespace", "name", "false", /* makeDefault= */ false)
            kosmos.fakeExecutor.runAllReady()
            assertThat(property).isFalse()
        }
}
+56 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.deviceconfig.domain.interactor

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.concurrency.fakeExecutor
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.kosmos.testScope
import com.android.systemui.testKosmos
import com.android.systemui.util.fakeDeviceConfigProxy
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith

@SmallTest
@RunWith(AndroidJUnit4::class)
class DeviceConfigInteractorTest : SysuiTestCase() {

    private val kosmos = testKosmos()
    private val testScope = kosmos.testScope
    private val dataSource = kosmos.fakeDeviceConfigProxy

    private val underTest = kosmos.deviceConfigInteractor

    @Test
    fun booleanProperty() =
        testScope.runTest {
            val property by collectLastValue(underTest.property("namespace", "name", false))
            assertThat(property).isFalse()

            dataSource.setProperty("namespace", "name", "true", /* makeDefault= */ false)
            kosmos.fakeExecutor.runAllReady()
            assertThat(property).isTrue()

            dataSource.setProperty("namespace", "name", "false", /* makeDefault= */ false)
            kosmos.fakeExecutor.runAllReady()
            assertThat(property).isFalse()
        }
}
+59 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.deviceconfig.data.repository

import android.provider.DeviceConfig
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.util.DeviceConfigProxy
import com.android.systemui.utils.coroutines.flow.conflatedCallbackFlow
import java.util.concurrent.Executor
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOn

@SysUISingleton
class DeviceConfigRepository
@Inject
constructor(
    @Background private val backgroundExecutor: Executor,
    @Background private val backgroundDispatcher: CoroutineDispatcher,
    private val dataSource: DeviceConfigProxy,
) {

    fun property(namespace: String, name: String, default: Boolean): Flow<Boolean> {
        return conflatedCallbackFlow {
                val listener = { properties: DeviceConfig.Properties ->
                    if (properties.keyset.contains(name)) {
                        trySend(properties.getBoolean(name, default))
                    }
                }

                dataSource.addOnPropertiesChangedListener(
                    namespace,
                    backgroundExecutor,
                    listener,
                )
                trySend(dataSource.getBoolean(namespace, name, default))

                awaitClose { dataSource.removeOnPropertiesChangedListener(listener) }
            }
            .flowOn(backgroundDispatcher)
    }
}
+34 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.deviceconfig.domain.interactor

import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.deviceconfig.data.repository.DeviceConfigRepository
import javax.inject.Inject
import kotlinx.coroutines.flow.Flow

@SysUISingleton
class DeviceConfigInteractor
@Inject
constructor(
    private val repository: DeviceConfigRepository,
) {

    fun property(namespace: String, name: String, default: Boolean): Flow<Boolean> {
        return repository.property(namespace, name, default)
    }
}
+31 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.deviceconfig.data.repository

import com.android.systemui.concurrency.fakeExecutor
import com.android.systemui.kosmos.Kosmos
import com.android.systemui.kosmos.Kosmos.Fixture
import com.android.systemui.kosmos.testDispatcher
import com.android.systemui.util.deviceConfigProxy

val Kosmos.deviceConfigRepository by Fixture {
    DeviceConfigRepository(
        backgroundExecutor = fakeExecutor,
        backgroundDispatcher = testDispatcher,
        dataSource = deviceConfigProxy,
    )
}
Loading