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

Commit b725d73b authored by Chaohui Wang's avatar Chaohui Wang Committed by Android (Google) Code Review
Browse files

Merge "[Spa] Add document for SpaEnvironment" into main

parents 2d9740f8 7f0f0733
Loading
Loading
Loading
Loading
+24 −6
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.settingslib.spa.framework.common
import android.app.Activity
import android.content.Context
import android.util.Log
import androidx.annotation.VisibleForTesting
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import com.android.settingslib.spa.framework.util.SystemProperties
@@ -28,10 +29,13 @@ private const val TAG = "SpaEnvironment"
object SpaEnvironmentFactory {
    private var spaEnvironment: SpaEnvironment? = null

    fun reset() {
        spaEnvironment = null
    }

    /**
     * Resets the SpaEnvironment to the given instance, which is usually required step to set up
     * SPA.
     *
     * This is usually be called in an Application class, but could be called in app initializors or
     * setup listeners.
     */
    fun reset(env: SpaEnvironment) {
        spaEnvironment = env
        Log.d(TAG, "reset")
@@ -65,11 +69,26 @@ object SpaEnvironmentFactory {
     *
     * Useful when there is fallback logic.
     */
    val optionalInstance: SpaEnvironment?
    internal val optionalInstance: SpaEnvironment?
        get() = spaEnvironment

    @VisibleForTesting
    internal fun clear() {
        spaEnvironment = null
    }
}

/**
 * The environment of SPA.
 *
 * This class is used to hold the global configurations of SPA.
 *
 * To set up SpaEnvironment,
 * 1. create a concrete class that extends [SpaEnvironment].
 * 2. call [SpaEnvironmentFactory.reset] with your implementation to set the global environment.
 */
abstract class SpaEnvironment(context: Context) {
    /** The repository of all page providers, SPA pages are setup here. */
    abstract val pageProviderRepository: Lazy<SettingsPageProviderRepository>

    val entryRepository = lazy { SettingsEntryRepository(pageProviderRepository.value) }
@@ -93,7 +112,6 @@ abstract class SpaEnvironment(context: Context) {
        SystemProperties.getBoolean("is_expressive_design_enabled", false)
    }

    // TODO: add other environment setup here.
    companion object {
        /**
         * Whether debug mode is on or off.
+1 −1
Original line number Diff line number Diff line
@@ -164,7 +164,7 @@ class SettingsEntryTest {
        assertThat(entry2.isAllowSearch).isFalse()

        // Clear SppHome in spa environment
        SpaEnvironmentFactory.reset()
        SpaEnvironmentFactory.clear()
        val entry3 = entryBuilder.build()
        assertThat(entry3.id).isEqualTo(genEntryId("myEntry", owner))
        assertThat(entry3.label).isEqualTo("myEntryDisplay")
+2 −2
Original line number Diff line number Diff line
@@ -37,7 +37,7 @@ class SpaEnvironmentTest {

    @Test
    fun testSpaEnvironmentFactory() {
        SpaEnvironmentFactory.reset()
        SpaEnvironmentFactory.clear()
        Truth.assertThat(SpaEnvironmentFactory.isReady()).isFalse()
        Assert.assertThrows(UnsupportedOperationException::class.java) {
            SpaEnvironmentFactory.instance
@@ -50,7 +50,7 @@ class SpaEnvironmentTest {

    @Test
    fun testSpaEnvironmentFactoryForPreview() {
        SpaEnvironmentFactory.reset()
        SpaEnvironmentFactory.clear()
        composeTestRule.setContent {
            Truth.assertThat(SpaEnvironmentFactory.isReady()).isFalse()
            SpaEnvironmentFactory.resetForPreview()