1 /*
2  * 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 
18 package com.android.settings.search
19 
20 import android.content.Context
21 import android.content.Intent
22 import android.net.Uri
23 import android.provider.Settings
24 import com.android.settings.search.SearchIndexableResourcesFactory.createSearchIndexableResources
25 import com.android.settings.spa.search.SpaSearchRepository
26 import com.android.settingslib.search.SearchIndexableResources
27 
28 /** FeatureProvider for the refactored search code. */
29 open class SearchFeatureProviderImpl : SearchFeatureProvider {
<lambda>null30     private val lazySearchIndexableResources by lazy {
31         createSearchIndexableResources().apply {
32             for (searchIndexableData in SpaSearchRepository().getSearchIndexableDataList()) {
33                 addIndex(searchIndexableData)
34             }
35         }
36     }
37 
verifyLaunchSearchResultPageCallernull38     override fun verifyLaunchSearchResultPageCaller(context: Context, callerPackage: String) {
39         require(callerPackage.isNotEmpty()) {
40             "ExternalSettingsTrampoline intents must be called with startActivityForResult"
41         }
42         val isSettingsPackage = callerPackage == context.packageName
43         if (isSettingsPackage ||
44             callerPackage == getSettingsIntelligencePkgName(context) ||
45             isSignatureAllowlisted(context, callerPackage)) {
46             return
47         }
48         throw SecurityException(
49             "Search result intents must be called with from an allowlisted package.")
50     }
51 
getSearchIndexableResourcesnull52     override fun getSearchIndexableResources(): SearchIndexableResources =
53         lazySearchIndexableResources
54 
55     override fun buildSearchIntent(context: Context, pageId: Int): Intent =
56         Intent(Settings.ACTION_APP_SEARCH_SETTINGS)
57             .setPackage(getSettingsIntelligencePkgName(context))
58             .putExtra(Intent.EXTRA_REFERRER, buildReferrer(context, pageId))
59 
60     protected open fun isSignatureAllowlisted(context: Context, callerPackage: String): Boolean =
61         false
62 
63     companion object {
64         private fun buildReferrer(context: Context, pageId: Int): Uri =
65             Uri.Builder()
66                 .scheme("android-app")
67                 .authority(context.packageName)
68                 .path(pageId.toString())
69                 .build()
70     }
71 }
72