1 /*
<lambda>null2 * 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.os.Bundle
20 import androidx.compose.runtime.Composable
21 import androidx.compose.runtime.remember
22 import androidx.navigation.NamedNavArgument
23 import com.android.settingslib.spa.framework.util.genPageId
24 import com.android.settingslib.spa.framework.util.normalizeArgList
25 import com.android.settingslib.spa.widget.scaffold.RegularScaffold
26
27 private const val NULL_PAGE_NAME = "NULL"
28
29 /**
30 * An SettingsPageProvider which is used to create Settings page instances.
31 */
32 interface SettingsPageProvider {
33
34 /** The page provider name, needs to be *unique* and *stable*. */
35 val name: String
36
37 /** The category id which is the PageId at SettingsEnums.*/
38 val metricsCategory: Int
39 get() = 0
40
41 enum class NavType {
42 Page,
43 Dialog,
44 }
45
46 val navType: NavType
47 get() = NavType.Page
48
49 /** The display name of this page provider, for better readability. */
50 val displayName: String
51 get() = name
52
53 /** The page parameters, default is no parameters. */
54 val parameter: List<NamedNavArgument>
55 get() = emptyList()
56
57 /**
58 * The API to indicate whether the page is enabled or not.
59 * During SPA page migration, one can use it to enable certain pages in one release.
60 * When the page is disabled, all its related functionalities, such as browsing, search,
61 * slice provider, are disabled as well.
62 */
63 fun isEnabled(arguments: Bundle?): Boolean = true
64
65 fun getTitle(arguments: Bundle?): String = displayName
66
67 fun buildEntry(arguments: Bundle?): List<SettingsEntry> = emptyList()
68
69 /** The [Composable] used to render this page. */
70 @Composable
71 fun Page(arguments: Bundle?) {
72 val title = remember { getTitle(arguments) }
73 val entries = remember { buildEntry(arguments) }
74 RegularScaffold(title) {
75 for (entry in entries) {
76 entry.UiLayout()
77 }
78 }
79 }
80 }
81
createSettingsPagenull82 fun SettingsPageProvider.createSettingsPage(arguments: Bundle? = null): SettingsPage {
83 return SettingsPage(
84 id = genPageId(name, parameter, arguments),
85 sppName = name,
86 metricsCategory = metricsCategory,
87 displayName = displayName + parameter.normalizeArgList(arguments, eraseRuntimeValues = true)
88 .joinToString("") { arg -> "/$arg" },
89 parameter = parameter,
90 arguments = arguments,
91 )
92 }
93
94 internal object NullPageProvider : SettingsPageProvider {
95 override val name = NULL_PAGE_NAME
96 }
97
getPageProvidernull98 fun getPageProvider(sppName: String): SettingsPageProvider? {
99 if (!SpaEnvironmentFactory.isReady()) return null
100 val pageProviderRepository by SpaEnvironmentFactory.instance.pageProviderRepository
101 return pageProviderRepository.getProviderOrNull(sppName)
102 }
103