- Default call volume now effects both voice call and music streams.

- Set audio mode only if needed.
This commit is contained in:
Juha Heinanen
2020-02-21 02:58:49 +02:00
parent fb5418af9c
commit be929c4cb4
@@ -49,7 +49,7 @@ class BaresipService: Service() {
internal var rtTimer: Timer? = null internal var rtTimer: Timer? = null
internal var audioFocusRequest: AudioFocusRequest? = null internal var audioFocusRequest: AudioFocusRequest? = null
internal var audioFocused = false internal var audioFocused = false
internal var origCallVolume = -1 internal var origVolumes = arrayOf(-1, -1, -1, -1)
internal val btAdapter = BluetoothAdapter.getDefaultAdapter() internal val btAdapter = BluetoothAdapter.getDefaultAdapter()
internal var activeNetwork = "" internal var activeNetwork = ""
@@ -643,7 +643,8 @@ class BaresipService: Service() {
call.hasHistory = true call.hasHistory = true
} }
stopRinging() stopRinging()
am.mode = AudioManager.MODE_IN_COMMUNICATION if (am.mode != AudioManager.MODE_IN_COMMUNICATION)
am.mode = AudioManager.MODE_IN_COMMUNICATION
if (!isAudioFocused()) { if (!isAudioFocused()) {
requestAudioFocus(AudioManager.STREAM_VOICE_CALL) requestAudioFocus(AudioManager.STREAM_VOICE_CALL)
setCallVolume() setCallVolume()
@@ -727,7 +728,9 @@ class BaresipService: Service() {
calls.remove(call) calls.remove(call)
if (Call.calls().size == 0) { if (Call.calls().size == 0) {
resetCallVolume() resetCallVolume()
am.mode = AudioManager.MODE_NORMAL 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 if (am.isSpeakerphoneOn) am.isSpeakerphoneOn = false
if (am.isBluetoothScoOn) { if (am.isBluetoothScoOn) {
Log.d(LOG_TAG, "Stopping Bluetooth SCO") Log.d(LOG_TAG, "Stopping Bluetooth SCO")
@@ -777,6 +780,7 @@ class BaresipService: Service() {
} }
val timeStamp = System.currentTimeMillis().toString() val timeStamp = System.currentTimeMillis().toString()
Log.d(LOG_TAG, "Message event for $uap from $peer at $timeStamp") 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(), Message.add(Message(ua.account.aor, peer, text, timeStamp.toLong(),
R.drawable.arrow_down_green, 0, "", true)) R.drawable.arrow_down_green, 0, "", true))
Message.save() Message.save()
@@ -909,11 +913,12 @@ class BaresipService: Service() {
private fun requestAudioFocus(streamType: Int) { private fun requestAudioFocus(streamType: Int) {
if ((Build.VERSION.SDK_INT >= 26) && (audioFocusRequest == null)) { if ((Build.VERSION.SDK_INT >= 26) && (audioFocusRequest == null)) {
@TargetApi(26) @TargetApi(26)
audioFocusRequest = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE).run { audioFocusRequest = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE)
setAudioAttributes(AudioAttributes.Builder().run { .run {
setLegacyStreamType(streamType) setAudioAttributes(AudioAttributes.Builder().run {
build() setLegacyStreamType(streamType)
}) build()
})
build() build()
} }
@TargetApi(26) @TargetApi(26)
@@ -1008,20 +1013,25 @@ class BaresipService: Service() {
private fun setCallVolume() { private fun setCallVolume() {
if (callVolume != 0) { if (callVolume != 0) {
origCallVolume = am.getStreamVolume(am.mode) for (streamType in listOf(AudioManager.STREAM_VOICE_CALL, AudioManager.STREAM_MUSIC)) {
am.setStreamVolume(am.mode, origVolumes[streamType] = am.getStreamVolume(streamType)
(callVolume * 0.1 * am.getStreamMaxVolume(am.mode)).roundToInt(), am.setStreamVolume(streamType,
0) (callVolume * 0.1 * am.getStreamMaxVolume(streamType)).roundToInt(),
Log.d(LOG_TAG, "Original/new call volume of stream ${am.mode} is " + 0)
"$origCallVolume/${am.getStreamVolume(am.mode)}") Log.d(LOG_TAG, "New call volume of stream type $streamType is " +
"${am.getStreamVolume(streamType)}")
}
} }
} }
private fun resetCallVolume() { private fun resetCallVolume() {
if (origCallVolume != -1) { for (streamType in listOf(AudioManager.STREAM_VOICE_CALL, AudioManager.STREAM_MUSIC)) {
am.setStreamVolume(am.mode, origCallVolume, 0) if (origVolumes[streamType] != -1) {
Log.d(LOG_TAG, "Reset call volume of stream ${am.mode} to ${am.getStreamVolume(am.mode)}") am.setStreamVolume(streamType, origVolumes[streamType], 0)
origCallVolume = -1 Log.d(LOG_TAG, "Reset volume of stream type $streamType to " +
"${am.getStreamVolume(streamType)}")
origVolumes[streamType] = -1
}
} }
} }