Skip to content

Commit

Permalink
Fix for vector-im#2100: Using Ringtone and RingtoneManager classes to…
Browse files Browse the repository at this point in the history
… control ringtones for incoming calls.

Using systemwide setting for ringtone and volume.
  • Loading branch information
lomion0815 committed Sep 27, 2020
1 parent 48b10e6 commit 67eff05
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
27 changes: 24 additions & 3 deletions vector/src/main/java/im/vector/app/core/services/CallRingPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package im.vector.app.core.services

import android.content.Context
import android.media.Ringtone
import android.media.RingtoneManager
import android.media.AudioAttributes
import android.media.AudioManager
import android.media.MediaPlayer
Expand All @@ -25,7 +27,26 @@ import androidx.core.content.getSystemService
import im.vector.app.R
import timber.log.Timber

class CallRingPlayer(
class CallRingPlayerIncoming(
context: Context
) {

private val applicationContext = context.applicationContext
private var r: Ringtone? = null

fun start() {
val notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE)
r = RingtoneManager.getRingtone(applicationContext, notification)
Timber.v("## VOIP Starting ringing incomming")
r?.play()
}

fun stop() {
r?.stop()
}
}

class CallRingPlayerOutgoing(
context: Context
) {

Expand All @@ -44,12 +65,12 @@ class CallRingPlayer(
try {
if (player?.isPlaying == false) {
player?.start()
Timber.v("## VOIP Starting ringing")
Timber.v("## VOIP Starting ringing outgoing")
} else {
Timber.v("## VOIP already playing")
}
} catch (failure: Throwable) {
Timber.e(failure, "## VOIP Failed to start ringing")
Timber.e(failure, "## VOIP Failed to start ringing outgoing")
player = null
}
} else {
Expand Down
22 changes: 14 additions & 8 deletions vector/src/main/java/im/vector/app/core/services/CallService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class CallService : VectorService(), WiredHeadsetStateReceiver.HeadsetEventListe
private lateinit var notificationUtils: NotificationUtils
private lateinit var webRtcPeerConnectionManager: WebRtcPeerConnectionManager

private var callRingPlayer: CallRingPlayer? = null
private var callRingPlayerIncoming: CallRingPlayerIncoming? = null
private var callRingPlayerOutgoing: CallRingPlayerOutgoing? = null

private var wiredHeadsetStateReceiver: WiredHeadsetStateReceiver? = null
private var bluetoothHeadsetStateReceiver: BluetoothHeadsetReceiver? = null
Expand All @@ -63,14 +64,16 @@ class CallService : VectorService(), WiredHeadsetStateReceiver.HeadsetEventListe
super.onCreate()
notificationUtils = vectorComponent().notificationUtils()
webRtcPeerConnectionManager = vectorComponent().webRtcPeerConnectionManager()
callRingPlayer = CallRingPlayer(applicationContext)
callRingPlayerIncoming = CallRingPlayerIncoming(applicationContext)
callRingPlayerOutgoing = CallRingPlayerOutgoing(applicationContext)
wiredHeadsetStateReceiver = WiredHeadsetStateReceiver.createAndRegister(this, this)
bluetoothHeadsetStateReceiver = BluetoothHeadsetReceiver.createAndRegister(this, this)
}

override fun onDestroy() {
super.onDestroy()
callRingPlayer?.stop()
callRingPlayerIncoming?.stop()
callRingPlayerOutgoing?.stop()
wiredHeadsetStateReceiver?.let { WiredHeadsetStateReceiver.unRegister(this, it) }
wiredHeadsetStateReceiver = null
bluetoothHeadsetStateReceiver?.let { BluetoothHeadsetReceiver.unRegister(this, it) }
Expand Down Expand Up @@ -100,32 +103,35 @@ class CallService : VectorService(), WiredHeadsetStateReceiver.HeadsetEventListe
when (intent.action) {
ACTION_INCOMING_RINGING_CALL -> {
mediaSession?.isActive = true
callRingPlayer?.start()
callRingPlayerIncoming?.start()
displayIncomingCallNotification(intent)
}
ACTION_OUTGOING_RINGING_CALL -> {
mediaSession?.isActive = true
callRingPlayer?.start()
callRingPlayerOutgoing?.start()
displayOutgoingRingingCallNotification(intent)
}
ACTION_ONGOING_CALL -> {
callRingPlayer?.stop()
callRingPlayerIncoming?.stop()
callRingPlayerOutgoing?.stop()
displayCallInProgressNotification(intent)
}
ACTION_NO_ACTIVE_CALL -> hideCallNotifications()
ACTION_CALL_CONNECTING -> {
// lower notification priority
displayCallInProgressNotification(intent)
// stop ringing
callRingPlayer?.stop()
callRingPlayerIncoming?.stop()
callRingPlayerOutgoing?.stop()
}
ACTION_ONGOING_CALL_BG -> {
// there is an ongoing call but call activity is in background
displayCallOnGoingInBackground(intent)
}
else -> {
// Should not happen
callRingPlayer?.stop()
callRingPlayerIncoming?.stop()
callRingPlayerOutgoing?.stop()
myStopSelf()
}
}
Expand Down

0 comments on commit 67eff05

Please sign in to comment.