From 6230a10f90148bf6397fdab43bed9aef9ac7ccc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Hertault?= Date: Tue, 23 Feb 2021 15:16:41 +0100 Subject: [PATCH] Format code --- .../TwilioPhoneModule.kt | 4 +- ios/TwilioPhone.swift | 162 +++++++++--------- 2 files changed, 84 insertions(+), 82 deletions(-) diff --git a/android/src/main/java/com/reactnativetwiliophone/TwilioPhoneModule.kt b/android/src/main/java/com/reactnativetwiliophone/TwilioPhoneModule.kt index 4a769265..a4411cca 100644 --- a/android/src/main/java/com/reactnativetwiliophone/TwilioPhoneModule.kt +++ b/android/src/main/java/com/reactnativetwiliophone/TwilioPhoneModule.kt @@ -21,7 +21,7 @@ class TwilioPhoneModule(reactContext: ReactApplicationContext) : ReactContextBas private var callListener = callListener() - private var audioManager: AudioManager = reactContext.getSystemService(Context.AUDIO_SERVICE) as AudioManager; + private var audioManager: AudioManager = reactContext.getSystemService(Context.AUDIO_SERVICE) as AudioManager override fun getName(): String { return "TwilioPhone" @@ -172,7 +172,7 @@ class TwilioPhoneModule(reactContext: ReactApplicationContext) : ReactContextBas @ReactMethod fun toggleSpeaker(speakerOn: Boolean) { Log.i(tag, "Toggling speaker") - audioManager.setSpeakerphoneOn(speakerOn); + audioManager.isSpeakerphoneOn = speakerOn } @ReactMethod diff --git a/ios/TwilioPhone.swift b/ios/TwilioPhone.swift index c674afaf..4d1ca677 100644 --- a/ios/TwilioPhone.swift +++ b/ios/TwilioPhone.swift @@ -4,11 +4,11 @@ import TwilioVoice class TwilioPhone: RCTEventEmitter { var hasListeners = false var audioDevice = DefaultAudioDevice() - + var activeCallInvites: [String: CallInvite]! = [:] var activeCalls: [String: Call]! = [:] var activeCall: Call? - + override func supportedEvents() -> [String]! { return [ "CallInvite", @@ -26,95 +26,95 @@ class TwilioPhone: RCTEventEmitter { "UnregistrationFailure" ] } - + override func startObserving() { hasListeners = true } - + override func stopObserving() { hasListeners = false } - + @objc override static func requiresMainQueueSetup() -> Bool { return true } - + @objc(register:withDeviceToken:) func register(accessToken: String, deviceToken: String) { NSLog("[TwilioPhone] Registering") - + let tokenData = hexToData(str: deviceToken) - + TwilioVoiceSDK.register(accessToken: accessToken, deviceToken: tokenData) { error in if let error = error { NSLog("[TwilioPhone] An error occurred while registering: \(error.localizedDescription)") - + if self.hasListeners { self.sendEvent(withName: "RegistrationFailure", body: ["errorMessage": error.localizedDescription]) } } else { NSLog("[TwilioPhone] Successfully registered for VoIP push notifications") - + if self.hasListeners { self.sendEvent(withName: "RegistrationSuccess", body: nil) } } } } - + @objc(handleMessage:) func handleMessage(payload: [String: String]) { NSLog("[TwilioPhone] Handling message") - + TwilioVoiceSDK.handleNotification(payload, delegate: self, delegateQueue: nil) } - + @objc(acceptCallInvite:) func acceptCallInvite(callSid: String) { NSLog("[TwilioPhone] Accepting call invite") - + guard let callInvite = activeCallInvites[callSid] else { NSLog("[TwilioPhone] No call invite to be accepted") return } - + let call = callInvite.accept(with: self) - + activeCalls[callSid] = call activeCallInvites.removeValue(forKey: callSid) } - + @objc(rejectCallInvite:) func rejectCallInvite(callSid: String) { NSLog("[TwilioPhone] Rejecting call invite") - + guard let callInvite = activeCallInvites[callSid] else { NSLog("[TwilioPhone] No call invite to be rejected") return } - + callInvite.reject() - + activeCallInvites.removeValue(forKey: callSid) } - + @objc(disconnectCall:) func disconnectCall(callSid: String) { NSLog("[TwilioPhone] Disconnecting call") - + guard let call = activeCalls[callSid] else { NSLog("[TwilioPhone] No call to be disconnected") return } - + call.disconnect() } - + @objc(endCall:) func endCall(callSid: String) { NSLog("[TwilioPhone] Ending call") - + if let callInvite = activeCallInvites[callSid] { callInvite.reject() activeCallInvites.removeValue(forKey: callSid) @@ -124,31 +124,33 @@ class TwilioPhone: RCTEventEmitter { NSLog("[TwilioPhone] Unknown sid to perform end-call action with") } } - + @objc(toggleMuteCall:withMute:) func toggleMuteCall(callSid: String, mute: Bool) { NSLog("[TwilioPhone] Toggling mute call") - + guard let activeCall = activeCalls[callSid] else { return } - + activeCall.isMuted = mute } - + @objc(toggleHoldCall:withHold:) func toggleHoldCall(callSid: String, hold: Bool) { NSLog("[TwilioPhone] Toggling hold call") - + guard let activeCall = activeCalls[callSid] else { return } - + activeCall.isOnHold = hold } - + @objc(toggleSpeaker:) func toggleAudioRoute(speakerOn: Bool) { + NSLog("[TwilioPhone] Toggling speaker") + audioDevice.block = { DefaultAudioDevice.DefaultAVAudioSessionConfigurationBlock() do { @@ -161,79 +163,79 @@ class TwilioPhone: RCTEventEmitter { NSLog("[TwilioPhone] Failed to toggle speaker: \(error.localizedDescription)") } } - + audioDevice.block() } - + @objc(sendDigits:withDigits:) func sendDigits(callSid: String, digits: String) { NSLog("[TwilioPhone] Sending digits") - + guard let activeCall = activeCalls[callSid] else { return } - + activeCall.sendDigits(digits) } - + @objc(startCall:withParams:) func startCall(accessToken: String, params: [String: String]) { NSLog("[TwilioPhone] Starting call") - + let connectOptions = ConnectOptions(accessToken: accessToken) { builder in builder.params = params } - + let call = TwilioVoiceSDK.connect(options: connectOptions, delegate: self) - + activeCall = call } - + @objc(unregister:withDeviceToken:) func unregister(accessToken: String, deviceToken: String) { NSLog("[TwilioPhone] Unregistering") - + let tokenData = hexToData(str: deviceToken) - + TwilioVoiceSDK.unregister(accessToken: accessToken, deviceToken: tokenData) { error in if let error = error { NSLog("[TwilioPhone] An error occurred while unregistering: \(error.localizedDescription)") - + if self.hasListeners { self.sendEvent(withName: "UnregistrationFailure", body: ["errorMessage": error.localizedDescription]) } } else { NSLog("[TwilioPhone] Successfully unregistered from VoIP push notifications") - + if self.hasListeners { self.sendEvent(withName: "UnregistrationSuccess", body: nil) } } } } - + @objc func activateAudio() { NSLog("[TwilioPhone] Activating audio") - + audioDevice.isEnabled = true } - + @objc func deactivateAudio() { NSLog("[TwilioPhone] Deactivating audio") - + audioDevice.isEnabled = false } - + @objc(checkPermissions:) func checkPermissions(callback: RCTResponseSenderBlock) { NSLog("[TwilioPhone] Checking permissions") - + var permissions: [String: String] = [:] - + let permissionStatus = AVAudioSession.sharedInstance().recordPermission - + switch permissionStatus { case .granted: permissions["RECORD"] = "GRANTED" @@ -241,29 +243,29 @@ class TwilioPhone: RCTEventEmitter { permissions["RECORD"] = "DENIED" case .undetermined: permissions["RECORD"] = "UNDETERMINED" - + AVAudioSession.sharedInstance().requestRecordPermission { granted in NSLog("[TwilioPhone] Record permission granted: \(granted)") } default: permissions["RECORD"] = "UNKNOWN" } - + callback([permissions]) } - + func hexToData(str: String) -> Data { let len = str.count / 2 var data = Data(capacity: len) let ptr = str.cString(using: String.Encoding.utf8)! - + for i in 0..