Add more options for custom intent search actions

This commit is contained in:
MM20
2025-09-21 18:02:38 +02:00
parent 960dd51594
commit 4d1bbec175
6 changed files with 262 additions and 79 deletions

View File

@@ -2,20 +2,33 @@ package de.mm20.launcher2.searchactions.actions
import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.core.net.toUri
import de.mm20.launcher2.ktx.tryStartActivity
class CustomIntentAction(
override val label: String,
val query: String,
private val queryKey: String,
private val queryKey: String?,
private val queryTemplate: String? = null,
private val baseIntent: Intent,
override val icon: SearchActionIcon = SearchActionIcon.Custom,
override val iconColor: Int = 1,
override val customIcon: String? = null,
) : SearchAction {
override fun start(context: Context) {
val query = if (queryTemplate.isNullOrBlank()) {
query
} else {
queryTemplate.replace("\${1}", query)
}
val intent = Intent(baseIntent).also {
it.putExtra(queryKey, query)
if (queryKey == null) {
it.setDataAndType(query.toUri(), baseIntent.type)
} else {
it.putExtra(queryKey, query)
}
}
context.tryStartActivity(intent)
}

View File

@@ -9,7 +9,11 @@ import de.mm20.launcher2.searchactions.actions.SearchActionIcon
data class CustomIntentActionBuilder(
override val label: String,
val queryKey: String,
/**
* The extra of the intent to put the query in. If null, the query will be passed in the data URI.
*/
val queryKey: String?,
val queryTemplate: String? = null,
val baseIntent: Intent,
override val icon: SearchActionIcon = SearchActionIcon.Custom,
override val iconColor: Int = 1,
@@ -20,7 +24,7 @@ data class CustomIntentActionBuilder(
override fun build(context: Context, classifiedQuery: TextClassificationResult): SearchAction {
return CustomIntentAction(
label, classifiedQuery.text, queryKey, baseIntent, icon, iconColor, customIcon
label, classifiedQuery.text, queryKey, queryTemplate, baseIntent, icon, iconColor, customIcon
)
}
}

View File

@@ -57,7 +57,8 @@ interface SearchActionBuilder {
iconColor = entity.color ?: 0,
icon = SearchActionIcon.fromInt(entity.icon),
customIcon = entity.customIcon,
queryKey = options?.getString("extra")?.takeIf { it.isNotEmpty() } ?: return null
queryKey = options?.optString("extra"),
queryTemplate = options?.optString("template")?.takeIf { it.isNotBlank() }
)
}
"call" -> return CallActionBuilder(context)
@@ -107,7 +108,8 @@ interface SearchActionBuilder {
icon = builder.icon.toInt(),
customIcon = builder.customIcon,
options = jsonObjectOf(
"extra" to builder.queryKey
"extra" to builder.queryKey,
"template" to builder.queryTemplate,
).toString()
)
else -> SearchActionEntity(