VLCAppDelegate.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. NSURL *_tempURL;
  14. PAPasscodeViewController *_passcodeLockController;
  15. }
  16. @property (nonatomic) BOOL passcodeValidated;
  17. @end
  18. @implementation VLCAppDelegate
  19. + (void)initialize
  20. {
  21. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  22. NSDictionary *appDefaults = @{kVLCSettingPasscodeKey : @"", kVLCSettingPasscodeOnKey : @(NO), kVLCSettingContinueAudioInBackgroundKey : @(YES), kVLCSettingStretchAudio : @(NO), kVLCSettingTextEncoding : kVLCSettingTextEncodingDefaultValue};
  23. [defaults registerDefaults:appDefaults];
  24. }
  25. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  26. {
  27. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  28. _playlistViewController = [[VLCPlaylistViewController alloc] init];
  29. self.navigationController = [[UINavigationController alloc] initWithRootViewController:_playlistViewController];
  30. UINavigationBar *navBar = self.navigationController.navigationBar;
  31. [navBar setBackgroundImage:[UIImage imageNamed:@"navBarBackground"] forBarMetrics:UIBarMetricsDefault];
  32. navBar.barStyle = UIBarStyleBlack;
  33. self.window.rootViewController = self.navigationController;
  34. [self.window makeKeyAndVisible];
  35. _dropboxTableViewController = [[VLCDropboxTableViewController alloc] initWithNibName:@"VLCDropboxTableViewController" bundle:nil];
  36. return YES;
  37. }
  38. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
  39. {
  40. if ([[DBSession sharedSession] handleOpenURL:url]) {
  41. [self.dropboxTableViewController updateViewAfterSessionChange];
  42. return YES;
  43. }
  44. if (_playlistViewController && url != nil) {
  45. APLog(@"%@ requested %@ to be opened", sourceApplication, url);
  46. if (url.isFileURL) {
  47. 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];
  48. _tempURL = url;
  49. [alert show];
  50. } else
  51. [_playlistViewController openMovieFromURL:url];
  52. return YES;
  53. }
  54. return NO;
  55. }
  56. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  57. {
  58. if (buttonIndex == 1) {
  59. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  60. NSString *directoryPath = searchPaths[0];
  61. NSURL *destinationURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", directoryPath, _tempURL.lastPathComponent]];
  62. NSError *theError;
  63. [[NSFileManager defaultManager] copyItemAtURL:_tempURL toURL:destinationURL error:&theError];
  64. if (theError.code != noErr)
  65. APLog(@"saving the file failed (%i): %@", theError.code, theError.localizedDescription);
  66. [self updateMediaList];
  67. } else
  68. [_playlistViewController openMovieFromURL:_tempURL];
  69. }
  70. - (void)applicationWillResignActive:(UIApplication *)application
  71. {
  72. [[MLMediaLibrary sharedMediaLibrary] applicationWillExit];
  73. }
  74. - (void)applicationWillEnterForeground:(UIApplication *)application
  75. {
  76. APLog(@"applicationWillEnterForeground: %i", self.passcodeValidated);
  77. }
  78. - (void)applicationDidBecomeActive:(UIApplication *)application
  79. {
  80. [self updateMediaList];
  81. }
  82. - (void)applicationDidEnterBackground:(UIApplication *)application
  83. {
  84. [self validatePasscode]; // Lock library when going to background
  85. }
  86. - (void)applicationWillTerminate:(UIApplication *)application
  87. {
  88. // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  89. }
  90. - (void)updateMediaList
  91. {
  92. #define LOCAL_PLAYBACK_HACK 1
  93. #if LOCAL_PLAYBACK_HACK && TARGET_IPHONE_SIMULATOR
  94. NSString *directoryPath = @"/Users/fkuehne/Desktop/VideoLAN docs/Clips/sel/";
  95. #else
  96. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  97. NSString *directoryPath = searchPaths[0];
  98. #endif
  99. NSArray *foundFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil];
  100. NSMutableArray *filePaths = [NSMutableArray arrayWithCapacity:[foundFiles count]];
  101. NSURL *fileURL;
  102. for (NSString *fileName in foundFiles) {
  103. if ([fileName rangeOfString:kSupportedFileExtensions options:NSRegularExpressionSearch|NSCaseInsensitiveSearch].length != 0) {
  104. [filePaths addObject:[directoryPath stringByAppendingPathComponent:fileName]];
  105. /* exclude media files from backup (QA1719) */
  106. fileURL = [NSURL URLWithString:[NSString stringWithFormat:@"file://%@/%@", directoryPath, fileName]];
  107. [fileURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  108. }
  109. }
  110. [[MLMediaLibrary sharedMediaLibrary] addFilePaths:filePaths];
  111. [_playlistViewController updateViewContents];
  112. }
  113. #pragma mark - pass code validation
  114. - (void)validatePasscode
  115. {
  116. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  117. NSString *passcode = [defaults objectForKey:kVLCSettingPasscodeKey];
  118. if ([passcode isEqualToString:@""] || ![[defaults objectForKey:kVLCSettingPasscodeOnKey] boolValue]) {
  119. self.passcodeValidated = YES;
  120. return;
  121. }
  122. if (!self.passcodeValidated) {
  123. if ([self.nextPasscodeCheckDate earlierDate:[NSDate date]] == self.nextPasscodeCheckDate) {
  124. _passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionEnter];
  125. _passcodeLockController.delegate = self;
  126. _passcodeLockController.passcode = passcode;
  127. self.window.rootViewController = _passcodeLockController;
  128. } else
  129. self.passcodeValidated = YES;
  130. }
  131. }
  132. - (void)PAPasscodeViewControllerDidEnterPasscode:(PAPasscodeViewController *)controller
  133. {
  134. // TODO add transition animation, i.e. fade
  135. self.nextPasscodeCheckDate = [NSDate dateWithTimeIntervalSinceNow:300];
  136. self.window.rootViewController = self.navigationController;
  137. }
  138. - (void)PAPasscodeViewController:(PAPasscodeViewController *)controller didFailToEnterPasscode:(NSInteger)attempts
  139. {
  140. // TODO handle error attempts
  141. }
  142. @end