You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-ios/Signal/src/AppDelegate.m

197 lines
8.0 KiB
Matlab

11 years ago
#import "AppDelegate.h"
#import "AppAudioManager.h"
#import "CallLogViewController.h"
#import "CategorizingLogger.h"
#import "DebugLogger.h"
11 years ago
#import "DialerViewController.h"
#import "DiscardingLog.h"
11 years ago
#import "Environment.h"
11 years ago
#import "InCallViewController.h"
#import "LeftSideMenuViewController.h"
#import "MMDrawerController.h"
#import "PreferencesUtil.h"
11 years ago
#import "NotificationTracker.h"
#import "PushManager.h"
11 years ago
#import "PriorityQueue.h"
#import "RecentCallManager.h"
#import "Release.h"
#import "SettingsViewController.h"
#import "TabBarParentViewController.h"
#import "Util.h"
11 years ago
#import "VersionMigrations.h"
11 years ago
#define kSignalVersionKey @"SignalUpdateVersionKey"
#ifdef __APPLE__
#include "TargetConditionals.h"
#endif
@interface AppDelegate ()
@property (nonatomic, strong) MMDrawerController *drawerController;
@property (nonatomic, strong) NotificationTracker *notificationTracker;
@end
@implementation AppDelegate
11 years ago
#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];
11 years ago
if (!previousVersion) {
DDLogError(@"No previous version found. Possibly first launch since install.");
[Environment resetAppData]; // We clean previous keychain entries in case their are some entries remaining.
11 years ago
} 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
11 years ago
[VersionMigrations migrationFrom1Dot0Dot2toLarger];
}
11 years ago
}
}
/**
* 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;
11 years ago
NSDictionary *attrs = [NSDictionary dictionaryWithObject:NSFileProtectionComplete forKey:NSFileProtectionKey];
[[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 = [NSDictionary dictionaryWithObject:NSFileProtectionComplete forKey:NSFileProtectionKey];
[[NSFileManager defaultManager] setAttributes:attrs ofItemAtPath:logPath error:&error];
for (NSUInteger i = 0; i < [logsFiles count]; i++) {
[pathsToExclude addObject:[logPath stringByAppendingString:[logsFiles objectAtIndex:i]]];
}
11 years ago
for (NSUInteger i = 0; i < [pathsToExclude count]; i++) {
[[NSURL fileURLWithPath:[pathsToExclude objectAtIndex:i]] setResourceValue: [NSNumber numberWithBool: YES]
11 years ago
forKey: NSURLIsExcludedFromBackupKey error: &error];
}
11 years ago
if (error) {
11 years ago
DDLogError(@"Error while removing log files from backup: %@", error.description);
11 years ago
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"WARNING", @"") message:NSLocalizedString(@"DISABLING_BACKUP_FAILED", @"") delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", @"") otherButtonTitles:nil, nil];
[alert show];
return;
11 years ago
}
11 years ago
11 years ago
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
BOOL loggingIsEnabled;
#ifdef DEBUG
loggingIsEnabled = TRUE;
[[DebugLogger sharedInstance] enableTTYLogging];
#elif RELEASE
loggingIsEnabled = [[Environment preferences] loggingIsEnabled];
#endif
if (loggingIsEnabled) {
[[DebugLogger sharedInstance] enableFileLogging];
}
11 years ago
[self performUpdateCheck];
[self protectPreferenceFiles];
11 years ago
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];
11 years ago
11 years ago
LeftSideMenuViewController *leftSideMenuViewController = [LeftSideMenuViewController new];
11 years ago
self.drawerController = [[MMDrawerController alloc] initWithCenterViewController:leftSideMenuViewController.centerTabBarViewController leftDrawerViewController:leftSideMenuViewController];
11 years ago
self.window.rootViewController = _drawerController;
[self.window makeKeyAndVisible];
11 years ago
11 years ago
//Accept push notification when app is not open
NSDictionary *remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotif) {
11 years ago
DDLogInfo(@"Application was launched by tapping a push notification.");
11 years ago
[self application:application didReceiveRemoteNotification:remoteNotif];
}
11 years ago
11 years ago
[[[Environment phoneManager] currentCallObservable] watchLatestValue:^(CallState* latestCall) {
if (latestCall == nil){
return;
}
11 years ago
InCallViewController *callViewController = [InCallViewController inCallViewControllerWithCallState:latestCall
11 years ago
andOptionallyKnownContact:[latestCall potentiallySpecifiedContact]];
11 years ago
[_drawerController.centerViewController presentViewController:callViewController animated:YES completion:nil];
} onThread:[NSThread mainThread] untilCancelled:nil];
11 years ago
return YES;
}
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
[[PushManager sharedManager] registerForPushWithToken:deviceToken];
11 years ago
}
11 years ago
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {
[[PushManager sharedManager]verifyPushActivated];
DDLogError(@"Failed to register for push notifications: %@", error);
11 years ago
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
ResponderSessionDescriptor* call;
@try {
call = [ResponderSessionDescriptor responderSessionDescriptorFromEncryptedRemoteNotification:userInfo];
DDLogDebug(@"Received remote notification. Parsed session descriptor: %@.", call);
11 years ago
} @catch (OperationFailed* ex) {
DDLogError(@"Error parsing remote notification. Error: %@.", ex);
11 years ago
return;
}
11 years ago
11 years ago
[[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];
} else{
DDLogDebug(@"Push already processed. Skipping.");
11 years ago
}
completionHandler(UIBackgroundFetchResultNewData);
}
-(void) applicationDidBecomeActive:(UIApplication *)application {
[[AppAudioManager sharedInstance] awake];
application.applicationIconBadgeNumber = 0;
if ([Environment isRegistered]) {
[[PushManager sharedManager] verifyPushActivated];
[[AppAudioManager sharedInstance] requestRequiredPermissionsIfNeeded];
}
11 years ago
}
@end