diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index f20a7aedf..5f4b43fe3 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -552,10 +552,6 @@ static NSTimeInterval launchStartedAt; OWSFailDebug(@"app launch failed"); return NO; } - if (self.backup.hasPendingRestoreDecision) { - OWSLogInfo(@"Ignoring URL; pending restore decision."); - return NO; - } if (!AppReadiness.isAppReady) { OWSLogWarn(@"Ignoring openURL: app not ready."); @@ -756,14 +752,9 @@ static NSTimeInterval launchStartedAt; completionHandler(NO); return; } - if (self.backup.hasPendingRestoreDecision) { - OWSLogInfo(@"Ignoring shortcut action; pending restore decision."); - completionHandler(NO); - return; - } [AppReadiness runNowOrWhenAppDidBecomeReady:^{ - if (![self.tsAccountManager isRegistered]) { + if (![self.tsAccountManager isRegisteredAndReady]) { UIAlertController *controller = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"REGISTER_CONTACTS_WELCOME", nil) message:NSLocalizedString(@"REGISTRATION_RESTRICTED_MESSAGE", nil) @@ -810,10 +801,6 @@ static NSTimeInterval launchStartedAt; OWSFailDebug(@"app launch failed"); return NO; } - if (self.backup.hasPendingRestoreDecision) { - OWSLogInfo(@"Ignoring user activity; pending restore decision."); - return NO; - } if ([userActivity.activityType isEqualToString:@"INStartVideoCallIntent"]) { if (!SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(10, 0)) { @@ -838,6 +825,11 @@ static NSTimeInterval launchStartedAt; } [AppReadiness runNowOrWhenAppDidBecomeReady:^{ + if (![self.tsAccountManager isRegisteredAndReady]) { + OWSLogInfo(@"Ignoring user activity; app not ready."); + return; + } + NSString *_Nullable phoneNumber = handle; if ([handle hasPrefix:CallKitCallManager.kAnonymousCallHandlePrefix]) { phoneNumber = [self.primaryStorage phoneNumberForCallKitId:handle]; @@ -895,6 +887,11 @@ static NSTimeInterval launchStartedAt; } [AppReadiness runNowOrWhenAppDidBecomeReady:^{ + if (![self.tsAccountManager isRegisteredAndReady]) { + OWSLogInfo(@"Ignoring user activity; app not ready."); + return; + } + NSString *_Nullable phoneNumber = handle; if ([handle hasPrefix:CallKitCallManager.kAnonymousCallHandlePrefix]) { phoneNumber = [self.primaryStorage phoneNumberForCallKitId:handle]; @@ -969,8 +966,8 @@ static NSTimeInterval launchStartedAt; OWSFailDebug(@"app launch failed"); return; } - if (self.backup.hasPendingRestoreDecision) { - OWSLogInfo(@"Ignoring remote notification; pending restore decision."); + if (!(AppReadiness.isAppReady && [self.tsAccountManager isRegisteredAndReady])) { + OWSLogInfo(@"Ignoring remote notification; app not ready."); return; } @@ -987,8 +984,8 @@ static NSTimeInterval launchStartedAt; OWSFailDebug(@"app launch failed"); return; } - if (self.backup.hasPendingRestoreDecision) { - OWSLogInfo(@"Ignoring remote notification; pending restore decision."); + if (!(AppReadiness.isAppReady && [self.tsAccountManager isRegisteredAndReady])) { + OWSLogInfo(@"Ignoring remote notification; app not ready."); return; } @@ -1005,13 +1002,14 @@ static NSTimeInterval launchStartedAt; OWSFailDebug(@"app launch failed"); return; } - if (self.backup.hasPendingRestoreDecision) { - OWSLogInfo(@"Ignoring local notification; pending restore decision."); - return; - } OWSLogInfo(@"%@", notification); [AppReadiness runNowOrWhenAppDidBecomeReady:^{ + if (![self.tsAccountManager isRegisteredAndReady]) { + OWSLogInfo(@"Ignoring action; app not ready."); + return; + } + [[PushManager sharedManager] application:application didReceiveLocalNotification:notification]; }]; } @@ -1028,11 +1026,6 @@ static NSTimeInterval launchStartedAt; completionHandler(); return; } - if (self.backup.hasPendingRestoreDecision) { - OWSLogInfo(@"Ignoring action; pending restore decision."); - completionHandler(); - return; - } // The docs for handleActionWithIdentifier:... state: // "You must call [completionHandler] at the end of your method.". @@ -1041,6 +1034,12 @@ static NSTimeInterval launchStartedAt; // // https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623068-application?language=objc [AppReadiness runNowOrWhenAppDidBecomeReady:^{ + if (![self.tsAccountManager isRegisteredAndReady]) { + OWSLogInfo(@"Ignoring action; app not ready."); + completionHandler(); + return; + } + [[PushManager sharedManager] application:application handleActionWithIdentifier:identifier forLocalNotification:notification @@ -1063,11 +1062,6 @@ static NSTimeInterval launchStartedAt; completionHandler(); return; } - if (self.backup.hasPendingRestoreDecision) { - OWSLogInfo(@"Ignoring action; pending restore decision."); - completionHandler(); - return; - } // The docs for handleActionWithIdentifier:... state: // "You must call [completionHandler] at the end of your method.". @@ -1076,6 +1070,12 @@ static NSTimeInterval launchStartedAt; // // https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623068-application?language=objc [AppReadiness runNowOrWhenAppDidBecomeReady:^{ + if (![self.tsAccountManager isRegisteredAndReady]) { + OWSLogInfo(@"Ignoring action; app not ready."); + completionHandler(); + return; + } + [[PushManager sharedManager] application:application handleActionWithIdentifier:identifier forLocalNotification:notification diff --git a/Signal/src/ViewControllers/HomeView/HomeViewController.m b/Signal/src/ViewControllers/HomeView/HomeViewController.m index f0d49cc5c..e21204abd 100644 --- a/Signal/src/ViewControllers/HomeView/HomeViewController.m +++ b/Signal/src/ViewControllers/HomeView/HomeViewController.m @@ -187,8 +187,8 @@ NSString *const kArchivedConversationsReuseIdentifier = @"kArchivedConversations name:YapDatabaseModifiedExternallyNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(deregistrationStateDidChange:) - name:DeregistrationStateDidChangeNotification + selector:@selector(registrationStateDidChange:) + name:RegistrationStateDidChangeNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(outageStateDidChange:) @@ -218,7 +218,7 @@ NSString *const kArchivedConversationsReuseIdentifier = @"kArchivedConversations [self reloadTableViewData]; } -- (void)deregistrationStateDidChange:(id)notification +- (void)registrationStateDidChange:(id)notification { OWSAssertIsOnMainThread(); diff --git a/Signal/src/util/Backup/OWSBackup.m b/Signal/src/util/Backup/OWSBackup.m index f60bb1b9e..4246256c0 100644 --- a/Signal/src/util/Backup/OWSBackup.m +++ b/Signal/src/util/Backup/OWSBackup.m @@ -18,7 +18,6 @@ NSString *const OWSPrimaryStorage_OWSBackupCollection = @"OWSPrimaryStorage_OWSB NSString *const OWSBackup_IsBackupEnabledKey = @"OWSBackup_IsBackupEnabledKey"; NSString *const OWSBackup_LastExportSuccessDateKey = @"OWSBackup_LastExportSuccessDateKey"; NSString *const OWSBackup_LastExportFailureDateKey = @"OWSBackup_LastExportFailureDateKey"; -NSString *const OWSBackup_HasPendingRestoreDecisionKey = @"OWSBackup_HasPendingRestoreDecisionKey"; NSString *NSStringForBackupExportState(OWSBackupState state) { @@ -247,20 +246,12 @@ NSString *NSStringForBackupImportState(OWSBackupState state) - (BOOL)hasPendingRestoreDecision { - return [self.dbConnection boolForKey:OWSBackup_HasPendingRestoreDecisionKey - inCollection:OWSPrimaryStorage_OWSBackupCollection - defaultValue:NO]; + return [self.tsAccountManager hasPendingBackupRestoreDecision]; } - (void)setHasPendingRestoreDecision:(BOOL)value { - OWSLogInfo(@"%d", value); - - [self.dbConnection setBool:value - forKey:OWSBackup_HasPendingRestoreDecisionKey - inCollection:OWSPrimaryStorage_OWSBackupCollection]; - - [self postDidChangeNotification]; + [self.tsAccountManager setHasPendingBackupRestoreDecision:value]; } - (BOOL)canBackupExport @@ -426,6 +417,8 @@ NSString *NSStringForBackupImportState(OWSBackupState state) OWSAssertIsOnMainThread(); [self ensureBackupExportState]; + + [self postDidChangeNotification]; } #pragma mark - OWSBackupJobDelegate diff --git a/SignalServiceKit/src/Account/TSAccountManager.h b/SignalServiceKit/src/Account/TSAccountManager.h index 2393e65a9..d6205a5b0 100644 --- a/SignalServiceKit/src/Account/TSAccountManager.h +++ b/SignalServiceKit/src/Account/TSAccountManager.h @@ -9,7 +9,6 @@ NS_ASSUME_NONNULL_BEGIN extern NSString *const TSRegistrationErrorDomain; extern NSString *const TSRegistrationErrorUserInfoHTTPStatus; extern NSString *const RegistrationStateDidChangeNotification; -extern NSString *const DeregistrationStateDidChangeNotification; extern NSString *const kNSNotificationName_LocalNumberDidChange; @class AnyPromise; @@ -21,6 +20,8 @@ typedef NS_ENUM(NSUInteger, OWSRegistrationState) { OWSRegistrationState_Unregistered, OWSRegistrationState_PendingBackupRestore, OWSRegistrationState_Registered, + OWSRegistrationState_Deregistered, + OWSRegistrationState_Reregistering, }; @interface TSAccountManager : NSObject @@ -38,12 +39,15 @@ typedef NS_ENUM(NSUInteger, OWSRegistrationState) { + (instancetype)sharedInstance; +- (OWSRegistrationState)registrationState; + /** * Returns if a user is registered or not * * @return registered or not */ - (BOOL)isRegistered; +- (BOOL)isRegisteredAndReady; /** * Returns current phone number for this device, which may not yet have been registered. @@ -129,6 +133,9 @@ typedef NS_ENUM(NSUInteger, OWSRegistrationState) { - (BOOL)isDeregistered; - (void)setIsDeregistered:(BOOL)isDeregistered; +- (BOOL)hasPendingBackupRestoreDecision; +- (void)setHasPendingBackupRestoreDecision:(BOOL)value; + #pragma mark - Re-registration // Re-registration is the process of re-registering _with the same phone number_. diff --git a/SignalServiceKit/src/Account/TSAccountManager.m b/SignalServiceKit/src/Account/TSAccountManager.m index d1da413e4..100b481d7 100644 --- a/SignalServiceKit/src/Account/TSAccountManager.m +++ b/SignalServiceKit/src/Account/TSAccountManager.m @@ -28,13 +28,13 @@ NS_ASSUME_NONNULL_BEGIN NSString *const TSRegistrationErrorDomain = @"TSRegistrationErrorDomain"; NSString *const TSRegistrationErrorUserInfoHTTPStatus = @"TSHTTPStatus"; NSString *const RegistrationStateDidChangeNotification = @"RegistrationStateDidChangeNotification"; -NSString *const DeregistrationStateDidChangeNotification = @"DeregistrationStateDidChangeNotification"; NSString *const kNSNotificationName_LocalNumberDidChange = @"kNSNotificationName_LocalNumberDidChange"; NSString *const TSAccountManager_RegisteredNumberKey = @"TSStorageRegisteredNumberKey"; NSString *const TSAccountManager_IsDeregisteredKey = @"TSAccountManager_IsDeregisteredKey"; NSString *const TSAccountManager_ReregisteringPhoneNumberKey = @"TSAccountManager_ReregisteringPhoneNumberKey"; NSString *const TSAccountManager_LocalRegistrationIdKey = @"TSStorageLocalRegistrationId"; +NSString *const TSAccountManager_HasPendingRestoreDecisionKey = @"TSAccountManager_HasPendingRestoreDecisionKey"; NSString *const TSAccountManager_UserAccountCollection = @"TSStorageUserAccountCollection"; NSString *const TSAccountManager_ServerAuthToken = @"TSStorageServerAuthToken"; @@ -135,6 +135,23 @@ NSString *const TSAccountManager_NeedsAccountAttributesUpdateKey = @"TSAccountMa userInfo:nil]; } +- (OWSRegistrationState)registrationState +{ + if (!self.isRegistered) { + return OWSRegistrationState_Unregistered; + } else if (self.isDeregistered) { + if (self.isReregistering) { + return OWSRegistrationState_Reregistering; + } else { + return OWSRegistrationState_Deregistered; + } + } else if (self.isDeregistered) { + return OWSRegistrationState_PendingBackupRestore; + } else { + return OWSRegistrationState_Registered; + } +} + - (BOOL)isRegistered { @synchronized (self) { @@ -148,6 +165,11 @@ NSString *const TSAccountManager_NeedsAccountAttributesUpdateKey = @"TSAccountMa } } +- (BOOL)isRegisteredAndReady +{ + return self.registrationState == OWSRegistrationState_Registered; +} + - (void)didRegister { OWSLogInfo(@"didRegister"); @@ -159,14 +181,12 @@ NSString *const TSAccountManager_NeedsAccountAttributesUpdateKey = @"TSAccountMa [self storeLocalNumber:phoneNumber]; - [[NSNotificationCenter defaultCenter] postNotificationNameAsync:RegistrationStateDidChangeNotification - object:nil - userInfo:nil]; - // Warm these cached values. [self isRegistered]; [self localNumber]; [self isDeregistered]; + + [self postRegistrationStateDidChangeNotification]; } + (nullable NSString *)localNumber @@ -491,9 +511,7 @@ NSString *const TSAccountManager_NeedsAccountAttributesUpdateKey = @"TSAccountMa // `RegistrationStateDidChangeNotification` which is only safe to fire after // the data store is reset. - [[NSNotificationCenter defaultCenter] postNotificationNameAsync:RegistrationStateDidChangeNotification - object:nil - userInfo:nil]; + [self.sharedInstance postRegistrationStateDidChangeNotification]; } failure:^(NSURLSessionDataTask *task, NSError *error) { if (!IsNSErrorNetworkFailure(error)) { @@ -554,9 +572,7 @@ NSString *const TSAccountManager_NeedsAccountAttributesUpdateKey = @"TSAccountMa inCollection:TSAccountManager_UserAccountCollection]; }]; - [[NSNotificationCenter defaultCenter] postNotificationNameAsync:DeregistrationStateDidChangeNotification - object:nil - userInfo:nil]; + [self postRegistrationStateDidChangeNotification]; } #pragma mark - Re-registration @@ -583,6 +599,9 @@ NSString *const TSAccountManager_NeedsAccountAttributesUpdateKey = @"TSAccountMa forKey:TSAccountManager_ReregisteringPhoneNumberKey inCollection:TSAccountManager_UserAccountCollection]; }]; + + [self postRegistrationStateDidChangeNotification]; + return YES; } } @@ -604,6 +623,24 @@ NSString *const TSAccountManager_NeedsAccountAttributesUpdateKey = @"TSAccountMa inCollection:TSAccountManager_UserAccountCollection]; } +- (BOOL)hasPendingBackupRestoreDecision +{ + return [self.dbConnection boolForKey:TSAccountManager_HasPendingRestoreDecisionKey + inCollection:TSAccountManager_UserAccountCollection + defaultValue:NO]; +} + +- (void)setHasPendingBackupRestoreDecision:(BOOL)value +{ + OWSLogInfo(@"%d", value); + + [self.dbConnection setBool:value + forKey:TSAccountManager_HasPendingRestoreDecisionKey + inCollection:TSAccountManager_UserAccountCollection]; + + [self postRegistrationStateDidChangeNotification]; +} + - (BOOL)isManualMessageFetchEnabled { return [self.dbConnection boolForKey:TSAccountManager_ManualMessageFetchKey @@ -688,6 +725,17 @@ NSString *const TSAccountManager_NeedsAccountAttributesUpdateKey = @"TSAccountMa }]; } +#pragma mark - Notifications + +- (void)postRegistrationStateDidChangeNotification +{ + OWSAssertIsOnMainThread(); + + [[NSNotificationCenter defaultCenter] postNotificationNameAsync:RegistrationStateDidChangeNotification + object:nil + userInfo:nil]; +} + @end NS_ASSUME_NONNULL_END