Allow both MODE_IN_CALL and MODE_IN_COMMUNICATION as call modes

This commit is contained in:
Juha Heinanen
2026-04-22 09:48:07 +03:00
parent ecb301ecb3
commit df4e6bbe0c
2 changed files with 20 additions and 12 deletions

View File

@ -470,7 +470,6 @@ class BaresipService: Service() {
val callp = intent.getLongExtra("callp", 0L) val callp = intent.getLongExtra("callp", 0L)
stopRinging() stopRinging()
stopMediaPlayer() stopMediaPlayer()
am.mode = MODE_IN_COMMUNICATION
setCallVolume() setCallVolume()
proximitySensing(proximitySensing) proximitySensing(proximitySensing)
Api.ua_answer(uap, callp, Api.VIDMODE_OFF) Api.ua_answer(uap, callp, Api.VIDMODE_OFF)
@ -1393,13 +1392,14 @@ class BaresipService: Service() {
am.mode = MODE_IN_COMMUNICATION am.mode = MODE_IN_COMMUNICATION
executeCall() executeCall()
} else { } else {
if (am.mode == MODE_IN_COMMUNICATION) { val isAnyCallMode = am.mode == MODE_IN_COMMUNICATION || am.mode == AudioManager.MODE_IN_CALL
Log.d(TAG, "Audio mode already in MODE_IN_COMMUNICATION") if (isAnyCallMode) {
Log.d(TAG, "Audio mode already in a call mode (${am.mode})")
executeCall() executeCall()
} else { } else {
audioModeChangedListener = AudioManager.OnModeChangedListener { mode -> audioModeChangedListener = AudioManager.OnModeChangedListener { mode ->
if (mode == MODE_IN_COMMUNICATION) { if (mode == MODE_IN_COMMUNICATION || mode == AudioManager.MODE_IN_CALL) {
Log.d(TAG, "Audio mode changed to MODE_IN_COMMUNICATION") Log.d(TAG, "Audio mode changed to $mode")
audioModeChangedListener?.let { audioModeChangedListener?.let {
am.removeOnModeChangedListener(it) am.removeOnModeChangedListener(it)
audioModeChangedListener = null audioModeChangedListener = null
@ -1918,25 +1918,33 @@ class BaresipService: Service() {
} }
private fun ensureCommunicationMode() { private fun ensureCommunicationMode() {
val targetMode = if (Call.inCall()) MODE_IN_COMMUNICATION else MODE_NORMAL
val currentMode = am.mode val currentMode = am.mode
val isAnyCallMode = currentMode == MODE_IN_COMMUNICATION || currentMode == AudioManager.MODE_IN_CALL
val isSpeakerphoneOn = if (VERSION.SDK_INT >= 31) val isSpeakerphoneOn = if (VERSION.SDK_INT >= 31)
am.communicationDevice?.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER am.communicationDevice?.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER
else { else {
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
am.isSpeakerphoneOn am.isSpeakerphoneOn
} }
if (targetMode == currentMode && (targetMode == MODE_NORMAL || isSpeakerphoneOn == speakerPhone)) {
Log.d(TAG, "ensureCommunicationMode: Already in $targetMode with correct speaker state, skipping.") if (Call.inCall() && isAnyCallMode) {
if (isSpeakerphoneOn == speakerPhone) {
Log.d(TAG, "Already in valid call mode ($currentMode) with correct speaker state.")
return
}
} else if (!Call.inCall() && currentMode == MODE_NORMAL) {
return return
} }
Log.d(TAG, "Scheduling ensureCommunicationMode ($targetMode) in 500ms")
Log.d(TAG, "Scheduling ensureCommunicationMode (current: $currentMode) in 500ms")
Handler(Looper.getMainLooper()).postDelayed({ Handler(Looper.getMainLooper()).postDelayed({
if (Call.inCall()) { if (Call.inCall()) {
if (am.mode != MODE_IN_COMMUNICATION) { if (am.mode != MODE_IN_COMMUNICATION && am.mode != AudioManager.MODE_IN_CALL) {
am.mode = MODE_IN_COMMUNICATION am.mode = MODE_IN_COMMUNICATION
Log.d(TAG, "Manual Mode Guard (SDK ${VERSION.SDK_INT}): Setting MODE_IN_COMMUNICATON") Log.d(TAG, "Manual Mode Guard (SDK ${VERSION.SDK_INT}): Setting MODE_IN_COMMUNICATON from ${am.mode}")
} }
Utils.setSpeakerPhone(mainExecutor, am, speakerPhone)
} else { } else {
if (am.mode != MODE_NORMAL) { if (am.mode != MODE_NORMAL) {
am.mode = MODE_NORMAL am.mode = MODE_NORMAL

View File

@ -897,7 +897,7 @@ object Utils {
} }
} }
private fun setSpeakerPhone(executor: Executor, am: AudioManager, enable: Boolean) { fun setSpeakerPhone(executor: Executor, am: AudioManager, enable: Boolean) {
if (Build.VERSION.SDK_INT >= 31) { if (Build.VERSION.SDK_INT >= 31) {
if (!enable) { if (!enable) {
Log.d(TAG, "Disabling speakerphone") Log.d(TAG, "Disabling speakerphone")