VLCAppDelegate.m 6.4 KB

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