VLCAppDelegate.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //
  2. // VLCAppDelegate.m
  3. // AspenProject
  4. //
  5. // Created by Felix Paul Kühne on 27.02.13.
  6. // Copyright (c) 2013 VideoLAN. All rights reserved.
  7. //
  8. #import "VLCAppDelegate.h"
  9. #import "DirectoryWatcher.h"
  10. #import "NSString+SupportedMedia.h"
  11. #import "VLCPlaylistViewController.h"
  12. #import "VLCMovieViewController.h"
  13. #import "PAPasscodeViewController.h"
  14. @interface VLCAppDelegate () <PAPasscodeViewControllerDelegate, DirectoryWatcherDelegate> {
  15. NSURL *_tempURL;
  16. PAPasscodeViewController *_passcodeLockController;
  17. DirectoryWatcher *_directoryWatcher;
  18. NSTimer *_addMediaTimer;
  19. NSMutableDictionary *_addedFiles;
  20. }
  21. @property (nonatomic) BOOL passcodeValidated;
  22. @end
  23. @implementation VLCAppDelegate
  24. + (void)initialize
  25. {
  26. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  27. NSDictionary *appDefaults = @{kVLCSettingPasscodeKey : @"", kVLCSettingPasscodeOnKey : @(NO), kVLCSettingContinueAudioInBackgroundKey : @(YES), kVLCSettingStretchAudio : @(NO), kVLCSettingTextEncoding : kVLCSettingTextEncodingDefaultValue};
  28. [defaults registerDefaults:appDefaults];
  29. }
  30. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  31. {
  32. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  33. _directoryWatcher = [DirectoryWatcher watchFolderWithPath:[self directoryPath] delegate:self];
  34. _playlistViewController = [[VLCPlaylistViewController alloc] init];
  35. self.navigationController = [[UINavigationController alloc] initWithRootViewController:_playlistViewController];
  36. UINavigationBar *navBar = self.navigationController.navigationBar;
  37. [navBar setBackgroundImage:[UIImage imageNamed:@"navBarBackground"] forBarMetrics:UIBarMetricsDefault];
  38. navBar.barStyle = UIBarStyleBlack;
  39. self.window.rootViewController = self.navigationController;
  40. [self.window makeKeyAndVisible];
  41. _dropboxTableViewController = [[VLCDropboxTableViewController alloc] initWithNibName:@"VLCDropboxTableViewController" bundle:nil];
  42. return YES;
  43. }
  44. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
  45. {
  46. if ([[DBSession sharedSession] handleOpenURL:url]) {
  47. [self.dropboxTableViewController updateViewAfterSessionChange];
  48. return YES;
  49. }
  50. if (_playlistViewController && url != nil) {
  51. APLog(@"%@ requested %@ to be opened", sourceApplication, url);
  52. if (url.isFileURL) {
  53. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"SAVE_FILE", @"") message:[NSString stringWithFormat:NSLocalizedString(@"SAVE_FILE_LONG", @""), url.lastPathComponent] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:NSLocalizedString(@"BUTTON_SAVE", @""), nil];
  54. _tempURL = url;
  55. [alert show];
  56. } else
  57. [_playlistViewController openMovieFromURL:url];
  58. return YES;
  59. }
  60. return NO;
  61. }
  62. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  63. {
  64. if (buttonIndex == 1) {
  65. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  66. NSString *directoryPath = searchPaths[0];
  67. NSURL *destinationURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", directoryPath, _tempURL.lastPathComponent]];
  68. NSError *theError;
  69. [[NSFileManager defaultManager] copyItemAtURL:_tempURL toURL:destinationURL error:&theError];
  70. if (theError.code != noErr)
  71. APLog(@"saving the file failed (%i): %@", theError.code, theError.localizedDescription);
  72. [self updateMediaList];
  73. } else
  74. [_playlistViewController openMovieFromURL:_tempURL];
  75. }
  76. - (void)applicationWillResignActive:(UIApplication *)application
  77. {
  78. [[MLMediaLibrary sharedMediaLibrary] applicationWillExit];
  79. }
  80. - (void)applicationWillEnterForeground:(UIApplication *)application
  81. {
  82. APLog(@"applicationWillEnterForeground: %i", self.passcodeValidated);
  83. }
  84. - (void)applicationDidBecomeActive:(UIApplication *)application
  85. {
  86. [self updateMediaList];
  87. }
  88. - (void)applicationDidEnterBackground:(UIApplication *)application
  89. {
  90. [self validatePasscode]; // Lock library when going to background
  91. }
  92. - (void)applicationWillTerminate:(UIApplication *)application
  93. {
  94. // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  95. }
  96. #pragma mark - directory watcher delegate
  97. - (void)addFileTimerFired
  98. {
  99. NSArray *allKeys = [_addedFiles allKeys];
  100. for (NSString *fileURL in allKeys) {
  101. NSDictionary *attribs = [[NSFileManager defaultManager] attributesOfItemAtPath:fileURL error:nil];
  102. NSNumber *prevFetchedSize = [_addedFiles objectForKey:fileURL];
  103. NSNumber *updatedSize = [attribs objectForKey:NSFileSize];
  104. if ([prevFetchedSize compare:updatedSize] == NSOrderedSame) {
  105. [_addedFiles removeObjectForKey:fileURL];
  106. [[MLMediaLibrary sharedMediaLibrary] addFilePaths:@[fileURL]];
  107. [_playlistViewController updateViewContents];
  108. } else {
  109. [_addedFiles setObject:updatedSize forKey:fileURL];
  110. }
  111. }
  112. if (_addedFiles.count == 0) {
  113. [_addMediaTimer invalidate];
  114. _addMediaTimer = nil;
  115. }
  116. }
  117. - (void)directoryDidChange:(DirectoryWatcher *)folderWatcher
  118. {
  119. NSArray *foundFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[self directoryPath] error:nil];
  120. NSMutableArray *matchedFiles = [NSMutableArray arrayWithCapacity:foundFiles.count];
  121. for (NSString *fileName in foundFiles) {
  122. if ([fileName isSupportedMediaFormat]) {
  123. [matchedFiles addObject:[[self directoryPath] stringByAppendingPathComponent:fileName]];
  124. }
  125. }
  126. NSArray *mediaFiles = [MLFile allFiles];
  127. NSMutableArray *mediaFilesPaths = [NSMutableArray arrayWithCapacity:mediaFiles.count];
  128. for (MLFile *file in mediaFiles) {
  129. [mediaFilesPaths addObject:file.url];
  130. }
  131. if (mediaFiles.count > matchedFiles.count) { // File was deleted
  132. [self updateMediaList];
  133. } else if (mediaFiles.count < matchedFiles.count) { // File was added
  134. NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"not (self in %@)", mediaFilesPaths];
  135. NSArray *addedFiles = [matchedFiles filteredArrayUsingPredicate:filterPredicate];
  136. _addedFiles = [NSMutableDictionary dictionaryWithCapacity:[addedFiles count]];
  137. for (NSString *fileURL in addedFiles) {
  138. [_addedFiles setObject:@(0) forKey:fileURL];
  139. }
  140. _addMediaTimer = [NSTimer scheduledTimerWithTimeInterval:2. target:self
  141. selector:@selector(addFileTimerFired)
  142. userInfo:nil repeats:YES];
  143. } else {
  144. APLog(@"Directory content changes for undefined reason");
  145. }
  146. }
  147. #pragma mark - media list methods
  148. - (NSString *)directoryPath
  149. {
  150. #define LOCAL_PLAYBACK_HACK 1
  151. #if LOCAL_PLAYBACK_HACK && TARGET_IPHONE_SIMULATOR
  152. NSString *directoryPath = @"/Users/fkuehne/Desktop/VideoLAN docs/Clips/sel/";
  153. #else
  154. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  155. NSString *directoryPath = searchPaths[0];
  156. #endif
  157. return directoryPath;
  158. }
  159. - (void)updateMediaList
  160. {
  161. NSString *directoryPath = [self directoryPath];
  162. NSArray *foundFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil];
  163. NSMutableArray *filePaths = [NSMutableArray arrayWithCapacity:[foundFiles count]];
  164. NSURL *fileURL;
  165. for (NSString *fileName in foundFiles) {
  166. if ([fileName isSupportedMediaFormat]) {
  167. [filePaths addObject:[directoryPath stringByAppendingPathComponent:fileName]];
  168. /* exclude media files from backup (QA1719) */
  169. fileURL = [NSURL URLWithString:[NSString stringWithFormat:@"file://%@/%@", directoryPath, fileName]];
  170. [fileURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  171. }
  172. }
  173. [[MLMediaLibrary sharedMediaLibrary] addFilePaths:filePaths];
  174. [_playlistViewController updateViewContents];
  175. }
  176. #pragma mark - pass code validation
  177. - (void)validatePasscode
  178. {
  179. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  180. NSString *passcode = [defaults objectForKey:kVLCSettingPasscodeKey];
  181. if ([passcode isEqualToString:@""] || ![[defaults objectForKey:kVLCSettingPasscodeOnKey] boolValue]) {
  182. self.passcodeValidated = YES;
  183. return;
  184. }
  185. if (!self.passcodeValidated) {
  186. if ([self.nextPasscodeCheckDate earlierDate:[NSDate date]] == self.nextPasscodeCheckDate) {
  187. _passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionEnter];
  188. _passcodeLockController.delegate = self;
  189. _passcodeLockController.passcode = passcode;
  190. self.window.rootViewController = _passcodeLockController;
  191. } else
  192. self.passcodeValidated = YES;
  193. }
  194. }
  195. - (void)PAPasscodeViewControllerDidEnterPasscode:(PAPasscodeViewController *)controller
  196. {
  197. // TODO add transition animation, i.e. fade
  198. self.nextPasscodeCheckDate = [NSDate dateWithTimeIntervalSinceNow:300];
  199. self.window.rootViewController = self.navigationController;
  200. }
  201. - (void)PAPasscodeViewController:(PAPasscodeViewController *)controller didFailToEnterPasscode:(NSInteger)attempts
  202. {
  203. // TODO handle error attempts
  204. }
  205. @end