VLCAppDelegate.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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)applicationDidBecomeActive:(UIApplication *)application
  86. {
  87. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  88. [self updateMediaList];
  89. }
  90. - (void)applicationDidEnterBackground:(UIApplication *)application
  91. {
  92. [self validatePasscode]; // Lock library when going to background
  93. }
  94. #pragma mark - properties
  95. - (VLCDropboxTableViewController *)dropboxTableViewController
  96. {
  97. if (_dropboxTableViewController == nil) {
  98. _dropboxTableViewController = [[VLCDropboxTableViewController alloc] initWithNibName:nil bundle:nil];
  99. }
  100. return _dropboxTableViewController;
  101. }
  102. #pragma mark - directory watcher delegate
  103. - (void)addFileTimerFired
  104. {
  105. NSArray *allKeys = [_addedFiles allKeys];
  106. for (NSString *fileURL in allKeys) {
  107. NSDictionary *attribs = [[NSFileManager defaultManager] attributesOfItemAtPath:fileURL error:nil];
  108. NSNumber *prevFetchedSize = [_addedFiles objectForKey:fileURL];
  109. NSNumber *updatedSize = [attribs objectForKey:NSFileSize];
  110. if ([prevFetchedSize compare:updatedSize] == NSOrderedSame) {
  111. [_addedFiles removeObjectForKey:fileURL];
  112. [[MLMediaLibrary sharedMediaLibrary] addFilePaths:@[fileURL]];
  113. /* exclude media files from backup (QA1719) */
  114. NSURL *excludeURL = [NSURL fileURLWithPath:fileURL];
  115. [excludeURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  116. // TODO Should we update media db after adding new files?
  117. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  118. [_playlistViewController updateViewContents];
  119. } else {
  120. [_addedFiles setObject:updatedSize forKey:fileURL];
  121. }
  122. }
  123. if (_addedFiles.count == 0) {
  124. [_addMediaTimer invalidate];
  125. _addMediaTimer = nil;
  126. }
  127. }
  128. - (void)directoryDidChange:(DirectoryWatcher *)folderWatcher
  129. {
  130. NSArray *foundFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[self directoryPath] error:nil];
  131. NSMutableArray *matchedFiles = [NSMutableArray arrayWithCapacity:foundFiles.count];
  132. for (NSString *fileName in foundFiles) {
  133. if ([fileName isSupportedMediaFormat]) {
  134. [matchedFiles addObject:[[self directoryPath] stringByAppendingPathComponent:fileName]];
  135. }
  136. }
  137. NSArray *mediaFiles = [MLFile allFiles];
  138. if (mediaFiles.count > matchedFiles.count) { // File was deleted
  139. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  140. [_playlistViewController updateViewContents];
  141. } else if (mediaFiles.count < matchedFiles.count) { // File was added
  142. NSMutableArray *addedFiles = [NSMutableArray array];
  143. for (NSString *fileName in matchedFiles) {
  144. NSURL *fileURL = [NSURL fileURLWithPath:fileName];
  145. BOOL found = NO;
  146. for (MLFile *mediaFile in mediaFiles) {
  147. if ([mediaFile.url isEqualToString:fileURL.absoluteString]) {
  148. found = YES;
  149. }
  150. }
  151. if (!found) {
  152. [addedFiles addObject:fileName];
  153. }
  154. }
  155. _addedFiles = [NSMutableDictionary dictionaryWithCapacity:[addedFiles count]];
  156. for (NSString *fileURL in addedFiles) {
  157. [_addedFiles setObject:@(0) forKey:fileURL];
  158. }
  159. _addMediaTimer = [NSTimer scheduledTimerWithTimeInterval:2. target:self
  160. selector:@selector(addFileTimerFired)
  161. userInfo:nil repeats:YES];
  162. }
  163. }
  164. #pragma mark - media list methods
  165. - (NSString *)directoryPath
  166. {
  167. #define LOCAL_PLAYBACK_HACK 1
  168. #if LOCAL_PLAYBACK_HACK && TARGET_IPHONE_SIMULATOR
  169. NSString *directoryPath = @"/Users/fkuehne/Desktop/VideoLAN docs/Clips/sel/";
  170. #else
  171. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  172. NSString *directoryPath = searchPaths[0];
  173. #endif
  174. return directoryPath;
  175. }
  176. - (void)updateMediaList
  177. {
  178. NSString *directoryPath = [self directoryPath];
  179. NSArray *foundFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil];
  180. NSMutableArray *filePaths = [NSMutableArray arrayWithCapacity:[foundFiles count]];
  181. NSURL *fileURL;
  182. for (NSString *fileName in foundFiles) {
  183. if ([fileName isSupportedMediaFormat]) {
  184. [filePaths addObject:[directoryPath stringByAppendingPathComponent:fileName]];
  185. /* exclude media files from backup (QA1719) */
  186. fileURL = [NSURL fileURLWithPath:[directoryPath stringByAppendingPathComponent:fileName]];
  187. [fileURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  188. }
  189. }
  190. [[MLMediaLibrary sharedMediaLibrary] addFilePaths:filePaths];
  191. [_playlistViewController updateViewContents];
  192. }
  193. #pragma mark - pass code validation
  194. - (void)validatePasscode
  195. {
  196. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  197. NSString *passcode = [defaults objectForKey:kVLCSettingPasscodeKey];
  198. if ([passcode isEqualToString:@""] || ![[defaults objectForKey:kVLCSettingPasscodeOnKey] boolValue]) {
  199. self.passcodeValidated = YES;
  200. return;
  201. }
  202. if (!self.passcodeValidated) {
  203. if ([self.nextPasscodeCheckDate earlierDate:[NSDate date]] == self.nextPasscodeCheckDate) {
  204. _passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionEnter];
  205. _passcodeLockController.delegate = self;
  206. _passcodeLockController.passcode = passcode;
  207. self.window.rootViewController = _passcodeLockController;
  208. } else
  209. self.passcodeValidated = YES;
  210. }
  211. }
  212. - (void)PAPasscodeViewControllerDidEnterPasscode:(PAPasscodeViewController *)controller
  213. {
  214. // TODO add transition animation, i.e. fade
  215. self.nextPasscodeCheckDate = [NSDate dateWithTimeIntervalSinceNow:300];
  216. self.window.rootViewController = self.navigationController;
  217. }
  218. - (void)PAPasscodeViewController:(PAPasscodeViewController *)controller didFailToEnterPasscode:(NSInteger)attempts
  219. {
  220. // TODO handle error attempts
  221. }
  222. @end