From a89bde933dd9d587635a7c8cc454cc38706f3d93 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Wed, 18 Jan 2017 09:50:22 -0500 Subject: [PATCH] Respect silent-switch pre-CallKit 8 Cases considered: (Silent Switch toggled vs. Silent Switch not-toggled) x (App in Foreground vs. App in Background) x (CallKit vs. NonCallKit) CallKit already does the "right thing" // FREEBIE --- Signal/src/Signal-Bridging-Header.h | 1 + Signal/src/environment/NotificationsManager.m | 3 ++- .../view controllers/CallViewController.swift | 25 +++++++++++-------- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/Signal/src/Signal-Bridging-Header.h b/Signal/src/Signal-Bridging-Header.h index aadc349fd..95b58ce4e 100644 --- a/Signal/src/Signal-Bridging-Header.h +++ b/Signal/src/Signal-Bridging-Header.h @@ -22,6 +22,7 @@ #import "UIFont+OWS.h" #import "UIUtil.h" #import "UIView+OWS.h" +#import #import #import #import diff --git a/Signal/src/environment/NotificationsManager.m b/Signal/src/environment/NotificationsManager.m index 627e803ff..348036bee 100644 --- a/Signal/src/environment/NotificationsManager.m +++ b/Signal/src/environment/NotificationsManager.m @@ -94,7 +94,8 @@ UILocalNotification *notification = [UILocalNotification new]; notification.category = PushManagerCategoriesIncomingCall; - notification.soundName = @"r.caf"; + // Rather than using notification sounds, we control the ringtone and repeat vibrations with the CallAudioManager. + // notification.soundName = @"r.caf"; NSString *localCallId = call.localId.UUIDString; notification.userInfo = @{ PushManagerUserInfoKeysLocalCallId : localCallId }; diff --git a/Signal/src/view controllers/CallViewController.swift b/Signal/src/view controllers/CallViewController.swift index c555531c6..d4f3137f6 100644 --- a/Signal/src/view controllers/CallViewController.swift +++ b/Signal/src/view controllers/CallViewController.swift @@ -11,6 +11,11 @@ import PromiseKit private let TAG = "[CallAudioService]" private var vibrateTimer: Timer? private let audioManager = AppAudioManager.sharedInstance() + private let soundPlayer = JSQSystemSoundPlayer.shared()! + + enum SoundFilenames: String { + case incomingRing = "r" + } // Mark: Vibration config private let vibrateRepeatDuration = 1.6 @@ -59,9 +64,9 @@ import PromiseKit private func handleLocalRinging() { Logger.debug("\(TAG) \(#function)") - audioManager.setAudioEnabled(true) - audioManager.handleInboundRing() - vibrateTimer = Timer.scheduledTimer(timeInterval: vibrateRepeatDuration, target: self, selector: #selector(vibrate), userInfo: nil, repeats: true) + + vibrateTimer = Timer.scheduledTimer(timeInterval: vibrateRepeatDuration, target: self, selector: #selector(ringVibration), userInfo: nil, repeats: true) + soundPlayer.playSound(withFilename: SoundFilenames.incomingRing.rawValue, fileExtension: kJSQSystemSoundTypeCAF) } private func handleConnected() { @@ -96,18 +101,18 @@ import PromiseKit // MARK: Helpers private func stopRinging() { - // Disables external speaker used for ringing, unless user enables speakerphone. - audioManager.setDefaultAudioProfile() - audioManager.cancelAllAudio() - vibrateTimer?.invalidate() vibrateTimer = nil + + soundPlayer.stopSound(withFilename: SoundFilenames.incomingRing.rawValue) } - public func vibrate() { - AudioServicesPlaySystemSound(kSystemSoundID_Vibrate) + public func ringVibration() { + // Since a call notification is more urgent than a message notifaction, we + // vibrate twice, like a pulse, to differentiate from a normal notification vibration. + soundPlayer.playVibrateSound() DispatchQueue.default.asyncAfter(deadline: DispatchTime.now() + pulseDuration) { - AudioServicesPlaySystemSound(kSystemSoundID_Vibrate) + self.soundPlayer.playVibrateSound() } } }