Resume Assist voice input on new intent (as assistant app/brought to front) (#3692)

* Resume Assist voice input on new intent (as assistant app/brought to front)

* Use constants for android.intent.action.ASSIST

 - android.intent.action.VOICE_ASSIST is marked as internal
This commit is contained in:
Joris Pelgröm 2023-07-21 20:11:57 +02:00 committed by GitHub
parent 605e6ec914
commit e4f697fc3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 49 additions and 0 deletions

View File

@ -146,4 +146,10 @@ class AssistActivity : BaseActivity() {
super.onPause()
viewModel.onPause()
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
this.intent = intent
viewModel.onNewIntent(intent)
}
}

View File

@ -1,6 +1,7 @@
package io.homeassistant.companion.android.assist
import android.app.Application
import android.content.Intent
import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
@ -101,6 +102,18 @@ class AssistViewModel @Inject constructor(
}
}
fun onNewIntent(intent: Intent?) {
if (
(
(intent?.flags != null && intent.flags and Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT != 0) ||
intent?.action in listOf(Intent.ACTION_ASSIST, "android.intent.action.VOICE_ASSIST")
) &&
(inputMode == AssistInputMode.VOICE_ACTIVE || inputMode == AssistInputMode.VOICE_INACTIVE)
) {
onMicrophoneInput()
}
}
override fun getInput(): AssistInputMode? = inputMode
override fun setInput(inputMode: AssistInputMode) {

View File

@ -90,6 +90,7 @@
android:name=".conversation.ConversationActivity"
android:icon="@mipmap/ic_assist_launcher"
android:label="@string/ha_assist"
android:launchMode="singleTask"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View File

@ -78,6 +78,16 @@ class ConversationActivity : ComponentActivity() {
}
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
this.intent = intent
val launchIntent = conversationViewModel.onNewIntent(intent)
if (launchIntent) {
launchVoiceInputIntent()
}
}
private fun launchVoiceInputIntent() {
val searchIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH).apply {
putExtra(

View File

@ -1,6 +1,7 @@
package io.homeassistant.companion.android.conversation
import android.app.Application
import android.content.Intent
import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
@ -99,6 +100,24 @@ class ConversationViewModel @Inject constructor(
return false
}
/** @return `true` if the voice input intent should be fired */
fun onNewIntent(intent: Intent?): Boolean {
if (
(
(intent?.flags != null && intent.flags and Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT != 0) ||
intent?.action in listOf(Intent.ACTION_ASSIST, "android.intent.action.VOICE_ASSIST")
) &&
inputMode != AssistInputMode.BLOCKED
) {
if (inputMode == AssistInputMode.TEXT) {
return true
} else {
onMicrophoneInput()
}
}
return false
}
override fun getInput(): AssistInputMode = inputMode
override fun setInput(inputMode: AssistInputMode) {