feat(healthchat): ring calls via gateway VoIP push

CallKit rejects the NSE hand-off without the filtering entitlement.
This commit is contained in:
Sedat Qaja
2026-10-12 11:42:53 +02:00
parent 71abd2097e
commit 2d86c4b92c
4 changed files with 230 additions and 607 deletions
@@ -15,6 +15,27 @@ import Foundation
import Macros
import SwiftUI
/// Common settings between app and NSE
nonisolated protocol CommonSettingsProtocol: AnyObject, Sendable {
var lastNotificationBootTime: TimeInterval? { get set }
var selectedNotificationTone: NotificationTone? { get set }
var logLevel: LogLevel { get }
var traceLogPacks: Set<TraceLogPack> { get }
var bugReportRageshakeURL: RemotePreference<RageshakeConfiguration> { get }
var contentScannerURL: RemotePreference<URL?> { get }
var forceDisableE2EE: RemotePreference<Bool> { get }
var mapTilerConfiguration: RemotePreference<MapTilerConfiguration> { get }
var enableOnlySignedDeviceIsolationMode: Bool { get }
var threadsEnabled: Bool { get }
var hideQuietNotificationAlerts: Bool { get }
var voipPusherAppID: String { get }
var pushKitDeviceToken: String? { get }
var voipPushGatewayNotifyURL: String? { get }
}
nonisolated enum AppBuildType {
case debug
case nightly
@@ -51,10 +72,15 @@ final nonisolated class AppSettings: @unchecked Sendable {
store.reset()
}
func resetSessionSpecificSettings() {
MXLog.warning("Resetting the user session specific AppSettings.")
resetHasRunIdentityConfirmationOnboarding()
}
// MARK: - Hooks
// swiftlint:disable:next function_parameter_count
func override(accountProviders: [AccountProvider],
func override(accountProviders: [String],
allowOtherAccountProviders: Bool,
hideBrandChrome: Bool,
pushGatewayBaseURL: URL,
@@ -115,15 +141,11 @@ final nonisolated class AppSettings: @unchecked Sendable {
@UserPreference(defaultValue: true)
var hasSeenNewSoundBanner: Bool
/// Defaults to `true` so new users never see it. Developer options arms it for now, an upgrade migration will at launch.
@UserPreference(defaultValue: true)
var hasSeenMultiAccountAnnouncement: Bool
/// The initial set of account providers shown to the user in the authentication flow.
///
/// Account provider is the friendly term for the server name. It should not contain an `https` prefix and should
/// match the last part of the user ID. For example `example.com` and not `https://matrix.example.com`.
private(set) var accountProviders: [AccountProvider] = [.managed(serverName: "matrix.org", baseURL: "https://matrix-client.matrix.org")]
private(set) var accountProviders = ["matrix.org"]
/// Whether or not the user is allowed to manually enter their own account provider or must select from one of `defaultAccountProviders`.
private(set) var allowOtherAccountProviders = true
/// Whether the components surrounding the app brand/logo should be hidden or not
@@ -131,8 +153,6 @@ final nonisolated class AppSettings: @unchecked Sendable {
/// The task identifier used for background app refresh. Also used in main target's the Info.plist
let backgroundAppRefreshTaskIdentifier = "io.element.elementx.background.refresh"
/// The task identifier used for the search index backfill. Also used in main target's the Info.plist
let searchBackfillTaskIdentifier = "io.element.elementx.background.search-backfill"
/// A URL where users can go read more about the app.
private(set) var websiteURL: URL = "https://element.io"
@@ -170,12 +190,8 @@ final nonisolated class AppSettings: @unchecked Sendable {
@UserPreference(key: "previousServers", defaultValue: [])
var previousServers: [String]
var defaultAccountProvider: AccountProvider {
if allowOtherAccountProviders {
previousServers.first.map { .generic($0) } ?? accountProviders[0]
} else {
accountProviders[0]
}
var defaultServer: String {
previousServers.first ?? accountProviders[0]
}
// MARK: - Security
@@ -232,6 +248,34 @@ final nonisolated class AppSettings: @unchecked Sendable {
pushGatewayBaseURL.appending(path: "_matrix/push/v1/notify")
}
// HealthChat: the NSE lacks the notification filtering entitlement, so it cannot hand ringing
// calls to CallKit directly. Instead it asks the push gateway for a PushKit (VoIP) push, the
// way the legacy element-ios app did. The app persists what the NSE needs for that request
// here, because the NSE neither receives PushKit credentials nor runs the app settings hook.
/// The push gateway app ID that delivers PushKit pushes (`push_type: voip` in Sygnal).
var voipPusherAppID: String {
#if DEBUG
InfoPlistReader.main.baseBundleIdentifier + ".ios.voip.dev"
#else
InfoPlistReader.main.baseBundleIdentifier + ".ios.voip.prod"
#endif
}
/// The device's PushKit token, base64 encoded like the APNs pushkey.
@UserPreference
var pushKitDeviceToken: String?
/// `pushGatewayNotifyEndpoint` as configured in the app, for the NSE.
@UserPreference
var voipPushGatewayNotifyURL: String?
@UserPreference(defaultValue: true)
var enableNotifications: Bool
@UserPreference(defaultValue: true)
var enableInAppNotifications: Bool
@UserPreference(defaultValue: false)
var hideQuietNotificationAlerts: Bool
@@ -243,9 +287,9 @@ final nonisolated class AppSettings: @unchecked Sendable {
@UserPreference
var lastNotificationBootTime: TimeInterval?
/// The app icon badge value the app last computed from the SDK's unread notification counts.
@UserPreference(defaultValue: 0)
var lastKnownBadgeCount: Int
/// The sound played when delivering noisy notifications. If nil, use the ElementX default
@UserPreference
var selectedNotificationTone: NotificationTone?
// MARK: - Logging
@@ -304,6 +348,9 @@ final nonisolated class AppSettings: @unchecked Sendable {
@UserPreference(defaultValue: false)
var hasRunNotificationPermissionsOnboarding: Bool
@UserPreference(defaultValue: false)
var hasRunIdentityConfirmationOnboarding: Bool
@UserPreference(defaultValue: false)
var hasRequestedLocationAlwaysLocationAuthorization: Bool
@@ -321,6 +368,14 @@ final nonisolated class AppSettings: @unchecked Sendable {
@UserPreference(defaultValue: false)
var liveLocationDisclaimerDisplayed: Bool
// MARK: - Home Screen
@UserPreference(defaultValue: RoomListActivityVisibility.current)
var roomListActivityVisibility: RoomListActivityVisibility
@UserPreference(defaultValue: false)
var roomListNotificationCountEnabled: Bool
// MARK: - Room Screen
@UserPreference(defaultValue: AppBuildType.current == .debug)
@@ -367,6 +422,11 @@ final nonisolated class AppSettings: @unchecked Sendable {
/// The MapTiler configuration used to build map URLs, which defaults to the bundled one.
private(set) var mapTilerConfiguration = RemotePreference(AppSettings.bundledMapTilerConfiguration)
// MARK: - Presence
@UserPreference(defaultValue: true)
var sharePresence: Bool
// MARK: - Feature Flags
/// Others
@@ -392,7 +452,7 @@ final nonisolated class AppSettings: @unchecked Sendable {
@UserPreference(defaultValue: false)
var messageMultiSelectEnabled: Bool
@UserPreference(key: "globalSearchEnabledV2", defaultValue: true)
@UserPreference(defaultValue: ProcessInfo().isiOSAppOnMac)
var globalSearchEnabled: Bool
@UserPreference(defaultValue: false)
@@ -412,32 +472,22 @@ final nonisolated class AppSettings: @unchecked Sendable {
@UserPreference(defaultValue: false)
var linkNewDeviceEnabled: Bool
@UserPreference(defaultValue: false)
var automaticBackPaginationEnabled: Bool
@UserPreference(key: "clientPausingAndResumingEnabledV2", defaultValue: false, volatile: true)
var clientPausingAndResumingEnabled: Bool
@UserPreference(defaultValue: AppBuildType.current != .release)
var developerOptionsEnabled: Bool
/// Runs calls through the native matrix-rust-rtc stack instead of the Element Call web view.
@UserPreference(defaultValue: false)
var nativeCallEnabled: Bool
@UserPreference(defaultValue: false)
var multiAccountEnabled: Bool
/// Lets the add account flow start once `multiAccountEnabled` shows its entry points. Not a user preference, so testers
/// can't add a second account while signing out of one isn't safe yet. Only set it to `true` locally, for development.
let addAccountFlowEnabled = false
init(store: UserDefaultsProtocol) {
self.store = store
}
func userSettings(for userID: String) -> UserSettings {
UserSettings(appSettings: self, accountSettings: .init(userID: userID, store: store))
}
static func volatile() -> AppSettings {
AppSettings(store: VolatileUserDefaults())
}
}
nonisolated extension AppSettings: CommonSettingsProtocol { }
@@ -9,7 +9,6 @@
import AVFoundation
import CallKit
import Combine
import ElementCall
import Foundation
import MatrixRustSDK
import PushKit
@@ -27,14 +26,13 @@ class ElementCallService: NSObject, ElementCallServiceProtocol, PKPushRegistryDe
let roomID: String
let rtcNotificationID: String?
let isVoiceCall: Bool
var isNative = false
}
private let appSettings: AppSettings
private let pushRegistry: PKPushRegistry
private let callController: CXCallControllerProtocol
private let callController = CXCallController()
private let callProvider: CXProviderProtocol
private let timeProvider: TimeProvider
private let appSettings: AppSettings
private weak var clientProxy: ClientProxyProtocol? {
didSet {
@@ -54,10 +52,6 @@ class ElementCallService: NSObject, ElementCallServiceProtocol, PKPushRegistryDe
private var endUnansweredCallTask: Task<Void, Never>?
/// A call answered for the native stack, until the stack takes it over.
private var answeredNativeCallID: CallID?
private var endPendingCallTask: Task<Void, Never>?
private var ongoingCallID: CallID? {
didSet { ongoingCallRoomIDSubject.send(ongoingCallID?.roomID) }
}
@@ -74,26 +68,11 @@ class ElementCallService: NSObject, ElementCallServiceProtocol, PKPushRegistryDe
private var declineListenerHandle: TaskHandle?
/// The native call stack, for as long as there's a session running calls through it.
private var nativeCallStack: NativeCallStack?
private var nativeCallCancellable: AnyCancellable?
private var mediaProvider: MediaProviderProtocol?
var nativeCallController: ElementCallController? {
nativeCallStack?.controller
}
private var cancellables = Set<AnyCancellable>()
init(appSettings: AppSettings,
callProvider: CXProviderProtocol? = nil,
callController: CXCallControllerProtocol? = nil,
timeProvider: TimeProvider? = nil) {
self.appSettings = appSettings
self.callController = callController ?? CXCallController()
init(callProvider: CXProviderProtocol? = nil, timeProvider: TimeProvider? = nil, appSettings: AppSettings) {
pushRegistry = PKPushRegistry(queue: nil)
self.timeProvider = timeProvider ?? TimeProvider(clock: ContinuousClock(), now: Date.init)
self.appSettings = appSettings
if let callProvider {
self.callProvider = callProvider
@@ -118,145 +97,45 @@ class ElementCallService: NSObject, ElementCallServiceProtocol, PKPushRegistryDe
pushRegistry.desiredPushTypes = [.voIP]
self.callProvider.setDelegate(self, queue: nil)
appSettings.nativeCallEnabledPublisher
.sink { [weak self] _ in
self?.updateNativeCallStack()
}
.store(in: &cancellables)
}
func setUserSession(_ userSession: UserSessionProtocol?) {
// A stack is built against one client proxy and can't be handed to another, which is what a
// soft logout or a cache clear brings: a new session without the sign out that drops this one.
if userSession?.clientProxy !== clientProxy {
stopNativeCallStack()
}
clientProxy = userSession?.clientProxy
mediaProvider = userSession?.mediaProvider
updateNativeCallStack()
func setClientProxy(_ clientProxy: any ClientProxyProtocol) {
self.clientProxy = clientProxy
}
@discardableResult
func handleNativeCallRequest(roomProxy: JoinedRoomProxyProtocol, isVoiceCall: Bool) -> Bool {
guard let nativeCallStack else {
return false
}
nativeCallStack.handleCallRequest(roomProxy: roomProxy, isVoiceCall: isVoiceCall)
return true
}
func minimizeNativeCall() {
nativeCallStack?.minimize()
}
func restoreNativeCall() {
nativeCallStack?.restore()
}
func setupCallSession(roomID: String, roomDisplayName: String, isVideo: Bool) async {
// Unlike the push handling below, this always runs with a session, so the stack itself is
// the answer rather than the setting that would have built one.
let isNative = nativeCallStack != nil
MXLog.info("Setting up a call session, native: \(isNative), video: \(isVideo), replacing an ongoing call: \(ongoingCallID != nil)")
func setupCallSession(roomID: String, roomDisplayName: String) async {
// Drop any ongoing calls when starting a new one
if ongoingCallID != nil {
tearDownCallSession(sendEndCallAction: true)
tearDownCallSession()
}
// If this starting from a ring reuse those identifiers
// Make sure the roomID matches
var callID = if let incomingCallID, incomingCallID.roomID == roomID {
let callID = if let incomingCallID, incomingCallID.roomID == roomID {
incomingCallID
} else {
CallID(callKitID: UUID(), roomID: roomID, rtcNotificationID: nil, isVoiceCall: !isVideo)
CallID(callKitID: UUID(), roomID: roomID, rtcNotificationID: nil, isVoiceCall: false)
}
callID.isNative = isNative
let isAnsweringIncomingCall = callID.rtcNotificationID != nil
clearIncomingCallState()
ongoingCallID = callID
// A web view call must not be tracked by CallKit, as that gives this process exclusive
// access to media and the web view runs in another (see `CXAnswerCallAction`).
// Don't bother starting another CallKit session as it won't work properly
// https://developer.apple.com/forums//thread/767949?answerId=812951022#812951022
guard isNative else {
return
}
// Answering the ringing call: it is already reported, keep it.
guard !isAnsweringIncomingCall else {
return
}
// let handle = CXHandle(type: .generic, value: roomDisplayName)
// let startCallAction = CXStartCallAction(call: callID.callKitID, handle: handle)
// startCallAction.isVideo = true
let handle = CXHandle(type: .generic, value: roomID)
let startCallAction = CXStartCallAction(call: callID.callKitID, handle: handle)
startCallAction.contactIdentifier = roomDisplayName
startCallAction.isVideo = isVideo
// Awaited because our membership goes out straight after: a peer that sees it before the
// system knows about the call reads the call as answered elsewhere.
await requestTransaction(CXTransaction(action: startCallAction), describedAs: "start call")
// Without this the system call UI shows the handle, i.e. the room ID.
let update = CXCallUpdate()
update.localizedCallerName = roomDisplayName
update.remoteHandle = handle
update.hasVideo = isVideo
callProvider.reportCall(with: callID.callKitID, updated: update)
// do {
// try await callController.request(CXTransaction(action: startCallAction))
// } catch {
// MXLog.error("Failed requesting start call action with error: \(error)")
// }
}
func reportCallSessionConnected(roomID: String) {
guard let ongoingCallID, ongoingCallID.roomID == roomID else {
// The system ends an outgoing call that never reports connecting, so a silent guard
// here shows up much later as a call that hangs itself up.
MXLog.warning("Not reporting the call as connected, no matching ongoing call")
return
}
guard ongoingCallID.isNative else {
MXLog.info("Not reporting a web view call as connected, CallKit doesn't track it")
return
}
MXLog.info("Reporting the call as connected")
callProvider.reportOutgoingCall(with: ongoingCallID.callKitID, connectedAt: nil)
}
func tearDownCallSession(roomID: String) {
if ongoingCallID?.roomID == roomID {
tearDownCallSession(sendEndCallAction: true)
return
}
// Answered and handed over, but the stack couldn't take it. Ending it here rather than
// waiting for the watchdog takes the system call UI away straight away.
if answeredNativeCallID?.roomID == roomID {
MXLog.warning("Ending an answered call the native call stack didn't take over")
endPendingCall()
return
}
MXLog.info("Not tearing down the call session, no call for room \(roomID)")
}
func reportCallSessionEnded(roomID: String, reason: CXCallEndedReason) {
guard let ongoingCallID, ongoingCallID.roomID == roomID else {
MXLog.info("Not reporting the end of a call for \(roomID), it isn't the ongoing call")
return
}
// A web view call is not tracked by CallKit, as in `reportCallSessionConnected`.
if ongoingCallID.isNative {
MXLog.info("Reporting the call as ended without us: \(reason.rawValue)")
callProvider.reportCall(with: ongoingCallID.callKitID, endedAt: nil, reason: reason)
}
self.ongoingCallID = nil
func tearDownCallSession() {
tearDownCallSession(sendEndCallAction: true)
}
func setAudioEnabled(_ enabled: Bool, roomID: String) {
@@ -278,60 +157,18 @@ class ElementCallService: NSObject, ElementCallServiceProtocol, PKPushRegistryDe
}
}
// MARK: - Native calls
/// Builds or releases the call stack to match the session and the setting.
private func updateNativeCallStack() {
guard appSettings.nativeCallEnabled, let clientProxy else {
stopNativeCallStack()
return
}
guard nativeCallStack == nil else { return }
guard let transport = clientProxy.makeNativeCallTransport() else {
MXLog.error("Cannot start the native call stack without a transport")
return
}
let style = ElementCallStyle(theme: NativeCallTheme(),
icons: NativeCallIcons(),
avatars: NativeCallAvatars(mediaProvider: mediaProvider),
strings: .init(you: L10n.commonYou,
error: L10n.commonError,
stop: L10n.actionStop,
back: L10n.actionBack))
// The stats overlay is raw RTP counters, so it follows whatever already reveals developer
// surface. Screen sharing stays off until the broadcast extension that makes it work lands.
let options = ElementCallOptions(isDeveloperModeEnabled: appSettings.developerOptionsEnabled)
let stack = NativeCallStack(transport: transport,
system: NativeCallSystem(service: self),
options: options,
style: style)
nativeCallStack = stack
nativeCallCancellable = stack.actions
.sink { [weak self] presentation in
self?.actionsSubject.send(.nativeCall(presentation))
}
Task { await stack.start() }
}
private func stopNativeCallStack() {
guard let nativeCallStack else { return }
self.nativeCallStack = nil
nativeCallCancellable = nil
nativeCallStack.stop()
actionsSubject.send(.nativeCall(.dismiss))
}
// MARK: - PKPushRegistryDelegate
func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) { }
func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
// HealthChat: stored for the NSE, which requests a VoIP push through the push gateway
// for ringing calls as long as the notification filtering entitlement is missing.
appSettings.pushKitDeviceToken = pushCredentials.token.base64EncodedString()
appSettings.voipPushGatewayNotifyURL = appSettings.pushGatewayNotifyEndpoint.absoluteString
}
func pushRegistry(_ registry: PKPushRegistry, didInvalidatePushTokenFor type: PKPushType) {
appSettings.pushKitDeviceToken = nil
}
func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) {
guard let roomID = payload.dictionaryPayload[ElementCallServiceNotificationKey.roomID.rawValue] as? String else {
@@ -363,7 +200,7 @@ class ElementCallService: NSObject, ElementCallServiceProtocol, PKPushRegistryDe
let callID = CallID(callKitID: UUID(), roomID: roomID, rtcNotificationID: rtcNotificationID, isVoiceCall: isVoiceCall)
incomingCallID = callID
guard let expirationDate = (payload.dictionaryPayload[ElementCallServiceNotificationKey.expirationDate.rawValue] as? Date) else {
guard let expirationDate = Self.expirationDate(from: payload.dictionaryPayload) else {
MXLog.error("Something went wrong, missing expiration timestamp for incoming voip call: \(payload)")
assertionFailure("Incoming voip push is missing the expiration timestamp")
clearIncomingCallState()
@@ -389,7 +226,7 @@ class ElementCallService: NSObject, ElementCallServiceProtocol, PKPushRegistryDe
// If not for audio call the app will not be put to foreground and the webview won't be able to handle the call...
// Consequence: The call will be presented to the user as a video call in CallKit UI,
// but once Element Call is launched it will correctly route to a voice-only call.
update.hasVideo = appSettings.nativeCallEnabled ? !isVoiceCall : true
update.hasVideo = true
update.localizedCallerName = roomDisplayName
// https://stackoverflow.com/a/41230020/730924
update.remoteHandle = .init(type: .generic, value: roomID)
@@ -427,19 +264,10 @@ class ElementCallService: NSObject, ElementCallServiceProtocol, PKPushRegistryDe
func provider(_ provider: CXProvider, didActivate audioSession: AVAudioSession) {
MXLog.info("Call provider did activate audio session")
actionsSubject.send(.audioSessionActivated)
}
func provider(_ provider: CXProvider, didDeactivate audioSession: AVAudioSession) {
MXLog.info("Call provider did deactivate audio session")
actionsSubject.send(.audioSessionDeactivated)
}
func provider(_ provider: CXProvider, perform action: CXStartCallAction) {
// The native controller configured the audio session already; CallKit activates it and
// reports back through `didActivate`.
action.fulfill()
callProvider.reportOutgoingCall(with: action.callUUID, startedConnectingAt: nil)
}
func providerDidReset(_ provider: CXProvider) {
@@ -470,15 +298,6 @@ class ElementCallService: NSObject, ElementCallServiceProtocol, PKPushRegistryDe
// First fullfill the action
action.fulfill()
if appSettings.nativeCallEnabled {
// The native stack owns media in this process, so the CallKit call stays up: the
// controller attaches to it in `setupCallSession`.
endUnansweredCallTask?.cancel()
endCallIfLeftPending(incomingCallID)
actionsSubject.send(.startCall(roomID: incomingCallID.roomID, isVoiceCall: incomingCallID.isVoiceCall))
return
}
// And delay ending the call so that the app has enough time
// to get deeplinked into
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
@@ -494,7 +313,7 @@ class ElementCallService: NSObject, ElementCallServiceProtocol, PKPushRegistryDe
if let ongoingCallID {
actionsSubject.send(.setAudioEnabled(!action.isMuted, roomID: ongoingCallID.roomID))
} else {
MXLog.info("Ignoring a mute action without an ongoing call")
MXLog.error("Failed muting/unmuting call, missing ongoingCallID")
}
action.fulfill()
@@ -505,56 +324,30 @@ class ElementCallService: NSObject, ElementCallServiceProtocol, PKPushRegistryDe
// This gets called for no reason on simulators, where CallKit
// isn't even supported, ignore it.
#else
endCall(withCallKitID: action.callUUID)
if let ongoingCallID {
actionsSubject.send(.endCall(roomID: ongoingCallID.roomID))
}
if let incomingCallID {
Task {
await sendDeclineCallEvent(incomingCallID)
}
}
if incomingCallID?.callKitID == action.callUUID {
clearIncomingCallState()
}
tearDownCallSession(sendEndCallAction: false)
action.fulfill()
#endif
}
/// Ends the call the system identified, whether that's the ongoing one, the ringing one, or
/// neither because it has already been replaced.
///
/// Kept out of the delegate method so that it's reachable on the simulator, where the provider
/// performs end call actions of its own and the delegate has to ignore them.
func endCall(withCallKitID callKitID: UUID) {
MXLog.info("Ending the call for \(callKitID), ongoing: \(ongoingCallID != nil), incoming: \(incomingCallID != nil)")
// Both branches are gated on the identifier. Leaving a call to start one in another room
// requests an end call action for the old call and replaces `ongoingCallID` before the
// system gets round to performing it, so an ungated handler reads it as the new call ending
// and hangs up the call that was just started.
if let ongoingCallID, ongoingCallID.callKitID == callKitID {
actionsSubject.send(.endCall(roomID: ongoingCallID.roomID))
tearDownCallSession(sendEndCallAction: false)
}
if let incomingCallID, incomingCallID.callKitID == callKitID {
Task {
await sendDeclineCallEvent(incomingCallID)
}
clearIncomingCallState()
}
}
// MARK: - Private
/// Bridges the controller's completion handler, which is all ``CXCallControllerProtocol`` can
/// carry, back to an awaitable call.
private func requestTransaction(_ transaction: CXTransaction, describedAs description: String) async {
await withCheckedContinuation { continuation in
callController.request(transaction) { error in
if let error {
MXLog.error("Failed requesting \(description) action with error: \(error)")
}
continuation.resume()
}
}
}
private func tearDownCallSession(sendEndCallAction: Bool = true) {
if sendEndCallAction, let ongoingCallID {
MXLog.info("Requesting an end call action for the ongoing call")
let transaction = CXTransaction(action: CXEndCallAction(call: ongoingCallID.callKitID))
callController.request(transaction) { error in
if let error {
@@ -571,6 +364,19 @@ class ElementCallService: NSObject, ElementCallServiceProtocol, PKPushRegistryDe
/// a call and immediately end it. `callerInfo` names the call after the room so the brief
/// system UI and the Recents entry show the room rather than "Unknown", and the entry can be
/// tapped to call back.
/// CallKit hands the NSE's payload over as-is, so the expiration arrives as a `Date`. A VoIP push
/// requested from the push gateway travels as JSON, where the same key is seconds since 1970.
private static func expirationDate(from payload: [AnyHashable: Any]) -> Date? {
switch payload[ElementCallServiceNotificationKey.expirationDate.rawValue] {
case let date as Date:
date
case let secondsSince1970 as TimeInterval:
Date(timeIntervalSince1970: secondsSince1970)
default:
nil
}
}
private func reportImmediatelyEndedCall(reason: CXCallEndedReason,
callerInfo: (roomID: String, roomDisplayName: String?)? = nil,
completion: @escaping () -> Void) {
@@ -599,32 +405,6 @@ class ElementCallService: NSObject, ElementCallServiceProtocol, PKPushRegistryDe
}
}
/// Ends an answered call that the native stack never took over, which nothing else would: the
/// call is deliberately left up for the controller to attach to in `setupCallSession`.
private func endCallIfLeftPending(_ callID: CallID) {
answeredNativeCallID = callID
endPendingCallTask = Task { [weak self] in
try? await self?.timeProvider.clock.sleep(for: .seconds(30))
guard let self, !Task.isCancelled, answeredNativeCallID == callID else {
return
}
MXLog.error("The native call stack never took over the answered call, ending it")
endPendingCall()
}
}
private func endPendingCall() {
guard let answeredNativeCallID else {
return
}
callProvider.reportCall(with: answeredNativeCallID.callKitID, endedAt: nil, reason: .failed)
clearIncomingCallState()
}
private func sendDeclineCallEvent(_ incomingCallID: CallID) async {
guard let rtcNotificationID = incomingCallID.rtcNotificationID else {
MXLog.info("No rtc notification event to decline.")
@@ -726,9 +506,6 @@ class ElementCallService: NSObject, ElementCallServiceProtocol, PKPushRegistryDe
private func clearIncomingCallState() {
endUnansweredCallTask?.cancel()
endUnansweredCallTask = nil
endPendingCallTask?.cancel()
endPendingCallTask = nil
answeredNativeCallID = nil
declineListenerHandle?.cancel()
declineListenerHandle = nil
incomingCallRoomInfoCancellable = nil