session-ios/Signal/src/util/MainAppContext.m

236 lines
7.3 KiB
Mathematica
Raw Normal View History

2017-11-29 20:51:39 +01:00
//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
2017-11-29 20:51:39 +01:00
//
#import "MainAppContext.h"
#import "Signal-Swift.h"
#import <SignalMessaging/Environment.h>
#import <SignalMessaging/OWSProfileManager.h>
#import <SignalMessaging/SignalMessaging-Swift.h>
#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;
@end
#pragma mark -
2017-11-29 20:51:39 +01:00
@implementation MainAppContext
@synthesize mainWindow = _mainWindow;
- (instancetype)init
{
self = [super init];
if (!self) {
return self;
}
2018-05-31 16:54:51 +02:00
self.reportedApplicationState = UIApplicationStateInactive;
_appLaunchTime = [NSDate new];
[[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];
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-05-31 16:54:51 +02:00
self.reportedApplicationState = UIApplicationStateInactive;
DDLogInfo(@"%@ %s", self.logTag, __PRETTY_FUNCTION__);
[NSNotificationCenter.defaultCenter postNotificationName:OWSApplicationWillEnterForegroundNotification object:nil];
}
- (void)applicationDidEnterBackground:(NSNotification *)notification
{
2018-04-11 21:17:34 +02:00
OWSAssertIsOnMainThread();
2018-05-31 16:54:51 +02:00
self.reportedApplicationState = UIApplicationStateBackground;
DDLogInfo(@"%@ %s", self.logTag, __PRETTY_FUNCTION__);
[DDLog flushLog];
[NSNotificationCenter.defaultCenter postNotificationName:OWSApplicationDidEnterBackgroundNotification object:nil];
}
- (void)applicationWillResignActive:(NSNotification *)notification
{
2018-04-11 21:17:34 +02:00
OWSAssertIsOnMainThread();
2018-05-31 16:54:51 +02:00
self.reportedApplicationState = UIApplicationStateInactive;
DDLogInfo(@"%@ %s", self.logTag, __PRETTY_FUNCTION__);
[DDLog flushLog];
[NSNotificationCenter.defaultCenter postNotificationName:OWSApplicationWillResignActiveNotification object:nil];
}
- (void)applicationDidBecomeActive:(NSNotification *)notification
{
2018-04-11 21:17:34 +02:00
OWSAssertIsOnMainThread();
2018-05-31 16:54:51 +02:00
self.reportedApplicationState = UIApplicationStateActive;
DDLogInfo(@"%@ %s", self.logTag, __PRETTY_FUNCTION__);
[NSNotificationCenter.defaultCenter postNotificationName:OWSApplicationDidBecomeActiveNotification object:nil];
}
- (void)applicationWillTerminate:(NSNotification *)notification
{
2018-04-11 21:17:34 +02:00
OWSAssertIsOnMainThread();
DDLogInfo(@"%@ %s", self.logTag, __PRETTY_FUNCTION__);
[DDLog flushLog];
}
#pragma mark -
2017-11-29 20:51:39 +01:00
- (BOOL)isMainApp
{
return YES;
}
- (BOOL)isMainAppAndActive
{
return [UIApplication sharedApplication].applicationState == UIApplicationStateActive;
}
- (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;
}
- (void)setStatusBarHidden:(BOOL)isHidden animated:(BOOL)isAnimated
{
[[UIApplication sharedApplication] setStatusBarHidden:isHidden animated:isAnimated];
}
- (CGFloat)statusBarHeight
{
return [UIApplication sharedApplication].statusBarFrame.size.height;
}
- (BOOL)isInBackground
{
2018-05-31 16:54:51 +02:00
return self.reportedApplicationState == UIApplicationStateBackground;
}
- (BOOL)isAppForegroundAndActive
{
return self.reportedApplicationState == UIApplicationStateActive;
}
2017-11-29 20:51:39 +01:00
- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:
(BackgroundTaskExpirationHandler)expirationHandler
{
return [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:expirationHandler];
}
- (void)endBackgroundTask:(UIBackgroundTaskIdentifier)backgroundTaskIdentifier
{
[UIApplication.sharedApplication endBackgroundTask:backgroundTaskIdentifier];
}
2018-03-02 05:30:28 +01:00
- (void)ensureSleepBlocking:(BOOL)shouldBeBlocking blockingObjects:(NSArray<id> *)blockingObjects
{
if (UIApplication.sharedApplication.isIdleTimerDisabled != shouldBeBlocking) {
if (shouldBeBlocking) {
NSMutableString *logString = [NSMutableString
stringWithFormat:@"%@ Blocking sleep because of: %@", self.logTag, blockingObjects.firstObject];
if (blockingObjects.count > 1) {
[logString appendString:[NSString stringWithFormat:@"(and %lu others)", blockingObjects.count - 1]];
}
DDLogInfo(@"%@", logString);
} else {
DDLogInfo(@"%@ Unblocking Sleep.", self.logTag);
}
}
UIApplication.sharedApplication.idleTimerDisabled = shouldBeBlocking;
}
- (void)setMainAppBadgeNumber:(NSInteger)value
{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:value];
}
- (nullable UIViewController *)frontmostViewController
{
return UIApplication.sharedApplication.frontmostViewControllerIgnoringAlerts;
}
- (nullable UIAlertAction *)openSystemSettingsAction
{
return [UIAlertAction actionWithTitle:CommonStrings.openSettingsButton
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *_Nonnull action) {
[UIApplication.sharedApplication openSystemSettings];
}];
}
- (void)doMultiDeviceUpdateWithProfileKey:(OWSAES256Key *)profileKey
{
OWSAssert(profileKey);
[MultiDeviceProfileKeyUpdateJob runWithProfileKey:profileKey
identityManager:OWSIdentityManager.sharedManager
messageSender:Environment.current.messageSender
profileManager:OWSProfileManager.sharedManager];
}
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];
}
2017-11-29 20:51:39 +01:00
@end
2017-11-30 16:10:30 +01:00
NS_ASSUME_NONNULL_END