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

Commit 898bcd5c authored by Alejandro Nijamkin's avatar Alejandro Nijamkin
Browse files

[flexiglass] Container config - domain layer.

The container configuration portion of the Flexiglass framework allows
OEMs to configure one or more Flexiglass containers with scenes. Each scene is a distinct bit of UI content that the
user can navigate to (for example by swiping in a direction) or that can
be navigated to automatically through the SceneInteractor methods.

Bug: 279501596
Test: Included unit tests. Manually tested through a testbed app that
uses Jetpack Compose to implement a simple UI to traverse all scenes.

Change-Id: I28708a55963ae79d48e91f2dcef06eeadea167c4
parent ceb61251
Loading
Loading
Loading
Loading
+73 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.scene.domain.interactor

import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.scene.data.repository.SceneContainerRepository
import com.android.systemui.scene.shared.model.SceneKey
import com.android.systemui.scene.shared.model.SceneModel
import javax.inject.Inject
import kotlinx.coroutines.flow.StateFlow

/** Business logic and app state accessors for the scene framework. */
@SysUISingleton
class SceneInteractor
@Inject
constructor(
    private val repository: SceneContainerRepository,
) {

    /**
     * Returns the keys of all scenes in the container with the given name.
     *
     * The scenes will be sorted in z-order such that the last one is the one that should be
     * rendered on top of all previous ones.
     */
    fun allSceneKeys(containerName: String): List<SceneKey> {
        return repository.allSceneKeys(containerName)
    }

    /** Sets the scene in the container with the given name. */
    fun setCurrentScene(containerName: String, scene: SceneModel) {
        repository.setCurrentScene(containerName, scene)
    }

    /** The current scene in the container with the given name. */
    fun currentScene(containerName: String): StateFlow<SceneModel> {
        return repository.currentScene(containerName)
    }

    /** Sets the visibility of the container with the given name. */
    fun setVisible(containerName: String, isVisible: Boolean) {
        return repository.setVisible(containerName, isVisible)
    }

    /** Whether the container with the given name is visible. */
    fun isVisible(containerName: String): StateFlow<Boolean> {
        return repository.isVisible(containerName)
    }

    /** Sets scene transition progress to the current scene in the container with the given name. */
    fun setSceneTransitionProgress(containerName: String, progress: Float) {
        repository.setSceneTransitionProgress(containerName, progress)
    }

    /** Progress of the transition into the current scene in the container with the given name. */
    fun sceneTransitionProgress(containerName: String): StateFlow<Float> {
        return repository.sceneTransitionProgress(containerName)
    }
}
+78 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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, ExperimentalCoroutinesApi::class)

package com.android.systemui.scene.domain.interactor

import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.scene.data.repository.fakeSceneContainerRepository
import com.android.systemui.scene.data.repository.fakeSceneKeys
import com.android.systemui.scene.shared.model.SceneKey
import com.android.systemui.scene.shared.model.SceneModel
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
import org.junit.runners.JUnit4

@SmallTest
@RunWith(JUnit4::class)
class SceneInteractorTest : SysuiTestCase() {

    private val underTest =
        SceneInteractor(
            repository = fakeSceneContainerRepository(),
        )

    @Test
    fun allSceneKeys() {
        assertThat(underTest.allSceneKeys("container1")).isEqualTo(fakeSceneKeys())
    }

    @Test
    fun sceneTransitions() = runTest {
        val currentScene by collectLastValue(underTest.currentScene("container1"))
        assertThat(currentScene).isEqualTo(SceneModel(SceneKey.LockScreen))

        underTest.setCurrentScene("container1", SceneModel(SceneKey.Shade))
        assertThat(currentScene).isEqualTo(SceneModel(SceneKey.Shade))
    }

    @Test
    fun sceneTransitionProgress() = runTest {
        val progress by collectLastValue(underTest.sceneTransitionProgress("container1"))
        assertThat(progress).isEqualTo(1f)

        underTest.setSceneTransitionProgress("container1", 0.55f)
        assertThat(progress).isEqualTo(0.55f)
    }

    @Test
    fun isVisible() = runTest {
        val isVisible by collectLastValue(underTest.isVisible("container1"))
        assertThat(isVisible).isTrue()

        underTest.setVisible("container1", false)
        assertThat(isVisible).isFalse()

        underTest.setVisible("container1", true)
        assertThat(isVisible).isTrue()
    }
}