diff --git a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt index 1cf7bcd6..5d67ce60 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt @@ -593,8 +593,8 @@ class BaresipService: Service() { var newEvent: String? = null - for (account_index in uas.indices) { - if (uas[account_index].account.aor == aor) { + for (accountIndex in uas.indices) { + if (uas[accountIndex].account.aor == aor) { when (ev[0]) { "registering", "unregistering" -> { ua.status = R.drawable.dot_yellow @@ -1201,6 +1201,7 @@ class BaresipService: Service() { .build() if (AudioManagerCompat.requestAudioFocus(am, audioFocusRequest!!) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { Log.d(TAG, "Audio focus granted") + isAudioFocused = true if (isBluetoothHeadsetConnected() && !am.isBluetoothScoOn) { Log.d(TAG, "Starting Bluetooth SCO") am.startBluetoothSco() @@ -1208,6 +1209,7 @@ class BaresipService: Service() { } else { Log.d(TAG, "Audio focus denied") audioFocusRequest = null + isAudioFocused = false } } @@ -1215,6 +1217,7 @@ class BaresipService: Service() { if (audioFocusRequest != null) { AudioManagerCompat.abandonAudioFocusRequest(am, audioFocusRequest!!) audioFocusRequest = null + isAudioFocused = false Log.d(TAG, "Audio focus abandoned") } } @@ -1509,6 +1512,7 @@ class BaresipService: Service() { var isServiceRunning = false var isConfigInitialized = false + var isAudioFocused = false var libraryLoaded = false var callVolume = 0 var dynDns = false @@ -1537,6 +1541,7 @@ class BaresipService: Service() { val serviceEvent = MutableLiveData>() val serviceEvents = mutableListOf() + } init { diff --git a/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt index 5e9123b7..ec7f895f 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt @@ -196,10 +196,10 @@ class MainActivity : AppCompatActivity() { if (device != null) { Log.d(TAG, "Com device changed to type ${device.type} in mode ${am.mode}") if (speakerIcon != null) { - if (device.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) - speakerIcon!!.setIcon(R.drawable.speaker_on) - else + if (device.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE) speakerIcon!!.setIcon(R.drawable.speaker_off) + else + speakerIcon!!.setIcon(R.drawable.speaker_on) } } } @@ -1258,6 +1258,9 @@ class MainActivity : AppCompatActivity() { } R.id.speakerIcon -> { + if (Build.VERSION.SDK_INT >= 31) + Log.d(TAG, "Toggling speakerphone when dev/mode is " + + "${am.communicationDevice!!.type}/${am.mode}") Utils.toggleSpeakerPhone(ContextCompat.getMainExecutor(this), am) if (Build.VERSION.SDK_INT < 31 && speakerIcon != null) { if (am.isSpeakerphoneOn) @@ -1840,7 +1843,7 @@ class MainActivity : AppCompatActivity() { hangupButton.isEnabled = false } } else { - Log.d(TAG, "Audio mode changed to MODE_NORMAL using " + + Log.d(TAG, "Audio mode changed to mode ${am.mode} using " + "device ${am.communicationDevice!!.type}") } } diff --git a/app/src/main/kotlin/com/tutpro/baresip/Utils.kt b/app/src/main/kotlin/com/tutpro/baresip/Utils.kt index 3ac1db0a..17238dc3 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/Utils.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/Utils.kt @@ -589,7 +589,6 @@ object Utils { } } - @Suppress("DEPRECATION") fun downloadsPath(fileName: String): String { return Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_DOWNLOADS).path + "/$fileName" @@ -893,62 +892,73 @@ object Utils { fun isSpeakerPhoneOn(am: AudioManager): Boolean { return if (Build.VERSION.SDK_INT >= 31) - am.communicationDevice!!.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER + am.communicationDevice!!.type != AudioDeviceInfo.TYPE_BUILTIN_EARPIECE else am.isSpeakerphoneOn } private fun setSpeakerPhone(executor: Executor, am: AudioManager, enable: Boolean) { - if (enable == isSpeakerPhoneOn(am)) - return - if (enable) { - if (Build.VERSION.SDK_INT >= 31) { - Log.d(TAG, "Setting current device from ${am.communicationDevice!!.type} to " + - "${AudioDeviceInfo.TYPE_BUILTIN_SPEAKER} in mode ${am.mode}") - var speakerDevice: AudioDeviceInfo? = null + if (Build.VERSION.SDK_INT >= 31) { + val current = am.communicationDevice!!.type + Log.d(TAG, "Current com dev/mode is $current/${am.mode}") + var speakerDevice: AudioDeviceInfo? = null + if (enable) { for (device in am.availableCommunicationDevices) - if (device.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) { + if (device.type == AudioDeviceInfo.TYPE_BLUETOOTH_SCO) { speakerDevice = device break } - if (speakerDevice != null) { - if (am.mode == AudioManager.MODE_NORMAL) { + if (speakerDevice == null) + for (device in am.availableCommunicationDevices) + if (device.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) { + speakerDevice = device + break + } + } else { + for (device in am.availableCommunicationDevices) + if (device.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE) { + speakerDevice = device + break + } + } + if (speakerDevice != null && current != speakerDevice.type) { + if (speakerDevice.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE) { + am.clearCommunicationDevice() + Log.d(TAG, "Setting com device to TYPE_BUILTIN_EARPIECE") + if (!am.setCommunicationDevice(speakerDevice)) + Log.e(TAG, "Could not set com device") + if (BaresipService.isAudioFocused && am.mode == AudioManager.MODE_NORMAL) { + Log.d(TAG, "Setting mode to communication") + am.mode = AudioManager.MODE_IN_COMMUNICATION + } + } else { + // Currently at API levels 31+, speakerphone needs normal mode + if (am.mode == AudioManager.MODE_NORMAL) { + Log.d(TAG, "Setting com device to ${speakerDevice.type} in MODE_NORMAL") if (!am.setCommunicationDevice(speakerDevice)) - Log.e(TAG, "Could not turn on speaker device") - Log.d(TAG, "Type of current device is ${am.communicationDevice!!.type}") + Log.e(TAG, "Could not set com device") } else { - // Currently at API levels 31+, speakerphone needs to be turned on in normal mode val normalListener = object : AudioManager.OnModeChangedListener { override fun onModeChanged(mode: Int) { if (mode == AudioManager.MODE_NORMAL) { am.removeOnModeChangedListener(this) + Log.d(TAG, "Setting com device to ${speakerDevice.type}" + + " in mode ${am.mode}") if (!am.setCommunicationDevice(speakerDevice)) - Log.e(TAG, "Could not turn on speaker device") - Log.d(TAG, "Type of current device is ${am.communicationDevice!!.type}") + Log.e(TAG, "Could not set com device") } } } am.addOnModeChangedListener(executor, normalListener) + Log.d(TAG, "Setting mode to NORMAL") am.mode = AudioManager.MODE_NORMAL } } - } else { - am.isSpeakerphoneOn = true - Log.d(TAG, "Speakerphone is ${am.isSpeakerphoneOn}") + Log.d(TAG, "New com device/mode is ${am.communicationDevice!!.type}/${am.mode}") } } else { - if (Build.VERSION.SDK_INT >= 31) { - Log.d(TAG, "Setting current device from type ${am.communicationDevice!!.type} to " + - "${AudioDeviceInfo.TYPE_BUILTIN_EARPIECE} in mode ${am.mode}") - am.clearCommunicationDevice() - // Restore communication mode - if (Call.call("connected") != null && am.mode == AudioManager.MODE_NORMAL) - am.mode = AudioManager.MODE_IN_COMMUNICATION - Log.d(TAG, "Type of current device is ${am.communicationDevice!!.type}") - } else { - am.isSpeakerphoneOn = false - Log.d(TAG, "Speakerphone is ${am.isSpeakerphoneOn}") - } + am.isSpeakerphoneOn = enable + Log.d(TAG, "Speakerphone is ${am.isSpeakerphoneOn}") } }