1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settingslib.spa.framework.common 18 19 import android.app.Activity 20 import android.content.Context 21 import android.util.Log 22 import androidx.compose.runtime.Composable 23 import androidx.compose.ui.platform.LocalContext 24 import com.android.settingslib.spa.framework.util.SystemProperties 25 26 private const val TAG = "SpaEnvironment" 27 28 object SpaEnvironmentFactory { 29 private var spaEnvironment: SpaEnvironment? = null 30 resetnull31 fun reset() { 32 spaEnvironment = null 33 } 34 resetnull35 fun reset(env: SpaEnvironment) { 36 spaEnvironment = env 37 Log.d(TAG, "reset") 38 } 39 40 @Composable resetForPreviewnull41 fun resetForPreview() { 42 val context = LocalContext.current 43 spaEnvironment = object : SpaEnvironment(context) { 44 override val pageProviderRepository = lazy { 45 SettingsPageProviderRepository( 46 allPageProviders = emptyList(), 47 rootPages = emptyList() 48 ) 49 } 50 } 51 Log.d(TAG, "resetForPreview") 52 } 53 isReadynull54 fun isReady(): Boolean = spaEnvironment != null 55 56 val instance: SpaEnvironment 57 get() { 58 if (spaEnvironment == null) 59 throw UnsupportedOperationException("Spa environment is not set") 60 return spaEnvironment!! 61 } 62 63 /** 64 * Optional instance of SpaEnvironment. 65 * 66 * Useful when there is fallback logic. 67 */ 68 val optionalInstance: SpaEnvironment? 69 get() = spaEnvironment 70 } 71 72 abstract class SpaEnvironment(context: Context) { 73 abstract val pageProviderRepository: Lazy<SettingsPageProviderRepository> 74 <lambda>null75 val entryRepository = lazy { SettingsEntryRepository(pageProviderRepository.value) } 76 77 // The application context. Use local context as fallback when applicationContext is not 78 // available (e.g. in Robolectric test). 79 val appContext: Context = context.applicationContext ?: context 80 81 // Set your SpaLogger implementation, for any SPA events logging. 82 open val logger: SpaLogger = object : SpaLogger {} 83 84 // Specify class name of browse activity, which is used to 85 // generate the necessary intents. 86 open val browseActivityClass: Class<out Activity>? = null 87 88 // Specify provider authorities for debugging purpose. 89 open val searchProviderAuthorities: String? = null 90 91 /** Specify whether expressive design is enabled. */ <lambda>null92 open val isSpaExpressiveEnabled by lazy { 93 SystemProperties.getBoolean("is_expressive_design_enabled", false) 94 } 95 96 // TODO: add other environment setup here. 97 companion object { 98 /** 99 * Whether debug mode is on or off. 100 * 101 * If set to true, this will also enable all the pages under development (allows browsing 102 * and searching). 103 */ 104 const val IS_DEBUG = false 105 } 106 } 107