Skip to content

Commit

Permalink
Do not register account if sleeping
Browse files Browse the repository at this point in the history
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
  • Loading branch information
eofster committed Dec 1, 2016
1 parent 3cbf7bd commit 483e6ec
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
6 changes: 4 additions & 2 deletions Telephone/AccountController.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -48,6 +48,7 @@ extern NSString * const kEmailSIPLabel;
@property(nonatomic, readonly) AKSIPUserAgent *userAgent;
@property(nonatomic, readonly) id<RingtonePlaybackUseCase> ringtonePlayback;
@property(nonatomic, readonly) id<MusicPlayer> musicPlayer;
@property(nonatomic, readonly) WorkspaceSleepStatus *sleepStatus;

// A Boolean value indicating whether account is registered.
@property(nonatomic, readonly, getter=isAccountRegistered) BOOL accountRegistered;
Expand Down Expand Up @@ -97,7 +98,8 @@ extern NSString * const kEmailSIPLabel;
- (instancetype)initWithSIPAccount:(AKSIPAccount *)account
userAgent:(AKSIPUserAgent *)userAgent
ringtonePlayback:(id<RingtonePlaybackUseCase>)ringtonePlayback
musicPlayer:(id<MusicPlayer>)musicPlayer;
musicPlayer:(id<MusicPlayer>)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;
Expand Down
8 changes: 6 additions & 2 deletions Telephone/AccountController.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
#import "IncomingCallViewController.h"
#import "UserDefaultsKeys.h"

#import "Telephone-Swift.h"


// Account state pop-up button widths.
//
Expand Down Expand Up @@ -222,7 +224,8 @@ - (AuthenticationFailureController *)authenticationFailureController {
- (instancetype)initWithSIPAccount:(AKSIPAccount *)account
userAgent:(AKSIPUserAgent *)userAgent
ringtonePlayback:(id<RingtonePlaybackUseCase>)ringtonePlayback
musicPlayer:(id<MusicPlayer>)musicPlayer {
musicPlayer:(id<MusicPlayer>)musicPlayer
sleepStatus:(WorkspaceSleepStatus *)sleepStatus {

self = [super initWithWindowNibName:@"Account"];
if (self == nil) {
Expand All @@ -233,6 +236,7 @@ - (instancetype)initWithSIPAccount:(AKSIPAccount *)account
_userAgent = userAgent;
_ringtonePlayback = ringtonePlayback;
_musicPlayer = musicPlayer;
_sleepStatus = sleepStatus;

_callControllers = [[NSMutableArray alloc] init];
_destinationToCall = @"";
Expand Down Expand Up @@ -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];
}
}
Expand Down
11 changes: 8 additions & 3 deletions Telephone/AppController.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ @interface AppController () <AKSIPUserAgentDelegate, NSUserNotificationCenterDel
@property(nonatomic, readonly) PreferencesController *preferencesController;
@property(nonatomic, readonly) id<RingtonePlaybackUseCase> ringtonePlayback;
@property(nonatomic, readonly) id<MusicPlayer> musicPlayer;
@property(nonatomic, readonly) WorkspaceSleepStatus *sleepStatus;
@property(nonatomic, getter=isFinishedLaunching) BOOL finishedLaunching;
@property(nonatomic, copy) NSString *destinationToCall;
@property(nonatomic, getter=isUserSessionActive) BOOL userSessionActive;
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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];

Expand Down
3 changes: 3 additions & 0 deletions Telephone/CompositionRoot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -116,6 +117,8 @@ final class CompositionRoot: NSObject {
defaults: SimpleMusicPlayerUserDefaults(defaults: defaults)
)

workstationSleepStatus = WorkspaceSleepStatus(workspace: NSWorkspace.shared())

userAgentNotificationsToEventTargetAdapter = UserAgentNotificationsToEventTargetAdapter(
target: userAgentSoundIOSelection,
agent: userAgent
Expand Down

0 comments on commit 483e6ec

Please sign in to comment.