2017-11-30 20:30:54 +01:00
|
|
|
//
|
2019-02-13 22:18:00 +01:00
|
|
|
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
|
2017-11-30 20:30:54 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
#import "ShareAppExtensionContext.h"
|
2020-11-11 07:45:50 +01:00
|
|
|
#import <SignalUtilitiesKit/UIViewController+OWS.h>
|
2020-11-25 06:15:16 +01:00
|
|
|
#import <SessionMessagingKit/OWSStorage.h>
|
2020-11-11 07:45:50 +01:00
|
|
|
#import <SignalUtilitiesKit/SignalUtilitiesKit-Swift.h>
|
|
|
|
#import <SignalUtilitiesKit/TSConstants.h>
|
2020-11-26 00:37:56 +01:00
|
|
|
#import <SessionUtilitiesKit/SessionUtilitiesKit-Swift.h>
|
2017-11-30 20:30:54 +01:00
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
2017-12-01 23:10:14 +01:00
|
|
|
@interface ShareAppExtensionContext ()
|
|
|
|
|
|
|
|
@property (nonatomic) UIViewController *rootViewController;
|
2018-05-31 16:54:51 +02:00
|
|
|
|
|
|
|
@property (atomic) UIApplicationState reportedApplicationState;
|
2017-12-01 23:10:14 +01:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
2017-11-30 20:30:54 +01:00
|
|
|
@implementation ShareAppExtensionContext
|
|
|
|
|
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-01 23:10:14 +01:00
|
|
|
- (instancetype)initWithRootViewController:(UIViewController *)rootViewController
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
|
|
|
|
if (!self) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-09-06 19:01:24 +02:00
|
|
|
OWSAssertDebug(rootViewController);
|
2017-12-01 23:10:14 +01:00
|
|
|
|
|
|
|
_rootViewController = rootViewController;
|
|
|
|
|
2018-06-11 20:49:27 +02:00
|
|
|
self.reportedApplicationState = UIApplicationStateActive;
|
2018-05-31 16:54:51 +02:00
|
|
|
|
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(extensionHostDidBecomeActive:)
|
|
|
|
name:NSExtensionHostDidBecomeActiveNotification
|
|
|
|
object:nil];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(extensionHostWillResignActive:)
|
|
|
|
name:NSExtensionHostWillResignActiveNotification
|
|
|
|
object:nil];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(extensionHostDidEnterBackground:)
|
|
|
|
name:NSExtensionHostDidEnterBackgroundNotification
|
|
|
|
object:nil];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(extensionHostWillEnterForeground:)
|
|
|
|
name:NSExtensionHostWillEnterForegroundNotification
|
|
|
|
object:nil];
|
|
|
|
|
2017-12-01 23:10:14 +01:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-12-12 22:31:03 +01:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Notifications
|
|
|
|
|
|
|
|
- (void)extensionHostDidBecomeActive:(NSNotification *)notification
|
|
|
|
{
|
2018-01-10 17:14:02 +01:00
|
|
|
OWSAssertIsOnMainThread();
|
|
|
|
|
2018-08-27 18:09:39 +02:00
|
|
|
OWSLogInfo(@"");
|
2018-01-10 17:14:02 +01:00
|
|
|
|
2018-05-31 16:54:51 +02:00
|
|
|
self.reportedApplicationState = UIApplicationStateActive;
|
2018-01-12 20:24:35 +01:00
|
|
|
|
2018-01-10 17:14:02 +01:00
|
|
|
[NSNotificationCenter.defaultCenter postNotificationName:OWSApplicationDidBecomeActiveNotification object:nil];
|
2017-12-12 22:31:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)extensionHostWillResignActive:(NSNotification *)notification
|
|
|
|
{
|
2018-01-10 17:14:02 +01:00
|
|
|
OWSAssertIsOnMainThread();
|
|
|
|
|
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 17:14:02 +01:00
|
|
|
|
|
|
|
[NSNotificationCenter.defaultCenter postNotificationName:OWSApplicationWillResignActiveNotification object:nil];
|
2017-12-12 22:31:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)extensionHostDidEnterBackground:(NSNotification *)notification
|
|
|
|
{
|
2018-01-10 17:14:02 +01:00
|
|
|
OWSAssertIsOnMainThread();
|
|
|
|
|
2018-08-27 18:09:39 +02:00
|
|
|
OWSLogInfo(@"");
|
2017-12-12 22:31:03 +01:00
|
|
|
[DDLog flushLog];
|
2018-01-10 17:14:02 +01:00
|
|
|
|
2018-05-31 16:54:51 +02:00
|
|
|
self.reportedApplicationState = UIApplicationStateBackground;
|
2018-01-12 20:24:35 +01:00
|
|
|
|
2018-01-10 17:14:02 +01:00
|
|
|
[NSNotificationCenter.defaultCenter postNotificationName:OWSApplicationDidEnterBackgroundNotification object:nil];
|
2017-12-12 22:31:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)extensionHostWillEnterForeground:(NSNotification *)notification
|
|
|
|
{
|
2018-01-10 17:14:02 +01:00
|
|
|
OWSAssertIsOnMainThread();
|
|
|
|
|
2018-08-27 18:09:39 +02:00
|
|
|
OWSLogInfo(@"");
|
2018-01-10 17:14:02 +01:00
|
|
|
|
2018-05-31 16:54:51 +02:00
|
|
|
self.reportedApplicationState = UIApplicationStateInactive;
|
2018-01-12 20:24:35 +01:00
|
|
|
|
2018-01-10 17:14:02 +01:00
|
|
|
[NSNotificationCenter.defaultCenter postNotificationName:OWSApplicationWillEnterForegroundNotification object:nil];
|
2017-12-12 22:31:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
2017-11-30 20:30:54 +01:00
|
|
|
- (BOOL)isMainApp
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isMainAppAndActive
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
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, ^{
|
|
|
|
// Borrowed from PureLayout's AppExtension compatible RTL support.
|
|
|
|
// App Extensions may not access -[UIApplication sharedApplication]; fall back to checking the bundle's
|
|
|
|
// preferred localization character direction
|
|
|
|
isRTL = [NSLocale characterDirectionForLanguage:[[NSBundle mainBundle] preferredLocalizations][0]]
|
|
|
|
== NSLocaleLanguageDirectionRightToLeft;
|
|
|
|
});
|
|
|
|
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
|
|
|
|
{
|
2018-08-27 18:00:28 +02:00
|
|
|
OWSLogInfo(@"Ignoring request to show/hide status bar since we're in an app extension");
|
2018-01-16 23:55:53 +01:00
|
|
|
}
|
|
|
|
|
2018-05-18 15:12:01 +02:00
|
|
|
- (CGFloat)statusBarHeight
|
|
|
|
{
|
|
|
|
return 20;
|
|
|
|
}
|
|
|
|
|
2018-01-12 20:24:35 +01:00
|
|
|
- (BOOL)isInBackground
|
|
|
|
{
|
2018-05-31 16:54:51 +02:00
|
|
|
return self.reportedApplicationState == UIApplicationStateBackground;
|
2017-11-30 20:30:54 +01:00
|
|
|
}
|
|
|
|
|
2018-05-31 17:27:07 +02:00
|
|
|
- (BOOL)isAppForegroundAndActive
|
|
|
|
{
|
|
|
|
return self.reportedApplicationState == UIApplicationStateActive;
|
|
|
|
}
|
|
|
|
|
2017-11-30 20:30:54 +01:00
|
|
|
- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:
|
|
|
|
(BackgroundTaskExpirationHandler)expirationHandler
|
|
|
|
{
|
|
|
|
return UIBackgroundTaskInvalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)endBackgroundTask:(UIBackgroundTaskIdentifier)backgroundTaskIdentifier
|
|
|
|
{
|
2018-09-06 19:01:24 +02:00
|
|
|
OWSAssertDebug(backgroundTaskIdentifier == UIBackgroundTaskInvalid);
|
2017-11-30 20:30:54 +01:00
|
|
|
}
|
|
|
|
|
2017-12-06 23:44:00 +01:00
|
|
|
- (void)ensureSleepBlocking:(BOOL)shouldBeBlocking blockingObjects:(NSArray<id> *)blockingObjects
|
|
|
|
{
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogDebug(@"Ignoring request to block sleep.");
|
2017-12-06 23:44:00 +01:00
|
|
|
}
|
|
|
|
|
2017-11-30 20:30:54 +01:00
|
|
|
- (void)setMainAppBadgeNumber:(NSInteger)value
|
|
|
|
{
|
2018-08-27 18:12:53 +02:00
|
|
|
OWSFailDebug(@"");
|
2017-11-30 20:30:54 +01:00
|
|
|
}
|
|
|
|
|
2017-12-01 23:22:40 +01:00
|
|
|
- (nullable UIViewController *)frontmostViewController
|
2017-12-01 23:10:14 +01:00
|
|
|
{
|
2018-09-06 19:01:24 +02:00
|
|
|
OWSAssertDebug(self.rootViewController);
|
2017-12-01 23:10:14 +01:00
|
|
|
|
2017-12-01 23:22:40 +01:00
|
|
|
return [self.rootViewController findFrontmostViewController:YES];
|
2017-12-01 23:10:14 +01:00
|
|
|
}
|
|
|
|
|
2018-02-06 17:32:09 +01:00
|
|
|
- (nullable UIAlertAction *)openSystemSettingsAction
|
2017-12-01 23:10:14 +01:00
|
|
|
{
|
2018-02-06 17:32:09 +01:00
|
|
|
return nil;
|
2017-12-01 23:10:14 +01:00
|
|
|
}
|
|
|
|
|
2017-12-04 18:38:44 +01:00
|
|
|
- (BOOL)isRunningTests
|
|
|
|
{
|
2018-02-12 16:00:20 +01:00
|
|
|
// We don't need to distinguish this in the SAE.
|
2017-12-04 18:38:44 +01:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2017-12-04 22:09:26 +01:00
|
|
|
- (void)setNetworkActivityIndicatorVisible:(BOOL)value
|
|
|
|
{
|
2018-08-27 18:12:53 +02:00
|
|
|
OWSFailDebug(@"");
|
2017-12-04 22:09:26 +01:00
|
|
|
}
|
|
|
|
|
2018-08-02 21:18:40 +02:00
|
|
|
- (void)runNowOrWhenMainAppIsActive:(AppActiveBlock)block
|
|
|
|
{
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSFailDebug(@"cannot run main app active blocks in share extension.");
|
2018-08-02 21:18:40 +02:00
|
|
|
}
|
|
|
|
|
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-30 20:30:54 +01:00
|
|
|
@end
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|