mirror of
https://github.com/home-assistant/android
synced 2024-09-19 08:01:31 +00:00
Allow notifications to do TTS (#1011)
* Allow TTS for notifications * Finalize approach, error and blank title handling * Use utterance progress indicator to properly shut down TTS engine
This commit is contained in:
parent
f40c95adcd
commit
a59ec710c6
|
@ -9,8 +9,11 @@ import android.graphics.BitmapFactory
|
||||||
import android.graphics.Color
|
import android.graphics.Color
|
||||||
import android.media.RingtoneManager
|
import android.media.RingtoneManager
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
|
import android.speech.tts.TextToSpeech
|
||||||
|
import android.speech.tts.UtteranceProgressListener
|
||||||
import android.text.Spanned
|
import android.text.Spanned
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
import android.widget.Toast
|
||||||
import androidx.annotation.RequiresApi
|
import androidx.annotation.RequiresApi
|
||||||
import androidx.core.app.NotificationCompat
|
import androidx.core.app.NotificationCompat
|
||||||
import androidx.core.app.NotificationManagerCompat
|
import androidx.core.app.NotificationManagerCompat
|
||||||
|
@ -59,6 +62,7 @@ class MessagingService : FirebaseMessagingService() {
|
||||||
const val REQUEST_LOCATION_UPDATE = "request_location_update"
|
const val REQUEST_LOCATION_UPDATE = "request_location_update"
|
||||||
const val CLEAR_NOTIFICATION = "clear_notification"
|
const val CLEAR_NOTIFICATION = "clear_notification"
|
||||||
const val REMOVE_CHANNEL = "remove_channel"
|
const val REMOVE_CHANNEL = "remove_channel"
|
||||||
|
const val TTS = "TTS"
|
||||||
}
|
}
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
|
@ -100,6 +104,10 @@ class MessagingService : FirebaseMessagingService() {
|
||||||
Log.d(TAG, "Removing Notification channel ${it["tag"]}")
|
Log.d(TAG, "Removing Notification channel ${it["tag"]}")
|
||||||
removeNotificationChannel(it["channel"]!!)
|
removeNotificationChannel(it["channel"]!!)
|
||||||
}
|
}
|
||||||
|
it[MESSAGE] == TTS -> {
|
||||||
|
Log.d(TAG, "Sending notification title to TTS")
|
||||||
|
speakNotification(it[TITLE])
|
||||||
|
}
|
||||||
else -> mainScope.launch {
|
else -> mainScope.launch {
|
||||||
Log.d(TAG, "Creating notification with following data: $it")
|
Log.d(TAG, "Creating notification with following data: $it")
|
||||||
sendNotification(it)
|
sendNotification(it)
|
||||||
|
@ -134,6 +142,38 @@ class MessagingService : FirebaseMessagingService() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun speakNotification(title: String?) {
|
||||||
|
var textToSpeech: TextToSpeech? = null
|
||||||
|
var tts = title
|
||||||
|
if (tts.isNullOrEmpty())
|
||||||
|
tts = getString(R.string.tts_no_title)
|
||||||
|
textToSpeech = TextToSpeech(applicationContext
|
||||||
|
) {
|
||||||
|
if (it == TextToSpeech.SUCCESS) {
|
||||||
|
val listener = object : UtteranceProgressListener() {
|
||||||
|
override fun onStart(p0: String?) {
|
||||||
|
// No op
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDone(p0: String?) {
|
||||||
|
textToSpeech?.stop()
|
||||||
|
textToSpeech?.shutdown()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onError(p0: String?) {
|
||||||
|
textToSpeech?.stop()
|
||||||
|
textToSpeech?.shutdown()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
textToSpeech?.setOnUtteranceProgressListener(listener)
|
||||||
|
textToSpeech?.speak(tts, TextToSpeech.QUEUE_ADD, null, "")
|
||||||
|
Log.d(TAG, "speaking notification")
|
||||||
|
} else {
|
||||||
|
Toast.makeText(applicationContext, getString(R.string.tts_error, tts), Toast.LENGTH_LONG).show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create and show a simple notification containing the received FCM message.
|
* Create and show a simple notification containing the received FCM message.
|
||||||
*
|
*
|
||||||
|
|
|
@ -95,6 +95,8 @@ to your home internet.</string>
|
||||||
<string name="sensors_with_settings">The following sensors offer custom settings: %1$s</string>
|
<string name="sensors_with_settings">The following sensors offer custom settings: %1$s</string>
|
||||||
<string name="enabled_summary">When enabled values will be sent to Home Assistant</string>
|
<string name="enabled_summary">When enabled values will be sent to Home Assistant</string>
|
||||||
<string name="enabled_title">Enabled</string>
|
<string name="enabled_title">Enabled</string>
|
||||||
|
<string name="tts_error">Unable to process notification "%1$s" as text to speech.</string>
|
||||||
|
<string name="tts_no_title">Please set a title for text to speech to process</string>
|
||||||
<string name="entity_attribute_checkbox">Append Attribute Value</string>
|
<string name="entity_attribute_checkbox">Append Attribute Value</string>
|
||||||
<string name="error_auth_revoked">It appears that your authorization was revoked, please reconnect to Home Assistant.</string>
|
<string name="error_auth_revoked">It appears that your authorization was revoked, please reconnect to Home Assistant.</string>
|
||||||
<string name="error_connection_failed">Unable to connect to Home Assistant.</string>
|
<string name="error_connection_failed">Unable to connect to Home Assistant.</string>
|
||||||
|
|
Loading…
Reference in a new issue