2017-02-01 23:49:32 +01:00
|
|
|
//
|
|
|
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2016-10-14 22:59:58 +02:00
|
|
|
#import "Environment.h"
|
2017-09-06 20:13:18 +02:00
|
|
|
#import "ConversationViewController.h"
|
2014-08-09 01:01:05 +02:00
|
|
|
#import "DebugLogger.h"
|
2014-05-06 19:41:08 +02:00
|
|
|
#import "FunctionalUtil.h"
|
2017-09-06 19:59:39 +02:00
|
|
|
#import "HomeViewController.h"
|
2016-11-12 18:22:29 +01:00
|
|
|
#import "Signal-Swift.h"
|
2014-11-21 00:51:23 +01:00
|
|
|
#import "SignalKeyingStorage.h"
|
2015-04-14 21:49:00 +02:00
|
|
|
#import "TSContactThread.h"
|
|
|
|
#import "TSGroupThread.h"
|
2016-10-14 22:59:58 +02:00
|
|
|
#import <SignalServiceKit/ContactsUpdater.h>
|
2017-09-12 23:42:28 +02:00
|
|
|
#import <SignalServiceKit/Threading.h>
|
2017-10-06 18:03:41 +02:00
|
|
|
#import <SignalServiceKit/OWSSignalService.h>
|
|
|
|
#import <SignalServiceKit/OWSMessageReceiver.h>
|
2014-07-11 00:33:51 +02:00
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
static Environment *environment = nil;
|
2014-05-06 19:41:08 +02:00
|
|
|
|
|
|
|
@implementation Environment
|
|
|
|
|
2017-02-02 00:26:47 +01:00
|
|
|
@synthesize accountManager = _accountManager,
|
|
|
|
callMessageHandler = _callMessageHandler,
|
|
|
|
callService = _callService,
|
2017-03-30 18:51:48 +02:00
|
|
|
contactsManager = _contactsManager,
|
|
|
|
contactsUpdater = _contactsUpdater,
|
2017-10-06 18:03:41 +02:00
|
|
|
messageFetcherJob = _messageFetcherJob,
|
2017-03-30 18:51:48 +02:00
|
|
|
messageSender = _messageSender,
|
|
|
|
networkManager = _networkManager,
|
2017-02-02 00:26:47 +01:00
|
|
|
notificationsManager = _notificationsManager,
|
|
|
|
preferences = _preferences,
|
|
|
|
outboundCallInitiator = _outboundCallInitiator;
|
2016-11-12 18:22:29 +01:00
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
+ (Environment *)getCurrent {
|
|
|
|
NSAssert((environment != nil), @"Environment is not defined.");
|
2014-05-06 19:41:08 +02:00
|
|
|
return environment;
|
|
|
|
}
|
2014-12-24 02:25:10 +01:00
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
+ (void)setCurrent:(Environment *)curEnvironment {
|
2014-05-06 19:41:08 +02:00
|
|
|
environment = curEnvironment;
|
|
|
|
}
|
|
|
|
|
2017-03-27 18:26:13 +02:00
|
|
|
- (instancetype)initWithContactsManager:(OWSContactsManager *)contactsManager
|
|
|
|
contactsUpdater:(ContactsUpdater *)contactsUpdater
|
|
|
|
networkManager:(TSNetworkManager *)networkManager
|
|
|
|
messageSender:(OWSMessageSender *)messageSender
|
2016-10-14 22:59:58 +02:00
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (!self) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
_contactsManager = contactsManager;
|
2016-11-12 18:22:55 +01:00
|
|
|
_contactsUpdater = contactsUpdater;
|
2016-10-14 22:59:58 +02:00
|
|
|
_networkManager = networkManager;
|
2016-11-01 20:02:15 +01:00
|
|
|
_messageSender = messageSender;
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2017-04-01 00:45:51 +02:00
|
|
|
OWSSingletonAssert();
|
|
|
|
|
2016-10-14 22:59:58 +02:00
|
|
|
return self;
|
2014-05-06 19:41:08 +02:00
|
|
|
}
|
2014-07-30 08:05:36 +02:00
|
|
|
|
2016-11-12 18:22:29 +01:00
|
|
|
- (AccountManager *)accountManager
|
|
|
|
{
|
|
|
|
@synchronized (self) {
|
|
|
|
if (!_accountManager) {
|
2017-08-11 22:29:05 +02:00
|
|
|
_accountManager = [[AccountManager alloc] initWithTextSecureAccountManager:[TSAccountManager sharedInstance]
|
|
|
|
preferences:self.preferences];
|
2016-11-12 18:22:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return _accountManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (OWSWebRTCCallMessageHandler *)callMessageHandler
|
|
|
|
{
|
|
|
|
@synchronized (self) {
|
|
|
|
if (!_callMessageHandler) {
|
|
|
|
_callMessageHandler = [[OWSWebRTCCallMessageHandler alloc] initWithAccountManager:self.accountManager
|
|
|
|
callService:self.callService
|
|
|
|
messageSender:self.messageSender];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return _callMessageHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (CallService *)callService
|
|
|
|
{
|
|
|
|
@synchronized (self) {
|
|
|
|
if (!_callService) {
|
2017-02-02 00:26:47 +01:00
|
|
|
OWSAssert(self.accountManager);
|
|
|
|
OWSAssert(self.contactsManager);
|
|
|
|
OWSAssert(self.messageSender);
|
2016-11-12 18:22:29 +01:00
|
|
|
_callService = [[CallService alloc] initWithAccountManager:self.accountManager
|
|
|
|
contactsManager:self.contactsManager
|
|
|
|
messageSender:self.messageSender
|
|
|
|
notificationsAdapter:[OWSCallNotificationsAdapter new]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return _callService;
|
|
|
|
}
|
|
|
|
|
2017-02-02 00:26:47 +01:00
|
|
|
- (CallUIAdapter *)callUIAdapter
|
|
|
|
{
|
|
|
|
return self.callService.callUIAdapter;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (OutboundCallInitiator *)outboundCallInitiator
|
|
|
|
{
|
|
|
|
@synchronized (self) {
|
|
|
|
if (!_outboundCallInitiator) {
|
|
|
|
OWSAssert(self.contactsManager);
|
|
|
|
OWSAssert(self.contactsUpdater);
|
2017-03-22 17:50:46 +01:00
|
|
|
_outboundCallInitiator = [[OutboundCallInitiator alloc] initWithContactsManager:self.contactsManager
|
2017-02-02 00:26:47 +01:00
|
|
|
contactsUpdater:self.contactsUpdater];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return _outboundCallInitiator;
|
|
|
|
}
|
|
|
|
|
2017-03-30 18:51:48 +02:00
|
|
|
- (OWSContactsManager *)contactsManager
|
|
|
|
{
|
|
|
|
OWSAssert(_contactsManager != nil);
|
|
|
|
return _contactsManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (ContactsUpdater *)contactsUpdater
|
|
|
|
{
|
|
|
|
OWSAssert(_contactsUpdater != nil);
|
|
|
|
return _contactsUpdater;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (TSNetworkManager *)networkManager
|
|
|
|
{
|
|
|
|
OWSAssert(_networkManager != nil);
|
|
|
|
return _networkManager;
|
|
|
|
}
|
|
|
|
|
2017-10-06 18:03:41 +02:00
|
|
|
- (OWSMessageFetcherJob *)messageFetcherJob
|
|
|
|
{
|
|
|
|
@synchronized(self)
|
|
|
|
{
|
|
|
|
if (!_messageFetcherJob) {
|
|
|
|
_messageFetcherJob =
|
|
|
|
[[OWSMessageFetcherJob alloc] initWithMessageReceiver:[OWSMessageReceiver sharedInstance]
|
|
|
|
networkManager:self.networkManager
|
|
|
|
signalService:[OWSSignalService sharedInstance]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _messageFetcherJob;
|
|
|
|
}
|
|
|
|
|
2017-03-30 18:51:48 +02:00
|
|
|
- (OWSMessageSender *)messageSender
|
|
|
|
{
|
|
|
|
OWSAssert(_messageSender != nil);
|
|
|
|
return _messageSender;
|
|
|
|
}
|
|
|
|
|
2016-11-12 18:22:29 +01:00
|
|
|
- (NotificationsManager *)notificationsManager
|
|
|
|
{
|
|
|
|
@synchronized (self) {
|
|
|
|
if (!_notificationsManager) {
|
|
|
|
_notificationsManager = [NotificationsManager new];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return _notificationsManager;
|
|
|
|
}
|
|
|
|
|
2017-09-14 21:45:04 +02:00
|
|
|
+ (OWSPreferences *)preferences
|
2016-11-12 18:22:29 +01:00
|
|
|
{
|
2017-03-13 14:33:58 +01:00
|
|
|
OWSAssert([Environment getCurrent] != nil);
|
|
|
|
OWSAssert([Environment getCurrent].preferences != nil);
|
2016-11-12 18:22:29 +01:00
|
|
|
return [Environment getCurrent].preferences;
|
|
|
|
}
|
|
|
|
|
2017-09-14 21:45:04 +02:00
|
|
|
- (OWSPreferences *)preferences
|
2016-11-12 18:22:29 +01:00
|
|
|
{
|
|
|
|
@synchronized (self) {
|
|
|
|
if (!_preferences) {
|
2017-09-14 21:45:04 +02:00
|
|
|
_preferences = [OWSPreferences new];
|
2016-11-12 18:22:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return _preferences;
|
2014-07-30 08:05:36 +02:00
|
|
|
}
|
|
|
|
|
2017-09-12 23:42:28 +02:00
|
|
|
- (void)setHomeViewController:(HomeViewController *)homeViewController
|
2017-09-06 19:59:39 +02:00
|
|
|
{
|
2017-09-12 23:42:28 +02:00
|
|
|
_homeViewController = homeViewController;
|
2014-11-26 20:29:45 +01:00
|
|
|
}
|
|
|
|
|
2014-12-05 11:34:45 +01:00
|
|
|
- (void)setSignUpFlowNavigationController:(UINavigationController *)navigationController {
|
|
|
|
_signUpFlowNavigationController = navigationController;
|
|
|
|
}
|
|
|
|
|
2017-09-12 23:42:28 +02:00
|
|
|
+ (void)presentConversationForRecipientId:(NSString *)recipientId
|
|
|
|
{
|
|
|
|
[self presentConversationForRecipientId:recipientId keyboardOnViewAppearing:YES callOnViewAppearing:NO];
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2017-09-12 23:42:28 +02:00
|
|
|
+ (void)presentConversationForRecipientId:(NSString *)recipientId withCompose:(BOOL)compose
|
|
|
|
{
|
|
|
|
[self presentConversationForRecipientId:recipientId keyboardOnViewAppearing:compose callOnViewAppearing:NO];
|
2015-04-14 21:49:00 +02:00
|
|
|
}
|
|
|
|
|
2017-09-12 23:42:28 +02:00
|
|
|
+ (void)callRecipientId:(NSString *)recipientId
|
|
|
|
{
|
|
|
|
[self presentConversationForRecipientId:recipientId keyboardOnViewAppearing:NO callOnViewAppearing:YES];
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2017-09-12 23:42:28 +02:00
|
|
|
+ (void)presentConversationForRecipientId:(NSString *)recipientId
|
|
|
|
keyboardOnViewAppearing:(BOOL)keyboardOnViewAppearing
|
|
|
|
callOnViewAppearing:(BOOL)callOnViewAppearing
|
|
|
|
{
|
2017-09-18 22:12:51 +02:00
|
|
|
// At most one.
|
|
|
|
OWSAssert(!keyboardOnViewAppearing || !callOnViewAppearing);
|
|
|
|
|
2017-09-12 23:47:43 +02:00
|
|
|
DispatchMainThreadSafe(^{
|
|
|
|
__block TSThread *thread = nil;
|
|
|
|
[[TSStorageManager sharedManager].dbReadWriteConnection
|
|
|
|
readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
|
|
|
|
thread = [TSContactThread getOrCreateThreadWithContactId:recipientId transaction:transaction];
|
|
|
|
}];
|
|
|
|
[self presentConversationForThread:thread
|
|
|
|
keyboardOnViewAppearing:keyboardOnViewAppearing
|
|
|
|
callOnViewAppearing:callOnViewAppearing];
|
|
|
|
});
|
2014-11-26 20:29:45 +01:00
|
|
|
}
|
|
|
|
|
2017-09-28 17:39:25 +02:00
|
|
|
+ (void)presentConversationForThreadId:(NSString *)threadId
|
|
|
|
{
|
|
|
|
OWSAssert(threadId.length > 0);
|
|
|
|
|
|
|
|
TSThread *thread = [TSThread fetchObjectWithUniqueID:threadId];
|
|
|
|
if (thread == nil) {
|
|
|
|
OWSFail(@"%@ unable to find thread with id: %@", self.tag, threadId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
[self presentConversationForThread:thread];
|
|
|
|
}
|
|
|
|
|
2017-09-12 23:42:28 +02:00
|
|
|
+ (void)presentConversationForThread:(TSThread *)thread
|
2017-04-18 22:08:01 +02:00
|
|
|
{
|
2017-10-14 19:20:46 +02:00
|
|
|
[self presentConversationForThread:thread withCompose:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void)presentConversationForThread:(TSThread *)thread withCompose:(BOOL)compose
|
|
|
|
{
|
|
|
|
[self presentConversationForThread:thread keyboardOnViewAppearing:compose callOnViewAppearing:NO];
|
2017-04-18 22:08:01 +02:00
|
|
|
}
|
|
|
|
|
2017-09-12 23:42:28 +02:00
|
|
|
+ (void)presentConversationForThread:(TSThread *)thread
|
|
|
|
keyboardOnViewAppearing:(BOOL)keyboardOnViewAppearing
|
|
|
|
callOnViewAppearing:(BOOL)callOnViewAppearing
|
|
|
|
{
|
2017-09-18 22:12:51 +02:00
|
|
|
// At most one.
|
|
|
|
OWSAssert(!keyboardOnViewAppearing || !callOnViewAppearing);
|
|
|
|
|
2017-09-12 23:42:28 +02:00
|
|
|
if (!thread) {
|
|
|
|
OWSFail(@"%@ Can't present nil thread.", self.tag);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DispatchMainThreadSafe(^{
|
|
|
|
UIViewController *frontmostVC = [[UIApplication sharedApplication] frontmostViewController];
|
|
|
|
|
|
|
|
if ([frontmostVC isKindOfClass:[ConversationViewController class]]) {
|
|
|
|
ConversationViewController *conversationVC = (ConversationViewController *)frontmostVC;
|
|
|
|
if ([conversationVC.thread.uniqueId isEqualToString:thread.uniqueId]) {
|
|
|
|
[conversationVC popKeyBoard];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2017-09-12 23:42:28 +02:00
|
|
|
Environment *env = [self getCurrent];
|
|
|
|
[env.homeViewController presentThread:thread
|
|
|
|
keyboardOnViewAppearing:keyboardOnViewAppearing
|
|
|
|
callOnViewAppearing:callOnViewAppearing];
|
|
|
|
});
|
2014-12-17 06:44:36 +01:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
+ (void)resetAppData {
|
2017-02-14 17:05:59 +01:00
|
|
|
// This _should_ be wiped out below.
|
|
|
|
DDLogError(@"%@ %s", self.tag, __PRETTY_FUNCTION__);
|
|
|
|
[DDLog flushLog];
|
|
|
|
|
2017-01-10 22:50:30 +01:00
|
|
|
[[TSStorageManager sharedManager] resetSignalStorage];
|
2017-08-04 16:32:00 +02:00
|
|
|
[[OWSProfileManager sharedManager] resetProfileStorage];
|
2014-09-08 01:31:05 +02:00
|
|
|
[Environment.preferences clear];
|
2017-07-01 03:57:06 +02:00
|
|
|
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
[DebugLogger.sharedLogger wipeLogs];
|
|
|
|
exit(0);
|
2014-07-11 00:33:51 +02:00
|
|
|
}
|
|
|
|
|
2017-02-14 17:05:59 +01:00
|
|
|
#pragma mark - Logging
|
|
|
|
|
|
|
|
+ (NSString *)tag
|
|
|
|
{
|
|
|
|
return [NSString stringWithFormat:@"[%@]", self.class];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)tag
|
|
|
|
{
|
|
|
|
return self.class.tag;
|
|
|
|
}
|
|
|
|
|
2014-05-06 19:41:08 +02:00
|
|
|
@end
|