Handle call answer in baresip service

No need to keep app in communication mode when no active calls
This commit is contained in:
Juha Heinanen
2021-11-05 20:30:49 +02:00
parent 5d9eeb0ca1
commit d28eff6997
2 changed files with 23 additions and 22 deletions

View File

@ -366,6 +366,21 @@ class BaresipService: Service() {
}
"Call Answer" -> {
val uap = intent!!.getStringExtra("uap")!!
val callp = intent.getStringExtra("callp")!!
val vidMode = intent.getIntExtra("video", Api.VIDMODE_OFF)
stopRinging()
stopMediaPlayer()
if (am.mode != AudioManager.MODE_IN_COMMUNICATION)
am.mode = AudioManager.MODE_IN_COMMUNICATION
requestAudioFocus(AudioManager.STREAM_VOICE_CALL,
AudioAttributes.CONTENT_TYPE_SPEECH)
setCallVolume()
proximitySensing(true)
Api.ua_answer(uap, callp, vidMode)
}
"Call Reject" -> {
val callp = intent!!.getStringExtra("callp")!!
val call = Call.ofCallp(callp)
@ -636,17 +651,6 @@ class BaresipService: Service() {
return
}
}
"call answered" -> {
stopRinging()
stopMediaPlayer()
if (am.mode != AudioManager.MODE_IN_COMMUNICATION)
am.mode = AudioManager.MODE_IN_COMMUNICATION
requestAudioFocus(AudioManager.STREAM_VOICE_CALL,
AudioAttributes.CONTENT_TYPE_SPEECH)
setCallVolume()
proximitySensing(true)
return
}
"call established" -> {
nm.cancel(CALL_NOTIFICATION_ID)
val call = Call.ofCallp(callp)
@ -745,10 +749,6 @@ class BaresipService: Service() {
}
Log.d(TAG, "AoR $aor call $callp is closed")
stopRinging()
if (isMainVisible)
am.mode = AudioManager.MODE_IN_COMMUNICATION
else
am.mode = AudioManager.MODE_NORMAL
stopMediaPlayer()
when (ev[2]) {
"busy" ->
@ -762,6 +762,7 @@ class BaresipService: Service() {
am.isSpeakerphoneOn = false
am.stopBluetoothSco()
abandonAudioFocus()
am.mode = AudioManager.MODE_NORMAL
proximitySensing(false)
}
if (ua.account.callHistory && !call.hasHistory) {
@ -1120,8 +1121,8 @@ class BaresipService: Service() {
}
private fun startRinging() {
am.mode = AudioManager.MODE_RINGTONE
requestAudioFocus(AudioManager.STREAM_RING, AudioAttributes.CONTENT_TYPE_MUSIC)
am.mode = AudioManager.MODE_RINGTONE
if (VERSION.SDK_INT >= 28) {
rt.isLooping = true
rt.play()

View File

@ -361,7 +361,12 @@ class MainActivity : AppCompatActivity() {
Log.d(TAG, "AoR $aor answering call $callp from ${callUri.text}")
answerButton.isEnabled = false
rejectButton.isEnabled = false
Api.ua_answer(ua.uap, callp, Api.VIDMODE_OFF)
val intent = Intent(this@MainActivity, BaresipService::class.java)
intent.action = "Call Answer"
intent.putExtra("uap", ua.uap)
intent.putExtra("callp", callp)
intent.putExtra("video", Api.VIDMODE_OFF)
startService(intent)
}
rejectButton.setOnClickListener {
@ -792,9 +797,6 @@ class MainActivity : AppCompatActivity() {
super.onResume()
Log.d(TAG, "Main onResume with action '$resumeAction'")
nm.cancelAll()
// Keep the app in communication mode in order to avoid audio delay when the mode changes
if (am.mode != AudioManager.MODE_RINGTONE && am.mode != AudioManager.MODE_IN_COMMUNICATION)
am.mode = AudioManager.MODE_IN_COMMUNICATION
BaresipService.isMainVisible = true
when (resumeAction) {
"call show" ->
@ -849,8 +851,6 @@ class MainActivity : AppCompatActivity() {
override fun onPause() {
Log.d(TAG, "Main onPause")
Utils.addActivity("main")
if (Call.calls().size == 0)
am.mode = AudioManager.MODE_NORMAL
BaresipService.isMainVisible = false
saveCallUri()
super.onPause()