From 483e6ec01eaf1d72fd11e980df7b4368d13606c0 Mon Sep 17 00:00:00 2001 From: Alexey Kuznetsov Date: Thu, 1 Dec 2016 18:01:23 +0100 Subject: [PATCH] Do not register account if sleeping Reachability callbacks can happen during Power Nap. As far as the app is concerned, the system sleeps between will-sleep and did-wake notifications and the app is not supposed to do any work during Power Nap. Issue #320 --- Telephone/AccountController.h | 6 ++++-- Telephone/AccountController.m | 8 ++++++-- Telephone/AppController.m | 11 ++++++++--- Telephone/CompositionRoot.swift | 3 +++ 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/Telephone/AccountController.h b/Telephone/AccountController.h index 2ae5d65f4..058683256 100644 --- a/Telephone/AccountController.h +++ b/Telephone/AccountController.h @@ -35,7 +35,7 @@ extern NSString * const kEmailSIPLabel; @class AKSIPURI, AKNetworkReachability; @class ActiveAccountViewController, AuthenticationFailureController; -@class CallTransferController; +@class CallTransferController, WorkspaceSleepStatus; @protocol MusicPlayer, RingtonePlaybackUseCase; // A SIP account controller. @@ -48,6 +48,7 @@ extern NSString * const kEmailSIPLabel; @property(nonatomic, readonly) AKSIPUserAgent *userAgent; @property(nonatomic, readonly) id ringtonePlayback; @property(nonatomic, readonly) id musicPlayer; +@property(nonatomic, readonly) WorkspaceSleepStatus *sleepStatus; // A Boolean value indicating whether account is registered. @property(nonatomic, readonly, getter=isAccountRegistered) BOOL accountRegistered; @@ -97,7 +98,8 @@ extern NSString * const kEmailSIPLabel; - (instancetype)initWithSIPAccount:(AKSIPAccount *)account userAgent:(AKSIPUserAgent *)userAgent ringtonePlayback:(id)ringtonePlayback - musicPlayer:(id)musicPlayer; + musicPlayer:(id)musicPlayer + sleepStatus:(WorkspaceSleepStatus *)sleepStatus; // Registers the account adding it to the user agent, if needed. The user agent will be started, if it hasn't been yet. - (void)registerAccount; diff --git a/Telephone/AccountController.m b/Telephone/AccountController.m index c4b3f770d..313b11d70 100644 --- a/Telephone/AccountController.m +++ b/Telephone/AccountController.m @@ -43,6 +43,8 @@ #import "IncomingCallViewController.h" #import "UserDefaultsKeys.h" +#import "Telephone-Swift.h" + // Account state pop-up button widths. // @@ -222,7 +224,8 @@ - (AuthenticationFailureController *)authenticationFailureController { - (instancetype)initWithSIPAccount:(AKSIPAccount *)account userAgent:(AKSIPUserAgent *)userAgent ringtonePlayback:(id)ringtonePlayback - musicPlayer:(id)musicPlayer { + musicPlayer:(id)musicPlayer + sleepStatus:(WorkspaceSleepStatus *)sleepStatus { self = [super initWithWindowNibName:@"Account"]; if (self == nil) { @@ -233,6 +236,7 @@ - (instancetype)initWithSIPAccount:(AKSIPAccount *)account _userAgent = userAgent; _ringtonePlayback = ringtonePlayback; _musicPlayer = musicPlayer; + _sleepStatus = sleepStatus; _callControllers = [[NSMutableArray alloc] init]; _destinationToCall = @""; @@ -1042,7 +1046,7 @@ - (void)SIPUserAgentDidFinishStarting:(NSNotification *)notification { // This is the moment when the application starts doing its main job. - (void)networkReachabilityDidBecomeReachable:(NSNotification *)notification { - if (![self isAccountUnavailable] && ![self isAccountRegistered]) { + if (!self.sleepStatus.isSleeping && !self.isAccountUnavailable && !self.isAccountRegistered) { [self registerAccount]; } } diff --git a/Telephone/AppController.m b/Telephone/AppController.m index 90b370da1..9efe33a1f 100644 --- a/Telephone/AppController.m +++ b/Telephone/AppController.m @@ -62,6 +62,7 @@ @interface AppController () ringtonePlayback; @property(nonatomic, readonly) id musicPlayer; +@property(nonatomic, readonly) WorkspaceSleepStatus *sleepStatus; @property(nonatomic, getter=isFinishedLaunching) BOOL finishedLaunching; @property(nonatomic, copy) NSString *destinationToCall; @property(nonatomic, getter=isUserSessionActive) BOOL userSessionActive; @@ -235,6 +236,7 @@ - (instancetype)init { _preferencesController = _compositionRoot.preferencesController; _ringtonePlayback = _compositionRoot.ringtonePlayback; _musicPlayer = _compositionRoot.musicPlayer; + _sleepStatus = _compositionRoot.workstationSleepStatus; _destinationToCall = @""; _userSessionActive = YES; _accountControllers = [[NSMutableArray alloc] init]; @@ -924,7 +926,8 @@ - (void)accountSetupControllerDidAddAccount:(NSNotification *)notification { AccountController *controller = [[AccountController alloc] initWithSIPAccount:account userAgent:self.userAgent ringtonePlayback:self.ringtonePlayback - musicPlayer:self.musicPlayer]; + musicPlayer:self.musicPlayer + sleepStatus:self.sleepStatus]; [controller setAccountDescription:[[controller account] SIPAddress]]; [[controller window] setExcludedFromWindowsMenu:YES]; @@ -996,7 +999,8 @@ - (void)preferencesControllerDidChangeAccountEnabled:(NSNotification *)notificat AccountController *controller = [[AccountController alloc] initWithSIPAccount:account userAgent:self.userAgent ringtonePlayback:self.ringtonePlayback - musicPlayer:self.musicPlayer]; + musicPlayer:self.musicPlayer + sleepStatus:self.sleepStatus]; [[controller window] setExcludedFromWindowsMenu:YES]; @@ -1303,7 +1307,8 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { AccountController *controller = [[AccountController alloc] initWithSIPAccount:account userAgent:self.userAgent ringtonePlayback:self.ringtonePlayback - musicPlayer:self.musicPlayer]; + musicPlayer:self.musicPlayer + sleepStatus:self.sleepStatus]; [[controller window] setExcludedFromWindowsMenu:YES]; diff --git a/Telephone/CompositionRoot.swift b/Telephone/CompositionRoot.swift index 87a94771c..d66a27f5a 100644 --- a/Telephone/CompositionRoot.swift +++ b/Telephone/CompositionRoot.swift @@ -27,6 +27,7 @@ final class CompositionRoot: NSObject { let storeWindowController: StoreWindowController let purchaseReminder: PurchaseReminderUseCase let musicPlayer: MusicPlayer + let workstationSleepStatus: WorkspaceSleepStatus private let defaults: UserDefaults private let queue: DispatchQueue @@ -116,6 +117,8 @@ final class CompositionRoot: NSObject { defaults: SimpleMusicPlayerUserDefaults(defaults: defaults) ) + workstationSleepStatus = WorkspaceSleepStatus(workspace: NSWorkspace.shared()) + userAgentNotificationsToEventTargetAdapter = UserAgentNotificationsToEventTargetAdapter( target: userAgentSoundIOSelection, agent: userAgent