Add isRegisteredAndReady to TSAccountManager.

pull/1/head
Matthew Chen 7 years ago
parent 8110e0c765
commit 70b2280aa7

@ -552,10 +552,6 @@ static NSTimeInterval launchStartedAt;
OWSFailDebug(@"app launch failed"); OWSFailDebug(@"app launch failed");
return NO; return NO;
} }
if (self.backup.hasPendingRestoreDecision) {
OWSLogInfo(@"Ignoring URL; pending restore decision.");
return NO;
}
if (!AppReadiness.isAppReady) { if (!AppReadiness.isAppReady) {
OWSLogWarn(@"Ignoring openURL: app not ready."); OWSLogWarn(@"Ignoring openURL: app not ready.");
@ -756,14 +752,9 @@ static NSTimeInterval launchStartedAt;
completionHandler(NO); completionHandler(NO);
return; return;
} }
if (self.backup.hasPendingRestoreDecision) {
OWSLogInfo(@"Ignoring shortcut action; pending restore decision.");
completionHandler(NO);
return;
}
[AppReadiness runNowOrWhenAppDidBecomeReady:^{ [AppReadiness runNowOrWhenAppDidBecomeReady:^{
if (![self.tsAccountManager isRegistered]) { if (![self.tsAccountManager isRegisteredAndReady]) {
UIAlertController *controller = UIAlertController *controller =
[UIAlertController alertControllerWithTitle:NSLocalizedString(@"REGISTER_CONTACTS_WELCOME", nil) [UIAlertController alertControllerWithTitle:NSLocalizedString(@"REGISTER_CONTACTS_WELCOME", nil)
message:NSLocalizedString(@"REGISTRATION_RESTRICTED_MESSAGE", nil) message:NSLocalizedString(@"REGISTRATION_RESTRICTED_MESSAGE", nil)
@ -810,10 +801,6 @@ static NSTimeInterval launchStartedAt;
OWSFailDebug(@"app launch failed"); OWSFailDebug(@"app launch failed");
return NO; return NO;
} }
if (self.backup.hasPendingRestoreDecision) {
OWSLogInfo(@"Ignoring user activity; pending restore decision.");
return NO;
}
if ([userActivity.activityType isEqualToString:@"INStartVideoCallIntent"]) { if ([userActivity.activityType isEqualToString:@"INStartVideoCallIntent"]) {
if (!SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(10, 0)) { if (!SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(10, 0)) {
@ -838,6 +825,11 @@ static NSTimeInterval launchStartedAt;
} }
[AppReadiness runNowOrWhenAppDidBecomeReady:^{ [AppReadiness runNowOrWhenAppDidBecomeReady:^{
if (![self.tsAccountManager isRegisteredAndReady]) {
OWSLogInfo(@"Ignoring user activity; app not ready.");
return;
}
NSString *_Nullable phoneNumber = handle; NSString *_Nullable phoneNumber = handle;
if ([handle hasPrefix:CallKitCallManager.kAnonymousCallHandlePrefix]) { if ([handle hasPrefix:CallKitCallManager.kAnonymousCallHandlePrefix]) {
phoneNumber = [self.primaryStorage phoneNumberForCallKitId:handle]; phoneNumber = [self.primaryStorage phoneNumberForCallKitId:handle];
@ -895,6 +887,11 @@ static NSTimeInterval launchStartedAt;
} }
[AppReadiness runNowOrWhenAppDidBecomeReady:^{ [AppReadiness runNowOrWhenAppDidBecomeReady:^{
if (![self.tsAccountManager isRegisteredAndReady]) {
OWSLogInfo(@"Ignoring user activity; app not ready.");
return;
}
NSString *_Nullable phoneNumber = handle; NSString *_Nullable phoneNumber = handle;
if ([handle hasPrefix:CallKitCallManager.kAnonymousCallHandlePrefix]) { if ([handle hasPrefix:CallKitCallManager.kAnonymousCallHandlePrefix]) {
phoneNumber = [self.primaryStorage phoneNumberForCallKitId:handle]; phoneNumber = [self.primaryStorage phoneNumberForCallKitId:handle];
@ -969,8 +966,8 @@ static NSTimeInterval launchStartedAt;
OWSFailDebug(@"app launch failed"); OWSFailDebug(@"app launch failed");
return; return;
} }
if (self.backup.hasPendingRestoreDecision) { if (!(AppReadiness.isAppReady && [self.tsAccountManager isRegisteredAndReady])) {
OWSLogInfo(@"Ignoring remote notification; pending restore decision."); OWSLogInfo(@"Ignoring remote notification; app not ready.");
return; return;
} }
@ -987,8 +984,8 @@ static NSTimeInterval launchStartedAt;
OWSFailDebug(@"app launch failed"); OWSFailDebug(@"app launch failed");
return; return;
} }
if (self.backup.hasPendingRestoreDecision) { if (!(AppReadiness.isAppReady && [self.tsAccountManager isRegisteredAndReady])) {
OWSLogInfo(@"Ignoring remote notification; pending restore decision."); OWSLogInfo(@"Ignoring remote notification; app not ready.");
return; return;
} }
@ -1005,13 +1002,14 @@ static NSTimeInterval launchStartedAt;
OWSFailDebug(@"app launch failed"); OWSFailDebug(@"app launch failed");
return; return;
} }
if (self.backup.hasPendingRestoreDecision) {
OWSLogInfo(@"Ignoring local notification; pending restore decision.");
return;
}
OWSLogInfo(@"%@", notification); OWSLogInfo(@"%@", notification);
[AppReadiness runNowOrWhenAppDidBecomeReady:^{ [AppReadiness runNowOrWhenAppDidBecomeReady:^{
if (![self.tsAccountManager isRegisteredAndReady]) {
OWSLogInfo(@"Ignoring action; app not ready.");
return;
}
[[PushManager sharedManager] application:application didReceiveLocalNotification:notification]; [[PushManager sharedManager] application:application didReceiveLocalNotification:notification];
}]; }];
} }
@ -1028,11 +1026,6 @@ static NSTimeInterval launchStartedAt;
completionHandler(); completionHandler();
return; return;
} }
if (self.backup.hasPendingRestoreDecision) {
OWSLogInfo(@"Ignoring action; pending restore decision.");
completionHandler();
return;
}
// The docs for handleActionWithIdentifier:... state: // The docs for handleActionWithIdentifier:... state:
// "You must call [completionHandler] at the end of your method.". // "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 // https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623068-application?language=objc
[AppReadiness runNowOrWhenAppDidBecomeReady:^{ [AppReadiness runNowOrWhenAppDidBecomeReady:^{
if (![self.tsAccountManager isRegisteredAndReady]) {
OWSLogInfo(@"Ignoring action; app not ready.");
completionHandler();
return;
}
[[PushManager sharedManager] application:application [[PushManager sharedManager] application:application
handleActionWithIdentifier:identifier handleActionWithIdentifier:identifier
forLocalNotification:notification forLocalNotification:notification
@ -1063,11 +1062,6 @@ static NSTimeInterval launchStartedAt;
completionHandler(); completionHandler();
return; return;
} }
if (self.backup.hasPendingRestoreDecision) {
OWSLogInfo(@"Ignoring action; pending restore decision.");
completionHandler();
return;
}
// The docs for handleActionWithIdentifier:... state: // The docs for handleActionWithIdentifier:... state:
// "You must call [completionHandler] at the end of your method.". // "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 // https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623068-application?language=objc
[AppReadiness runNowOrWhenAppDidBecomeReady:^{ [AppReadiness runNowOrWhenAppDidBecomeReady:^{
if (![self.tsAccountManager isRegisteredAndReady]) {
OWSLogInfo(@"Ignoring action; app not ready.");
completionHandler();
return;
}
[[PushManager sharedManager] application:application [[PushManager sharedManager] application:application
handleActionWithIdentifier:identifier handleActionWithIdentifier:identifier
forLocalNotification:notification forLocalNotification:notification

@ -187,8 +187,8 @@ NSString *const kArchivedConversationsReuseIdentifier = @"kArchivedConversations
name:YapDatabaseModifiedExternallyNotification name:YapDatabaseModifiedExternallyNotification
object:nil]; object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(deregistrationStateDidChange:) selector:@selector(registrationStateDidChange:)
name:DeregistrationStateDidChangeNotification name:RegistrationStateDidChangeNotification
object:nil]; object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(outageStateDidChange:) selector:@selector(outageStateDidChange:)
@ -218,7 +218,7 @@ NSString *const kArchivedConversationsReuseIdentifier = @"kArchivedConversations
[self reloadTableViewData]; [self reloadTableViewData];
} }
- (void)deregistrationStateDidChange:(id)notification - (void)registrationStateDidChange:(id)notification
{ {
OWSAssertIsOnMainThread(); OWSAssertIsOnMainThread();

@ -18,7 +18,6 @@ NSString *const OWSPrimaryStorage_OWSBackupCollection = @"OWSPrimaryStorage_OWSB
NSString *const OWSBackup_IsBackupEnabledKey = @"OWSBackup_IsBackupEnabledKey"; NSString *const OWSBackup_IsBackupEnabledKey = @"OWSBackup_IsBackupEnabledKey";
NSString *const OWSBackup_LastExportSuccessDateKey = @"OWSBackup_LastExportSuccessDateKey"; NSString *const OWSBackup_LastExportSuccessDateKey = @"OWSBackup_LastExportSuccessDateKey";
NSString *const OWSBackup_LastExportFailureDateKey = @"OWSBackup_LastExportFailureDateKey"; NSString *const OWSBackup_LastExportFailureDateKey = @"OWSBackup_LastExportFailureDateKey";
NSString *const OWSBackup_HasPendingRestoreDecisionKey = @"OWSBackup_HasPendingRestoreDecisionKey";
NSString *NSStringForBackupExportState(OWSBackupState state) NSString *NSStringForBackupExportState(OWSBackupState state)
{ {
@ -247,20 +246,12 @@ NSString *NSStringForBackupImportState(OWSBackupState state)
- (BOOL)hasPendingRestoreDecision - (BOOL)hasPendingRestoreDecision
{ {
return [self.dbConnection boolForKey:OWSBackup_HasPendingRestoreDecisionKey return [self.tsAccountManager hasPendingBackupRestoreDecision];
inCollection:OWSPrimaryStorage_OWSBackupCollection
defaultValue:NO];
} }
- (void)setHasPendingRestoreDecision:(BOOL)value - (void)setHasPendingRestoreDecision:(BOOL)value
{ {
OWSLogInfo(@"%d", value); [self.tsAccountManager setHasPendingBackupRestoreDecision:value];
[self.dbConnection setBool:value
forKey:OWSBackup_HasPendingRestoreDecisionKey
inCollection:OWSPrimaryStorage_OWSBackupCollection];
[self postDidChangeNotification];
} }
- (BOOL)canBackupExport - (BOOL)canBackupExport
@ -426,6 +417,8 @@ NSString *NSStringForBackupImportState(OWSBackupState state)
OWSAssertIsOnMainThread(); OWSAssertIsOnMainThread();
[self ensureBackupExportState]; [self ensureBackupExportState];
[self postDidChangeNotification];
} }
#pragma mark - OWSBackupJobDelegate #pragma mark - OWSBackupJobDelegate

@ -9,7 +9,6 @@ NS_ASSUME_NONNULL_BEGIN
extern NSString *const TSRegistrationErrorDomain; extern NSString *const TSRegistrationErrorDomain;
extern NSString *const TSRegistrationErrorUserInfoHTTPStatus; extern NSString *const TSRegistrationErrorUserInfoHTTPStatus;
extern NSString *const RegistrationStateDidChangeNotification; extern NSString *const RegistrationStateDidChangeNotification;
extern NSString *const DeregistrationStateDidChangeNotification;
extern NSString *const kNSNotificationName_LocalNumberDidChange; extern NSString *const kNSNotificationName_LocalNumberDidChange;
@class AnyPromise; @class AnyPromise;
@ -21,6 +20,8 @@ typedef NS_ENUM(NSUInteger, OWSRegistrationState) {
OWSRegistrationState_Unregistered, OWSRegistrationState_Unregistered,
OWSRegistrationState_PendingBackupRestore, OWSRegistrationState_PendingBackupRestore,
OWSRegistrationState_Registered, OWSRegistrationState_Registered,
OWSRegistrationState_Deregistered,
OWSRegistrationState_Reregistering,
}; };
@interface TSAccountManager : NSObject @interface TSAccountManager : NSObject
@ -38,12 +39,15 @@ typedef NS_ENUM(NSUInteger, OWSRegistrationState) {
+ (instancetype)sharedInstance; + (instancetype)sharedInstance;
- (OWSRegistrationState)registrationState;
/** /**
* Returns if a user is registered or not * Returns if a user is registered or not
* *
* @return registered or not * @return registered or not
*/ */
- (BOOL)isRegistered; - (BOOL)isRegistered;
- (BOOL)isRegisteredAndReady;
/** /**
* Returns current phone number for this device, which may not yet have been registered. * 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; - (BOOL)isDeregistered;
- (void)setIsDeregistered:(BOOL)isDeregistered; - (void)setIsDeregistered:(BOOL)isDeregistered;
- (BOOL)hasPendingBackupRestoreDecision;
- (void)setHasPendingBackupRestoreDecision:(BOOL)value;
#pragma mark - Re-registration #pragma mark - Re-registration
// Re-registration is the process of re-registering _with the same phone number_. // Re-registration is the process of re-registering _with the same phone number_.

@ -28,13 +28,13 @@ NS_ASSUME_NONNULL_BEGIN
NSString *const TSRegistrationErrorDomain = @"TSRegistrationErrorDomain"; NSString *const TSRegistrationErrorDomain = @"TSRegistrationErrorDomain";
NSString *const TSRegistrationErrorUserInfoHTTPStatus = @"TSHTTPStatus"; NSString *const TSRegistrationErrorUserInfoHTTPStatus = @"TSHTTPStatus";
NSString *const RegistrationStateDidChangeNotification = @"RegistrationStateDidChangeNotification"; NSString *const RegistrationStateDidChangeNotification = @"RegistrationStateDidChangeNotification";
NSString *const DeregistrationStateDidChangeNotification = @"DeregistrationStateDidChangeNotification";
NSString *const kNSNotificationName_LocalNumberDidChange = @"kNSNotificationName_LocalNumberDidChange"; NSString *const kNSNotificationName_LocalNumberDidChange = @"kNSNotificationName_LocalNumberDidChange";
NSString *const TSAccountManager_RegisteredNumberKey = @"TSStorageRegisteredNumberKey"; NSString *const TSAccountManager_RegisteredNumberKey = @"TSStorageRegisteredNumberKey";
NSString *const TSAccountManager_IsDeregisteredKey = @"TSAccountManager_IsDeregisteredKey"; NSString *const TSAccountManager_IsDeregisteredKey = @"TSAccountManager_IsDeregisteredKey";
NSString *const TSAccountManager_ReregisteringPhoneNumberKey = @"TSAccountManager_ReregisteringPhoneNumberKey"; NSString *const TSAccountManager_ReregisteringPhoneNumberKey = @"TSAccountManager_ReregisteringPhoneNumberKey";
NSString *const TSAccountManager_LocalRegistrationIdKey = @"TSStorageLocalRegistrationId"; NSString *const TSAccountManager_LocalRegistrationIdKey = @"TSStorageLocalRegistrationId";
NSString *const TSAccountManager_HasPendingRestoreDecisionKey = @"TSAccountManager_HasPendingRestoreDecisionKey";
NSString *const TSAccountManager_UserAccountCollection = @"TSStorageUserAccountCollection"; NSString *const TSAccountManager_UserAccountCollection = @"TSStorageUserAccountCollection";
NSString *const TSAccountManager_ServerAuthToken = @"TSStorageServerAuthToken"; NSString *const TSAccountManager_ServerAuthToken = @"TSStorageServerAuthToken";
@ -135,6 +135,23 @@ NSString *const TSAccountManager_NeedsAccountAttributesUpdateKey = @"TSAccountMa
userInfo:nil]; 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 - (BOOL)isRegistered
{ {
@synchronized (self) { @synchronized (self) {
@ -148,6 +165,11 @@ NSString *const TSAccountManager_NeedsAccountAttributesUpdateKey = @"TSAccountMa
} }
} }
- (BOOL)isRegisteredAndReady
{
return self.registrationState == OWSRegistrationState_Registered;
}
- (void)didRegister - (void)didRegister
{ {
OWSLogInfo(@"didRegister"); OWSLogInfo(@"didRegister");
@ -159,14 +181,12 @@ NSString *const TSAccountManager_NeedsAccountAttributesUpdateKey = @"TSAccountMa
[self storeLocalNumber:phoneNumber]; [self storeLocalNumber:phoneNumber];
[[NSNotificationCenter defaultCenter] postNotificationNameAsync:RegistrationStateDidChangeNotification
object:nil
userInfo:nil];
// Warm these cached values. // Warm these cached values.
[self isRegistered]; [self isRegistered];
[self localNumber]; [self localNumber];
[self isDeregistered]; [self isDeregistered];
[self postRegistrationStateDidChangeNotification];
} }
+ (nullable NSString *)localNumber + (nullable NSString *)localNumber
@ -491,9 +511,7 @@ NSString *const TSAccountManager_NeedsAccountAttributesUpdateKey = @"TSAccountMa
// `RegistrationStateDidChangeNotification` which is only safe to fire after // `RegistrationStateDidChangeNotification` which is only safe to fire after
// the data store is reset. // the data store is reset.
[[NSNotificationCenter defaultCenter] postNotificationNameAsync:RegistrationStateDidChangeNotification [self.sharedInstance postRegistrationStateDidChangeNotification];
object:nil
userInfo:nil];
} }
failure:^(NSURLSessionDataTask *task, NSError *error) { failure:^(NSURLSessionDataTask *task, NSError *error) {
if (!IsNSErrorNetworkFailure(error)) { if (!IsNSErrorNetworkFailure(error)) {
@ -554,9 +572,7 @@ NSString *const TSAccountManager_NeedsAccountAttributesUpdateKey = @"TSAccountMa
inCollection:TSAccountManager_UserAccountCollection]; inCollection:TSAccountManager_UserAccountCollection];
}]; }];
[[NSNotificationCenter defaultCenter] postNotificationNameAsync:DeregistrationStateDidChangeNotification [self postRegistrationStateDidChangeNotification];
object:nil
userInfo:nil];
} }
#pragma mark - Re-registration #pragma mark - Re-registration
@ -583,6 +599,9 @@ NSString *const TSAccountManager_NeedsAccountAttributesUpdateKey = @"TSAccountMa
forKey:TSAccountManager_ReregisteringPhoneNumberKey forKey:TSAccountManager_ReregisteringPhoneNumberKey
inCollection:TSAccountManager_UserAccountCollection]; inCollection:TSAccountManager_UserAccountCollection];
}]; }];
[self postRegistrationStateDidChangeNotification];
return YES; return YES;
} }
} }
@ -604,6 +623,24 @@ NSString *const TSAccountManager_NeedsAccountAttributesUpdateKey = @"TSAccountMa
inCollection:TSAccountManager_UserAccountCollection]; 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 - (BOOL)isManualMessageFetchEnabled
{ {
return [self.dbConnection boolForKey:TSAccountManager_ManualMessageFetchKey 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 @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

Loading…
Cancel
Save