• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2024 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.settings.spa.search
18 
19 import android.content.Context
20 import android.provider.SearchIndexableResource
21 import android.util.Log
22 import androidx.annotation.VisibleForTesting
23 import com.android.settings.network.telephony.MobileNetworkSettingsSearchIndex
24 import com.android.settings.spa.SpaSearchLanding.SpaSearchLandingKey
25 import com.android.settings.spa.SpaSearchLanding.SpaSearchLandingSpaPage
26 import com.android.settingslib.search.Indexable
27 import com.android.settingslib.search.SearchIndexableData
28 import com.android.settingslib.search.SearchIndexableRaw
29 import com.android.settingslib.spa.framework.common.SettingsPageProvider
30 import com.android.settingslib.spa.framework.common.SpaEnvironment
31 import com.android.settingslib.spa.framework.common.SpaEnvironmentFactory
32 
33 class SpaSearchRepository(
34     private val spaEnvironment: SpaEnvironment = SpaEnvironmentFactory.instance,
35 ) {
36     fun getSearchIndexableDataList(): List<SearchIndexableData> {
37         Log.d(TAG, "getSearchIndexableDataList")
38         return spaEnvironment.pageProviderRepository.value.getAllProviders().mapNotNull { page ->
39             if (page is SearchablePage) {
40                 page.createSearchIndexableData(
41                     page::getPageTitleForSearch, page::getSearchableTitles)
42             } else null
43         } + MobileNetworkSettingsSearchIndex().createSearchIndexableData()
44     }
45 
46     companion object {
47         private const val TAG = "SpaSearchRepository"
48 
49         @VisibleForTesting
50         fun SettingsPageProvider.createSearchIndexableData(
51             getPageTitleForSearch: (context: Context) -> String,
52             titlesProvider: (context: Context) -> List<String>,
53         ): SearchIndexableData {
54             val key =
55                 SpaSearchLandingKey.newBuilder()
56                     .setSpaPage(SpaSearchLandingSpaPage.newBuilder().setDestination(name))
57                     .build()
58             val indexableClass = this::class.java
59             val searchIndexProvider = searchIndexProviderOf { context ->
60                 val pageTitle = getPageTitleForSearch(context)
61                 titlesProvider(context).map { itemTitle ->
62                     createSearchIndexableRaw(context, key, itemTitle, indexableClass, pageTitle)
63                 }
64             }
65             return SearchIndexableData(indexableClass, searchIndexProvider)
66         }
67 
68         fun searchIndexProviderOf(
69             getDynamicRawDataToIndex: (context: Context) -> List<SearchIndexableRaw>,
70         ) =
71             object : Indexable.SearchIndexProvider {
72                 override fun getXmlResourcesToIndex(
73                     context: Context,
74                     enabled: Boolean,
75                 ): List<SearchIndexableResource> = emptyList()
76 
77                 override fun getRawDataToIndex(
78                     context: Context,
79                     enabled: Boolean,
80                 ): List<SearchIndexableRaw> = emptyList()
81 
82                 override fun getDynamicRawDataToIndex(
83                     context: Context,
84                     enabled: Boolean,
85                 ): List<SearchIndexableRaw> = getDynamicRawDataToIndex(context)
86 
87                 override fun getNonIndexableKeys(context: Context): List<String> = emptyList()
88             }
89 
90         fun createSearchIndexableRaw(
91             context: Context,
92             spaSearchLandingKey: SpaSearchLandingKey,
93             itemTitle: String,
94             indexableClass: Class<*>,
95             pageTitle: String,
96             keywords: String? = null,
97         ) =
98             SearchIndexableRaw(context).apply {
99                 key = spaSearchLandingKey.encodeToString()
100                 title = itemTitle
101                 this.keywords = keywords
102                 intentAction = SEARCH_LANDING_ACTION
103                 intentTargetClass = SpaSearchLandingActivity::class.qualifiedName
104                 packageName = context.packageName
105                 className = indexableClass.name
106                 screenTitle = pageTitle
107             }
108 
109         private const val SEARCH_LANDING_ACTION = "android.settings.SPA_SEARCH_LANDING"
110     }
111 }
112