diff --git a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt index 69ec77ff..0b6e2816 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt @@ -48,7 +48,7 @@ class BaresipService: Service() { internal var rtTimer: Timer? = null internal var audioFocusRequest: AudioFocusRequest? = null - internal var audioFocused = false + internal var audioFocusUsage = -1 internal var origVolumes = arrayOf(-1, -1, -1, -1) internal val btAdapter = BluetoothAdapter.getDefaultAdapter() internal var activeNetwork = "" @@ -543,12 +543,9 @@ class BaresipService: Service() { return } "call progress", "call ringing" -> { - if (!isAudioFocused()) { - requestAudioFocus(AudioManager.STREAM_VOICE_CALL) - setCallVolume() - } else { - return - } + requestAudioFocus(AudioAttributes.USAGE_VOICE_COMMUNICATION) + setCallVolume() + return } "call incoming" -> { val peerUri = Api.call_peeruri(callp) @@ -645,10 +642,8 @@ class BaresipService: Service() { stopRinging() if (am.mode != AudioManager.MODE_IN_COMMUNICATION) am.mode = AudioManager.MODE_IN_COMMUNICATION - if (!isAudioFocused()) { - requestAudioFocus(AudioManager.STREAM_VOICE_CALL) - setCallVolume() - } + requestAudioFocus(AudioAttributes.USAGE_VOICE_COMMUNICATION) + setCallVolume() if (!Utils.isVisible()) return } @@ -728,7 +723,6 @@ class BaresipService: Service() { calls.remove(call) if (Call.calls().size == 0) { resetCallVolume() - Log.d(LOG_TAG, "Audio mode is ${am.mode}") if (am.mode != AudioManager.MODE_NORMAL) am.mode = AudioManager.MODE_NORMAL if (am.isSpeakerphoneOn) am.isSpeakerphoneOn = false @@ -780,7 +774,6 @@ class BaresipService: Service() { } val timeStamp = System.currentTimeMillis().toString() Log.d(LOG_TAG, "Message event for $uap from $peer at $timeStamp") - Log.d(LOG_TAG, "Audio mode is ${am.mode}") Message.add(Message(ua.account.aor, peer, text, timeStamp.toLong(), R.drawable.arrow_down_green, 0, "", true)) Message.save() @@ -910,20 +903,27 @@ class BaresipService: Service() { - private fun requestAudioFocus(streamType: Int) { + private fun requestAudioFocus(usage: Int) { + if (audioFocusUsage != -1) { + if (audioFocusUsage == usage) + return + else + abandonAudioFocus() + } if ((Build.VERSION.SDK_INT >= 26) && (audioFocusRequest == null)) { @TargetApi(26) audioFocusRequest = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE) .run { setAudioAttributes(AudioAttributes.Builder().run { - setLegacyStreamType(streamType) + setUsage(usage) build() }) build() } @TargetApi(26) if (am.requestAudioFocus(audioFocusRequest!!) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { - Log.d(LOG_TAG, "Audio focus granted for stream $streamType") + Log.d(LOG_TAG, "Audio focus granted for usage $usage") + audioFocusUsage = usage if (isBluetoothHeadsetConnected() && !am.isBluetoothScoOn) { Log.d(LOG_TAG, "Starting Bluetooth Sco") am.startBluetoothSco() @@ -931,17 +931,18 @@ class BaresipService: Service() { } else { Log.d(LOG_TAG, "Audio focus denied") audioFocusRequest = null + audioFocusUsage = -1 } } else { - if (am.requestAudioFocus(null, streamType, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE) == + if (am.requestAudioFocus(null, usage, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { - Log.d(LOG_TAG, "Audio focus granted for stream $streamType") + Log.d(LOG_TAG, "Audio focus granted for usage $usage") + audioFocusUsage = usage if (isBluetoothHeadsetConnected() && !am.isBluetoothScoOn) am.startBluetoothSco() - audioFocused = true } else { Log.d(LOG_TAG, "Audio focus denied") - audioFocused = false + audioFocusUsage = -1 } } } @@ -953,10 +954,7 @@ class BaresipService: Service() { } private fun isAudioFocused(): Boolean { - if (Build.VERSION.SDK_INT >= 26) - return audioFocusRequest != null - else - return audioFocused + return audioFocusUsage != -1 } private fun abandonAudioFocus() { @@ -966,15 +964,16 @@ class BaresipService: Service() { AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { Log.d(LOG_TAG, "Audio focus abandoned") audioFocusRequest = null + audioFocusUsage = -1 } else { Log.d(LOG_TAG, "Failed to abandon audio focus") } } } else { - if (audioFocused) { + if (audioFocusUsage != -1) { if (am.abandonAudioFocus(null) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { Log.d(LOG_TAG, "Audio focus abandoned") - audioFocused = false + audioFocusUsage = -1 } else { Log.d(LOG_TAG, "Failed to abandon audio focus") } @@ -984,7 +983,7 @@ class BaresipService: Service() { private fun startRinging() { am.mode = AudioManager.MODE_RINGTONE - requestAudioFocus(AudioManager.STREAM_RING) + requestAudioFocus(AudioAttributes.USAGE_NOTIFICATION_RINGTONE) if (Build.VERSION.SDK_INT >= 28) { rt.isLooping = true rt.play() @@ -1012,14 +1011,14 @@ class BaresipService: Service() { } private fun setCallVolume() { - if (callVolume != 0) { + if ((callVolume != 0) && (origVolumes.contentEquals(arrayOf(-1, -1, -1, -1)))) { for (streamType in listOf(AudioManager.STREAM_VOICE_CALL, AudioManager.STREAM_MUSIC)) { origVolumes[streamType] = am.getStreamVolume(streamType) am.setStreamVolume(streamType, (callVolume * 0.1 * am.getStreamMaxVolume(streamType)).roundToInt(), 0) - Log.d(LOG_TAG, "New call volume of stream type $streamType is " + - "${am.getStreamVolume(streamType)}") + Log.d(LOG_TAG, "Orig/new call volume of stream type $streamType is " + + "${origVolumes[streamType]}/${am.getStreamVolume(streamType)}") } } }