2017-11-29 20:51:39 +01:00
|
|
|
//
|
2018-01-10 16:54:17 +01:00
|
|
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
2017-11-29 20:51:39 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
#import "MainAppContext.h"
|
2017-12-01 23:22:40 +01:00
|
|
|
#import "Signal-Swift.h"
|
2018-09-21 21:41:10 +02:00
|
|
|
#import <SignalCoreKit/Threading.h>
|
2017-12-04 16:35:47 +01:00
|
|
|
#import <SignalMessaging/Environment.h>
|
|
|
|
#import <SignalMessaging/OWSProfileManager.h>
|
2018-02-06 17:32:09 +01:00
|
|
|
#import <SignalMessaging/SignalMessaging-Swift.h>
|
2017-12-04 16:35:47 +01:00
|
|
|
#import <SignalServiceKit/OWSIdentityManager.h>
|
2017-11-29 20:51:39 +01:00
|
|
|
|
2017-11-30 16:10:30 +01:00
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
2018-05-31 16:54:51 +02:00
|
|
|
@interface MainAppContext ()
|
|
|
|
|
|
|
|
@property (atomic) UIApplicationState reportedApplicationState;
|
|
|
|
|
2018-08-02 21:18:40 +02:00
|
|
|
@property (nonatomic, nullable) NSMutableArray<AppActiveBlock> *appActiveBlocks;
|
|
|
|
|
2018-05-31 16:54:51 +02:00
|
|
|
@end
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
2017-11-29 20:51:39 +01:00
|
|
|
@implementation MainAppContext
|
|
|
|
|
2018-04-16 23:19:06 +02:00
|
|
|
@synthesize mainWindow = _mainWindow;
|
2018-08-06 21:46:56 +02:00
|
|
|
@synthesize appLaunchTime = _appLaunchTime;
|
2018-04-16 23:19:06 +02:00
|
|
|
|
2017-12-12 22:31:03 +01:00
|
|
|
- (instancetype)init
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
|
|
|
|
if (!self) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-05-31 16:54:51 +02:00
|
|
|
self.reportedApplicationState = UIApplicationStateInactive;
|
|
|
|
|
2018-08-06 19:48:50 +02:00
|
|
|
_appLaunchTime = [NSDate new];
|
|
|
|
|
2017-12-12 22:31:03 +01:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(applicationWillEnterForeground:)
|
|
|
|
name:UIApplicationWillEnterForegroundNotification
|
|
|
|
object:nil];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(applicationDidEnterBackground:)
|
|
|
|
name:UIApplicationDidEnterBackgroundNotification
|
|
|
|
object:nil];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(applicationWillResignActive:)
|
|
|
|
name:UIApplicationWillResignActiveNotification
|
|
|
|
object:nil];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(applicationDidBecomeActive:)
|
|
|
|
name:UIApplicationDidBecomeActiveNotification
|
|
|
|
object:nil];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(applicationWillTerminate:)
|
|
|
|
name:UIApplicationWillTerminateNotification
|
|
|
|
object:nil];
|
|
|
|
|
2018-08-02 21:18:40 +02:00
|
|
|
// We can't use OWSSingletonAssert() since it uses the app context.
|
|
|
|
|
|
|
|
self.appActiveBlocks = [NSMutableArray new];
|
|
|
|
|
2017-12-12 22:31:03 +01:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Notifications
|
|
|
|
|
|
|
|
- (void)applicationWillEnterForeground:(NSNotification *)notification
|
|
|
|
{
|
2018-04-11 21:17:34 +02:00
|
|
|
OWSAssertIsOnMainThread();
|
2018-01-10 16:54:17 +01:00
|
|
|
|
2018-05-31 16:54:51 +02:00
|
|
|
self.reportedApplicationState = UIApplicationStateInactive;
|
|
|
|
|
2018-08-27 18:09:39 +02:00
|
|
|
OWSLogInfo(@"");
|
2018-01-10 16:54:17 +01:00
|
|
|
|
|
|
|
[NSNotificationCenter.defaultCenter postNotificationName:OWSApplicationWillEnterForegroundNotification object:nil];
|
2017-12-12 22:31:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)applicationDidEnterBackground:(NSNotification *)notification
|
|
|
|
{
|
2018-04-11 21:17:34 +02:00
|
|
|
OWSAssertIsOnMainThread();
|
2018-01-10 16:54:17 +01:00
|
|
|
|
2018-05-31 16:54:51 +02:00
|
|
|
self.reportedApplicationState = UIApplicationStateBackground;
|
|
|
|
|
2018-08-27 18:09:39 +02:00
|
|
|
OWSLogInfo(@"");
|
2017-12-12 22:31:03 +01:00
|
|
|
[DDLog flushLog];
|
2018-01-10 16:54:17 +01:00
|
|
|
|
|
|
|
[NSNotificationCenter.defaultCenter postNotificationName:OWSApplicationDidEnterBackgroundNotification object:nil];
|
2017-12-12 22:31:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)applicationWillResignActive:(NSNotification *)notification
|
|
|
|
{
|
2018-04-11 21:17:34 +02:00
|
|
|
OWSAssertIsOnMainThread();
|
2018-01-10 16:54:17 +01:00
|
|
|
|
2018-05-31 16:54:51 +02:00
|
|
|
self.reportedApplicationState = UIApplicationStateInactive;
|
|
|
|
|
2018-08-27 18:09:39 +02:00
|
|
|
OWSLogInfo(@"");
|
2017-12-12 22:31:03 +01:00
|
|
|
[DDLog flushLog];
|
2018-01-10 16:54:17 +01:00
|
|
|
|
|
|
|
[NSNotificationCenter.defaultCenter postNotificationName:OWSApplicationWillResignActiveNotification object:nil];
|
2017-12-12 22:31:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)applicationDidBecomeActive:(NSNotification *)notification
|
|
|
|
{
|
2018-04-11 21:17:34 +02:00
|
|
|
OWSAssertIsOnMainThread();
|
2018-01-10 16:54:17 +01:00
|
|
|
|
2018-05-31 16:54:51 +02:00
|
|
|
self.reportedApplicationState = UIApplicationStateActive;
|
|
|
|
|
2018-08-27 18:09:39 +02:00
|
|
|
OWSLogInfo(@"");
|
2018-01-10 16:54:17 +01:00
|
|
|
|
|
|
|
[NSNotificationCenter.defaultCenter postNotificationName:OWSApplicationDidBecomeActiveNotification object:nil];
|
2018-08-02 21:18:40 +02:00
|
|
|
|
|
|
|
[self runAppActiveBlocks];
|
2017-12-12 22:31:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)applicationWillTerminate:(NSNotification *)notification
|
|
|
|
{
|
2018-04-11 21:17:34 +02:00
|
|
|
OWSAssertIsOnMainThread();
|
2018-01-10 16:54:17 +01:00
|
|
|
|
2018-08-27 18:09:39 +02:00
|
|
|
OWSLogInfo(@"");
|
2017-12-12 22:31:03 +01:00
|
|
|
[DDLog flushLog];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
2017-11-29 20:51:39 +01:00
|
|
|
- (BOOL)isMainApp
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isMainAppAndActive
|
|
|
|
{
|
|
|
|
return [UIApplication sharedApplication].applicationState == UIApplicationStateActive;
|
|
|
|
}
|
|
|
|
|
2017-12-02 00:43:40 +01:00
|
|
|
- (BOOL)isRTL
|
|
|
|
{
|
2018-06-29 23:00:22 +02:00
|
|
|
static BOOL isRTL = NO;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
isRTL = [[UIApplication sharedApplication] userInterfaceLayoutDirection]
|
|
|
|
== UIUserInterfaceLayoutDirectionRightToLeft;
|
|
|
|
});
|
|
|
|
return isRTL;
|
2017-12-02 00:43:40 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 23:55:53 +01:00
|
|
|
- (void)setStatusBarHidden:(BOOL)isHidden animated:(BOOL)isAnimated
|
|
|
|
{
|
|
|
|
[[UIApplication sharedApplication] setStatusBarHidden:isHidden animated:isAnimated];
|
|
|
|
}
|
|
|
|
|
2018-05-18 15:12:01 +02:00
|
|
|
- (CGFloat)statusBarHeight
|
|
|
|
{
|
|
|
|
return [UIApplication sharedApplication].statusBarFrame.size.height;
|
|
|
|
}
|
|
|
|
|
2018-01-12 20:24:35 +01:00
|
|
|
- (BOOL)isInBackground
|
|
|
|
{
|
2018-05-31 16:54:51 +02:00
|
|
|
return self.reportedApplicationState == UIApplicationStateBackground;
|
2017-11-29 22:32:49 +01:00
|
|
|
}
|
|
|
|
|
2018-05-31 17:27:07 +02:00
|
|
|
- (BOOL)isAppForegroundAndActive
|
|
|
|
{
|
|
|
|
return self.reportedApplicationState == UIApplicationStateActive;
|
|
|
|
}
|
|
|
|
|
2017-11-29 20:51:39 +01:00
|
|
|
- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:
|
|
|
|
(BackgroundTaskExpirationHandler)expirationHandler
|
|
|
|
{
|
|
|
|
return [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:expirationHandler];
|
|
|
|
}
|
|
|
|
|
2017-11-29 21:27:19 +01:00
|
|
|
- (void)endBackgroundTask:(UIBackgroundTaskIdentifier)backgroundTaskIdentifier
|
|
|
|
{
|
|
|
|
[UIApplication.sharedApplication endBackgroundTask:backgroundTaskIdentifier];
|
|
|
|
}
|
|
|
|
|
2018-03-02 05:30:28 +01:00
|
|
|
- (void)ensureSleepBlocking:(BOOL)shouldBeBlocking blockingObjects:(NSArray<id> *)blockingObjects
|
2017-12-06 23:44:00 +01:00
|
|
|
{
|
|
|
|
if (UIApplication.sharedApplication.isIdleTimerDisabled != shouldBeBlocking) {
|
|
|
|
if (shouldBeBlocking) {
|
2018-08-27 18:55:37 +02:00
|
|
|
NSMutableString *logString =
|
|
|
|
[NSMutableString stringWithFormat:@"Blocking sleep because of: %@", blockingObjects.firstObject];
|
2017-12-06 23:44:00 +01:00
|
|
|
if (blockingObjects.count > 1) {
|
|
|
|
[logString appendString:[NSString stringWithFormat:@"(and %lu others)", blockingObjects.count - 1]];
|
|
|
|
}
|
2018-08-27 18:00:28 +02:00
|
|
|
OWSLogInfo(@"%@", logString);
|
2017-12-06 23:44:00 +01:00
|
|
|
} else {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogInfo(@"Unblocking Sleep.");
|
2017-12-06 23:44:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
UIApplication.sharedApplication.idleTimerDisabled = shouldBeBlocking;
|
|
|
|
}
|
|
|
|
|
2017-11-29 21:27:19 +01:00
|
|
|
- (void)setMainAppBadgeNumber:(NSInteger)value
|
|
|
|
{
|
|
|
|
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:value];
|
|
|
|
}
|
|
|
|
|
2017-12-01 23:22:40 +01:00
|
|
|
- (nullable UIViewController *)frontmostViewController
|
2017-12-01 23:10:14 +01:00
|
|
|
{
|
2017-12-01 23:22:40 +01:00
|
|
|
return UIApplication.sharedApplication.frontmostViewControllerIgnoringAlerts;
|
|
|
|
}
|
|
|
|
|
2018-02-06 17:32:09 +01:00
|
|
|
- (nullable UIAlertAction *)openSystemSettingsAction
|
2017-12-01 23:22:40 +01:00
|
|
|
{
|
2018-02-06 17:32:09 +01:00
|
|
|
return [UIAlertAction actionWithTitle:CommonStrings.openSettingsButton
|
|
|
|
style:UIAlertActionStyleDefault
|
|
|
|
handler:^(UIAlertAction *_Nonnull action) {
|
|
|
|
[UIApplication.sharedApplication openSystemSettings];
|
|
|
|
}];
|
2017-12-01 23:10:14 +01:00
|
|
|
}
|
|
|
|
|
2017-12-04 18:38:44 +01:00
|
|
|
- (BOOL)isRunningTests
|
|
|
|
{
|
|
|
|
return getenv("runningTests_dontStartApp");
|
|
|
|
}
|
|
|
|
|
2017-12-04 22:09:26 +01:00
|
|
|
- (void)setNetworkActivityIndicatorVisible:(BOOL)value
|
|
|
|
{
|
|
|
|
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:value];
|
|
|
|
}
|
|
|
|
|
2018-08-02 21:18:40 +02:00
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
- (void)runNowOrWhenMainAppIsActive:(AppActiveBlock)block
|
|
|
|
{
|
2018-09-06 19:01:24 +02:00
|
|
|
OWSAssertDebug(block);
|
2018-08-02 21:18:40 +02:00
|
|
|
|
|
|
|
DispatchMainThreadSafe(^{
|
|
|
|
if (self.isMainAppAndActive) {
|
|
|
|
// App active blocks typically will be used to safely access the
|
|
|
|
// shared data container, so use a background task to protect this
|
|
|
|
// work.
|
|
|
|
OWSBackgroundTask *_Nullable backgroundTask =
|
|
|
|
[OWSBackgroundTask backgroundTaskWithLabelStr:__PRETTY_FUNCTION__];
|
|
|
|
block();
|
2018-09-06 19:01:24 +02:00
|
|
|
OWSAssertDebug(backgroundTask);
|
2018-08-02 21:18:40 +02:00
|
|
|
backgroundTask = nil;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
[self.appActiveBlocks addObject:block];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)runAppActiveBlocks
|
|
|
|
{
|
|
|
|
OWSAssertIsOnMainThread();
|
2018-09-06 19:01:24 +02:00
|
|
|
OWSAssertDebug(self.isMainAppAndActive);
|
2018-08-02 21:18:40 +02:00
|
|
|
|
|
|
|
// App active blocks typically will be used to safely access the
|
|
|
|
// shared data container, so use a background task to protect this
|
|
|
|
// work.
|
|
|
|
OWSBackgroundTask *_Nullable backgroundTask = [OWSBackgroundTask backgroundTaskWithLabelStr:__PRETTY_FUNCTION__];
|
|
|
|
|
|
|
|
NSArray<AppActiveBlock> *appActiveBlocks = [self.appActiveBlocks copy];
|
|
|
|
[self.appActiveBlocks removeAllObjects];
|
|
|
|
for (AppActiveBlock block in appActiveBlocks) {
|
|
|
|
block();
|
|
|
|
}
|
|
|
|
|
2018-09-06 19:01:24 +02:00
|
|
|
OWSAssertDebug(backgroundTask);
|
2018-08-02 21:18:40 +02:00
|
|
|
backgroundTask = nil;
|
|
|
|
}
|
|
|
|
|
2018-09-07 22:05:57 +02:00
|
|
|
- (id<SSKKeychainStorage>)keychainStorage
|
2018-08-31 19:01:47 +02:00
|
|
|
{
|
2018-09-07 22:05:57 +02:00
|
|
|
return [SSKDefaultKeychainStorage shared];
|
2018-08-31 19:01:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)appDocumentDirectoryPath
|
|
|
|
{
|
|
|
|
NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
|
|
NSURL *documentDirectoryURL =
|
|
|
|
[[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
|
|
|
|
return [documentDirectoryURL path];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)appSharedDataDirectoryPath
|
|
|
|
{
|
|
|
|
NSURL *groupContainerDirectoryURL =
|
|
|
|
[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:SignalApplicationGroup];
|
|
|
|
return [groupContainerDirectoryURL path];
|
|
|
|
}
|
|
|
|
|
2018-10-22 20:43:07 +02:00
|
|
|
- (NSUserDefaults *)appUserDefaults
|
|
|
|
{
|
|
|
|
return [[NSUserDefaults alloc] initWithSuiteName:SignalApplicationGroup];
|
|
|
|
}
|
|
|
|
|
2017-11-29 20:51:39 +01:00
|
|
|
@end
|
2017-11-30 16:10:30 +01:00
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|