123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- //
- // VLCAppDelegate.m
- // AspenProject
- //
- // Created by Felix Paul Kühne on 27.02.13.
- // Copyright (c) 2013 VideoLAN. All rights reserved.
- //
- // Refer to the COPYING file of the official project for license.
- //
- #import "VLCAppDelegate.h"
- #import "VLCMediaFileDiscoverer.h"
- #import "NSString+SupportedMedia.h"
- #import "UIDevice+SpeedCategory.h"
- #import "VLCPlaylistViewController.h"
- #import "VLCMenuViewController.h"
- #import "VLCMovieViewController.h"
- #import "PAPasscodeViewController.h"
- #import "UINavigationController+Theme.h"
- @interface VLCAppDelegate () <PAPasscodeViewControllerDelegate, VLCMediaFileDiscovererDelegate> {
- PAPasscodeViewController *_passcodeLockController;
- VLCDropboxTableViewController *_dropboxTableViewController;
- }
- @property (nonatomic) BOOL passcodeValidated;
- @end
- @implementation VLCAppDelegate
- + (void)initialize
- {
- NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
- NSNumber *skipLoopFilterDefaultValue;
- int deviceSpeedCategory = [[UIDevice currentDevice] speedCategory];
- if (deviceSpeedCategory < 3)
- skipLoopFilterDefaultValue = kVLCSettingSkipLoopFilterNonKey;
- else
- skipLoopFilterDefaultValue = kVLCSettingSkipLoopFilterNonRef;
- NSDictionary *appDefaults = @{kVLCSettingPasscodeKey : @"", kVLCSettingPasscodeOnKey : @(NO), kVLCSettingContinueAudioInBackgroundKey : @(YES), kVLCSettingStretchAudio : @(NO), kVLCSettingTextEncoding : kVLCSettingTextEncodingDefaultValue, kVLCSettingSkipLoopFilter : skipLoopFilterDefaultValue};
- [defaults registerDefaults:appDefaults];
- }
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- _playlistViewController = [[VLCPlaylistViewController alloc] init];
- self.navigationController = [[UINavigationController alloc] initWithRootViewController:_playlistViewController];
- [self.navigationController loadTheme];
- self.window.rootViewController = self.navigationController;
- [self.window makeKeyAndVisible];
- VLCMediaFileDiscoverer *discoverer = [VLCMediaFileDiscoverer sharedInstance];
- [discoverer addObserver:self];
- [discoverer startDiscovering:[self directoryPath]];
- [self validatePasscode];
- return YES;
- }
- - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
- {
- if ([[DBSession sharedSession] handleOpenURL:url]) {
- [self.dropboxTableViewController updateViewAfterSessionChange];
- return YES;
- }
- if (_playlistViewController && url != nil) {
- APLog(@"%@ requested %@ to be opened", sourceApplication, url);
- if (url.isFileURL) {
- NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *directoryPath = searchPaths[0];
- NSURL *destinationURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", directoryPath, url.lastPathComponent]];
- NSError *theError;
- [[NSFileManager defaultManager] moveItemAtURL:url toURL:destinationURL error:&theError];
- if (theError.code != noErr)
- APLog(@"saving the file failed (%i): %@", theError.code, theError.localizedDescription);
- [self updateMediaList];
- } else {
- NSURL *parsedUrl = [self parseOpenURL:url];
- [_playlistViewController openMovieFromURL:parsedUrl];
- }
- return YES;
- }
- return NO;
- }
- - (NSURL *)parseOpenURL:(NSURL *)url
- {
- NSString *receivedUrl = [url absoluteString];
- if ([receivedUrl length] > 6) {
- NSString *verifyVlcUrl = [receivedUrl substringToIndex:6];
- if ([verifyVlcUrl isEqualToString:@"vlc://"]) {
- NSString *parsedString = [receivedUrl substringFromIndex:6];
- // "url decode" so we can parse http:// links
- parsedString = [parsedString stringByReplacingOccurrencesOfString:@"+"withString:@" "];
- parsedString = [parsedString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- // add http:// if nothing is there
- NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:(NSTextCheckingTypes)NSTextCheckingTypeLink error:nil];
- NSUInteger parsedStringLength = [parsedString length];
- NSUInteger numberOfUrlMatches = [detector numberOfMatchesInString:parsedString options:0 range:NSMakeRange(0, parsedStringLength)];
- if (numberOfUrlMatches == 0) {
- parsedString = [@"http://" stringByAppendingString:parsedString];
- }
- NSURL *targetUrl = [NSURL URLWithString:parsedString];
- return targetUrl;
- }
- }
- return url;
- }
- - (void)applicationWillResignActive:(UIApplication *)application
- {
- [[MLMediaLibrary sharedMediaLibrary] applicationWillExit];
- }
- - (void)applicationDidBecomeActive:(UIApplication *)application
- {
- [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
- [self updateMediaList];
- }
- - (void)applicationDidEnterBackground:(UIApplication *)application
- {
- [self validatePasscode]; // Lock library when going to background
- }
- #pragma mark - properties
- - (VLCDropboxTableViewController *)dropboxTableViewController
- {
- if (_dropboxTableViewController == nil) {
- _dropboxTableViewController = [[VLCDropboxTableViewController alloc] initWithNibName:nil bundle:nil];
- }
- return _dropboxTableViewController;
- }
- #pragma mark - media discovering
- - (void)mediaFileAdded:(NSString *)fileName loading:(BOOL)isLoading {
- if (!isLoading) {
- MLMediaLibrary *sharedLibrary = [MLMediaLibrary sharedMediaLibrary];
- [sharedLibrary addFilePaths:@[fileName]];
- /* exclude media files from backup (QA1719) */
- NSURL *excludeURL = [NSURL fileURLWithPath:fileName];
- [excludeURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
- // TODO Should we update media db after adding new files?
- [sharedLibrary updateMediaDatabase];
- [_playlistViewController updateViewContents];
- }
- }
- - (void)mediaFileDeleted:(NSString *)name {
- [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
- [_playlistViewController updateViewContents];
- }
- #pragma mark - media list methods
- - (NSString *)directoryPath
- {
- #define LOCAL_PLAYBACK_HACK 0
- #if LOCAL_PLAYBACK_HACK && TARGET_IPHONE_SIMULATOR
- NSString *directoryPath = @"/Users/fkuehne/Desktop/VideoLAN docs/Clips/sel/";
- #else
- NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *directoryPath = searchPaths[0];
- #endif
- return directoryPath;
- }
- - (void)updateMediaList
- {
- NSString *directoryPath = [self directoryPath];
- NSArray *foundFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil];
- NSMutableArray *filePaths = [NSMutableArray arrayWithCapacity:[foundFiles count]];
- NSURL *fileURL;
- for (NSString *fileName in foundFiles) {
- if ([fileName isSupportedMediaFormat]) {
- [filePaths addObject:[directoryPath stringByAppendingPathComponent:fileName]];
- /* exclude media files from backup (QA1719) */
- fileURL = [NSURL fileURLWithPath:[directoryPath stringByAppendingPathComponent:fileName]];
- [fileURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
- }
- }
- [[MLMediaLibrary sharedMediaLibrary] addFilePaths:filePaths];
- [_playlistViewController updateViewContents];
- }
- #pragma mark - pass code validation
- - (void)validatePasscode
- {
- NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
- NSString *passcode = [defaults objectForKey:kVLCSettingPasscodeKey];
- if ([passcode isEqualToString:@""] || ![[defaults objectForKey:kVLCSettingPasscodeOnKey] boolValue]) {
- self.passcodeValidated = YES;
- return;
- }
- if (!self.passcodeValidated) {
- if ([self.nextPasscodeCheckDate earlierDate:[NSDate date]] == self.nextPasscodeCheckDate) {
- _passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionEnter];
- _passcodeLockController.delegate = self;
- _passcodeLockController.passcode = passcode;
- self.window.rootViewController = _passcodeLockController;
- } else
- self.passcodeValidated = YES;
- }
- }
- - (void)PAPasscodeViewControllerDidEnterPasscode:(PAPasscodeViewController *)controller
- {
- // TODO add transition animation, i.e. fade
- self.nextPasscodeCheckDate = [NSDate dateWithTimeIntervalSinceNow:300];
- self.window.rootViewController = self.navigationController;
- }
- - (void)PAPasscodeViewController:(PAPasscodeViewController *)controller didFailToEnterPasscode:(NSInteger)attempts
- {
- // TODO handle error attempts
- }
- @end
|