From 6972f82f2a5f124b2eb1abf7af01c3f17f9ddf84 Mon Sep 17 00:00:00 2001 From: Daniel Shokouhi Date: Tue, 13 Oct 2020 09:38:51 -0700 Subject: [PATCH] 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 --- .../android/notifications/MessagingService.kt | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/app/src/full/java/io/homeassistant/companion/android/notifications/MessagingService.kt b/app/src/full/java/io/homeassistant/companion/android/notifications/MessagingService.kt index 7c0f025be..d4d9b76a8 100644 --- a/app/src/full/java/io/homeassistant/companion/android/notifications/MessagingService.kt +++ b/app/src/full/java/io/homeassistant/companion/android/notifications/MessagingService.kt @@ -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) { 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() + } } } }