From 9445cf99fd8d6e76960d8d8ca99ab04299bb56bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Wed, 22 May 2019 16:43:23 +0200 Subject: [PATCH] Revert "ios: remove no longer needed code" This reverts commit 603d16178859953a5ee7c88c6172a88d2e22d87f. --- ios/sdk/src/callkit/JMCallKitEmitter.swift | 25 +++++++++++++++++++--- ios/sdk/src/callkit/JMCallKitProxy.swift | 9 ++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/ios/sdk/src/callkit/JMCallKitEmitter.swift b/ios/sdk/src/callkit/JMCallKitEmitter.swift index 6012d2a79..e35fa0d71 100644 --- a/ios/sdk/src/callkit/JMCallKitEmitter.swift +++ b/ios/sdk/src/callkit/JMCallKitEmitter.swift @@ -21,6 +21,7 @@ import Foundation internal final class JMCallKitEmitter: NSObject, CXProviderDelegate { private let listeners = NSMutableArray() + private var pendingMuteActions = Set() internal override init() {} @@ -36,6 +37,12 @@ internal final class JMCallKitEmitter: NSObject, CXProviderDelegate { listeners.remove(listener) } + // MARK: - Add mute action + + func addMuteAction(_ actionUUID: UUID) { + pendingMuteActions.insert(actionUUID) + } + // MARK: - CXProviderDelegate func providerDidReset(_ provider: CXProvider) { @@ -43,6 +50,7 @@ internal final class JMCallKitEmitter: NSObject, CXProviderDelegate { let listener = $0 as! JMCallKitListener listener.providerDidReset?() } + pendingMuteActions.removeAll() } func provider(_ provider: CXProvider, perform action: CXAnswerCallAction) { @@ -64,9 +72,20 @@ internal final class JMCallKitEmitter: NSObject, CXProviderDelegate { } func provider(_ provider: CXProvider, perform action: CXSetMutedCallAction) { - listeners.forEach { - let listener = $0 as! JMCallKitListener - listener.performSetMutedCall?(UUID: action.callUUID, isMuted: action.isMuted) + let uuid = pendingMuteActions.remove(action.uuid) + + // Avoid mute actions ping-pong: if the mute action was caused by + // the JS side (we requested a transaction) don't call the delegate + // method. If it was called by the provder itself (when the user presses + // the mute button in the CallKit view) then call the delegate method. + // + // NOTE: don't try to be clever and remove this. Been there, done that. + // Won't work. + if (uuid == nil) { + listeners.forEach { + let listener = $0 as! JMCallKitListener + listener.performSetMutedCall?(UUID: action.callUUID, isMuted: action.isMuted) + } } action.fulfill() diff --git a/ios/sdk/src/callkit/JMCallKitProxy.swift b/ios/sdk/src/callkit/JMCallKitProxy.swift index b1e4b2953..ab0a8016a 100644 --- a/ios/sdk/src/callkit/JMCallKitProxy.swift +++ b/ios/sdk/src/callkit/JMCallKitProxy.swift @@ -160,6 +160,14 @@ import Foundation completion: @escaping (Error?) -> Swift.Void) { guard enabled else { return } + // XXX keep track of muted actions to avoid "ping-pong"ing. See + // JMCallKitEmitter for details on the CXSetMutedCallAction handling. + for action in transaction.actions { + if (action as? CXSetMutedCallAction) != nil { + emitter.addMuteAction(action.uuid) + } + } + callController.request(transaction, completion: completion) } @@ -187,3 +195,4 @@ import Foundation return update } } +