VLCAppDelegate.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  39. [navBar setBackgroundImage:[UIImage imageNamed:@"navBarBackgroundPhoneLandscape"] forBarMetrics:UIBarMetricsLandscapePhone];
  40. navBar.barStyle = UIBarStyleBlack;
  41. self.window.rootViewController = self.navigationController;
  42. [self.window makeKeyAndVisible];
  43. [self validatePasscode];
  44. _dropboxTableViewController = [[VLCDropboxTableViewController alloc] initWithNibName:@"VLCDropboxTableViewController" bundle:nil];
  45. return YES;
  46. }
  47. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
  48. {
  49. if ([[DBSession sharedSession] handleOpenURL:url]) {
  50. [self.dropboxTableViewController updateViewAfterSessionChange];
  51. return YES;
  52. }
  53. if (_playlistViewController && url != nil) {
  54. APLog(@"%@ requested %@ to be opened", sourceApplication, url);
  55. if (url.isFileURL) {
  56. 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];
  57. _tempURL = url;
  58. [alert show];
  59. } else
  60. [_playlistViewController openMovieFromURL:url];
  61. return YES;
  62. }
  63. return NO;
  64. }
  65. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  66. {
  67. if (buttonIndex == 1) {
  68. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  69. NSString *directoryPath = searchPaths[0];
  70. NSURL *destinationURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", directoryPath, _tempURL.lastPathComponent]];
  71. NSError *theError;
  72. [[NSFileManager defaultManager] copyItemAtURL:_tempURL toURL:destinationURL error:&theError];
  73. if (theError.code != noErr)
  74. APLog(@"saving the file failed (%i): %@", theError.code, theError.localizedDescription);
  75. [self updateMediaList];
  76. } else
  77. [_playlistViewController openMovieFromURL:_tempURL];
  78. }
  79. - (void)applicationWillResignActive:(UIApplication *)application
  80. {
  81. [[MLMediaLibrary sharedMediaLibrary] applicationWillExit];
  82. }
  83. - (void)applicationWillEnterForeground:(UIApplication *)application
  84. {
  85. APLog(@"applicationWillEnterForeground: %i", self.passcodeValidated);
  86. }
  87. - (void)applicationDidBecomeActive:(UIApplication *)application
  88. {
  89. [self updateMediaList];
  90. }
  91. - (void)applicationDidEnterBackground:(UIApplication *)application
  92. {
  93. [self validatePasscode]; // Lock library when going to background
  94. }
  95. - (void)applicationWillTerminate:(UIApplication *)application
  96. {
  97. // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  98. }
  99. #pragma mark - directory watcher delegate
  100. - (void)addFileTimerFired
  101. {
  102. NSArray *allKeys = [_addedFiles allKeys];
  103. for (NSString *fileURL in allKeys) {
  104. NSDictionary *attribs = [[NSFileManager defaultManager] attributesOfItemAtPath:fileURL error:nil];
  105. NSNumber *prevFetchedSize = [_addedFiles objectForKey:fileURL];
  106. NSNumber *updatedSize = [attribs objectForKey:NSFileSize];
  107. if ([prevFetchedSize compare:updatedSize] == NSOrderedSame) {
  108. [_addedFiles removeObjectForKey:fileURL];
  109. [[MLMediaLibrary sharedMediaLibrary] addFilePaths:@[fileURL]];
  110. /* exclude media files from backup (QA1719) */
  111. NSURL *excludeURL = [NSURL fileURLWithPath:fileURL];
  112. [excludeURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  113. // TODO Should we update media db after adding new files?
  114. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  115. [_playlistViewController updateViewContents];
  116. } else {
  117. [_addedFiles setObject:updatedSize forKey:fileURL];
  118. }
  119. }
  120. if (_addedFiles.count == 0) {
  121. [_addMediaTimer invalidate];
  122. _addMediaTimer = nil;
  123. }
  124. }
  125. - (void)directoryDidChange:(DirectoryWatcher *)folderWatcher
  126. {
  127. NSArray *foundFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[self directoryPath] error:nil];
  128. NSMutableArray *matchedFiles = [NSMutableArray arrayWithCapacity:foundFiles.count];
  129. for (NSString *fileName in foundFiles) {
  130. if ([fileName isSupportedMediaFormat]) {
  131. [matchedFiles addObject:[[self directoryPath] stringByAppendingPathComponent:fileName]];
  132. }
  133. }
  134. NSArray *mediaFiles = [MLFile allFiles];
  135. if (mediaFiles.count > matchedFiles.count) { // File was deleted
  136. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  137. [_playlistViewController updateViewContents];
  138. } else if (mediaFiles.count < matchedFiles.count) { // File was added
  139. NSMutableArray *addedFiles = [NSMutableArray array];
  140. for (NSString *fileName in matchedFiles) {
  141. NSURL *fileURL = [NSURL fileURLWithPath:fileName];
  142. BOOL found = NO;
  143. for (MLFile *mediaFile in mediaFiles) {
  144. if ([mediaFile.url isEqualToString:fileURL.absoluteString]) {
  145. found = YES;
  146. }
  147. }
  148. if (!found) {
  149. [addedFiles addObject:fileName];
  150. }
  151. }
  152. _addedFiles = [NSMutableDictionary dictionaryWithCapacity:[addedFiles count]];
  153. for (NSString *fileURL in addedFiles) {
  154. [_addedFiles setObject:@(0) forKey:fileURL];
  155. }
  156. _addMediaTimer = [NSTimer scheduledTimerWithTimeInterval:2. target:self
  157. selector:@selector(addFileTimerFired)
  158. userInfo:nil repeats:YES];
  159. }
  160. }
  161. #pragma mark - media list methods
  162. - (NSString *)directoryPath
  163. {
  164. #define LOCAL_PLAYBACK_HACK 1
  165. #if LOCAL_PLAYBACK_HACK && TARGET_IPHONE_SIMULATOR
  166. NSString *directoryPath = @"/Users/fkuehne/Desktop/VideoLAN docs/Clips/sel/";
  167. #else
  168. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  169. NSString *directoryPath = searchPaths[0];
  170. #endif
  171. return directoryPath;
  172. }
  173. - (void)updateMediaList
  174. {
  175. NSString *directoryPath = [self directoryPath];
  176. NSArray *foundFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil];
  177. NSMutableArray *filePaths = [NSMutableArray arrayWithCapacity:[foundFiles count]];
  178. NSURL *fileURL;
  179. for (NSString *fileName in foundFiles) {
  180. if ([fileName isSupportedMediaFormat]) {
  181. [filePaths addObject:[directoryPath stringByAppendingPathComponent:fileName]];
  182. /* exclude media files from backup (QA1719) */
  183. fileURL = [NSURL fileURLWithPath:[directoryPath stringByAppendingPathComponent:fileName]];
  184. [fileURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  185. }
  186. }
  187. [[MLMediaLibrary sharedMediaLibrary] addFilePaths:filePaths];
  188. [_playlistViewController updateViewContents];
  189. }
  190. #pragma mark - pass code validation
  191. - (void)validatePasscode
  192. {
  193. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  194. NSString *passcode = [defaults objectForKey:kVLCSettingPasscodeKey];
  195. if ([passcode isEqualToString:@""] || ![[defaults objectForKey:kVLCSettingPasscodeOnKey] boolValue]) {
  196. self.passcodeValidated = YES;
  197. return;
  198. }
  199. if (!self.passcodeValidated) {
  200. if ([self.nextPasscodeCheckDate earlierDate:[NSDate date]] == self.nextPasscodeCheckDate) {
  201. _passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionEnter];
  202. _passcodeLockController.delegate = self;
  203. _passcodeLockController.passcode = passcode;
  204. self.window.rootViewController = _passcodeLockController;
  205. } else
  206. self.passcodeValidated = YES;
  207. }
  208. }
  209. - (void)PAPasscodeViewControllerDidEnterPasscode:(PAPasscodeViewController *)controller
  210. {
  211. // TODO add transition animation, i.e. fade
  212. self.nextPasscodeCheckDate = [NSDate dateWithTimeIntervalSinceNow:300];
  213. self.window.rootViewController = self.navigationController;
  214. }
  215. - (void)PAPasscodeViewController:(PAPasscodeViewController *)controller didFailToEnterPasscode:(NSInteger)attempts
  216. {
  217. // TODO handle error attempts
  218. }
  219. @end