Fix deprecations/wrap STT code in if block.

Rebase code with master branch.
Add SDK conditional to check for package manager.
Wrap `onCreate` in an if block that checks if Speech Recognition is even possible.
This commit is contained in:
Inhishonor
2025-02-21 10:30:32 -08:00
parent 45b96f5375
commit 4c32d1280a
@@ -25,8 +25,9 @@ import android.app.DatePickerDialog
import android.app.TimePickerDialog import android.app.TimePickerDialog
import android.content.ContentValues import android.content.ContentValues
import android.content.Intent import android.content.Intent
import android.content.res.ColorStateList
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.content.res.ColorStateList
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
@@ -218,6 +219,24 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.autoDisposable(scope()) .autoDisposable(scope())
} }
private fun isSpeechRecognitionAvailable(): Boolean {
// Check if there is a speech recognition service available
val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
val packageManager = this.packageManager
val activities: List<ResolveInfo>
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
this.packageManager
activities = packageManager.queryIntentActivities(
intent,
PackageManager.ResolveInfoFlags.of(PackageManager.MATCH_DEFAULT_ONLY.toLong())
)
} else {
// else, pre-tiramisu versions
val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
activities = packageManager.queryIntentActivities(intent, 0)
}
return activities.isNotEmpty()
}
private val speechResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> private val speechResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode != Activity.RESULT_OK) if (result.resultCode != Activity.RESULT_OK)
@@ -245,17 +264,11 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
setContentView(R.layout.compose_activity) setContentView(R.layout.compose_activity)
showBackButton(true) showBackButton(true)
viewModel.bindView(this) viewModel.bindView(this)
val isSpeechRecognitionAvailable = isSpeechRecognitionAvailable()
// Check if there is a speech recognition service available if (isSpeechRecognitionAvailable) {
val packageManager = packageManager // Speech recognition is available, enable related features
val speechRecognizerIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH) // For example, you might want to show a button to start speech recognition
val resolveInfoList = packageManager.queryIntentActivities(speechRecognizerIntent, PackageManager.MATCH_DEFAULT_ONLY) // startSpeechRecognitionButton.visibility = View.VISIBLE
if (resolveInfoList.isEmpty()) {
// No STT provider found, show a Toast message
Toast.makeText(this, getString(R.string.stt_toast_no_provider), Toast.LENGTH_SHORT).show()
}
contentView.layoutTransition = LayoutTransition().apply { contentView.layoutTransition = LayoutTransition().apply {
disableTransitionType(LayoutTransition.CHANGING) disableTransitionType(LayoutTransition.CHANGING)
@@ -269,32 +282,12 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
messageAdapter.autoScrollToStart(messageList) messageAdapter.autoScrollToStart(messageList)
messageAdapter.emptyView = messagesEmpty messageAdapter.emptyView = messagesEmpty
// drag drop handlers for speech-to-text icon messageList.setHasFixedSize(true)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { messageList.adapter = messageAdapter
speechToTextIcon.setOnLongClickListener {
it.startDragAndDrop(null, View.DragShadowBuilder(speechToTextFrame), null, 0)
speechToTextFrame.isVisible = false
contentView.setOnDragListener { _, event -> messageAttachments.adapter = composeAttachmentAdapter
when (event.action) {
ACTION_DROP -> {
speechToTextFrame.x = (event.x - (speechToTextFrame.width / 2))
speechToTextFrame.y = (event.y - (speechToTextFrame.height / 2))
// get offset from root view as a percentage of root view for saving message.supportsInputContent = true
prefs.showSttOffsetX.set((speechToTextFrame.x - contentView.x) / contentView.width)
prefs.showSttOffsetY.set((speechToTextFrame.y - contentView.y) / contentView.height)
}
ACTION_DRAG_ENDED, ACTION_DRAG_EXITED -> {
speechToTextFrame.isVisible = true
}
// Add other cases if needed
}
true // Return true to indicate the event was handled
}
true // Return true to indicate the long click was handled
}
}
theme theme
.doOnNext { loading.setTint(it.theme) } .doOnNext { loading.setTint(it.theme) }
@@ -322,6 +315,32 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
.autoDisposable(scope()) .autoDisposable(scope())
.subscribe { registerForContextMenu(it) } .subscribe { registerForContextMenu(it) }
// drag drop handlers for speech-to-text icon
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
speechToTextIcon.setOnLongClickListener {
it.startDragAndDrop(null, View.DragShadowBuilder(speechToTextFrame), null, 0)
speechToTextFrame.isVisible = false
contentView.setOnDragListener { _, event ->
when (event.action) {
ACTION_DROP -> {
speechToTextFrame.x = (event.x - (speechToTextFrame.width / 2))
speechToTextFrame.y =(event.y - (speechToTextFrame.height / 2))
// get offset from root view as a percentage of root view for saving
prefs.showSttOffsetX.set((speechToTextFrame.x - contentView.x) / contentView.width)
prefs.showSttOffsetY.set((speechToTextFrame.y - contentView.y) / contentView.height)
}
ACTION_DRAG_ENDED, ACTION_DRAG_EXITED -> {
speechToTextFrame.isVisible = true
}
}
true
}
true
}
}
// start/stop audio message recording // start/stop audio message recording
audioMsgRecord.setOnClickListener { audioMsgRecord.setOnClickListener {
recordAudioRecord.onNext(audioMsgRecord.getState()) recordAudioRecord.onNext(audioMsgRecord.getState())
@@ -413,6 +432,9 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
) )
window.callback = ComposeWindowCallback(window.callback, this) window.callback = ComposeWindowCallback(window.callback, this)
} else {
Toast.makeText(this, getString(R.string.stt_toast_no_provider), Toast.LENGTH_SHORT).show()
}
} }
override fun onStart() { override fun onStart() {