session-ios/Signal/src/AppDelegate.m

184 lines
7.9 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 "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 "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 ()
@property (nonatomic, strong) MMDrawerController *drawerController;
@property (nonatomic, strong) NotificationTracker *notificationTracker;
@property (nonatomic) DDFileLogger *fileLogger;
2014-05-06 19:41:08 +02:00
@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 = [[NSUserDefaults standardUserDefaults] objectForKey:kSignalVersionKey];
NSString *currentVersion = [NSString stringWithFormat:@"%@", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]];
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
}
[[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:kSignalVersionKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
/**
* Protects the preference and logs file with disk encryption and prevents them to leak to iCloud.
*/
- (void)protectPreferenceFiles{
NSMutableArray *pathsToExclude = [NSMutableArray array];
[pathsToExclude addObject:[[[NSHomeDirectory() stringByAppendingString:@"/Library/Preferences/"] stringByAppendingString:[[NSBundle mainBundle] bundleIdentifier]] stringByAppendingString:@".plist"]];
NSError *error;
2014-08-02 01:24:26 +02:00
NSString *logPath = [NSHomeDirectory() stringByAppendingString:@"/Library/Caches/Logs/"];
NSArray *logsFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:logPath error:&error];
for (NSUInteger i = 0; i < [logsFiles count]; i++) {
[pathsToExclude addObject:[logPath stringByAppendingString:[logsFiles objectAtIndex:i]]];
}
2014-05-06 19:41:08 +02:00
for (NSUInteger i = 0; i < [pathsToExclude count]; i++) {
[[NSURL fileURLWithPath:[pathsToExclude objectAtIndex:i]] setResourceValue: [NSNumber numberWithBool: YES]
2014-08-02 01:24:26 +02:00
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 {
[DDLog addLogger:[DDTTYLogger sharedInstance]];
self.fileLogger = [[DDFileLogger alloc] init]; //Logging to file, because it's in the Cache folder, they are not uploaded in iTunes/iCloud backups.
self.fileLogger.rollingFrequency = 60 * 60 * 24; // 24 hour rolling.
self.fileLogger.logFileManager.maximumNumberOfLogFiles = 3; // Keep three days of logs.
[DDLog addLogger:self.fileLogger];
2014-05-06 19:41:08 +02:00
[self performUpdateCheck];
[self protectPreferenceFiles];
2014-05-06 19:41:08 +02:00
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
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 objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
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
2014-05-06 19:41:08 +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
2014-08-02 01:24:26 +02:00
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
2014-05-06 19:41:08 +02:00
[[Environment phoneManager] incomingCallWithSession:call];
}
-(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];
application.applicationIconBadgeNumber = 0;
if ([Environment isRegistered]) {
[[PushManager sharedManager] verifyPushActivated];
[[AppAudioManager sharedInstance] requestRequiredPermissionsIfNeeded];
}
2014-05-06 19:41:08 +02:00
}
@end