From 2b528ad8944885b2ea0a66ba8b5200b7671bec97 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 12 Jan 2018 14:24:35 -0500 Subject: [PATCH] Don't use mainApplicationState in business logic. --- Signal/src/util/MainAppContext.m | 5 +++++ SignalMessaging/Views/ThreadViewHelper.m | 3 +-- SignalMessaging/contacts/SystemContactsFetcher.swift | 10 ++++------ SignalServiceKit/src/Storage/OWSStorage.m | 6 ++---- SignalServiceKit/src/Util/AppContext.h | 6 +++++- .../utils/ShareAppExtensionContext.m | 12 ++++++++++++ 6 files changed, 29 insertions(+), 13 deletions(-) diff --git a/Signal/src/util/MainAppContext.m b/Signal/src/util/MainAppContext.m index 1b4f45d9b..55097dfbf 100644 --- a/Signal/src/util/MainAppContext.m +++ b/Signal/src/util/MainAppContext.m @@ -120,6 +120,11 @@ NS_ASSUME_NONNULL_BEGIN [[UIApplication sharedApplication] setStatusBarStyle:statusBarStyle]; } +- (BOOL)isInBackground +{ + return [UIApplication sharedApplication].applicationState == UIApplicationStateBackground; +} + - (UIApplicationState)mainApplicationState { return [UIApplication sharedApplication].applicationState; diff --git a/SignalMessaging/Views/ThreadViewHelper.m b/SignalMessaging/Views/ThreadViewHelper.m index 7f282bc1c..878f1f6b7 100644 --- a/SignalMessaging/Views/ThreadViewHelper.m +++ b/SignalMessaging/Views/ThreadViewHelper.m @@ -64,8 +64,7 @@ NS_ASSUME_NONNULL_BEGIN name:OWSApplicationDidEnterBackgroundNotification object:nil]; - self.shouldObserveDBModifications - = !(CurrentAppContext().isMainApp && CurrentAppContext().mainApplicationState == UIApplicationStateBackground); + self.shouldObserveDBModifications = !CurrentAppContext().isInBackground; } - (void)applicationWillEnterForeground:(NSNotification *)notification diff --git a/SignalMessaging/contacts/SystemContactsFetcher.swift b/SignalMessaging/contacts/SystemContactsFetcher.swift index 75742594d..aecbf6d88 100644 --- a/SignalMessaging/contacts/SystemContactsFetcher.swift +++ b/SignalMessaging/contacts/SystemContactsFetcher.swift @@ -401,12 +401,10 @@ public class SystemContactsFetcher: NSObject { switch authorizationStatus { case .notDetermined: - if CurrentAppContext().isMainApp { - if CurrentAppContext().mainApplicationState() == .background { - Logger.error("\(self.TAG) do not request contacts permission when app is in background") - completion(nil) - return - } + if CurrentAppContext().isInBackground() { + Logger.error("\(self.TAG) do not request contacts permission when app is in background") + completion(nil) + return } self.contactStoreAdapter.requestAccess { (granted, error) in if let error = error { diff --git a/SignalServiceKit/src/Storage/OWSStorage.m b/SignalServiceKit/src/Storage/OWSStorage.m index 9ffdbdc9f..f1e0db456 100644 --- a/SignalServiceKit/src/Storage/OWSStorage.m +++ b/SignalServiceKit/src/Storage/OWSStorage.m @@ -531,8 +531,7 @@ static NSString *keychainDBPassAccount = @"TSDatabasePass"; [DDLog flushLog]; if (CurrentAppContext().isMainApp) { - UIApplicationState applicationState = CurrentAppContext().mainApplicationState; - if (applicationState == UIApplicationStateBackground) { + if (CurrentAppContext().isInBackground) { // TODO: Rather than crash here, we should detect the situation earlier // and exit gracefully - (in the app delegate?). See the ` // This is a last ditch effort to avoid blowing away the user's database. @@ -572,8 +571,7 @@ static NSString *keychainDBPassAccount = @"TSDatabasePass"; - (void)backgroundedAppDatabasePasswordInaccessibleWithErrorDescription:(NSString *)errorDescription { - OWSAssert( - CurrentAppContext().isMainApp && CurrentAppContext().mainApplicationState == UIApplicationStateBackground); + OWSAssert(CurrentAppContext().isMainApp && CurrentAppContext().isInBackground); // Sleep to give analytics events time to be delivered. [NSThread sleepForTimeInterval:5.0f]; diff --git a/SignalServiceKit/src/Util/AppContext.h b/SignalServiceKit/src/Util/AppContext.h index f32bf894e..b8c596b73 100755 --- a/SignalServiceKit/src/Util/AppContext.h +++ b/SignalServiceKit/src/Util/AppContext.h @@ -34,9 +34,13 @@ typedef void (^BackgroundTaskExpirationHandler)(void); // Should only be called if isMainApp is YES. // -// In general, isMainAppAndActive will probably yield more readable code. +// Wherever possible, use isMainAppAndActive or isInBackground instead. +// This should only be used by debugging/logging code. - (UIApplicationState)mainApplicationState; +// Similar to UIApplicationStateBackground, but works in SAE. +- (BOOL)isInBackground; + // Should start a background task if isMainApp is YES. // Should just return UIBackgroundTaskInvalid if isMainApp is NO. - (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler: diff --git a/SignalShareExtension/utils/ShareAppExtensionContext.m b/SignalShareExtension/utils/ShareAppExtensionContext.m index f0c52b946..220d53430 100644 --- a/SignalShareExtension/utils/ShareAppExtensionContext.m +++ b/SignalShareExtension/utils/ShareAppExtensionContext.m @@ -11,6 +11,7 @@ NS_ASSUME_NONNULL_BEGIN @interface ShareAppExtensionContext () @property (nonatomic) UIViewController *rootViewController; +@property (atomic) BOOL isSAEInBackground; @end @@ -63,6 +64,8 @@ NS_ASSUME_NONNULL_BEGIN DDLogInfo(@"%@ %s", self.logTag, __PRETTY_FUNCTION__); + self.isSAEInBackground = NO; + [NSNotificationCenter.defaultCenter postNotificationName:OWSApplicationDidBecomeActiveNotification object:nil]; } @@ -83,6 +86,8 @@ NS_ASSUME_NONNULL_BEGIN DDLogInfo(@"%@ %s", self.logTag, __PRETTY_FUNCTION__); [DDLog flushLog]; + self.isSAEInBackground = YES; + [NSNotificationCenter.defaultCenter postNotificationName:OWSApplicationDidEnterBackgroundNotification object:nil]; } @@ -92,6 +97,8 @@ NS_ASSUME_NONNULL_BEGIN DDLogInfo(@"%@ %s", self.logTag, __PRETTY_FUNCTION__); + self.isSAEInBackground = NO; + [NSNotificationCenter.defaultCenter postNotificationName:OWSApplicationWillEnterForegroundNotification object:nil]; } @@ -121,6 +128,11 @@ NS_ASSUME_NONNULL_BEGIN DDLogInfo(@"Ignoring request to set status bar style since we're in an app extension"); } +- (BOOL)isInBackground +{ + return self.isSAEInBackground; +} + - (UIApplicationState)mainApplicationState { OWSFail(@"%@ called %s.", self.logTag, __PRETTY_FUNCTION__);