Skip to content

Commit

Permalink
Service/TTS code improvements
Browse files Browse the repository at this point in the history
Should fix shake not stopping speech on some devices.
  • Loading branch information
pilot51 committed Jan 31, 2025
1 parent b4c8a5c commit e2cd587
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/com/pilot51/voicenotify/IgnoreReason.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ enum class IgnoreReason(
TTS_FAILED(R.string.reason_tts_failed),
TTS_RESTARTED(R.string.reason_tts_restarted),
TTS_INTERRUPTED(R.string.reason_tts_interrupted),
TTS_ERROR(R.string.reason_tts_error),
TTS_LENGTH_LIMIT(R.string.reason_tts_length_limit);

/** @return The user-visible string for this ignore reason. */
Expand Down
48 changes: 30 additions & 18 deletions app/src/main/java/com/pilot51/voicenotify/Service.kt
Original file line number Diff line number Diff line change
Expand Up @@ -160,22 +160,24 @@ class Service : NotificationListenerService() {
Log.d(TAG, "TTS starting utterance ID $utteranceId")
speakingUtteranceId = utteranceId.toLong()
}

override fun onStop(utteranceId: String, interrupted: Boolean) {
Log.d(TAG, "Stopped utterance ID $utteranceId - interrupted? $interrupted")
if (interrupted) {
ttsQueueMutex.launchWithLock(ioScope) {
val info = ttsQueue[utteranceId.toLong()]
if (info != null) {
speakingUtteranceId = null
ioScope.launch {
ttsQueueMutex.withLock {
ttsQueue.remove(utteranceId.toLong())
}?.let { info ->
if (interrupted) {
info.isInterrupted = true
if (info.ignoreReasons.isEmpty()) {
info.addIgnoreReasons(IgnoreReason.TTS_INTERRUPTED)
}
NotifyList.updateInfo(info)
}
ttsQueue.clear()
}
if (ttsQueue.isEmpty()) onDoneSpeaking()
}
onDone(utteranceId)
}

override fun onDone(utteranceId: String) {
Expand All @@ -192,7 +194,18 @@ class Service : NotificationListenerService() {
@Deprecated("Deprecated in Java")
override fun onError(utteranceId: String) {
Log.e(TAG, "Error on utterance ID $utteranceId")
val interrupted = speakingUtteranceId == utteranceId.toLong()
speakingUtteranceId = null
ioScope.launch {
ttsQueueMutex.withLock {
ttsQueue.remove(utteranceId.toLong())
}?.let { info ->
info.isInterrupted = interrupted
info.addIgnoreReasons(IgnoreReason.TTS_ERROR)
NotifyList.updateInfo(info)
}
if (ttsQueue.isEmpty()) onDoneSpeaking()
}
}
})
})
Expand Down Expand Up @@ -509,17 +522,15 @@ class Service : NotificationListenerService() {
registerReceiver(stateReceiver, filter)
shake.onShake = {
Log.i(TAG, "TTS silenced by shake")
ttsQueueMutex.launchWithLock(ioScope) {
for (info in ttsQueue.values) {
info.addIgnoreReasons(IgnoreReason.SHAKE)
if (info == ttsQueue[speakingUtteranceId]) {
info.isInterrupted = true
ioScope.launch {
ttsQueueMutex.withLock {
ttsQueue.values.forEach { info ->
info.addIgnoreReasons(IgnoreReason.SHAKE)
NotifyList.updateInfo(info)
}
NotifyList.updateInfo(info)
}
ttsQueue.clear()
tts?.stop()
}
onDoneSpeaking()
}
setInitialized(true)
}
Expand Down Expand Up @@ -624,9 +635,10 @@ class Service : NotificationListenerService() {
}

private fun processIgnoreForQueue() {
tts?.run {
ttsQueueMutex.launchWithLock(ioScope) {
for (info in ttsQueue.values) {
if (tts == null) return
ioScope.launch {
ttsQueueMutex.withLock {
ttsQueue.values.forEach { info ->
val ignoreReasons = ignore(info.settings)
if (ignoreReasons.isNotEmpty()) {
Log.i(TAG, "Notification from ${info.app?.label} silenced/ignored" +
Expand All @@ -635,7 +647,7 @@ class Service : NotificationListenerService() {
}
}
}
stop()
tts?.stop()
}
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -240,5 +240,6 @@
<string name="reason_tts_failed">TTS failed. Restart and retry attempted.\nThis text should be yellow if retry successful or red if not.\nPlease try restarting the Voice Notify service if notifications fail to be spoken.</string>
<string name="reason_tts_restarted">TTS restarted while message in queue or speaking. Re-queued.</string>
<string name="reason_tts_interrupted">TTS interrupted for unknown reason</string>
<string name="reason_tts_error">TTS processing error</string>
<string name="reason_tts_length_limit">Message exceeded TTS length limit</string>
</resources>

0 comments on commit e2cd587

Please sign in to comment.