VLCAppDelegate.m 6.6 KB

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