Simplified abandoning audio focus

This commit is contained in:
Juha Heinanen
2023-04-15 07:26:33 +03:00
parent 01bef1ca0b
commit 0e5ba4e559
2 changed files with 8 additions and 12 deletions
@@ -889,9 +889,7 @@ class BaresipService: Service() {
call.remove() call.remove()
if (!Call.inCall()) { if (!Call.inCall()) {
resetCallVolume() resetCallVolume()
if (!abandonAudioFocus(applicationContext)) abandonAudioFocus(applicationContext)
Log.e(TAG, "Failed to abandon audio focus")
am.mode = MODE_NORMAL
proximitySensing(false) proximitySensing(false)
} }
val missed = call.startTime == null && call.dir == "in" && !call.rejected val missed = call.startTime == null && call.dir == "in" && !call.rejected
@@ -1446,8 +1444,8 @@ class BaresipService: Service() {
this.unregisterReceiver(bluetoothReceiver) this.unregisterReceiver(bluetoothReceiver)
this.unregisterReceiver(hotSpotReceiver) this.unregisterReceiver(hotSpotReceiver)
stopRinging() stopRinging()
abandonAudioFocus(applicationContext)
stopMediaPlayer() stopMediaPlayer()
abandonAudioFocus(applicationContext)
uas.clear() uas.clear()
callHistory.clear() callHistory.clear()
messages.clear() messages.clear()
@@ -1535,18 +1533,20 @@ class BaresipService: Service() {
return audioFocusRequest != null return audioFocusRequest != null
} }
fun abandonAudioFocus(ctx: Context): Boolean { fun abandonAudioFocus(ctx: Context) {
val am = ctx.getSystemService(Context.AUDIO_SERVICE) as AudioManager
if (audioFocusRequest != null) { if (audioFocusRequest != null) {
Log.d(TAG, "Abandoning audio focus") Log.d(TAG, "Abandoning audio focus")
val am = ctx.getSystemService(Context.AUDIO_SERVICE) as AudioManager
if (AudioManagerCompat.abandonAudioFocusRequest(am, audioFocusRequest!!) == if (AudioManagerCompat.abandonAudioFocusRequest(am, audioFocusRequest!!) ==
AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
audioFocusRequest = null audioFocusRequest = null
if (isBluetoothHeadsetConnected(ctx)) if (isBluetoothHeadsetConnected(ctx))
stopBluetoothSco(ctx) stopBluetoothSco(ctx)
} else {
Log.e(TAG, "Failed to abandon audio focus")
} }
} }
return audioFocusRequest == null am.mode = MODE_NORMAL
} }
private fun isBluetoothHeadsetConnected(ctx: Context): Boolean { private fun isBluetoothHeadsetConnected(ctx: Context): Boolean {
@@ -364,7 +364,6 @@ class MainActivity : AppCompatActivity() {
callHandler.removeCallbacks(callRunnable!!) callHandler.removeCallbacks(callRunnable!!)
callRunnable = null callRunnable = null
BaresipService.abandonAudioFocus(applicationContext) BaresipService.abandonAudioFocus(applicationContext)
am.mode = AudioManager.MODE_NORMAL
showCall(ua) showCall(ua)
return@setOnClickListener return@setOnClickListener
} }
@@ -373,7 +372,6 @@ class MainActivity : AppCompatActivity() {
am.removeOnModeChangedListener(audioModeChangedListener!!) am.removeOnModeChangedListener(audioModeChangedListener!!)
audioModeChangedListener = null audioModeChangedListener = null
BaresipService.abandonAudioFocus(applicationContext) BaresipService.abandonAudioFocus(applicationContext)
am.mode = AudioManager.MODE_NORMAL
showCall(ua) showCall(ua)
return@setOnClickListener return@setOnClickListener
} }
@@ -1803,8 +1801,6 @@ class MainActivity : AppCompatActivity() {
onHoldCall.newCall = null onHoldCall.newCall = null
call.remove() call.remove()
call.destroy() call.destroy()
if (!BaresipService.abandonAudioFocus(applicationContext))
Log.e(TAG, "Failed to abandon audio focus")
showCall(ua) showCall(ua)
false false
} }
@@ -1925,7 +1921,7 @@ class MainActivity : AppCompatActivity() {
callRunnable = Runnable { callRunnable = Runnable {
callRunnable = null callRunnable = null
if (!call(ua, uri)) { if (!call(ua, uri)) {
am.mode = AudioManager.MODE_NORMAL BaresipService.abandonAudioFocus(applicationContext)
callButton.visibility = View.VISIBLE callButton.visibility = View.VISIBLE
callButton.isEnabled = true callButton.isEnabled = true
hangupButton.visibility = View.INVISIBLE hangupButton.visibility = View.INVISIBLE