diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index 2f027f1d..9551540b 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -171,6 +171,7 @@ 7229E1BA1C97CC4D00CB50D0 /* SPUScheduledUpdateDriver.m in Sources */ = {isa = PBXBuildFile; fileRef = 7229E1B81C97CC4D00CB50D0 /* SPUScheduledUpdateDriver.m */; }; 7229E1BD1C98EFF200CB50D0 /* SPUDownloadDriver.h in Headers */ = {isa = PBXBuildFile; fileRef = 7229E1BB1C98EFF200CB50D0 /* SPUDownloadDriver.h */; }; 7229E1BE1C98EFF200CB50D0 /* SPUDownloadDriver.m in Sources */ = {isa = PBXBuildFile; fileRef = 7229E1BC1C98EFF200CB50D0 /* SPUDownloadDriver.m */; }; + 722C9539286EA68C0033908A /* SPUGentleUserDriverReminders.h in Headers */ = {isa = PBXBuildFile; fileRef = 722C9538286EA54A0033908A /* SPUGentleUserDriverReminders.h */; settings = {ATTRIBUTES = (Private, ); }; }; 722FB7E5260EE53F00EB571C /* SUNormalization.m in Sources */ = {isa = PBXBuildFile; fileRef = 722FB7E4260EE53F00EB571C /* SUNormalization.m */; }; 722FB7E6260EE53F00EB571C /* SUNormalization.m in Sources */ = {isa = PBXBuildFile; fileRef = 722FB7E4260EE53F00EB571C /* SUNormalization.m */; }; 72316BD31E0DA8430039EFD9 /* SUUnarchiverNotifier.m in Sources */ = {isa = PBXBuildFile; fileRef = 72316BD21E0DA8430039EFD9 /* SUUnarchiverNotifier.m */; }; @@ -1010,6 +1011,7 @@ 7229E1B81C97CC4D00CB50D0 /* SPUScheduledUpdateDriver.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPUScheduledUpdateDriver.m; sourceTree = ""; }; 7229E1BB1C98EFF200CB50D0 /* SPUDownloadDriver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPUDownloadDriver.h; sourceTree = ""; }; 7229E1BC1C98EFF200CB50D0 /* SPUDownloadDriver.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPUDownloadDriver.m; sourceTree = ""; }; + 722C9538286EA54A0033908A /* SPUGentleUserDriverReminders.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SPUGentleUserDriverReminders.h; sourceTree = ""; }; 722FB7CA1DD69897001D40CE /* ConfigSwift.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = ConfigSwift.xcconfig; sourceTree = ""; }; 722FB7E3260EE53F00EB571C /* SUNormalization.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SUNormalization.h; sourceTree = ""; }; 722FB7E4260EE53F00EB571C /* SUNormalization.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SUNormalization.m; sourceTree = ""; }; @@ -2159,6 +2161,7 @@ 725CB9581C7121830064365A /* SPUStandardUserDriver.h */, 725CB9591C7121830064365A /* SPUStandardUserDriver.m */, 726E4A361C89116000C57C6A /* SPUStandardUserDriverDelegate.h */, + 722C9538286EA54A0033908A /* SPUGentleUserDriverReminders.h */, 726DF88D1C84277500188804 /* SPUUserUpdateState.h */, 7269E49C2648FC6C0088C213 /* SPUUserUpdateState+Private.h */, 7269E4992648F7C00088C213 /* SPUUserUpdateState.m */, @@ -2433,6 +2436,7 @@ 7246E0A31C83B685003B4E75 /* SPUStandardUpdaterController.h in Headers */, 725CB95A1C7121830064365A /* SPUStandardUserDriver.h in Headers */, 726E4A371C89116000C57C6A /* SPUStandardUserDriverDelegate.h in Headers */, + 722C9539286EA68C0033908A /* SPUGentleUserDriverReminders.h in Headers */, 729F7EAF273F1840004592DC /* SPUUserAgent+Private.h in Headers */, 72EE17FC26D1CC9D00C58B19 /* SPUInstallationType.h in Headers */, 72EF30BE2675CF38008CE987 /* SPUAppcastItemStateResolver.h in Headers */, diff --git a/Sparkle/SPUGentleUserDriverReminders.h b/Sparkle/SPUGentleUserDriverReminders.h new file mode 100644 index 00000000..a509e0e0 --- /dev/null +++ b/Sparkle/SPUGentleUserDriverReminders.h @@ -0,0 +1,22 @@ +// +// SPUGentleUserDriverReminders.h +// Sparkle +// +// Copyright © 2022 Sparkle Project. All rights reserved. +// + +#ifndef SPUGentleUserDriverReminders_h +#define SPUGentleUserDriverReminders_h + +/** + A private protocol for user drivers implementing gentle scheduled reminders + */ +@protocol SPUGentleUserDriverReminders + +- (void)logGentleScheduledUpdateReminderWarningIfNeeded; + +- (void)resetTimeSinceOpportuneUpdateNotice; + +@end + +#endif /* SPUGentleUserDriverReminders_h */ diff --git a/Sparkle/SPUStandardUserDriver.m b/Sparkle/SPUStandardUserDriver.m index 4014993a..54d306f8 100644 --- a/Sparkle/SPUStandardUserDriver.m +++ b/Sparkle/SPUStandardUserDriver.m @@ -10,6 +10,7 @@ #import "SPUStandardUserDriver.h" #import "SPUStandardUserDriverDelegate.h" +#import "SPUGentleUserDriverReminders.h" #import "SUAppcastItem.h" #import "SUVersionDisplayProtocol.h" #import "SUHost.h" @@ -33,7 +34,7 @@ // The amount of time the app is allowed to be idle for us to consider showing an update prompt right away when the app is active static const NSTimeInterval SUScheduledUpdateIdleEventLeewayInterval = DEBUG ? 30.0 : 5 * 60.0; -@interface SPUStandardUserDriver () +@interface SPUStandardUserDriver () @property (nonatomic, readonly) SUHost *host; // We must store the oldHostName before the host is potentially replaced @@ -59,7 +60,7 @@ static const NSTimeInterval SUScheduledUpdateIdleEventLeewayInterval = DEBUG ? 3 @implementation SPUStandardUserDriver { mach_timebase_info_data_t _timebaseInfo; - double _initializationTime; + double _timeSinceOpportuneUpdateNotice; id _applicationBecameActiveAfterUpdateAlertBecameKeyObserver; NSValue *_updateAlertWindowFrameValue; BOOL _updateAlertWindowWasInactive; @@ -98,8 +99,6 @@ static const NSTimeInterval SUScheduledUpdateIdleEventLeewayInterval = DEBUG ? 3 _timebaseInfo.numer = 0; _timebaseInfo.denom = 0; - } else { - [self _resetInitializationTime]; } } return self; @@ -114,9 +113,10 @@ static const NSTimeInterval SUScheduledUpdateIdleEventLeewayInterval = DEBUG ? 3 } } -- (void)_resetInitializationTime +// This private method is used by SPUUpdater for resetting the opportune time to show an update notice in utmost focus +- (void)resetTimeSinceOpportuneUpdateNotice { - _initializationTime = [self currentTime]; + _timeSinceOpportuneUpdateNotice = [self currentTime]; } #pragma mark Update Permission @@ -131,8 +131,6 @@ static const NSTimeInterval SUScheduledUpdateIdleEventLeewayInterval = DEBUG ? 3 __weak __typeof__(self) weakSelf = self; self.permissionPrompt = [[SUUpdatePermissionPrompt alloc] initPromptWithHost:self.host request:request reply:^(SUUpdatePermissionResponse *response) { - [weakSelf _resetInitializationTime]; - reply(response); weakSelf.permissionPrompt = nil; }]; @@ -143,7 +141,7 @@ static const NSTimeInterval SUScheduledUpdateIdleEventLeewayInterval = DEBUG ? 3 #pragma mark Update Alert Focus // This private method is used by SPUUpdater when scheduling for update checks -- (void)_logGentleScheduledUpdateReminderWarningIfNeeded +- (void)logGentleScheduledUpdateReminderWarningIfNeeded { id delegate = self.delegate; if (!_loggedGentleUpdateReminderWarning && (![delegate respondsToSelector:@selector(supportsGentleScheduledUpdateReminders)] || !delegate.supportsGentleScheduledUpdateReminders)) { @@ -177,10 +175,10 @@ static const NSTimeInterval SUScheduledUpdateIdleEventLeewayInterval = DEBUG ? 3 [self.activeUpdateAlert setInstallButtonFocus:YES]; } else { // Handle scheduled update check - uint64_t timeElapsedSinceInitialization = (uint64_t)([self currentTime] - _initializationTime); + uint64_t timeElapsedSinceOpportuneUpdateNotice = (uint64_t)([self currentTime] - _timeSinceOpportuneUpdateNotice); - // Give scheduled update alerts priority if 3 or less seconds have passed since initialization - BOOL appNearUpdaterInitialization = (timeElapsedSinceInitialization <= 3000000000ULL); + // Give scheduled update alerts priority if 3 or less seconds have passed since our last opportune time + BOOL appNearUpdaterInitialization = (timeElapsedSinceOpportuneUpdateNotice <= 3000000000ULL); // We will always show an update alert at the right time [self.activeUpdateAlert setInstallButtonFocus:YES]; diff --git a/Sparkle/SPUUpdater.h b/Sparkle/SPUUpdater.h index 558fa4fc..c9698a1e 100644 --- a/Sparkle/SPUUpdater.h +++ b/Sparkle/SPUUpdater.h @@ -32,7 +32,7 @@ NS_ASSUME_NONNULL_BEGIN Prefer to set initial properties in your bundle's Info.plist as described in [Customizing Sparkle](https://sparkle-project.org/documentation/customization/). - Otherwise only if you need dynamic behavior (eg. for user preferences) should you set properties on the updater such as: + Otherwise only if you need dynamic behavior for user preferences should you set properties on the updater such as: - `automaticallyChecksForUpdates` - `updateCheckInterval` - `automaticallyDownloadsUpdates` @@ -102,19 +102,24 @@ SU_EXPORT @interface SPUUpdater : NSObject - (void)checkForUpdates; /** - Checks for updates, but does not display any UI unless an update is found. + Checks for updates, but does not show any UI unless an update is found. You usually do not need to call this method directly. If `automaticallyChecksForUpdates` is @c YES, Sparkle calls this method automatically according to its update schedule using the `updateCheckInterval` - and the `lastUpdateCheckDate`. + and the `lastUpdateCheckDate`. Therefore, you should typically only consider calling this method directly if you + opt out of automatic update checks. + + This is meant for programmatically initating a check for updates in the background without the user initiating it. + This check will not show UI if no new updates are found. + + If a new update is found, the updater's user driver may handle showing it at an appropriate (but not necessarily immediate) time. + If you want control over when and how a new update is shown, please see https://sparkle-project.org/documentation/gentle-reminders/ + + Note if automated updating is turned on, either a new update may be downloaded in the background to be installed silently, + or an already downloaded update may be shown. - This is meant for programmatically initating a check for updates. - That is, it will display no UI unless it finds an update, in which case it proceeds as usual. This will not find updates that the user has opted into skipping. - Note if there is no resumable update found, and automated updating is turned on, - the update will be downloaded in the background without disrupting the user. - This method does not do anything if there is a `sessionInProgress`. */ - (void)checkForUpdatesInBackground; @@ -177,7 +182,9 @@ SU_EXPORT @interface SPUUpdater : NSObject this permission request is not performed however. Setting this property will persist in the host bundle's user defaults. - Only set this property if you need dynamic behavior (e.g. user preferences). + Only set this property if the user wants to change the default via a user preferences option. + For testing environments, you can disable update checks by passing `-SUEnableAutomaticChecks NO` + to your app's command line arguments instead of setting this property. The update schedule cycle will be reset in a short delay after the property's new value is set. This is to allow reverting this property without kicking off a schedule change immediately @@ -187,9 +194,10 @@ SU_EXPORT @interface SPUUpdater : NSObject /** A property indicating the current automatic update check interval in seconds. + Prefer to set SUScheduledCheckInterval directly in your Info.plist for setting the initial value. + Setting this property will persist in the host bundle's user defaults. - For this reason, only set this property if you need dynamic behavior (eg user preferences). - Otherwise prefer to set SUScheduledCheckInterval directly in your Info.plist. + Only set this property if the user wants to change the default via a user preferences option. The update schedule cycle will be reset in a short delay after the property's new value is set. This is to allow reverting this property without kicking off a schedule change immediately @@ -205,7 +213,7 @@ SU_EXPORT @interface SPUUpdater : NSObject In this case, this property will return NO regardless of how this property is set. Setting this property will persist in the host bundle's user defaults. - For this reason, only set this property if you need dynamic behavior (eg user preferences). + For this reason, only set this property if the user wants to change the default via a user preferences option. Otherwise prefer to set SUAutomaticallyUpdate directly in your Info.plist. */ @property (nonatomic) BOOL automaticallyDownloadsUpdates; diff --git a/Sparkle/SPUUpdater.m b/Sparkle/SPUUpdater.m index 0fb5e149..9cac02d1 100644 --- a/Sparkle/SPUUpdater.m +++ b/Sparkle/SPUUpdater.m @@ -32,16 +32,11 @@ #import "SPUResumableUpdate.h" #import "SUSignatures.h" #import "SPUUserAgent+Private.h" +#import "SPUGentleUserDriverReminders.h" #include "AppKitPrevention.h" -@interface NSObject (SPUStandardUserDriverPrivate) - -- (void)_logGentleScheduledUpdateReminderWarningIfNeeded; - -@end - NSString *const SUUpdaterDidFinishLoadingAppCastNotification = @"SUUpdaterDidFinishLoadingAppCastNotification"; NSString *const SUUpdaterDidFindValidUpdateNotification = @"SUUpdaterDidFindValidUpdateNotification"; NSString *const SUUpdaterDidNotFindUpdateNotification = @"SUUpdaterDidNotFindUpdateNotification"; @@ -175,6 +170,10 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification return NO; } + if ([self.userDriver respondsToSelector:@selector(resetTimeSinceOpportuneUpdateNotice)]) { + [(id)self.userDriver resetTimeSinceOpportuneUpdateNotice]; + } + self.startedUpdater = YES; self.canCheckForUpdates = YES; @@ -520,8 +519,8 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification [self.delegate updater:self willScheduleUpdateCheckAfterDelay:delayUntilCheck]; } - if ([self.userDriver respondsToSelector:@selector(_logGentleScheduledUpdateReminderWarningIfNeeded)]) { - [(NSObject *)self.userDriver _logGentleScheduledUpdateReminderWarningIfNeeded]; + if ([self.userDriver respondsToSelector:@selector(logGentleScheduledUpdateReminderWarningIfNeeded)]) { + [(id)self.userDriver logGentleScheduledUpdateReminderWarningIfNeeded]; } [self.updaterTimer startAndFireAfterDelay:delayUntilCheck]; @@ -807,6 +806,12 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification return; // not even ready yet } + // Note this resets the opportune time when user grants Sparkle permission to check for updates + // and when the user changes preferences on automatically checking for updates or the update time check interval + if ([self.userDriver respondsToSelector:@selector(resetTimeSinceOpportuneUpdateNotice)]) { + [(id)self.userDriver resetTimeSinceOpportuneUpdateNotice]; + } + if (!self.sessionInProgress) { [self cancelNextUpdateCycle]; [self scheduleNextUpdateCheckFiringImmediately:NO usingCurrentDate:YES];