Allow notification sound check from any thread.

// FREEBIE
pull/1/head
Matthew Chen 7 years ago
parent e8a0cc08e6
commit bdfa435738

@ -310,36 +310,37 @@
- (BOOL)shouldPlaySoundForNotification - (BOOL)shouldPlaySoundForNotification
{ {
OWSAssert([NSThread isMainThread]); @synchronized(self)
{
// Play no more than 2 notification sounds in a given // Play no more than 2 notification sounds in a given
// five-second window. // five-second window.
const CGFloat kNotificationWindowSeconds = 5.f; const CGFloat kNotificationWindowSeconds = 5.f;
const NSUInteger kMaxNotificationRate = 2; const NSUInteger kMaxNotificationRate = 2;
// Cull obsolete notification timestamps from the thread's notification history. // Cull obsolete notification timestamps from the thread's notification history.
while (self.notificationHistory.count > 0) { while (self.notificationHistory.count > 0) {
NSDate *notificationTimestamp = self.notificationHistory[0]; NSDate *notificationTimestamp = self.notificationHistory[0];
CGFloat notificationAgeSeconds = fabs(notificationTimestamp.timeIntervalSinceNow); CGFloat notificationAgeSeconds = fabs(notificationTimestamp.timeIntervalSinceNow);
if (notificationAgeSeconds > kNotificationWindowSeconds) { if (notificationAgeSeconds > kNotificationWindowSeconds) {
[self.notificationHistory removeObjectAtIndex:0]; [self.notificationHistory removeObjectAtIndex:0];
} else { } else {
break; break;
}
} }
}
// Ignore notifications if necessary. // Ignore notifications if necessary.
BOOL shouldPlaySound = self.notificationHistory.count < kMaxNotificationRate; BOOL shouldPlaySound = self.notificationHistory.count < kMaxNotificationRate;
if (shouldPlaySound) { if (shouldPlaySound) {
// Add new notification timestamp to the thread's notification history. // Add new notification timestamp to the thread's notification history.
NSDate *newNotificationTimestamp = [NSDate new]; NSDate *newNotificationTimestamp = [NSDate new];
[self.notificationHistory addObject:newNotificationTimestamp]; [self.notificationHistory addObject:newNotificationTimestamp];
return YES; return YES;
} else { } else {
DDLogDebug(@"Skipping sound for notification"); DDLogDebug(@"Skipping sound for notification");
return NO; return NO;
}
} }
} }

Loading…
Cancel
Save