mirror of
https://github.com/home-assistant/android
synced 2024-09-19 16:11:37 +00:00
Allow TTS notifications to use the alarm stream (#1037)
* Allow TTS notifications to use the alarm stream * Fix toast error message not displaying in case of TTS failure
This commit is contained in:
parent
c3aa5847f9
commit
6972f82f2a
|
@ -13,6 +13,8 @@ import android.media.AudioAttributes
|
||||||
import android.media.AudioManager
|
import android.media.AudioManager
|
||||||
import android.media.RingtoneManager
|
import android.media.RingtoneManager
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
|
import android.os.Handler
|
||||||
|
import android.os.Looper
|
||||||
import android.speech.tts.TextToSpeech
|
import android.speech.tts.TextToSpeech
|
||||||
import android.speech.tts.UtteranceProgressListener
|
import android.speech.tts.UtteranceProgressListener
|
||||||
import android.text.Spanned
|
import android.text.Spanned
|
||||||
|
@ -128,7 +130,7 @@ class MessagingService : FirebaseMessagingService() {
|
||||||
}
|
}
|
||||||
it[MESSAGE] == TTS -> {
|
it[MESSAGE] == TTS -> {
|
||||||
Log.d(TAG, "Sending notification title to TTS")
|
Log.d(TAG, "Sending notification title to TTS")
|
||||||
speakNotification(it[TITLE])
|
speakNotification(it)
|
||||||
}
|
}
|
||||||
it[MESSAGE] in DEVICE_COMMANDS -> {
|
it[MESSAGE] in DEVICE_COMMANDS -> {
|
||||||
Log.d(TAG, "Processing device command")
|
Log.d(TAG, "Processing device command")
|
||||||
|
@ -197,9 +199,9 @@ class MessagingService : FirebaseMessagingService() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun speakNotification(title: String?) {
|
private fun speakNotification(data: Map<String, String>) {
|
||||||
var textToSpeech: TextToSpeech? = null
|
var textToSpeech: TextToSpeech? = null
|
||||||
var tts = title
|
var tts = data[TITLE]
|
||||||
if (tts.isNullOrEmpty())
|
if (tts.isNullOrEmpty())
|
||||||
tts = getString(R.string.tts_no_title)
|
tts = getString(R.string.tts_no_title)
|
||||||
textToSpeech = TextToSpeech(applicationContext
|
textToSpeech = TextToSpeech(applicationContext
|
||||||
|
@ -221,10 +223,23 @@ class MessagingService : FirebaseMessagingService() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
textToSpeech?.setOnUtteranceProgressListener(listener)
|
textToSpeech?.setOnUtteranceProgressListener(listener)
|
||||||
|
if (data["channel"] == "alarm_stream") {
|
||||||
|
val audioAttributes = AudioAttributes.Builder()
|
||||||
|
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
|
||||||
|
.setUsage(AudioAttributes.USAGE_ALARM)
|
||||||
|
.build()
|
||||||
|
textToSpeech?.setAudioAttributes(audioAttributes)
|
||||||
|
}
|
||||||
textToSpeech?.speak(tts, TextToSpeech.QUEUE_ADD, null, "")
|
textToSpeech?.speak(tts, TextToSpeech.QUEUE_ADD, null, "")
|
||||||
Log.d(TAG, "speaking notification")
|
Log.d(TAG, "speaking notification")
|
||||||
} else {
|
} else {
|
||||||
Toast.makeText(applicationContext, getString(R.string.tts_error, tts), Toast.LENGTH_LONG).show()
|
Handler(Looper.getMainLooper()).post {
|
||||||
|
Toast.makeText(
|
||||||
|
applicationContext,
|
||||||
|
getString(R.string.tts_error, tts),
|
||||||
|
Toast.LENGTH_LONG
|
||||||
|
).show()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue