VLCAppDelegate.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "VLCPlaylistViewController.h"
  10. #import "VLCMovieViewController.h"
  11. #import "PAPasscodeViewController.h"
  12. @interface VLCAppDelegate () <PAPasscodeViewControllerDelegate>
  13. @property (nonatomic) BOOL passcodeValidated;
  14. @end
  15. @implementation VLCAppDelegate
  16. + (void)initialize
  17. {
  18. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  19. NSDictionary *appDefaults = @{kVLCSettingPasscodeKey : @"", kVLCSettingPasscodeOnKey : @(NO), kVLCSettingContinueAudioInBackgroundKey : @(YES), kVLCSettingStretchAudio : @(NO), kVLCSettingVerboseOutput : @(NO), kVLCSettingTextEncoding : kVLCSettingTextEncodingDefaultValue};
  20. [defaults registerDefaults:appDefaults];
  21. }
  22. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  23. {
  24. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  25. _playlistViewController = [[VLCPlaylistViewController alloc] initWithNibName:@"VLCPlaylistViewController" bundle:nil];
  26. self.navigationController = [[UINavigationController alloc] initWithRootViewController:_playlistViewController];
  27. self.window.rootViewController = self.navigationController;
  28. self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
  29. [self.window makeKeyAndVisible];
  30. return YES;
  31. }
  32. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
  33. {
  34. if (_playlistViewController && url != nil) {
  35. APLog(@"%@ requested %@ to be opened", sourceApplication, url);
  36. if (url.isFileURL) {
  37. 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];
  38. _tempURL = url;
  39. [alert show];
  40. } else
  41. [_playlistViewController openMovieFromURL:url];
  42. return YES;
  43. }
  44. return NO;
  45. }
  46. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  47. {
  48. if (buttonIndex == 1) {
  49. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  50. NSString *directoryPath = searchPaths[0];
  51. NSURL *destinationURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", directoryPath, _tempURL.lastPathComponent]];
  52. NSError *theError;
  53. [[NSFileManager defaultManager] copyItemAtURL:_tempURL toURL:destinationURL error:&theError];
  54. if (theError.code != noErr)
  55. APLog(@"saving the file failed (%i): %@", theError.code, theError.localizedDescription);
  56. [self updateMediaList];
  57. } else
  58. [_playlistViewController openMovieFromURL:_tempURL];
  59. }
  60. - (void)applicationWillResignActive:(UIApplication *)application
  61. {
  62. [[MLMediaLibrary sharedMediaLibrary] applicationWillExit];
  63. }
  64. - (void)applicationWillEnterForeground:(UIApplication *)application
  65. {
  66. NSLog(@"applicationWillEnterForeground: %i", self.passcodeValidated);
  67. }
  68. - (void)applicationDidBecomeActive:(UIApplication *)application
  69. {
  70. [self updateMediaList];
  71. }
  72. - (void)applicationDidEnterBackground:(UIApplication *)application
  73. {
  74. [self validatePasscode]; // Lock library when going to background
  75. }
  76. - (void)applicationWillTerminate:(UIApplication *)application
  77. {
  78. // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  79. }
  80. - (void)updateMediaList
  81. {
  82. #define LOCAL_PLAYBACK_HACK 1
  83. #if LOCAL_PLAYBACK_HACK && TARGET_IPHONE_SIMULATOR
  84. NSString *directoryPath = @"/Users/fkuehne/Desktop/VideoLAN docs/Clips/sel/";
  85. #else
  86. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  87. NSString *directoryPath = searchPaths[0];
  88. #endif
  89. NSArray *foundFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil];
  90. NSMutableArray *filePaths = [NSMutableArray arrayWithCapacity:[foundFiles count]];
  91. NSURL *fileURL;
  92. for (NSString *fileName in foundFiles) {
  93. if ([fileName rangeOfString:@"\\.(3gp|3gp|3gp2|3gpp|amv|asf|avi|axv|divx|dv|flv|f4v|gvi|gxf|m1v|m2p|m2t|m2ts|m2v|m4v|mks|mkv|moov|mov|mp2v|mp4|mpeg|mpeg1|mpeg2|mpeg4|mpg|mpv|mt2s|mts|mxf|nsv|nuv|oga|ogg|ogm|ogv|ogx|spx|ps|qt|rar|rec|rm|rmvb|tod|ts|tts|vob|vro|webm|wm|wmv|wtv|xesc)$" options:NSRegularExpressionSearch|NSCaseInsensitiveSearch].length != 0) {
  94. [filePaths addObject:[directoryPath stringByAppendingPathComponent:fileName]];
  95. /* exclude media files from backup (QA1719) */
  96. fileURL = [NSURL URLWithString:[NSString stringWithFormat:@"file://%@/%@", directoryPath, fileName]];
  97. [fileURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  98. }
  99. }
  100. [[MLMediaLibrary sharedMediaLibrary] addFilePaths:filePaths];
  101. [_playlistViewController updateViewContents];
  102. }
  103. #pragma mark - pass code validation
  104. - (void)validatePasscode
  105. {
  106. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  107. NSString *passcode = [defaults objectForKey:kVLCSettingPasscodeKey];
  108. if ([passcode isEqualToString:@""]) {
  109. self.passcodeValidated = YES;
  110. return;
  111. }
  112. if (!self.passcodeValidated) {
  113. if ([self.nextPasscodeCheckDate earlierDate:[NSDate date]] == self.nextPasscodeCheckDate) {
  114. _passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionEnter];
  115. _passcodeLockController.delegate = self;
  116. _passcodeLockController.passcode = passcode;
  117. self.window.rootViewController = _passcodeLockController;
  118. } else
  119. self.passcodeValidated = YES;
  120. }
  121. }
  122. - (void)PAPasscodeViewControllerDidCancel:(PAPasscodeViewController *)controller
  123. {
  124. // TODO remove cancel button for Enter action
  125. }
  126. - (void)PAPasscodeViewControllerDidEnterPasscode:(PAPasscodeViewController *)controller
  127. {
  128. // TODO add transition animation, i.e. fade
  129. self.nextPasscodeCheckDate = [NSDate dateWithTimeIntervalSinceNow:300];
  130. self.window.rootViewController = self.navigationController;
  131. }
  132. - (void)PAPasscodeViewController:(PAPasscodeViewController *)controller didFailToEnterPasscode:(NSInteger)attempts
  133. {
  134. // TODO handle error attempts
  135. }
  136. @end