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
@@ -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" -> { "Call Reject" -> {
val callp = intent!!.getStringExtra("callp")!! val callp = intent!!.getStringExtra("callp")!!
val call = Call.ofCallp(callp) val call = Call.ofCallp(callp)
@@ -636,17 +651,6 @@ class BaresipService: Service() {
return 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" -> { "call established" -> {
nm.cancel(CALL_NOTIFICATION_ID) nm.cancel(CALL_NOTIFICATION_ID)
val call = Call.ofCallp(callp) val call = Call.ofCallp(callp)
@@ -745,10 +749,6 @@ class BaresipService: Service() {
} }
Log.d(TAG, "AoR $aor call $callp is closed") Log.d(TAG, "AoR $aor call $callp is closed")
stopRinging() stopRinging()
if (isMainVisible)
am.mode = AudioManager.MODE_IN_COMMUNICATION
else
am.mode = AudioManager.MODE_NORMAL
stopMediaPlayer() stopMediaPlayer()
when (ev[2]) { when (ev[2]) {
"busy" -> "busy" ->
@@ -762,6 +762,7 @@ class BaresipService: Service() {
am.isSpeakerphoneOn = false am.isSpeakerphoneOn = false
am.stopBluetoothSco() am.stopBluetoothSco()
abandonAudioFocus() abandonAudioFocus()
am.mode = AudioManager.MODE_NORMAL
proximitySensing(false) proximitySensing(false)
} }
if (ua.account.callHistory && !call.hasHistory) { if (ua.account.callHistory && !call.hasHistory) {
@@ -1120,8 +1121,8 @@ class BaresipService: Service() {
} }
private fun startRinging() { private fun startRinging() {
am.mode = AudioManager.MODE_RINGTONE
requestAudioFocus(AudioManager.STREAM_RING, AudioAttributes.CONTENT_TYPE_MUSIC) requestAudioFocus(AudioManager.STREAM_RING, AudioAttributes.CONTENT_TYPE_MUSIC)
am.mode = AudioManager.MODE_RINGTONE
if (VERSION.SDK_INT >= 28) { if (VERSION.SDK_INT >= 28) {
rt.isLooping = true rt.isLooping = true
rt.play() rt.play()
@@ -361,7 +361,12 @@ class MainActivity : AppCompatActivity() {
Log.d(TAG, "AoR $aor answering call $callp from ${callUri.text}") Log.d(TAG, "AoR $aor answering call $callp from ${callUri.text}")
answerButton.isEnabled = false answerButton.isEnabled = false
rejectButton.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 { rejectButton.setOnClickListener {
@@ -792,9 +797,6 @@ class MainActivity : AppCompatActivity() {
super.onResume() super.onResume()
Log.d(TAG, "Main onResume with action '$resumeAction'") Log.d(TAG, "Main onResume with action '$resumeAction'")
nm.cancelAll() 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 BaresipService.isMainVisible = true
when (resumeAction) { when (resumeAction) {
"call show" -> "call show" ->
@@ -849,8 +851,6 @@ class MainActivity : AppCompatActivity() {
override fun onPause() { override fun onPause() {
Log.d(TAG, "Main onPause") Log.d(TAG, "Main onPause")
Utils.addActivity("main") Utils.addActivity("main")
if (Call.calls().size == 0)
am.mode = AudioManager.MODE_NORMAL
BaresipService.isMainVisible = false BaresipService.isMainVisible = false
saveCallUri() saveCallUri()
super.onPause() super.onPause()