From bdfa435738b0e4bffbacff8ab511a6719b01ef9c Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Tue, 13 Jun 2017 13:45:07 -0400 Subject: [PATCH] Allow notification sound check from any thread. // FREEBIE --- Signal/src/environment/NotificationsManager.m | 53 ++++++++++--------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/Signal/src/environment/NotificationsManager.m b/Signal/src/environment/NotificationsManager.m index 08f507251..d137c2eef 100644 --- a/Signal/src/environment/NotificationsManager.m +++ b/Signal/src/environment/NotificationsManager.m @@ -310,36 +310,37 @@ - (BOOL)shouldPlaySoundForNotification { - OWSAssert([NSThread isMainThread]); - - // Play no more than 2 notification sounds in a given - // five-second window. - const CGFloat kNotificationWindowSeconds = 5.f; - const NSUInteger kMaxNotificationRate = 2; - - // Cull obsolete notification timestamps from the thread's notification history. - while (self.notificationHistory.count > 0) { - NSDate *notificationTimestamp = self.notificationHistory[0]; - CGFloat notificationAgeSeconds = fabs(notificationTimestamp.timeIntervalSinceNow); - if (notificationAgeSeconds > kNotificationWindowSeconds) { - [self.notificationHistory removeObjectAtIndex:0]; - } else { - break; + @synchronized(self) + { + // Play no more than 2 notification sounds in a given + // five-second window. + const CGFloat kNotificationWindowSeconds = 5.f; + const NSUInteger kMaxNotificationRate = 2; + + // Cull obsolete notification timestamps from the thread's notification history. + while (self.notificationHistory.count > 0) { + NSDate *notificationTimestamp = self.notificationHistory[0]; + CGFloat notificationAgeSeconds = fabs(notificationTimestamp.timeIntervalSinceNow); + if (notificationAgeSeconds > kNotificationWindowSeconds) { + [self.notificationHistory removeObjectAtIndex:0]; + } else { + break; + } } - } - // Ignore notifications if necessary. - BOOL shouldPlaySound = self.notificationHistory.count < kMaxNotificationRate; + // Ignore notifications if necessary. + BOOL shouldPlaySound = self.notificationHistory.count < kMaxNotificationRate; - if (shouldPlaySound) { - // Add new notification timestamp to the thread's notification history. - NSDate *newNotificationTimestamp = [NSDate new]; - [self.notificationHistory addObject:newNotificationTimestamp]; + if (shouldPlaySound) { + // Add new notification timestamp to the thread's notification history. + NSDate *newNotificationTimestamp = [NSDate new]; + [self.notificationHistory addObject:newNotificationTimestamp]; - return YES; - } else { - DDLogDebug(@"Skipping sound for notification"); - return NO; + return YES; + } else { + DDLogDebug(@"Skipping sound for notification"); + return NO; + } } }