• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 display name of the page, for better readability.
37     val displayName: String,
38 
39     // The parameters defined in its page provider.
40     val parameter: List<NamedNavArgument> = emptyList(),
41 
42     // The arguments of this page.
43     val arguments: Bundle? = null,
44 ) {
45     companion object {
46         // TODO: cleanup it once all its usage in Settings are switched to Spp.createSettingsPage
createnull47         fun create(
48             name: String,
49             displayName: String? = null,
50             parameter: List<NamedNavArgument> = emptyList(),
51             arguments: Bundle? = null
52         ): SettingsPage {
53             return SettingsPage(
54                 id = genPageId(name, parameter, arguments),
55                 sppName = name,
56                 displayName = displayName ?: name,
57                 parameter = parameter,
58                 arguments = arguments
59             )
60         }
61     }
62 
63     // Returns if this Settings Page is created by the given Spp.
isCreateBynull64     fun isCreateBy(SppName: String): Boolean {
65         return sppName == SppName
66     }
67 
buildRoutenull68     fun buildRoute(): String {
69         return sppName + parameter.navLink(arguments)
70     }
71 
isBrowsablenull72     fun isBrowsable(): Boolean {
73         if (sppName == NullPageProvider.name) return false
74         for (navArg in parameter) {
75             if (navArg.isRuntimeParam()) return false
76         }
77         return true
78     }
79 
isEnablednull80     fun isEnabled(): Boolean =
81         SpaEnvironment.IS_DEBUG || getPageProvider(sppName)?.isEnabled(arguments) ?: false
82 
83     fun getTitle(): String {
84         return getPageProvider(sppName)?.getTitle(arguments) ?: ""
85     }
86 
87     @Composable
UiLayoutnull88     fun UiLayout() {
89         getPageProvider(sppName)?.Page(arguments)
90     }
91 }
92