new setting to show/hide new stt button - defaults to 'show' to enable discovery by user. move compose stt feature from double-tap compose entry box to new button on compose activity

This commit is contained in:
gavine99
2025-02-12 14:35:17 +11:00
parent 7003306666
commit 51a0576ce0
11 changed files with 112 additions and 59 deletions
@@ -108,6 +108,9 @@ import kotlinx.android.synthetic.main.compose_activity.sendAsGroupBackground
import kotlinx.android.synthetic.main.compose_activity.sendAsGroupSwitch
import kotlinx.android.synthetic.main.compose_activity.sim
import kotlinx.android.synthetic.main.compose_activity.simIndex
import kotlinx.android.synthetic.main.compose_activity.speechToTextFrame
import kotlinx.android.synthetic.main.compose_activity.speechToTextIcon
import kotlinx.android.synthetic.main.compose_activity.speechToTextIconBorder
import kotlinx.android.synthetic.main.compose_activity.toolbarSubtitle
import kotlinx.android.synthetic.main.compose_activity.toolbarTitle
import kotlinx.android.synthetic.main.main_activity.toolbar
@@ -160,31 +163,12 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
override val backPressedIntent: Subject<Unit> = PublishSubject.create()
override val confirmDeleteIntent: Subject<List<Long>> = PublishSubject.create()
override val messageLinkAskIntent: Subject<Uri> by lazy { messageAdapter.messageLinkClicks }
override val speechRecogniserIntent by lazy { speechToTextIconBorder.clicks() }
private val viewModel by lazy { ViewModelProviders.of(this, viewModelFactory)[ComposeViewModel::class.java] }
private var cameraDestination: Uri? = null
private val speechResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode != Activity.RESULT_OK)
return@registerForActivityResult
// check returned results are good
val match = result.data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
if ((match === null) || (match.size < 1) || (match[0].isNullOrEmpty()))
return@registerForActivityResult
// get the edit text view
val message = findViewById<QkEditText>(R.id.message)
if (message === null)
return@registerForActivityResult
// populate message box with data returned by STT, set cursor to end, and focus
message.append(match[0])
message.setSelection(message.text.length)
message.requestFocus()
}
override fun onCreate(savedInstanceState: Bundle?) {
AndroidInjection.inject(this)
super.onCreate(savedInstanceState)
@@ -215,46 +199,13 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
.doOnNext { loading.setTint(it.theme) }
.doOnNext { attach.setBackgroundTint(it.theme) }
.doOnNext { attach.setTint(it.textPrimary) }
.doOnNext { speechToTextIconBorder.setBackgroundTint(it.theme) }
.doOnNext { speechToTextIcon.setBackgroundTint(it.textPrimary) }
.doOnNext { speechToTextIcon.setTint(it.theme) }
.doOnNext { messageAdapter.theme = it }
.autoDisposable(scope())
.subscribe()
message.setOnTouchListener(object : OnTouchListener {
private val gestureDetector =
GestureDetector(this@ComposeActivity, object : SimpleOnGestureListener() {
private var lastUpEvent: MotionEvent? = null
override fun onDoubleTap(e: MotionEvent): Boolean {
val speechRecognizerIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
.putExtra(
RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
)
// include if want a custom message that the STT can (optionally) display .putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak your message")
speechResultLauncher.launch(speechRecognizerIntent)
return true
}
override fun onSingleTapConfirmed(e: MotionEvent): Boolean {
if (lastUpEvent !== null) {
message.onTouchEvent(lastUpEvent)
lastUpEvent?.recycle()
lastUpEvent = null
}
return true
}
override fun onSingleTapUp(e: MotionEvent): Boolean {
lastUpEvent = MotionEvent.obtain(e)
return true // don't show soft keyboard on this event
}
})
override fun onTouch(v: View, e: MotionEvent): Boolean {
return gestureDetector.onTouchEvent(e)
}
})
// context menu registration for message parts
messagePartContextMenuRegistrar
.mapNotNull { it }
@@ -375,6 +326,8 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
// if scheduling mode is set, show schedule dialog
if (state.scheduling)
scheduleAction.onNext(true)
speechToTextFrame.isVisible = (prefs.showStt.get())
}
override fun clearSelection() = messageAdapter.clearSelection()
@@ -460,6 +413,18 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
startActivityForResult(intent, ComposeView.SelectContactRequestCode)
}
override fun startSpeechRecognition() {
startActivityForResult(
Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH).putExtra(
RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
),
// include below if want a custom message that the STT can (optionally) display
// .putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak your message")
ComposeView.SpeechRecognitionRequestCode
)
}
override fun themeChanged() {
messageList.scrapViews()
}
@@ -576,6 +541,21 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
data?.data?.let(contactSelectedIntent::onNext)
}
ComposeView.SpeechRecognitionRequestCode -> {
// check returned results are good
val match = data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
if ((match !== null) && (match.size > 0) && (!match[0].isNullOrEmpty())) {
// get the edit text view
val messageEditBox = findViewById<QkEditText>(R.id.message)
if (messageEditBox !== null) {
// populate message box with data returned by STT, set cursor to end, and focus
messageEditBox.append(match[0])
messageEditBox.setSelection(messageEditBox.text.length)
messageEditBox.requestFocus()
}
}
}
else -> super.onActivityResult(requestCode, resultCode, data)
}
}
@@ -34,8 +34,9 @@ interface ComposeView : QkView<ComposeState> {
companion object {
const val SelectContactRequestCode = 0
const val TakePhotoRequestCode = 1
const val AttachAFileRequestCode = 4
const val AttachContactRequestCode = 3
const val AttachAFileRequestCode = 4
const val SpeechRecognitionRequestCode = 5
const val CameraDestinationKey = "camera_destination"
}
@@ -73,6 +74,7 @@ interface ComposeView : QkView<ComposeState> {
val backPressedIntent: Observable<Unit>
val confirmDeleteIntent: Observable<List<Long>>
val messageLinkAskIntent: Observable<Uri>
val speechRecogniserIntent: Observable<*>
fun clearSelection()
fun toggleSelectAll()
@@ -93,5 +95,6 @@ interface ComposeView : QkView<ComposeState> {
fun scrollToMessage(id: Long)
fun showQksmsPlusSnackbar(@StringRes message: Int)
fun showDeleteDialog( messages: List<Long>)
fun startSpeechRecognition()
}
@@ -763,6 +763,11 @@ class ComposeViewModel @Inject constructor(
.autoDisposable(view.scope())
.subscribe()
// speech recognition button clicked
view.speechRecogniserIntent
.autoDisposable(view.scope())
.subscribe { view.startSpeechRecognition() }
// Send a message when the send button is clicked, and disable editing mode if it's enabled
view.sendIntent
.withLatestFrom(view.textChangedIntent) { _, body -> body.toString() }
@@ -184,6 +184,8 @@ class SettingsController : QkController<SettingsView, SettingsState, SettingsPre
systemFont.checkbox.isChecked = state.systemFontEnabled
showStt.checkbox.isChecked = state.showStt
unicode.checkbox.isChecked = state.stripUnicodeEnabled
mobileOnly.checkbox.isChecked = state.mobileOnly
@@ -116,7 +116,10 @@ class SettingsPresenter @Inject constructor(
.subscribe { autoColor -> newState { copy(autoColor = autoColor) } }
disposables += prefs.systemFont.asObservable()
.subscribe { enabled -> newState { copy(systemFontEnabled = enabled) } }
.subscribe { enabled -> newState { copy(systemFontEnabled = enabled) } }
disposables += prefs.showStt.asObservable()
.subscribe { enabled -> newState { copy(showStt = enabled) } }
disposables += prefs.unicode.asObservable()
.subscribe { enabled -> newState { copy(stripUnicodeEnabled = enabled) } }
@@ -207,6 +210,8 @@ class SettingsPresenter @Inject constructor(
R.id.systemFont -> prefs.systemFont.set(!prefs.systemFont.get())
R.id.showStt -> prefs.showStt.set(!prefs.showStt.get())
R.id.unicode -> prefs.unicode.set(!prefs.unicode.get())
R.id.mobileOnly -> prefs.mobileOnly.set(!prefs.mobileOnly.get())
@@ -39,6 +39,7 @@ data class SettingsState(
val textSizeSummary: String = "",
val textSizeId: Int = Preferences.TEXT_SIZE_NORMAL,
val systemFontEnabled: Boolean = false,
val showStt: Boolean = true,
val splitSmsEnabled: Boolean = false,
val stripUnicodeEnabled: Boolean = false,
val mobileOnly: Boolean = false,