diff --git a/data/src/main/AndroidManifest.xml b/data/src/main/AndroidManifest.xml
index 6cf78b81..b7fb36b8 100644
--- a/data/src/main/AndroidManifest.xml
+++ b/data/src/main/AndroidManifest.xml
@@ -21,5 +21,7 @@
+
+
diff --git a/data/src/main/java/com/moez/QKSMS/manager/BluetoothMicManager.kt b/data/src/main/java/com/moez/QKSMS/manager/BluetoothMicManager.kt
new file mode 100644
index 00000000..8ddeec27
--- /dev/null
+++ b/data/src/main/java/com/moez/QKSMS/manager/BluetoothMicManager.kt
@@ -0,0 +1,145 @@
+/*
+ * Copyright (C) 2025
+ *
+ * This file is part of QUIK.
+ *
+ * QUIK 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.
+ *
+ * QUIK 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 QUIK. If not, see .
+ */
+package com.moez.QKSMS.manager
+
+import android.content.BroadcastReceiver
+import android.content.Context
+import android.content.Intent
+import android.content.IntentFilter
+import android.media.AudioDeviceInfo
+import android.media.AudioManager
+import android.media.AudioManager.GET_DEVICES_INPUTS
+
+
+// this class is, by design, as simplistic it can be to support easy and fast connection
+// for recording from first bluetooth mic found, if any
+
+class BluetoothMicManager(
+ private val context: Context,
+ private val callbacks: Callbacks
+)
+ : BroadcastReceiver() {
+ interface Callbacks {
+ fun onNoDeviceFound()
+ fun onDeviceFound(device: AudioDeviceInfo?)
+ fun onConnecting(device: AudioDeviceInfo?)
+ fun onConnected(device: AudioDeviceInfo?)
+ fun onDisconnected(device: AudioDeviceInfo?)
+ }
+
+ init {
+ // register for bluetooth sco broadcast intents
+ context.registerReceiver(this, IntentFilter().apply {
+ addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED)
+ })
+ }
+
+ enum class StartBluetoothDevice {
+ NoDevice,
+ Connecting,
+ Connected
+ }
+
+ var device: AudioDeviceInfo? = null
+ private set
+
+ private var hadFirstStickyIntent: Boolean = false
+ private var closed: Boolean = false
+
+ fun close() {
+ if (closed)
+ return
+ closed = true
+
+ device = null
+ (context.getSystemService(Context.AUDIO_SERVICE) as AudioManager).stopBluetoothSco()
+ context.unregisterReceiver(this)
+ }
+
+ // note: caller may get connected() callback before this method returns if this method is
+ // called from a non-UI thread and the UI thread processes a sticky sco_audio_state_connected
+ fun startBluetoothDevice() {
+ device = null
+ val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
+
+ // get all audio input devices
+ val allAudioInputDevices = audioManager.getDevices(GET_DEVICES_INPUTS)
+
+// @TODO: below code can be used for BLE headset devices if and when one can be tested
+// : also need to register for BLE headset intent broadcasts and other things later
+// // firstly (highest priority), check for a connected BLE headset input device
+// for (btInputDevice in allAudioInputDevices) {
+// if (btInputDevice.type == AudioDeviceInfo.TYPE_BLE_HEADSET) {
+// device = btInputDevice
+// break
+// }
+// }
+
+ // if no ble device found, check if platform supports non-phone-call sco connections
+ if ((device == null) &&
+ (audioManager.isBluetoothScoAvailableOffCall)) {
+ // find first SCO input device
+ for (btInputDevice in allAudioInputDevices) {
+ if (btInputDevice.type == AudioDeviceInfo.TYPE_BLUETOOTH_SCO) {
+ device = btInputDevice
+ break
+ }
+ }
+ }
+
+ // if no usable device found, callback to caller and return
+ if (device == null) {
+ callbacks.onNoDeviceFound()
+ return
+ }
+
+ // callback that a sco capable bluetooth device was found
+ callbacks.onDeviceFound(device)
+
+ // start sco connection on bluetooth device (even if already started by other app)
+ try { // see https://stackoverflow.com/a/26929741 for try-catch reason
+ audioManager.startBluetoothSco()
+ }
+ catch (e: Exception) { /* nothing */ }
+
+ // if sco device is already on (maybe by another app?) callback now
+ if (audioManager.isBluetoothScoOn)
+ callbacks.onConnected(device)
+ }
+
+ override fun onReceive(context: Context, intent: Intent) {
+ if (!hadFirstStickyIntent) {
+ hadFirstStickyIntent = true
+ return
+ }
+
+ // if sco connection state has changed
+ val action = intent.action ?: return
+ if (action == AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED) {
+ when (intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1)) {
+ AudioManager.SCO_AUDIO_STATE_CONNECTED -> callbacks.onConnected(device)
+ AudioManager.SCO_AUDIO_STATE_CONNECTING -> callbacks.onConnecting(device)
+ AudioManager.SCO_AUDIO_STATE_DISCONNECTED -> {
+ close()
+ callbacks.onDisconnected(device)
+ }
+ }
+ }
+ }
+}
diff --git a/data/src/main/java/com/moez/QKSMS/manager/MediaRecorderManager.kt b/data/src/main/java/com/moez/QKSMS/manager/MediaRecorderManager.kt
index 157f325b..a33e8b27 100644
--- a/data/src/main/java/com/moez/QKSMS/manager/MediaRecorderManager.kt
+++ b/data/src/main/java/com/moez/QKSMS/manager/MediaRecorderManager.kt
@@ -19,8 +19,10 @@
package com.moez.QKSMS.manager
import android.content.Context
+import android.media.AudioDeviceInfo
import android.media.MediaRecorder
import android.net.Uri
+import android.os.Build
import androidx.core.net.toUri
import java.io.File
import java.util.UUID
@@ -59,7 +61,7 @@ object MediaRecorderManager : MediaRecorder() {
}
}
- fun startRecording(context: Context): Uri {
+ fun startRecording(context: Context, preferredAudioDevice: AudioDeviceInfo? = null): Uri {
return try {
val file = File(
context.cacheDir,
@@ -73,6 +75,11 @@ object MediaRecorderManager : MediaRecorder() {
setAudioSource(AudioSource.MIC)
setOutputFormat(OutputFormat.THREE_GPP)
setAudioEncoder(AudioEncoder.AMR_WB)
+
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
+ preferredDevice = preferredAudioDevice
+ }
+
recordingState = RecordingState.DataSourceConfigured
setOutputFile(file.path)
diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeActivity.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeActivity.kt
index f3172ffb..f72df1c5 100644
--- a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeActivity.kt
+++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeActivity.kt
@@ -27,6 +27,7 @@ import android.content.ContentValues
import android.content.Intent
import android.content.res.ColorStateList
import android.net.Uri
+import android.os.Build
import android.os.Bundle
import android.os.SystemClock
import android.provider.ContactsContract
@@ -41,7 +42,6 @@ import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.Toast
-import android.view.View.OnTouchListener
import android.widget.SeekBar
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
@@ -116,6 +116,7 @@ import kotlinx.android.synthetic.main.compose_activity.noValidRecipients
import kotlinx.android.synthetic.main.compose_activity.messageAttachments
import kotlinx.android.synthetic.main.compose_activity.attachAnAudioMessageIcon
import kotlinx.android.synthetic.main.compose_activity.attachAnAudioMessageLabel
+import kotlinx.android.synthetic.main.compose_activity.audioMsgBluetooth
import kotlinx.android.synthetic.main.compose_activity.audioMsgDuration
import kotlinx.android.synthetic.main.compose_activity.recordAudioMsg
import kotlinx.android.synthetic.main.compose_activity.schedule
@@ -187,6 +188,7 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
override val messageLinkAskIntent: Subject by lazy { messageAdapter.messageLinkClicks }
override val speechRecogniserIntent by lazy { speechToTextIcon.clicks() }
override val shadeIntent by lazy { shadeBackground.clicks() }
+ override val recordAudioStartStopRecording: Subject = PublishSubject.create()
override val recordAnAudioMessage by lazy {
Observable.merge(recordAudioMsg.clicks(),
attachAnAudioMessageIcon.clicks(),
@@ -197,8 +199,9 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
override val recordAudioPlayerPlayPause: Subject = PublishSubject.create()
override val recordAudioPlayerConfigUI: Subject = PublishSubject.create()
override val recordAudioPlayerVisible: Subject = PublishSubject.create()
+ override val recordAudioMsgRecordVisible: Subject = PublishSubject.create()
override val recordAudioChronometer: Subject = PublishSubject.create()
- override val recordAudioStartStop: Subject = PublishSubject.create()
+ override val recordAudioRecord: Subject = PublishSubject.create()
private var seekBarUpdater: Disposable? = null
@@ -286,32 +289,34 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
.subscribe { registerForContextMenu(it) }
// drag drop handlers for speech-to-text icon
- speechToTextIcon.setOnLongClickListener {
- it.startDragAndDrop(null, View.DragShadowBuilder(speechToTextFrame), null, 0)
- speechToTextFrame.isVisible = false
+ 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))
+ 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
+ // 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
}
- true
}
// start/stop audio message recording
audioMsgRecord.setOnClickListener {
- recordAudioStartStop.onNext(audioMsgRecord.getState())
+ recordAudioRecord.onNext(audioMsgRecord.getState())
}
recordAudioChronometer
@@ -334,6 +339,16 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
)
}
+ recordAudioMsgRecordVisible
+ .subscribeOn(AndroidSchedulers.mainThread())
+ .distinctUntilChanged()
+ .autoDisposable(scope())
+ .subscribe {
+ audioMsgRecord.isVisible = it
+ audioMsgDuration.isVisible = it // chronometer follows record button visibility
+ audioMsgBluetooth.isVisible = !it
+ }
+
recordAudioPlayerVisible
.subscribeOn(AndroidSchedulers.mainThread())
.distinctUntilChanged()
diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeView.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeView.kt
index 13a819fb..6fed5e39 100644
--- a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeView.kt
+++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeView.kt
@@ -78,13 +78,15 @@ interface ComposeView : QkView {
val messageLinkAskIntent: Observable
val speechRecogniserIntent: Observable<*>
val shadeIntent: Observable
+ val recordAudioStartStopRecording: Subject
val recordAnAudioMessage: Observable
val recordAudioAbort: Observable
val recordAudioAttach: Observable
val recordAudioPlayerPlayPause: Observable
val recordAudioPlayerConfigUI: Subject
val recordAudioPlayerVisible: Subject
- val recordAudioStartStop: Subject
+ val recordAudioMsgRecordVisible: Subject
+ val recordAudioRecord: Subject
val recordAudioChronometer: Subject
fun clearSelection()
diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeViewModel.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeViewModel.kt
index ec0bcb31..ca5ef06f 100644
--- a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeViewModel.kt
+++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeViewModel.kt
@@ -21,6 +21,7 @@ package dev.octoshrimpy.quik.feature.compose
import android.annotation.SuppressLint
import android.content.Context
import android.media.AudioAttributes
+import android.media.AudioDeviceInfo
import android.net.Uri
import android.os.Vibrator
import android.telephony.SmsMessage
@@ -30,6 +31,7 @@ import androidx.core.net.toUri
import com.moez.QKSMS.common.QkMediaPlayer
import com.moez.QKSMS.contentproviders.MmsPartProvider
import com.moez.QKSMS.manager.MediaRecorderManager
+import com.moez.QKSMS.manager.BluetoothMicManager
import dev.octoshrimpy.quik.R
import dev.octoshrimpy.quik.common.Navigator
import dev.octoshrimpy.quik.common.base.QkViewModel
@@ -81,7 +83,6 @@ import io.reactivex.subjects.Subject
import timber.log.Timber
import java.io.File
import java.util.*
-import java.util.concurrent.TimeUnit
import javax.inject.Inject
import javax.inject.Named
@@ -130,6 +131,8 @@ class ComposeViewModel @Inject constructor(
private var shouldShowContacts = threadId == 0L && addresses.isEmpty()
+ private var bluetoothMicManager: BluetoothMicManager? = null
+
init {
// set shared attachments into state, if any
newState { copy(attachments = sharedAttachments) }
@@ -798,7 +801,7 @@ class ComposeViewModel @Inject constructor(
.autoDisposable(view.scope())
.subscribe { newState { copy(attaching = false) } }
- // starting or stopping (change state) of audio message recording
+ // starting or stopping (change state) of audio message ui
state
.distinctUntilChanged { state -> state.audioMsgRecording }
.skip(1) // skip initial value
@@ -811,24 +814,68 @@ class ComposeViewModel @Inject constructor(
if (!it.audioMsgRecording) {
// ensure recording stopped and delete any recording file
safeDeleteLocalFile(MediaRecorderManager.stopRecording())
- view.recordAudioChronometer.onNext(false) // stop chronometer
+ view.recordAudioStartStopRecording.onNext(false)
}
}
- // start recording an audio message
+ // starting or stopping the recording of audio
+ view.recordAudioStartStopRecording
+ .autoDisposable(view.scope())
+ .subscribe {
+ // if start recording
+ if (it == true) {
+ view.recordAudioPlayerVisible.onNext(false) // hide audio player
+
+ // check have permissions to record audio
+ if (permissionManager.hasRecordAudio().also {
+ if (!it) view.requestRecordAudioPermission()
+ }) {
+ // create bluetooth mic device manager
+ bluetoothMicManager?.close()
+ bluetoothMicManager = BluetoothMicManager(
+ context,
+ object : BluetoothMicManager.Callbacks {
+ override fun onNoDeviceFound() {
+ // no bluetooth sco device found, use built-in mic
+ this.onConnected(null)
+ }
+ override fun onDeviceFound(device: AudioDeviceInfo?) {
+ // show bluetooth placeholder until bluetooth connected
+ view.recordAudioMsgRecordVisible.onNext(false)
+ }
+ override fun onConnecting(device: AudioDeviceInfo?) { /* nothing */ }
+ override fun onConnected(device: AudioDeviceInfo?) {
+ // show record button and chronometer, hide bluetooth placeholder
+ view.recordAudioMsgRecordVisible.onNext(true)
+ view.recordAudioChronometer.onNext(true) // start chronometer
+ MediaRecorderManager.startRecording(context, device)
+ }
+ override fun onDisconnected(device: AudioDeviceInfo?) {
+ // if bluetooth disconnects, stop recording
+ if (device != null) {
+ view.recordAudioRecord.onNext(
+ MicInputCloudView.ViewState.PAUSED_STATE
+ )
+ }
+ }
+ }
+ )
+ bluetoothMicManager?.startBluetoothDevice()
+ }
+ } else {
+ // stop recording
+ bluetoothMicManager?.close()
+ view.recordAudioChronometer.onNext(false) // stop chronometer
+ MediaRecorderManager.stopRecording()
+ }
+ }
+
+ // record an audio message menu item or main mic icon
view.recordAnAudioMessage
.autoDisposable(view.scope())
.subscribe {
- view.recordAudioPlayerVisible.onNext(false) // hide audio player
-
- // check have permissions to record audio
- if (permissionManager.hasRecordAudio().also {
- if (!it) view.requestRecordAudioPermission()
- }) {
- newState { copy( attaching = false, audioMsgRecording = true) }
- view.recordAudioChronometer.onNext(true) // start chronometer
- MediaRecorderManager.startRecording(context)
- }
+ view.recordAudioStartStopRecording.onNext(true) // start recording
+ newState { copy( attaching = false, audioMsgRecording = true) }
}
// abort recording audio message button
@@ -838,19 +885,15 @@ class ComposeViewModel @Inject constructor(
.subscribe { newState { copy( audioMsgRecording = false) } }
// main record/stop recording audio message button
- view.recordAudioStartStop
+ view.recordAudioRecord
.autoDisposable(view.scope())
.subscribe {
if (it == MicInputCloudView.ViewState.PAUSED_STATE) {
- view.recordAudioChronometer.onNext(false) // stop chronometer
- MediaRecorderManager.stopRecording()
+ view.recordAudioStartStopRecording.onNext(false) // stop recording
view.recordAudioPlayerVisible.onNext(true) // show audio player
} else { // state = start recording
- // delete old recording file
- safeDeleteLocalFile(MediaRecorderManager.uri)
- view.recordAudioPlayerVisible.onNext(false) // hide audio player
- view.recordAudioChronometer.onNext(true) // start chronometer
- MediaRecorderManager.startRecording(context)
+ safeDeleteLocalFile(MediaRecorderManager.uri) // delete old recording file
+ view.recordAudioStartStopRecording.onNext(true) // start new recording
}
}
@@ -861,7 +904,8 @@ class ComposeViewModel @Inject constructor(
MediaRecorderManager.stopRecording()
try {
- // create new filename for recorded file
+ // create new filename for recorded file (so cache clean doesn't accidentally
+ // delete the attachment file)
val newFile = File(
context.cacheDir,
"${AUDIO_FILE_PREFIX}${UUID.randomUUID()}${MediaRecorderManager.AUDIO_FILE_SUFFIX}"
diff --git a/presentation/src/main/res/layout/compose_activity.xml b/presentation/src/main/res/layout/compose_activity.xml
index 5bf8c05a..34ae0283 100644
--- a/presentation/src/main/res/layout/compose_activity.xml
+++ b/presentation/src/main/res/layout/compose_activity.xml
@@ -755,16 +755,28 @@
app:srcCompat="@drawable/ic_delete_white_24dp"
app:tint="?android:attr/textColorPrimary" />
-
+ android:layout_weight="1">
+
+
+
+
+