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.BroadcastReceiver
21 import android.content.Context
22 import android.util.Log
23 import androidx.compose.runtime.Composable
24 import androidx.compose.ui.platform.LocalContext
25 import com.android.settingslib.spa.slice.SettingsSliceDataRepository
26 
27 private const val TAG = "SpaEnvironment"
28 
29 object SpaEnvironmentFactory {
30     private var spaEnvironment: SpaEnvironment? = null
31 
resetnull32     fun reset() {
33         spaEnvironment = null
34     }
35 
resetnull36     fun reset(env: SpaEnvironment) {
37         spaEnvironment = env
38         Log.d(TAG, "reset")
39     }
40 
41     @Composable
resetForPreviewnull42     fun resetForPreview() {
43         val context = LocalContext.current
44         spaEnvironment = object : SpaEnvironment(context) {
45             override val pageProviderRepository = lazy {
46                 SettingsPageProviderRepository(
47                     allPageProviders = emptyList(),
48                     rootPages = emptyList()
49                 )
50             }
51         }
52         Log.d(TAG, "resetForPreview")
53     }
54 
isReadynull55     fun isReady(): Boolean {
56         return spaEnvironment != null
57     }
58 
59     val instance: SpaEnvironment
60         get() {
61             if (spaEnvironment == null)
62                 throw UnsupportedOperationException("Spa environment is not set")
63             return spaEnvironment!!
64         }
65 }
66 
67 abstract class SpaEnvironment(context: Context) {
68     abstract val pageProviderRepository: Lazy<SettingsPageProviderRepository>
69 
<lambda>null70     val entryRepository = lazy { SettingsEntryRepository(pageProviderRepository.value) }
71 
<lambda>null72     val sliceDataRepository = lazy { SettingsSliceDataRepository(entryRepository.value) }
73 
74     // The application context. Use local context as fallback when applicationContext is not
75     // available (e.g. in Robolectric test).
76     val appContext: Context = context.applicationContext ?: context
77 
78     // Set your SpaLogger implementation, for any SPA events logging.
79     open val logger: SpaLogger = object : SpaLogger {}
80 
81     // Specify class name of browse activity and slice broadcast receiver, which is used to
82     // generate the necessary intents.
83     open val browseActivityClass: Class<out Activity>? = null
84     open val sliceBroadcastReceiverClass: Class<out BroadcastReceiver>? = null
85 
86     // Specify provider authorities for debugging purpose.
87     open val searchProviderAuthorities: String? = null
88     open val sliceProviderAuthorities: String? = null
89 
90     // TODO: add other environment setup here.
91     companion object {
92         /**
93          * Whether debug mode is on or off.
94          *
95          * If set to true, this will also enable all the pages under development (allows browsing
96          * and searching).
97          */
98         const val IS_DEBUG = false
99     }
100 }
101