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:
Daniel Shokouhi 2020-10-13 09:38:51 -07:00 committed by GitHub
parent c3aa5847f9
commit 6972f82f2a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -13,6 +13,8 @@ import android.media.AudioAttributes
import android.media.AudioManager
import android.media.RingtoneManager
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.speech.tts.TextToSpeech
import android.speech.tts.UtteranceProgressListener
import android.text.Spanned
@ -128,7 +130,7 @@ class MessagingService : FirebaseMessagingService() {
}
it[MESSAGE] == TTS -> {
Log.d(TAG, "Sending notification title to TTS")
speakNotification(it[TITLE])
speakNotification(it)
}
it[MESSAGE] in DEVICE_COMMANDS -> {
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 tts = title
var tts = data[TITLE]
if (tts.isNullOrEmpty())
tts = getString(R.string.tts_no_title)
textToSpeech = TextToSpeech(applicationContext
@ -221,10 +223,23 @@ class MessagingService : FirebaseMessagingService() {
}
}
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, "")
Log.d(TAG, "speaking notification")
} 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()
}
}
}
}