Improved Telecom Helper use
This commit is contained in:
parent
0c7c3ea751
commit
68152b5b3e
5 changed files with 58 additions and 12 deletions
|
@ -23,6 +23,7 @@ import androidx.lifecycle.MutableLiveData
|
||||||
import org.linphone.R
|
import org.linphone.R
|
||||||
import org.linphone.activities.main.settings.SettingListenerStub
|
import org.linphone.activities.main.settings.SettingListenerStub
|
||||||
import org.linphone.core.MediaEncryption
|
import org.linphone.core.MediaEncryption
|
||||||
|
import org.linphone.core.tools.Log
|
||||||
import org.linphone.mediastream.Version
|
import org.linphone.mediastream.Version
|
||||||
import org.linphone.telecom.TelecomHelper
|
import org.linphone.telecom.TelecomHelper
|
||||||
import org.linphone.utils.Event
|
import org.linphone.utils.Event
|
||||||
|
@ -67,7 +68,11 @@ class CallSettingsViewModel : GenericSettingsViewModel() {
|
||||||
if (newValue) {
|
if (newValue) {
|
||||||
enableTelecomManagerEvent.value = Event(true)
|
enableTelecomManagerEvent.value = Event(true)
|
||||||
} else {
|
} else {
|
||||||
if (TelecomHelper.exists()) TelecomHelper.get().removeAccount()
|
if (TelecomHelper.exists()) {
|
||||||
|
Log.i("[Call Settings] Removing Telecom Manager account & destroying singleton")
|
||||||
|
TelecomHelper.get().removeAccount()
|
||||||
|
TelecomHelper.destroy()
|
||||||
|
}
|
||||||
prefs.useTelecomManager = newValue
|
prefs.useTelecomManager = newValue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -136,7 +136,7 @@ class CoreContext(val context: Context, coreConfig: Config) {
|
||||||
) {
|
) {
|
||||||
Log.i("[Context] Call state changed [$state]")
|
Log.i("[Context] Call state changed [$state]")
|
||||||
if (state == Call.State.IncomingReceived || state == Call.State.IncomingEarlyMedia) {
|
if (state == Call.State.IncomingReceived || state == Call.State.IncomingEarlyMedia) {
|
||||||
if (!corePreferences.useTelecomManager) {
|
if (!corePreferences.useTelecomManager) { // Can't use the following call with Telecom Manager API as it will "fake" GSM calls
|
||||||
var gsmCallActive = false
|
var gsmCallActive = false
|
||||||
if (::phoneStateListener.isInitialized) {
|
if (::phoneStateListener.isInitialized) {
|
||||||
gsmCallActive = phoneStateListener.isInCall()
|
gsmCallActive = phoneStateListener.isInCall()
|
||||||
|
@ -147,6 +147,18 @@ class CoreContext(val context: Context, coreConfig: Config) {
|
||||||
call.decline(Reason.Busy)
|
call.decline(Reason.Busy)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if (TelecomHelper.exists()) {
|
||||||
|
if (!TelecomHelper.get().isIncomingCallPermitted() ||
|
||||||
|
TelecomHelper.get().isInManagedCall()
|
||||||
|
) {
|
||||||
|
Log.w("[Context] Refusing the call with reason busy because Telecom Manager will reject the call")
|
||||||
|
call.decline(Reason.Busy)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Log.e("[Context] Telecom Manager singleton wasn't created!")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Starting SDK 24 (Android 7.0) we rely on the fullscreen intent of the call incoming notification
|
// Starting SDK 24 (Android 7.0) we rely on the fullscreen intent of the call incoming notification
|
||||||
|
@ -297,13 +309,14 @@ class CoreContext(val context: Context, coreConfig: Config) {
|
||||||
|
|
||||||
notificationsManager.onCoreReady()
|
notificationsManager.onCoreReady()
|
||||||
|
|
||||||
|
core.addListener(listener)
|
||||||
|
|
||||||
|
// CoreContext listener must be added first!
|
||||||
if (Version.sdkAboveOrEqual(Version.API26_O_80) && corePreferences.useTelecomManager) {
|
if (Version.sdkAboveOrEqual(Version.API26_O_80) && corePreferences.useTelecomManager) {
|
||||||
Log.i("[Context] Creating telecom helper")
|
Log.i("[Context] Creating telecom helper")
|
||||||
TelecomHelper.create(context)
|
TelecomHelper.create(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
core.addListener(listener)
|
|
||||||
|
|
||||||
if (isPush) {
|
if (isPush) {
|
||||||
Log.i("[Context] Push received, assume in background")
|
Log.i("[Context] Push received, assume in background")
|
||||||
core.enterBackground()
|
core.enterBackground()
|
||||||
|
|
|
@ -23,7 +23,6 @@ import android.graphics.drawable.Icon
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.telecom.CallAudioState
|
import android.telecom.CallAudioState
|
||||||
import android.telecom.Connection
|
import android.telecom.Connection
|
||||||
import android.telecom.DisconnectCause
|
|
||||||
import android.telecom.StatusHints
|
import android.telecom.StatusHints
|
||||||
import org.linphone.LinphoneApplication.Companion.coreContext
|
import org.linphone.LinphoneApplication.Companion.coreContext
|
||||||
import org.linphone.R
|
import org.linphone.R
|
||||||
|
@ -95,7 +94,6 @@ class NativeCallWrapper(var callId: String) : Connection() {
|
||||||
|
|
||||||
override fun onReject() {
|
override fun onReject() {
|
||||||
Log.i("[Connection] Rejecting telecom call with id: $callId")
|
Log.i("[Connection] Rejecting telecom call with id: $callId")
|
||||||
setDisconnected(DisconnectCause(DisconnectCause.REJECTED))
|
|
||||||
getCall()?.terminate()
|
getCall()?.terminate()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -48,6 +48,7 @@ class TelecomConnectionService : ConnectionService() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Call.State.Error -> onCallError(call)
|
||||||
Call.State.End, Call.State.Released -> onCallEnded(call)
|
Call.State.End, Call.State.Released -> onCallEnded(call)
|
||||||
Call.State.Connected -> onCallConnected(call)
|
Call.State.Connected -> onCallConnected(call)
|
||||||
}
|
}
|
||||||
|
@ -160,12 +161,23 @@ class TelecomConnectionService : ConnectionService() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun onCallError(call: Call) {
|
||||||
|
val connection = getConnectionForCallId(call.callLog.callId)
|
||||||
|
connection ?: return
|
||||||
|
|
||||||
|
connections.remove(connection)
|
||||||
|
connection.setDisconnected(DisconnectCause(DisconnectCause.ERROR))
|
||||||
|
connection.destroy()
|
||||||
|
}
|
||||||
|
|
||||||
private fun onCallEnded(call: Call) {
|
private fun onCallEnded(call: Call) {
|
||||||
val connection = getConnectionForCallId(call.callLog.callId)
|
val connection = getConnectionForCallId(call.callLog.callId)
|
||||||
connection ?: return
|
connection ?: return
|
||||||
|
|
||||||
connections.remove(connection)
|
connections.remove(connection)
|
||||||
connection.setDisconnected(DisconnectCause(DisconnectCause.REJECTED))
|
val reason = call.reason
|
||||||
|
Log.i("[Telecom Connection Service] Call ended with reason: $reason")
|
||||||
|
connection.setDisconnected(DisconnectCause(DisconnectCause.LOCAL))
|
||||||
connection.destroy()
|
connection.destroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -50,13 +50,17 @@ class TelecomHelper private constructor(context: Context) {
|
||||||
private var account: PhoneAccount = initPhoneAccount(context)
|
private var account: PhoneAccount = initPhoneAccount(context)
|
||||||
|
|
||||||
private val listener: CoreListenerStub = object : CoreListenerStub() {
|
private val listener: CoreListenerStub = object : CoreListenerStub() {
|
||||||
override fun onFirstCallStarted(core: Core) {
|
override fun onCallStateChanged(
|
||||||
val call = core.calls.firstOrNull()
|
core: Core,
|
||||||
call ?: return
|
call: Call,
|
||||||
|
state: Call.State?,
|
||||||
|
message: String
|
||||||
|
) {
|
||||||
|
Log.i("[Telecom Helper] Call state changed: ${call.state}")
|
||||||
|
|
||||||
if (call.dir == Call.Dir.Incoming) {
|
if (call.dir == Call.Dir.Incoming && call.state == Call.State.IncomingReceived) {
|
||||||
onIncomingCall(call)
|
onIncomingCall(call)
|
||||||
} else {
|
} else if (call.dir == Call.Dir.Outgoing && call.state == Call.State.OutgoingProgress) {
|
||||||
onOutgoingCall(call)
|
onOutgoingCall(call)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -72,6 +76,20 @@ class TelecomHelper private constructor(context: Context) {
|
||||||
Log.i("[Telecom Helper] Destroyed")
|
Log.i("[Telecom Helper] Destroyed")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun isIncomingCallPermitted(): Boolean {
|
||||||
|
val incomingCallPermitted = telecomManager.isIncomingCallPermitted(account.accountHandle)
|
||||||
|
Log.i("[Telecom Helper] Is incoming call permitted? $incomingCallPermitted")
|
||||||
|
return incomingCallPermitted
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressLint("MissingPermission")
|
||||||
|
fun isInManagedCall(): Boolean {
|
||||||
|
// Don't use telecomManager.isInCall as our own self-managed calls will be considered!
|
||||||
|
val isInManagedCall = telecomManager.isInManagedCall
|
||||||
|
Log.i("[Telecom Helper] Is in managed call? $isInManagedCall")
|
||||||
|
return isInManagedCall
|
||||||
|
}
|
||||||
|
|
||||||
fun isAccountEnabled(): Boolean {
|
fun isAccountEnabled(): Boolean {
|
||||||
val enabled = account.isEnabled
|
val enabled = account.isEnabled
|
||||||
Log.i("[Telecom Helper] Is account enabled ? $enabled")
|
Log.i("[Telecom Helper] Is account enabled ? $enabled")
|
||||||
|
|
Loading…
Reference in a new issue