From ffa69b3502236f06fd8975373197c5781f2fb4ef Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Wed, 29 Nov 2017 11:01:30 -0500 Subject: [PATCH] Add application context class. --- Signal/src/AppDelegate.m | 25 ++++++++++++++++++++++++- SignalServiceKit/src/Util/AppContext.h | 18 ++++++++++++++++++ SignalServiceKit/src/Util/AppContext.m | 21 +++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100755 SignalServiceKit/src/Util/AppContext.h create mode 100755 SignalServiceKit/src/Util/AppContext.m diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index fff346c7c..e06a473b0 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -25,6 +25,7 @@ #import "ViewControllerUtils.h" #import #import +#import #import #import #import @@ -50,7 +51,7 @@ static NSString *const kInitialViewControllerIdentifier = @"UserInitialViewContr static NSString *const kURLSchemeSGNLKey = @"sgnl"; static NSString *const kURLHostVerifyPrefix = @"verify"; -@interface AppDelegate () +@interface AppDelegate () @property (nonatomic) UIWindow *screenProtectionWindow; @property (nonatomic) OWSContactsSyncing *contactsSyncing; @@ -89,6 +90,10 @@ static NSString *const kURLHostVerifyPrefix = @"verify"; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { + + // This should be the first thing we do. + SetCurrentAppContext(self); + BOOL loggingIsEnabled; #ifdef DEBUG // Specified at Product -> Scheme -> Edit Scheme -> Test -> Arguments -> Environment to avoid things like @@ -968,4 +973,22 @@ static NSString *const kURLHostVerifyPrefix = @"verify"; [AppUpdateNag.sharedInstance showAppUpgradeNagIfNecessary]; } +#pragma mark - AppContext + +- (BOOL)isMainApp +{ + return YES; +} + +- (BOOL)isMainAppAndActive +{ + return [UIApplication sharedApplication].applicationState == UIApplicationStateActive; +} + +- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler: + (BackgroundTaskExpirationHandler)expirationHandler +{ + return [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:expirationHandler]; +} + @end diff --git a/SignalServiceKit/src/Util/AppContext.h b/SignalServiceKit/src/Util/AppContext.h new file mode 100755 index 000000000..705c4272e --- /dev/null +++ b/SignalServiceKit/src/Util/AppContext.h @@ -0,0 +1,18 @@ +// +// Copyright (c) 2017 Open Whisper Systems. All rights reserved. +// + +typedef void (^BackgroundTaskExpirationHandler)(void); + +@protocol AppContext + +- (BOOL)isMainApp; +- (BOOL)isMainAppAndActive; + +- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler: + (BackgroundTaskExpirationHandler)expirationHandler; + +@end + +id CurrentAppContext(); +void SetCurrentAppContext(id appContext); diff --git a/SignalServiceKit/src/Util/AppContext.m b/SignalServiceKit/src/Util/AppContext.m new file mode 100755 index 000000000..816eec5c4 --- /dev/null +++ b/SignalServiceKit/src/Util/AppContext.m @@ -0,0 +1,21 @@ +// +// Copyright (c) 2017 Open Whisper Systems. All rights reserved. +// + +#import "AppContext.h" + +static id currentAppContext = nil; + +id CurrentAppContext() +{ + OWSCAssert(currentAppContext); + + return currentAppContext; +} + +void SetCurrentAppContext(id appContext) +{ + OWSCAssert(!currentAppContext); + + currentAppContext = appContext; +}