VLCAppDelegate.m 10 KB

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