Allow changing name of custom web search suggestions

This commit is contained in:
SuperDragonXD
2025-06-22 19:25:46 +08:00
parent 0b17d8376d
commit 07d73efd72
6 changed files with 59 additions and 14 deletions

View File

@@ -803,4 +803,5 @@
<string name="warn_contact_permission_content">To search for contacts, grant contacts and phone permissions to Lawnchair</string>
<string name="warn_files_permission_content">To search your files, grant storage permissions to Lawnchair</string>
<string name="grant_requested_permissions">Grant permissions</string>
<string name="custom_search_label">Custom search engine name</string>
</resources>

View File

@@ -513,6 +513,11 @@ class PreferenceManager2 private constructor(private val context: Context) :
defaultValue = "https://google.com/complete/search?client=chrome&q=%s",
)
val webSuggestionProviderName = preference(
key = stringPreferencesKey(name = "web_suggestion_provider_name"),
defaultValue = context.resources.getString(R.string.custom),
)
val maxAppSearchResultCount = preference(
key = intPreferencesKey(name = "max_search_result_count"),
defaultValue = resourceProvider.getInt(R.dimen.config_default_search_max_result_count),

View File

@@ -12,6 +12,7 @@ import android.os.Process
import android.provider.ContactsContract
import android.provider.MediaStore
import android.util.Log
import androidx.core.net.toUri
import androidx.core.os.bundleOf
import app.lawnchair.allapps.views.SearchResultView
import app.lawnchair.search.algorithms.data.Calculation
@@ -73,7 +74,7 @@ class SearchTargetFactory(
fun createWebSuggestionsTarget(suggestion: String, suggestionProvider: String, suggestionUrl: String = ""): SearchTargetCompat {
val webSearchProvider = WebSearchProvider.fromString(suggestionProvider)
val url = if (webSearchProvider is CustomWebSearchProvider) webSearchProvider.getCustomSearchUrl(suggestion, suggestionUrl) else webSearchProvider.getSearchUrl(suggestion)
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
val browserIntent = Intent(Intent.ACTION_VIEW, url.toUri())
val id = suggestion + url
val action = SearchActionCompat.Builder(id, suggestion).apply {
setIcon(
@@ -143,7 +144,7 @@ class SearchTargetFactory(
val value = recentKeyword.getValueByKey("display1") ?: ""
val webSearchProvider = WebSearchProvider.fromString(suggestionProvider)
val url = if (webSearchProvider is CustomWebSearchProvider) webSearchProvider.getCustomSearchUrl(value, "%s") else webSearchProvider.getSearchUrl(value)
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
val browserIntent = Intent(Intent.ACTION_VIEW, url.toUri())
val id = recentKeyword.data.toString() + url
val action = SearchActionCompat.Builder(id, value)
.setIcon(
@@ -239,10 +240,16 @@ class SearchTargetFactory(
internal fun createWebSearchTarget(
query: String,
suggestionProvider: String,
suggestionName: String,
suggestionUrl: String = "",
): SearchTargetCompat {
val webSearchProvider = WebSearchProvider.fromString(suggestionProvider)
val webSearchLabel = context.getString(webSearchProvider.label)
val webSearchLabel =
if (webSearchProvider is CustomWebSearchProvider) {
suggestionName
} else {
context.getString(webSearchProvider.label)
}
val url =
if (webSearchProvider is CustomWebSearchProvider) {
webSearchProvider.getCustomSearchUrl(query, suggestionUrl)
@@ -250,12 +257,8 @@ class SearchTargetFactory(
webSearchProvider.getSearchUrl(query)
}
val id = "browser:$query"
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
val string = if (webSearchProvider is CustomWebSearchProvider) {
context.getString(R.string.all_apps_search_on_web_general)
} else {
context.getString(R.string.all_apps_search_on_web_message, webSearchLabel)
}
val browserIntent = Intent(Intent.ACTION_VIEW, url.toUri())
val string = context.getString(R.string.all_apps_search_on_web_message, webSearchLabel)
val action = SearchActionCompat.Builder(id, string)
.setIcon(Icon.createWithResource(context, webSearchProvider.iconRes))
.setIntent(browserIntent)
@@ -275,7 +278,7 @@ class SearchTargetFactory(
val contactIntent = Intent(Intent.ACTION_VIEW, contactUri)
val action = SearchActionCompat.Builder(id, info.name)
.setIcon(ContactsTarget.displayContactPhoto(context, info.name, Uri.parse(info.uri)))
.setIcon(ContactsTarget.displayContactPhoto(context, info.name, info.uri.toUri()))
.setContentDescription(info.contactId)
.setSubtitle(info.number)
.setIntent(contactIntent)

View File

@@ -69,6 +69,7 @@ class LawnchairLocalSearchAlgorithm(context: Context) : LawnchairSearchAlgorithm
private var enableFuzzySearch = false
private var useWebSuggestions = true
private var webSuggestionsProvider = ""
private var webSuggestionsProviderName = ""
private var webSuggestionsProviderUrl = ""
private var webSuggestionProviderSuggestionsUrl = ""
@@ -108,6 +109,9 @@ class LawnchairLocalSearchAlgorithm(context: Context) : LawnchairSearchAlgorithm
pref2.webSuggestionProviderSuggestionsUrl.onEach(launchIn = coroutineScope) {
webSuggestionProviderSuggestionsUrl = it
}
pref2.webSuggestionProviderName.onEach(launchIn = coroutineScope) {
webSuggestionsProviderName = it
}
pref2.maxAppSearchResultCount.onEach(launchIn = coroutineScope) {
maxAppResultsCount = it
@@ -215,6 +219,7 @@ class LawnchairLocalSearchAlgorithm(context: Context) : LawnchairSearchAlgorithm
searchTargetFactory.createWebSearchTarget(
query,
webSuggestionsProvider,
webSuggestionsProviderName,
suggestionUrl,
),
)

View File

@@ -199,6 +199,7 @@ private fun LocalSearchSettings(
)
WebSearchProvider(
adapter = prefs2.webSuggestionProvider.getAdapter(),
nameAdapter = prefs2.webSuggestionProviderName.getAdapter(),
urlAdapter = prefs2.webSuggestionProviderUrl.getAdapter(),
suggestionsUrlAdapter = prefs2.webSuggestionProviderSuggestionsUrl.getAdapter(),
)

View File

@@ -35,6 +35,7 @@ import com.android.launcher3.R
@Composable
fun WebSearchProvider(
adapter: PreferenceAdapter<WebSearchProvider>,
nameAdapter: PreferenceAdapter<String>,
urlAdapter: PreferenceAdapter<String>,
suggestionsUrlAdapter: PreferenceAdapter<String>,
modifier: Modifier = Modifier,
@@ -55,6 +56,13 @@ fun WebSearchProvider(
label = stringResource(R.string.allapps_web_suggestion_provider_label),
)
if (adapter.state.value == WebSearchProvider.fromString("custom")) {
SearchPopupPreference(
title = stringResource(R.string.custom_search_label),
initialValue = nameAdapter.state.value,
placeholder = stringResource(R.string.custom),
onConfirm = nameAdapter::onChange,
isErrorCheck = { it.isEmpty() },
)
SearchUrlPreference(
title = stringResource(R.string.custom_search_url),
initialValue = urlAdapter.state.value,
@@ -75,6 +83,26 @@ fun SearchUrlPreference(
initialValue: String,
onConfirm: (String) -> Unit,
modifier: Modifier = Modifier,
) {
SearchPopupPreference(
title = title,
initialValue = initialValue,
placeholder = stringResource(R.string.custom_search_input_placeholder),
hint = stringResource(R.string.custom_search_input_hint),
onConfirm = onConfirm,
modifier = modifier,
)
}
@Composable
fun SearchPopupPreference(
title: String,
initialValue: String,
placeholder: String,
onConfirm: (String) -> Unit,
modifier: Modifier = Modifier,
hint: String? = null,
isErrorCheck: (String) -> Boolean = { it.isEmpty() || !it.contains("%s") },
) {
var showPopup by remember { mutableStateOf(false) }
var value by remember { mutableStateOf(TextFieldValue(initialValue)) }
@@ -111,13 +139,15 @@ fun SearchUrlPreference(
onValueChange = { value = it },
modifier = Modifier.fillMaxWidth(),
singleLine = true,
isError = value.text.isEmpty() || !value.text.contains("%s"),
isError = isErrorCheck(value.text),
placeholder = {
Text(stringResource(R.string.custom_search_input_placeholder))
Text(placeholder)
},
)
Spacer(modifier = Modifier.height(8.dp))
Text(stringResource(R.string.custom_search_input_hint))
if (hint != null) {
Spacer(modifier = Modifier.height(8.dp))
Text(hint)
}
}
},
)