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. #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. VLCDropboxTableViewController *_dropboxTableViewController;
  18. DirectoryWatcher *_directoryWatcher;
  19. NSTimer *_addMediaTimer;
  20. NSMutableDictionary *_addedFiles;
  21. }
  22. @property (nonatomic) BOOL passcodeValidated;
  23. @end
  24. @implementation VLCAppDelegate
  25. + (void)initialize
  26. {
  27. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  28. NSDictionary *appDefaults = @{kVLCSettingPasscodeKey : @"", kVLCSettingPasscodeOnKey : @(NO), kVLCSettingContinueAudioInBackgroundKey : @(YES), kVLCSettingStretchAudio : @(NO), kVLCSettingTextEncoding : kVLCSettingTextEncodingDefaultValue};
  29. [defaults registerDefaults:appDefaults];
  30. }
  31. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  32. {
  33. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  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. _directoryWatcher = [DirectoryWatcher watchFolderWithPath:[self directoryPath] delegate:self];
  44. [self validatePasscode];
  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 - properties
  100. - (VLCDropboxTableViewController *)dropboxTableViewController
  101. {
  102. if (_dropboxTableViewController == nil) {
  103. _dropboxTableViewController = [[VLCDropboxTableViewController alloc] initWithNibName:@"VLCDropboxTableViewController" bundle:nil];
  104. }
  105. return _dropboxTableViewController;
  106. }
  107. #pragma mark - directory watcher delegate
  108. - (void)addFileTimerFired
  109. {
  110. NSArray *allKeys = [_addedFiles allKeys];
  111. for (NSString *fileURL in allKeys) {
  112. NSDictionary *attribs = [[NSFileManager defaultManager] attributesOfItemAtPath:fileURL error:nil];
  113. NSNumber *prevFetchedSize = [_addedFiles objectForKey:fileURL];
  114. NSNumber *updatedSize = [attribs objectForKey:NSFileSize];
  115. if ([prevFetchedSize compare:updatedSize] == NSOrderedSame) {
  116. [_addedFiles removeObjectForKey:fileURL];
  117. [[MLMediaLibrary sharedMediaLibrary] addFilePaths:@[fileURL]];
  118. /* exclude media files from backup (QA1719) */
  119. NSURL *excludeURL = [NSURL fileURLWithPath:fileURL];
  120. [excludeURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  121. // TODO Should we update media db after adding new files?
  122. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  123. [_playlistViewController updateViewContents];
  124. } else {
  125. [_addedFiles setObject:updatedSize forKey:fileURL];
  126. }
  127. }
  128. if (_addedFiles.count == 0) {
  129. [_addMediaTimer invalidate];
  130. _addMediaTimer = nil;
  131. }
  132. }
  133. - (void)directoryDidChange:(DirectoryWatcher *)folderWatcher
  134. {
  135. NSArray *foundFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[self directoryPath] error:nil];
  136. NSMutableArray *matchedFiles = [NSMutableArray arrayWithCapacity:foundFiles.count];
  137. for (NSString *fileName in foundFiles) {
  138. if ([fileName isSupportedMediaFormat]) {
  139. [matchedFiles addObject:[[self directoryPath] stringByAppendingPathComponent:fileName]];
  140. }
  141. }
  142. NSArray *mediaFiles = [MLFile allFiles];
  143. if (mediaFiles.count > matchedFiles.count) { // File was deleted
  144. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  145. [_playlistViewController updateViewContents];
  146. } else if (mediaFiles.count < matchedFiles.count) { // File was added
  147. NSMutableArray *addedFiles = [NSMutableArray array];
  148. for (NSString *fileName in matchedFiles) {
  149. NSURL *fileURL = [NSURL fileURLWithPath:fileName];
  150. BOOL found = NO;
  151. for (MLFile *mediaFile in mediaFiles) {
  152. if ([mediaFile.url isEqualToString:fileURL.absoluteString]) {
  153. found = YES;
  154. }
  155. }
  156. if (!found) {
  157. [addedFiles addObject:fileName];
  158. }
  159. }
  160. _addedFiles = [NSMutableDictionary dictionaryWithCapacity:[addedFiles count]];
  161. for (NSString *fileURL in addedFiles) {
  162. [_addedFiles setObject:@(0) forKey:fileURL];
  163. }
  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