diff --git a/Ring/Ring/AppDelegate.swift b/Ring/Ring/AppDelegate.swift index f17e5b05294841abf2ac3b90a2a2e377cbf788d1..a5859cb0f85ac7eaf6528995336ad9e8de6adaf8 100644 --- a/Ring/Ring/AppDelegate.swift +++ b/Ring/Ring/AppDelegate.swift @@ -428,7 +428,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD DispatchQueue.main.async {[weak self] in guard let self = self else { return } // If the app is running in the background and there are no waiting calls, the extension should handle the notification. - if UIApplication.shared.applicationState == .background && !self.presentingCallScreen && !self.callsProvider.hasPendingTransactions() { + if UIApplication.shared.applicationState == .background && !self.presentingCallScreen && !self.callsProvider.hasActiveCalls() { return } // emit signal that app is active for notification extension @@ -586,7 +586,7 @@ extension AppDelegate { Othervise it was called from Contacts app. We need find contact and start a call */ - if self.callsProvider.hasPendingTransactions() { return false } + if self.callsProvider.hasActiveCalls() { return false } guard let handle = userActivity.startCallHandle else { return false } @@ -671,7 +671,10 @@ extension AppDelegate: PKPushRegistryDelegate { let peerId: String = payload.dictionaryPayload["peerId"] as? String ?? "" let hasVideo = payload.dictionaryPayload["hasVideo"] as? String ?? "true" let displayName = payload.dictionaryPayload["displayName"] as? String ?? "" - callsProvider.previewPendingCall(peerId: peerId, withVideo: hasVideo.boolValue, displayName: displayName) { _ in + callsProvider.previewPendingCall(peerId: peerId, withVideo: hasVideo.boolValue, displayName: displayName) { error in + if error != nil { + self.updateCallScreenState(presenting: false) + } completion() } } diff --git a/Ring/Ring/Services/CallsProviderService.swift b/Ring/Ring/Services/CallsProviderService.swift index 825423ec8df4f318343becd08467d75e48a2c340..a75fab5a9dc63826366fd23f2079c47c54d14fef 100644 --- a/Ring/Ring/Services/CallsProviderService.swift +++ b/Ring/Ring/Services/CallsProviderService.swift @@ -59,6 +59,7 @@ class CallsProviderService: NSObject { private let responseStream = PublishSubject<ServiceEvent>() var sharedResponseStream: Observable<ServiceEvent> + var jamiCallUUIDs = Set<UUID>() init(provider: CXProvider, controller: CXCallController) { self.sharedResponseStream = responseStream.share() @@ -89,8 +90,16 @@ extension CallsProviderService { self.requestTransaction(transaction) } - func hasPendingTransactions() -> Bool { - return !self.callController.callObserver.calls.isEmpty + func hasActiveCalls() -> Bool { + let calls = self.callController.callObserver.calls + let jamiCalls = calls.filter { call in + !call.hasEnded && isJamiCall(call) + } + return !jamiCalls.isEmpty + } + + func isJamiCall(_ call: CXCall) -> Bool { + return jamiCallUUIDs.contains(call.uuid) } func handleIncomingCall(account: AccountModel, call: CallModel) { @@ -121,9 +130,9 @@ extension CallsProviderService { update.remoteHandle = CXHandle(type: handleType, value: handleInfo.handle) self.setUpCallUpdate(update: update, localizedCallerName: handleInfo.displayName, videoFlag: !call.isAudioOnly) self.provider.reportNewIncomingCall(with: call.callUUID, - update: update) { error in + update: update) { [weak self] error in if error == nil { - return + self?.jamiCallUUIDs.insert(call.callUUID) } completion?(error) } @@ -155,9 +164,9 @@ extension CallsProviderService { let unhandeledCall = UnhandeledCall(peerId: peerId) unhandeledCalls.insert(unhandeledCall) self.provider.reportNewIncomingCall(with: unhandeledCall.uuid, - update: update) { error in + update: update) { [weak self] error in if error == nil { - return + self?.jamiCallUUIDs.insert(unhandeledCall.uuid) } completion?(error) } @@ -287,6 +296,7 @@ extension CallsProviderService: CXProviderDelegate { defer { action.fulfill() } + jamiCallUUIDs.remove(action.callUUID) if let call = getUnhandeledCall(UUID: action.callUUID) { call.state = .declined return @@ -301,6 +311,7 @@ extension CallsProviderService: CXProviderDelegate { defer { action.fulfill() } + self.jamiCallUUIDs.insert(action.callUUID) /* To display correct name in call history create an update and report it to the provider. diff --git a/Ring/Ring/Services/ConversationsManager.swift b/Ring/Ring/Services/ConversationsManager.swift index 6fdd93168e39a11d996107e60df70bfcc1d4fde3..fceb5a81319c7a1a0f9475ebb95d601fb725bb84 100644 --- a/Ring/Ring/Services/ConversationsManager.swift +++ b/Ring/Ring/Services/ConversationsManager.swift @@ -150,7 +150,7 @@ class ConversationsManager { the notification extension should handle incoming notifications unless there is a pending call. */ func updateBackgroundState() { - if self.callsProvider.hasPendingTransactions() { return } + if self.callsProvider.hasActiveCalls() { return } if let appDelegate = UIApplication.shared.delegate as? AppDelegate { appDelegate.updateCallScreenState(presenting: false) }