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.debug
18 
19 import com.android.settingslib.spa.framework.common.EntrySearchData
20 import com.android.settingslib.spa.framework.common.EntryStatusData
21 import com.android.settingslib.spa.framework.common.SettingsEntry
22 import com.android.settingslib.spa.framework.common.SettingsEntryRepository
23 import com.android.settingslib.spa.framework.common.SettingsPage
24 import com.android.settingslib.spa.framework.util.normalize
25 
EntrySearchDatanull26 private fun EntrySearchData.debugContent(): String {
27     val content = listOf(
28         "search_title = $title",
29         "search_keyword = $keyword",
30     )
31     return content.joinToString("\n")
32 }
33 
EntryStatusDatanull34 private fun EntryStatusData.debugContent(): String {
35     val content = listOf(
36         "is_disabled = $isDisabled",
37         "is_switch_off = $isSwitchOff",
38     )
39     return content.joinToString("\n")
40 }
41 
SettingsPagenull42 fun SettingsPage.debugArguments(): String {
43     val normArguments = parameter.normalize(arguments, eraseRuntimeValues = true)
44     if (normArguments == null || normArguments.isEmpty) return "[No arguments]"
45     return normArguments.toString().removeRange(0, 6)
46 }
47 
SettingsPagenull48 fun SettingsPage.debugBrief(): String {
49     return displayName
50 }
51 
debugBriefnull52 fun SettingsEntry.debugBrief(): String {
53     return "${owner.displayName}:$label"
54 }
55 
SettingsEntrynull56 fun SettingsEntry.debugContent(entryRepository: SettingsEntryRepository): String {
57     val searchData = getSearchData()
58     val statusData = getStatusData()
59     val entryPathWithLabel = entryRepository.getEntryPathWithLabel(id)
60     val entryPathWithTitle = entryRepository.getEntryPathWithTitle(id,
61         searchData?.title ?: label)
62     val content = listOf(
63         "------ STATIC ------",
64         "id = $id",
65         "owner = ${owner.debugBrief()} ${owner.debugArguments()}",
66         "linkFrom = ${fromPage?.debugBrief()} ${fromPage?.debugArguments()}",
67         "linkTo = ${toPage?.debugBrief()} ${toPage?.debugArguments()}",
68         "hierarchy_path = $entryPathWithLabel",
69         "------ ATTRIBUTION ------",
70         "allowSearch = $isAllowSearch",
71         "isSearchDynamic = $isSearchDataDynamic",
72         "isSearchMutable = $hasMutableStatus",
73         "hasSlice = $hasSliceSupport",
74         "------ SEARCH ------",
75         "search_path = $entryPathWithTitle",
76         searchData?.debugContent() ?: "no search data",
77         statusData?.debugContent() ?: "no status data",
78     )
79     return content.joinToString("\n")
80 }
81