session-ios/Signal/src/AppDelegate.m

253 lines
9.8 KiB
Mathematica
Raw Normal View History

2014-05-06 19:41:08 +02:00
#import "AppDelegate.h"
#import "AppAudioManager.h"
#import "CallLogViewController.h"
#import "CategorizingLogger.h"
#import "DebugLogger.h"
2014-05-06 19:41:08 +02:00
#import "DialerViewController.h"
#import "DiscardingLog.h"
2014-08-02 01:24:26 +02:00
#import "Environment.h"
2014-05-06 19:41:08 +02:00
#import "InCallViewController.h"
#import "LeftSideMenuViewController.h"
#import "MMDrawerController.h"
#import "PreferencesUtil.h"
2014-05-06 19:41:08 +02:00
#import "NotificationTracker.h"
#import "PushManager.h"
2014-05-06 19:41:08 +02:00
#import "PriorityQueue.h"
#import "RecentCallManager.h"
#import "Release.h"
#import "SettingsViewController.h"
#import "TabBarParentViewController.h"
#import "Util.h"
2014-08-02 01:24:26 +02:00
#import "VersionMigrations.h"
2014-05-06 19:41:08 +02:00
#define kSignalVersionKey @"SignalUpdateVersionKey"
#ifdef __APPLE__
#include "TargetConditionals.h"
#endif
@interface AppDelegate ()
2014-08-09 18:27:26 +02:00
@property (nonatomic, retain) UIWindow *blankWindow;
@property (nonatomic, strong) MMDrawerController *drawerController;
2014-05-06 19:41:08 +02:00
@property (nonatomic, strong) NotificationTracker *notificationTracker;
@end
@implementation AppDelegate
2014-05-06 19:41:08 +02:00
#pragma mark Detect updates - perform migrations
- (void)performUpdateCheck{
// We check if NSUserDefaults key for version exists.
NSString *previousVersion = Environment.preferences.lastRanVersion;
NSString *currentVersion = [Environment.preferences setAndGetCurrentVersion];
2014-05-06 19:41:08 +02:00
if (!previousVersion) {
2014-07-11 00:33:51 +02:00
DDLogError(@"No previous version found. Possibly first launch since install.");
[Environment resetAppData]; // We clean previous keychain entries in case their are some entries remaining.
2014-07-20 00:25:22 +02:00
} else if ([currentVersion compare:previousVersion options:NSNumericSearch] == NSOrderedDescending){
// Application was updated, let's see if we have a migration scheme for it.
if ([previousVersion isEqualToString:@"1.0.2"]) {
// Migrate from custom preferences to NSUserDefaults
2014-08-02 01:24:26 +02:00
[VersionMigrations migrationFrom1Dot0Dot2toLarger];
}
2014-05-06 19:41:08 +02:00
}
}
/**
* Protects the preference and logs file with disk encryption and prevents them to leak to iCloud.
*/
- (void)protectPreferenceFiles{
NSMutableArray *pathsToExclude = [NSMutableArray array];
NSString *preferencesPath =[NSHomeDirectory() stringByAppendingString:@"/Library/Preferences/"];
NSError *error;
2014-08-02 01:24:26 +02:00
NSDictionary *attrs = @{NSFileProtectionKey: NSFileProtectionCompleteUntilFirstUserAuthentication};
[[NSFileManager defaultManager] setAttributes:attrs ofItemAtPath:preferencesPath error:&error];
[pathsToExclude addObject:[[preferencesPath stringByAppendingString:NSBundle.mainBundle.bundleIdentifier] stringByAppendingString:@".plist"]];
NSString *logPath = [NSHomeDirectory() stringByAppendingString:@"/Library/Caches/Logs/"];
NSArray *logsFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:logPath error:&error];
attrs = @{NSFileProtectionKey: NSFileProtectionCompleteUntilFirstUserAuthentication};
[[NSFileManager defaultManager] setAttributes:attrs ofItemAtPath:logPath error:&error];
2014-08-14 03:13:24 +02:00
for (NSUInteger i = 0; i < logsFiles.count; i++) {
[pathsToExclude addObject:[logPath stringByAppendingString:logsFiles[i]]];
}
2014-05-06 19:41:08 +02:00
2014-08-14 03:13:24 +02:00
for (NSUInteger i = 0; i < pathsToExclude.count; i++) {
2014-08-14 17:52:24 +02:00
[[NSURL fileURLWithPath:pathsToExclude[i]] setResourceValue:@YES
forKey:NSURLIsExcludedFromBackupKey
error:&error];
}
2014-05-06 19:41:08 +02:00
if (error) {
2014-08-02 01:24:26 +02:00
DDLogError(@"Error while removing log files from backup: %@", error.description);
2014-05-06 19:41:08 +02:00
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"WARNING", @"") message:NSLocalizedString(@"DISABLING_BACKUP_FAILED", @"") delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", @"") otherButtonTitles:nil, nil];
[alert show];
return;
2014-05-06 19:41:08 +02:00
}
2014-08-02 01:24:26 +02:00
2014-05-06 19:41:08 +02:00
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
BOOL loggingIsEnabled;
#ifdef DEBUG
// Specified at Product -> Scheme -> Edit Scheme -> Test -> Arguments -> Environment to avoid things like
// the phone directory being looked up during tests.
if (getenv("runningTests_dontStartApp")) {
return YES;
}
loggingIsEnabled = TRUE;
[DebugLogger.sharedInstance enableTTYLogging];
#elif RELEASE
loggingIsEnabled = Environment.preferences.loggingIsEnabled;
#endif
if (loggingIsEnabled) {
[DebugLogger.sharedInstance enableFileLogging];
}
2014-05-06 19:41:08 +02:00
[self performUpdateCheck];
[self protectPreferenceFiles];
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
2014-08-09 18:27:26 +02:00
[self prepareScreenshotProtection];
2014-05-06 19:41:08 +02:00
self.notificationTracker = [NotificationTracker notificationTracker];
CategorizingLogger* logger = [CategorizingLogger categorizingLogger];
[logger addLoggingCallback:^(NSString *category, id details, NSUInteger index) {}];
[Environment setCurrent:[Release releaseEnvironmentWithLogging:logger]];
[Environment.getCurrent.phoneDirectoryManager startUntilCancelled:nil];
[Environment.getCurrent.contactsManager doAfterEnvironmentInitSetup];
[UIApplication.sharedApplication setStatusBarStyle:UIStatusBarStyleDefault];
2014-08-02 01:24:26 +02:00
2014-05-06 19:41:08 +02:00
LeftSideMenuViewController *leftSideMenuViewController = [LeftSideMenuViewController new];
2014-08-02 01:24:26 +02:00
self.drawerController = [[MMDrawerController alloc] initWithCenterViewController:leftSideMenuViewController.centerTabBarViewController leftDrawerViewController:leftSideMenuViewController];
2014-05-06 19:41:08 +02:00
self.window.rootViewController = _drawerController;
[self.window makeKeyAndVisible];
2014-08-02 01:24:26 +02:00
2014-05-06 19:41:08 +02:00
//Accept push notification when app is not open
NSDictionary *remoteNotif = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
2014-05-06 19:41:08 +02:00
if (remoteNotif) {
2014-07-20 00:25:22 +02:00
DDLogInfo(@"Application was launched by tapping a push notification.");
2014-05-06 19:41:08 +02:00
[self application:application didReceiveRemoteNotification:remoteNotif];
}
2014-08-02 01:24:26 +02:00
[Environment.phoneManager.currentCallObservable watchLatestValue:^(CallState* latestCall) {
if (latestCall == nil){
return;
}
2014-05-06 19:41:08 +02:00
InCallViewController *callViewController = [InCallViewController inCallViewControllerWithCallState:latestCall
andOptionallyKnownContact:latestCall.potentiallySpecifiedContact];
2014-05-06 19:41:08 +02:00
[_drawerController.centerViewController presentViewController:callViewController animated:YES completion:nil];
} onThread:NSThread.mainThread untilCancelled:nil];
2014-05-06 19:41:08 +02:00
return YES;
}
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
[PushManager.sharedManager registerForPushWithToken:deviceToken];
2014-05-06 19:41:08 +02:00
}
2014-05-06 19:41:08 +02:00
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {
[PushManager.sharedManager verifyPushActivated];
2014-07-03 18:45:06 +02:00
DDLogError(@"Failed to register for push notifications: %@", error);
2014-05-06 19:41:08 +02:00
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
ResponderSessionDescriptor* call;
@try {
call = [ResponderSessionDescriptor responderSessionDescriptorFromEncryptedRemoteNotification:userInfo];
2014-07-03 18:45:06 +02:00
DDLogDebug(@"Received remote notification. Parsed session descriptor: %@.", call);
2014-05-06 19:41:08 +02:00
} @catch (OperationFailed* ex) {
2014-07-03 18:45:06 +02:00
DDLogError(@"Error parsing remote notification. Error: %@.", ex);
2014-05-06 19:41:08 +02:00
return;
}
2014-08-02 01:24:26 +02:00
if (!call) {
DDLogError(@"Decryption of session descriptor failed");
return;
}
[Environment.phoneManager incomingCallWithSession:call];
2014-05-06 19:41:08 +02:00
}
-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
if([self.notificationTracker shouldProcessNotification:userInfo]){
[self application:application didReceiveRemoteNotification:userInfo];
2014-07-03 18:45:06 +02:00
} else{
DDLogDebug(@"Push already processed. Skipping.");
2014-05-06 19:41:08 +02:00
}
completionHandler(UIBackgroundFetchResultNewData);
}
-(void) applicationDidBecomeActive:(UIApplication *)application {
[AppAudioManager.sharedInstance awake];
// Hacky way to clear notification center after processed push
[UIApplication.sharedApplication setApplicationIconBadgeNumber:1];
[UIApplication.sharedApplication setApplicationIconBadgeNumber:0];
2014-08-09 18:27:26 +02:00
[self removeScreenProtection];
if (Environment.isRegistered) {
[PushManager.sharedManager verifyPushActivated];
[AppAudioManager.sharedInstance requestRequiredPermissionsIfNeeded];
}
2014-05-06 19:41:08 +02:00
}
2014-08-09 18:27:26 +02:00
- (void)applicationWillResignActive:(UIApplication *)application{
[self protectScreen];
}
- (void)prepareScreenshotProtection{
self.blankWindow = ({
UIWindow *window = [[UIWindow alloc] initWithFrame:self.window.bounds];
window.hidden = YES;
window.opaque = YES;
window.userInteractionEnabled = NO;
window.windowLevel = CGFLOAT_MAX;
window;
});
}
- (void)protectScreen{
if (Environment.preferences.screenSecurityIsEnabled) {
self.blankWindow.rootViewController = [UIViewController new];
2014-08-09 18:27:26 +02:00
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.blankWindow.bounds];
if (self.blankWindow.bounds.size.height == 568) {
imageView.image = [UIImage imageNamed:@"Default-568h"];
} else {
imageView.image = [UIImage imageNamed:@"Default"];
}
imageView.opaque = YES;
[self.blankWindow.rootViewController.view addSubview:imageView];
self.blankWindow.hidden = NO;
}
}
- (void)removeScreenProtection{
if (Environment.preferences.screenSecurityIsEnabled) {
2014-08-09 18:27:26 +02:00
self.blankWindow.rootViewController = nil;
self.blankWindow.hidden = YES;
}
}
2014-05-06 19:41:08 +02:00
@end