Start screen lock countdown if app is inactive for more than N seconds.

pull/1/head
Matthew Chen 7 years ago
parent 1ae3f77db3
commit 1424149a74

@ -36,6 +36,8 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, nullable) NSDate *lastUnlockAttemptDate; @property (nonatomic, nullable) NSDate *lastUnlockAttemptDate;
@property (nonatomic, nullable) NSDate *lastUnlockSuccessDate; @property (nonatomic, nullable) NSDate *lastUnlockSuccessDate;
@property (nonatomic, nullable) NSTimer *inactiveTimer;
@end @end
#pragma mark - #pragma mark -
@ -116,8 +118,27 @@ NS_ASSUME_NONNULL_BEGIN
- (void)setAppIsInactive:(BOOL)appIsInactive - (void)setAppIsInactive:(BOOL)appIsInactive
{ {
BOOL didChange = _appIsInactive != appIsInactive;
_appIsInactive = appIsInactive; _appIsInactive = appIsInactive;
if (didChange) {
// If app is inactive for more than N seconds,
// treat this as "entering the background" for the purposes
// of Screen Lock.
if (!appIsInactive) {
[self.inactiveTimer invalidate];
self.inactiveTimer = nil;
} else if (!self.isShowingScreenLockUI) {
[self.inactiveTimer invalidate];
self.inactiveTimer = [NSTimer weakScheduledTimerWithTimeInterval:45.f
target:self
selector:@selector(inactiveTimerDidFire)
userInfo:nil
repeats:NO];
}
}
[self ensureScreenProtection]; [self ensureScreenProtection];
} }
@ -125,10 +146,7 @@ NS_ASSUME_NONNULL_BEGIN
{ {
if (appIsInBackground) { if (appIsInBackground) {
if (!_appIsInBackground) { if (!_appIsInBackground) {
// Record the time when app entered background. [self markAppAsInBackground];
self.appEnteredBackgroundDate = [NSDate new];
self.didLastUnlockAttemptFail = NO;
} }
} }
@ -137,6 +155,17 @@ NS_ASSUME_NONNULL_BEGIN
[self ensureScreenProtection]; [self ensureScreenProtection];
} }
- (void)markAppAsInBackground
{
// Record the time when app entered background.
self.appEnteredBackgroundDate = [NSDate new];
self.didLastUnlockAttemptFail = NO;
[self.inactiveTimer invalidate];
self.inactiveTimer = nil;
}
- (void)ensureScreenProtection - (void)ensureScreenProtection
{ {
OWSAssertIsOnMainThread(); OWSAssertIsOnMainThread();
@ -514,6 +543,11 @@ NS_ASSUME_NONNULL_BEGIN
self.appIsInBackground = YES; self.appIsInBackground = YES;
} }
- (void)inactiveTimerDidFire
{
[self markAppAsInBackground];
}
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

Loading…
Cancel
Save