VLCAppDelegate.m 10 KB

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