mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-16 11:08:52 +01:00
added a couple of files that weren't added in last commit
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
|
||||
*
|
||||
* This file is part of QKSMS.
|
||||
*
|
||||
* QKSMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* QKSMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package dev.octoshrimpy.quik.interactor
|
||||
|
||||
import android.content.res.Resources
|
||||
import com.moez.QKSMS.manager.SpeakManager
|
||||
import dev.octoshrimpy.quik.domain.R
|
||||
import dev.octoshrimpy.quik.extensions.mapNotNull
|
||||
import dev.octoshrimpy.quik.repository.ConversationRepository
|
||||
import dev.octoshrimpy.quik.repository.MessageRepository
|
||||
import io.reactivex.Flowable
|
||||
import io.reactivex.Single
|
||||
import javax.inject.Inject
|
||||
|
||||
class SpeakThreads @Inject constructor(
|
||||
private val conversationRepo: ConversationRepository,
|
||||
private val messageRepo: MessageRepository,
|
||||
) : Interactor<List<Long>>() {
|
||||
|
||||
companion object {
|
||||
private var noMessagesStr = "No messages" // default value
|
||||
|
||||
fun setNoMessagesString(newNoMessagesString: String) {
|
||||
noMessagesStr = newNoMessagesString
|
||||
}
|
||||
}
|
||||
|
||||
override fun buildObservable(threadIds: List<Long>): Flowable<*> {
|
||||
val speakManager = SpeakManager()
|
||||
|
||||
if (threadIds.isEmpty())
|
||||
return Single.just(0)
|
||||
.doOnSubscribe { speakManager.startSpeakSession(noMessagesStr) }
|
||||
.map { speakManager.speak(noMessagesStr) }
|
||||
.doOnTerminate { speakManager.endSpeakSession() }
|
||||
.toFlowable()
|
||||
|
||||
return Flowable.fromIterable(threadIds)
|
||||
.doOnSubscribe { speakManager.startSpeakSession("threads:" + threadIds.sorted().joinToString()) }
|
||||
.mapNotNull { threadId -> conversationRepo.getConversationAndLastSenderContactName(threadId) }
|
||||
.map { conversationAndSender ->
|
||||
if (speakManager.speakConversationLastSms(conversationAndSender))
|
||||
messageRepo.markSeen(conversationAndSender.first!!.id)
|
||||
}
|
||||
.doOnTerminate { speakManager.endSpeakSession() }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,284 @@
|
||||
package com.moez.QKSMS.manager
|
||||
|
||||
import android.content.Context
|
||||
import android.media.AudioAttributes
|
||||
import android.media.AudioFocusRequest
|
||||
import android.media.AudioManager
|
||||
import android.os.Build
|
||||
import android.speech.tts.TextToSpeech
|
||||
import android.speech.tts.UtteranceProgressListener
|
||||
import android.text.format.DateFormat
|
||||
import dev.octoshrimpy.quik.model.Conversation
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Calendar
|
||||
import java.util.Locale
|
||||
import java.util.concurrent.atomic.AtomicReference
|
||||
import kotlin.coroutines.resume
|
||||
import kotlin.coroutines.suspendCoroutine
|
||||
|
||||
|
||||
class SpeakManager() {
|
||||
companion object {
|
||||
// system TextToSpeech engine
|
||||
private var staticTextToSpeech = AtomicReference<TextToSpeech>(null)
|
||||
|
||||
val lock = Any()
|
||||
|
||||
private var context: Context? = null
|
||||
|
||||
// currently speaking sessionId
|
||||
private var currentSessionId: String? = null
|
||||
|
||||
// audio manager items for audio focus
|
||||
private var audioManager: AudioManager? = null
|
||||
private var audioFocusRequest: AudioFocusRequest? = null
|
||||
|
||||
fun setContext(inContext: Context) {
|
||||
synchronized(lock) {
|
||||
if (context !== null)
|
||||
return
|
||||
context = inContext
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var sessionId: String? = null
|
||||
private var sessionStopped = false
|
||||
|
||||
|
||||
private fun getSystemTtsEngine(): TextToSpeech? {
|
||||
// if system TextToSpeech already assigned
|
||||
var tts = staticTextToSpeech.get()
|
||||
if (tts !== null)
|
||||
return tts
|
||||
|
||||
// if context not set then the global engine can not be instantiated
|
||||
if (context === null)
|
||||
return null
|
||||
|
||||
val audioAttributes = AudioAttributes.Builder(). run {
|
||||
setUsage(AudioAttributes.USAGE_ASSISTANT)
|
||||
setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
|
||||
build()
|
||||
}
|
||||
|
||||
var localAudioManager: AudioManager? = null
|
||||
var localAudioFocusRequest: AudioFocusRequest? = null
|
||||
|
||||
synchronized(lock) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
// init audio manager and audio focus request first time through only
|
||||
localAudioManager =
|
||||
context?.getSystemService(Context.AUDIO_SERVICE) as AudioManager
|
||||
if (localAudioManager === null)
|
||||
return null
|
||||
|
||||
localAudioFocusRequest =
|
||||
AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT)
|
||||
.run {
|
||||
setAudioAttributes(audioAttributes)
|
||||
setAcceptsDelayedFocusGain(false)
|
||||
build()
|
||||
}
|
||||
if (localAudioFocusRequest === null)
|
||||
return null
|
||||
}
|
||||
|
||||
// blocking to allow waiting for tts engine response in a synchronous manner
|
||||
runBlocking {
|
||||
// get system tts engine
|
||||
val ttsResult = suspendCoroutine { continuation ->
|
||||
tts = TextToSpeech(context) { status ->
|
||||
if (status == TextToSpeech.ERROR) {
|
||||
tts?.shutdown() // release resources in case of failure to start
|
||||
continuation.resume(false)
|
||||
} else
|
||||
continuation.resume(true)
|
||||
}
|
||||
}
|
||||
|
||||
// if error reported in onInitListener.onInit from system TextToSpeech Engine
|
||||
if (!ttsResult)
|
||||
tts = null
|
||||
}
|
||||
|
||||
// system TextToSpeech not able to be initialised, return failure
|
||||
if (tts == null)
|
||||
return null
|
||||
|
||||
// old-school setting of audio attribute. > Build.VERSION_CODES.O uses audio focus
|
||||
tts.setAudioAttributes(audioAttributes)
|
||||
|
||||
// handlers for start, done and error utterance events
|
||||
tts.setOnUtteranceProgressListener(object :
|
||||
UtteranceProgressListener() {
|
||||
override fun onStart(utteranceId: String) {
|
||||
currentSessionId = utteranceId
|
||||
|
||||
// request audio focus so other audio can be ducked/paused
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
|
||||
audioManager?.requestAudioFocus(audioFocusRequest!!)
|
||||
}
|
||||
|
||||
override fun onDone(utteranceId: String) {
|
||||
currentSessionId = null
|
||||
|
||||
// abandon audio focus
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
|
||||
audioManager?.abandonAudioFocusRequest(audioFocusRequest!!)
|
||||
}
|
||||
|
||||
override fun onError(utteranceId: String) {
|
||||
currentSessionId = null
|
||||
|
||||
// abandon audio focus
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
|
||||
audioManager?.abandonAudioFocusRequest(audioFocusRequest!!)
|
||||
}
|
||||
})
|
||||
|
||||
// assign all these local vars to statics at once so if staticTextToSpeech is set then we know all are set
|
||||
staticTextToSpeech.set(tts)
|
||||
audioManager = localAudioManager
|
||||
audioFocusRequest = localAudioFocusRequest
|
||||
}
|
||||
|
||||
return tts
|
||||
}
|
||||
|
||||
private fun formatDateForSpeech(date: Long): String {
|
||||
fun getFormatter(pattern: String): SimpleDateFormat {
|
||||
var formattedPattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), pattern)
|
||||
|
||||
if (DateFormat.is24HourFormat(context)) {
|
||||
formattedPattern = formattedPattern
|
||||
.replace("h", "HH")
|
||||
.replace("K", "HH")
|
||||
.replace("\\s+a".toRegex(), "")
|
||||
}
|
||||
|
||||
return SimpleDateFormat(formattedPattern, Locale.getDefault())
|
||||
}
|
||||
|
||||
val now = Calendar.getInstance()
|
||||
val then = Calendar.getInstance()
|
||||
val yesterdayCheck = Calendar.getInstance()
|
||||
val lastWeekCheck = Calendar.getInstance()
|
||||
|
||||
then.timeInMillis = date
|
||||
yesterdayCheck.timeInMillis = date
|
||||
yesterdayCheck.add(Calendar.DATE, 1)
|
||||
lastWeekCheck.timeInMillis = date
|
||||
lastWeekCheck.add(Calendar.DATE, 7)
|
||||
|
||||
return when {
|
||||
(then.get(Calendar.DATE) == now.get(Calendar.DATE)) -> StringBuilder() // today
|
||||
(yesterdayCheck.get(Calendar.DATE) == now.get(Calendar.DATE)) -> StringBuilder("yesterday") // yesterday
|
||||
(lastWeekCheck.get(Calendar.DATE) > now.get(Calendar.DATE)) -> StringBuilder("on ").append(getFormatter("EEEE").format(date)) // during last week
|
||||
(then.get(Calendar.YEAR) == now.get(Calendar.YEAR)) -> StringBuilder("on ").append(getFormatter("MMMM d").format(date)) // this year
|
||||
else -> StringBuilder("on").append(getFormatter("MMMM d yyyy").format(date)) // otherwise
|
||||
}.append(" at ").append(getFormatter("h:mm a").format(date)).toString()
|
||||
}
|
||||
|
||||
// toggle is used to stop and not repeat if a sessionId of the same name is currently being read aloud
|
||||
fun startSpeakSession(sessionId: String? = null, toggle: Boolean = true) {
|
||||
// get or init system TextToSpeech engine
|
||||
val tts = getSystemTtsEngine()
|
||||
if (tts === null)
|
||||
return
|
||||
|
||||
this.sessionId = sessionId
|
||||
|
||||
sessionStopped = if ((this.sessionId !== null) && (toggle) && (currentSessionId == sessionId)) {
|
||||
true // do not output any speech in this session
|
||||
} else
|
||||
false
|
||||
|
||||
// stop any current speech immediately
|
||||
stopSpeaking()
|
||||
}
|
||||
|
||||
fun endSpeakSession() {
|
||||
sessionStopped = true
|
||||
}
|
||||
|
||||
fun stopSpeaking() {
|
||||
// get or init system TextToSpeech engine
|
||||
val tts = getSystemTtsEngine()
|
||||
if (tts === null)
|
||||
return
|
||||
|
||||
currentSessionId = null // no utteranceprogresslistener callback so set this explicitly here
|
||||
tts.stop()
|
||||
|
||||
// abandon audio focus
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
|
||||
audioManager?.abandonAudioFocusRequest(audioFocusRequest!!)
|
||||
}
|
||||
|
||||
fun speak(utterance: String) {
|
||||
// if session stopped, return now
|
||||
if (sessionStopped)
|
||||
return
|
||||
|
||||
// get or init system TextToSpeech engine
|
||||
val tts = getSystemTtsEngine()
|
||||
if (tts === null)
|
||||
return
|
||||
|
||||
tts.speak(
|
||||
utterance,
|
||||
TextToSpeech.QUEUE_ADD,
|
||||
null,
|
||||
sessionId
|
||||
)
|
||||
}
|
||||
|
||||
fun speakConversationLastSms(conversationAndSender: Pair<Conversation?, String?>): Boolean {
|
||||
// if session stopped, return now
|
||||
if (sessionStopped)
|
||||
return false
|
||||
|
||||
// get or init system TextToSpeech engine
|
||||
val tts = getSystemTtsEngine()
|
||||
if (tts === null)
|
||||
return false
|
||||
|
||||
// no conversation?, fail
|
||||
val conversation = conversationAndSender.first
|
||||
if (conversation === null)
|
||||
return false
|
||||
|
||||
// no text in conversation's last message?, ditch
|
||||
val conversationLastSms = conversation.lastMessage
|
||||
val conversationLastSmsText = conversationLastSms?.getText()
|
||||
if (conversationLastSmsText === null)
|
||||
return false
|
||||
|
||||
val utterance = StringBuilder()
|
||||
|
||||
// more than 1 recipient
|
||||
if (conversation.recipients.count() > 1)
|
||||
utterance.append("Group SMS ")
|
||||
|
||||
// message sender
|
||||
if (conversationLastSms.isMe())
|
||||
utterance.append("Sent by you to ")
|
||||
else
|
||||
utterance.append("from ")
|
||||
|
||||
utterance.append(conversationAndSender.second ?: "an unknown number ")
|
||||
|
||||
// message date
|
||||
if (conversation.date > 0)
|
||||
utterance.append(formatDateForSpeech(conversationLastSms.date))
|
||||
|
||||
// small delay
|
||||
utterance.append(". ").append(conversationLastSmsText)
|
||||
|
||||
speak(utterance.toString())
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user