Merge pull request #290 from gavine99/bluetooth-input-to-record-audio-msg

use bluetooth sco source device, if available, to record audio
This commit is contained in:
octoshrimpy
2025-02-18 08:35:17 -05:00
committed by GitHub
7 changed files with 278 additions and 51 deletions
+2
View File
@@ -21,5 +21,7 @@
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.BLUETOOTH" />
</manifest>
@@ -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 <http://www.gnu.org/licenses/>.
*/
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)
}
}
}
}
}
@@ -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)
@@ -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<Uri> by lazy { messageAdapter.messageLinkClicks }
override val speechRecogniserIntent by lazy { speechToTextIcon.clicks() }
override val shadeIntent by lazy { shadeBackground.clicks() }
override val recordAudioStartStopRecording: Subject<Boolean> = 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<QkMediaPlayer.PlayingState> = PublishSubject.create()
override val recordAudioPlayerConfigUI: Subject<QkMediaPlayer.PlayingState> = PublishSubject.create()
override val recordAudioPlayerVisible: Subject<Boolean> = PublishSubject.create()
override val recordAudioMsgRecordVisible: Subject<Boolean> = PublishSubject.create()
override val recordAudioChronometer: Subject<Boolean> = PublishSubject.create()
override val recordAudioStartStop: Subject<MicInputCloudView.ViewState> = PublishSubject.create()
override val recordAudioRecord: Subject<MicInputCloudView.ViewState> = 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()
@@ -78,13 +78,15 @@ interface ComposeView : QkView<ComposeState> {
val messageLinkAskIntent: Observable<Uri>
val speechRecogniserIntent: Observable<*>
val shadeIntent: Observable<Unit>
val recordAudioStartStopRecording: Subject<Boolean>
val recordAnAudioMessage: Observable<Unit>
val recordAudioAbort: Observable<Unit>
val recordAudioAttach: Observable<Unit>
val recordAudioPlayerPlayPause: Observable<QkMediaPlayer.PlayingState>
val recordAudioPlayerConfigUI: Subject<QkMediaPlayer.PlayingState>
val recordAudioPlayerVisible: Subject<Boolean>
val recordAudioStartStop: Subject<MicInputCloudView.ViewState>
val recordAudioMsgRecordVisible: Subject<Boolean>
val recordAudioRecord: Subject<MicInputCloudView.ViewState>
val recordAudioChronometer: Subject<Boolean>
fun clearSelection()
@@ -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}"
@@ -755,16 +755,28 @@
app:srcCompat="@drawable/ic_delete_white_24dp"
app:tint="?android:attr/textColorPrimary" />
<dev.octoshrimpy.quik.common.widget.MicInputCloudView
android:id="@+id/audioMsgRecord"
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:background="?android:attr/selectableItemBackgroundBorderless"
app:pauseIcon="@drawable/exo_icon_stop"
app:playIcon="@android:drawable/ic_btn_speak_now"
app:primaryColor="@color/textPrimaryDark" />
android:layout_weight="1">
<ImageView
android:id="@+id/audioMsgBluetooth"
android:layout_width="76dp"
android:layout_height="76dp"
android:layout_gravity="center"
app:srcCompat="@android:drawable/stat_sys_data_bluetooth" />
<dev.octoshrimpy.quik.common.widget.MicInputCloudView
android:id="@+id/audioMsgRecord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="?android:attr/selectableItemBackgroundBorderless"
app:pauseIcon="@drawable/exo_icon_stop"
app:playIcon="@android:drawable/ic_btn_speak_now"
app:primaryColor="@color/textPrimaryDark" />
</FrameLayout>
<ImageView
android:id="@+id/audioMsgAttach"