Fix mic button showing on invalid intent

This commit is contained in:
SuperDragonXD
2024-07-19 18:23:36 +08:00
parent febc8660c3
commit 7cd01c0789
3 changed files with 26 additions and 4 deletions
@@ -115,8 +115,9 @@ class AllAppsSearchInput(context: Context, attrs: AttributeSet?) :
val supportsLens = searchProvider == Google || searchProvider == PixelSearch
val lensIntent = getLensIntent(context)
val voiceIntent = AssistantIconView.getVoiceIntent(searchProvider, context)
micIcon.isVisible = shouldShowIcons
micIcon.isVisible = shouldShowIcons && voiceIntent != null
lensIcon.isVisible = shouldShowIcons && supportsLens && lensIntent != null
with(input) {
@@ -162,6 +163,9 @@ class AllAppsSearchInput(context: Context, attrs: AttributeSet?) :
}
with(micIcon) {
setIcon(isGoogle, themed)
setOnClickListener {
context.startActivity(voiceIntent)
}
}
with(lensIcon) {
if (lensIntent != null) {
@@ -2,10 +2,12 @@ package app.lawnchair.qsb
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.util.AttributeSet
import android.widget.ImageButton
import androidx.core.view.isVisible
import app.lawnchair.preferences2.PreferenceManager2
import app.lawnchair.qsb.providers.QsbSearchProvider
import com.android.launcher3.R
@SuppressLint("AppCompatCustomView")
@@ -13,9 +15,9 @@ class AssistantIconView(context: Context, attrs: AttributeSet?) : ImageButton(co
init {
val provider = LawnQsbLayout.getSearchProvider(context, PreferenceManager2.getInstance(context))
val intent = if (provider.supportVoiceIntent) provider.createVoiceIntent() else null
if (intent == null || !LawnQsbLayout.resolveIntent(context, intent)) isVisible = false
val intent = getVoiceIntent(provider, context)
isVisible = intent != null
setOnClickListener {
context.startActivity(intent)
@@ -34,4 +36,20 @@ class AssistantIconView(context: Context, attrs: AttributeSet?) : ImageButton(co
method = themingMethod,
)
}
companion object {
fun getVoiceIntent(
provider: QsbSearchProvider,
context: Context
): Intent? {
val intent = if (provider.supportVoiceIntent) provider.createVoiceIntent() else null
return if (intent == null || !LawnQsbLayout.resolveIntent(context, intent)) {
null
} else {
intent
}
}
}
}
@@ -21,7 +21,7 @@ fun createSearchTarget(appInfo: AppInfo, asRow: Boolean = false): SearchTargetCo
val user = appInfo.user
return SearchTargetCompat.Builder(
SearchTargetCompat.RESULT_TYPE_APPLICATION,
if (asRow) LayoutType.ICON_HORIZONTAL_TEXT else LayoutType.ICON_SINGLE_VERTICAL_TEXT,
if (asRow) LayoutType.SMALL_ICON_HORIZONTAL_TEXT else LayoutType.ICON_SINGLE_VERTICAL_TEXT,
generateHashKey(ComponentKey(componentName, user).toString()),
)
.setPackageName(componentName.packageName)