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.os.Bundle
20 import androidx.compose.runtime.Composable
21 import androidx.navigation.NamedNavArgument
22 import com.android.settingslib.spa.framework.util.genPageId
23 import com.android.settingslib.spa.framework.util.isRuntimeParam
24 import com.android.settingslib.spa.framework.util.navLink
25 
26 /**
27  * Defines data to identify a Settings page.
28  */
29 data class SettingsPage(
30     // The unique id of this page, which is computed by sppName + normalized(arguments)
31     val id: String,
32 
33     // The name of the page provider, who creates this page. It is used to compute the unique id.
34     val sppName: String,
35 
36     // The category id of the page provider which is the PageId at SettingsEnums.
37     val metricsCategory: Int = 0,
38 
39     // The display name of the page, for better readability.
40     val displayName: String,
41 
42     // The parameters defined in its page provider.
43     val parameter: List<NamedNavArgument> = emptyList(),
44 
45     // The arguments of this page.
46     val arguments: Bundle? = null,
47 ) {
48     companion object {
49         // TODO: cleanup it once all its usage in Settings are switched to Spp.createSettingsPage
createnull50         fun create(
51             name: String,
52             metricsCategory: Int = 0,
53             displayName: String? = null,
54             parameter: List<NamedNavArgument> = emptyList(),
55             arguments: Bundle? = null
56         ): SettingsPage {
57             return SettingsPage(
58                 id = genPageId(name, parameter, arguments),
59                 sppName = name,
60                 metricsCategory = metricsCategory,
61                 displayName = displayName ?: name,
62                 parameter = parameter,
63                 arguments = arguments
64             )
65         }
66     }
67 
68     // Returns if this Settings Page is created by the given Spp.
isCreateBynull69     fun isCreateBy(SppName: String): Boolean {
70         return sppName == SppName
71     }
72 
buildRoutenull73     fun buildRoute(): String {
74         return sppName + parameter.navLink(arguments)
75     }
76 
isBrowsablenull77     fun isBrowsable(): Boolean {
78         if (sppName == NullPageProvider.name) return false
79         for (navArg in parameter) {
80             if (navArg.isRuntimeParam()) return false
81         }
82         return true
83     }
84 
isEnablednull85     fun isEnabled(): Boolean =
86         SpaEnvironment.IS_DEBUG || getPageProvider(sppName)?.isEnabled(arguments) ?: false
87 
88     fun getTitle(): String {
89         return getPageProvider(sppName)?.getTitle(arguments) ?: ""
90     }
91 
92     @Composable
UiLayoutnull93     fun UiLayout() {
94         getPageProvider(sppName)?.Page(arguments)
95     }
96 }
97