VLCAppDelegate.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. // Refer to the COPYING file of the official project for license.
  9. //
  10. #import "VLCAppDelegate.h"
  11. #import "VLCMediaFileDiscoverer.h"
  12. #import "NSString+SupportedMedia.h"
  13. #import "UIDevice+SpeedCategory.h"
  14. #import "VLCPlaylistViewController.h"
  15. #import "VLCMenuViewController.h"
  16. #import "VLCMovieViewController.h"
  17. #import "PAPasscodeViewController.h"
  18. #import "UINavigationController+Theme.h"
  19. @interface VLCAppDelegate () <PAPasscodeViewControllerDelegate, VLCMediaFileDiscovererDelegate> {
  20. PAPasscodeViewController *_passcodeLockController;
  21. VLCDropboxTableViewController *_dropboxTableViewController;
  22. }
  23. @property (nonatomic) BOOL passcodeValidated;
  24. @end
  25. @implementation VLCAppDelegate
  26. + (void)initialize
  27. {
  28. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  29. NSNumber *skipLoopFilterDefaultValue;
  30. int deviceSpeedCategory = [[UIDevice currentDevice] speedCategory];
  31. if (deviceSpeedCategory < 3)
  32. skipLoopFilterDefaultValue = kVLCSettingSkipLoopFilterNonKey;
  33. else
  34. skipLoopFilterDefaultValue = kVLCSettingSkipLoopFilterNonRef;
  35. NSDictionary *appDefaults = @{kVLCSettingPasscodeKey : @"", kVLCSettingPasscodeOnKey : @(NO), kVLCSettingContinueAudioInBackgroundKey : @(YES), kVLCSettingStretchAudio : @(NO), kVLCSettingTextEncoding : kVLCSettingTextEncodingDefaultValue, kVLCSettingSkipLoopFilter : skipLoopFilterDefaultValue};
  36. [defaults registerDefaults:appDefaults];
  37. }
  38. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  39. {
  40. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  41. _playlistViewController = [[VLCPlaylistViewController alloc] init];
  42. self.navigationController = [[UINavigationController alloc] initWithRootViewController:_playlistViewController];
  43. [self.navigationController loadTheme];
  44. self.window.rootViewController = self.navigationController;
  45. [self.window makeKeyAndVisible];
  46. VLCMediaFileDiscoverer *discoverer = [VLCMediaFileDiscoverer sharedInstance];
  47. [discoverer addObserver:self];
  48. [discoverer startDiscovering:[self directoryPath]];
  49. [self validatePasscode];
  50. return YES;
  51. }
  52. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
  53. {
  54. if ([[DBSession sharedSession] handleOpenURL:url]) {
  55. [self.dropboxTableViewController updateViewAfterSessionChange];
  56. return YES;
  57. }
  58. if (_playlistViewController && url != nil) {
  59. APLog(@"%@ requested %@ to be opened", sourceApplication, url);
  60. if (url.isFileURL) {
  61. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  62. NSString *directoryPath = searchPaths[0];
  63. NSURL *destinationURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", directoryPath, url.lastPathComponent]];
  64. NSError *theError;
  65. [[NSFileManager defaultManager] moveItemAtURL:url toURL:destinationURL error:&theError];
  66. if (theError.code != noErr)
  67. APLog(@"saving the file failed (%i): %@", theError.code, theError.localizedDescription);
  68. [self updateMediaList];
  69. } else {
  70. NSURL *parsedUrl = [self parseOpenURL:url];
  71. [_playlistViewController openMovieFromURL:parsedUrl];
  72. }
  73. return YES;
  74. }
  75. return NO;
  76. }
  77. - (NSURL *)parseOpenURL:(NSURL *)url
  78. {
  79. NSString *receivedUrl = [url absoluteString];
  80. if ([receivedUrl length] > 6) {
  81. NSString *verifyVlcUrl = [receivedUrl substringToIndex:6];
  82. if ([verifyVlcUrl isEqualToString:@"vlc://"]) {
  83. NSString *parsedString = [receivedUrl substringFromIndex:6];
  84. // "url decode" so we can parse http:// links
  85. parsedString = [parsedString stringByReplacingOccurrencesOfString:@"+"withString:@" "];
  86. parsedString = [parsedString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  87. // add http:// if nothing is there
  88. NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:(NSTextCheckingTypes)NSTextCheckingTypeLink error:nil];
  89. NSUInteger parsedStringLength = [parsedString length];
  90. NSUInteger numberOfUrlMatches = [detector numberOfMatchesInString:parsedString options:0 range:NSMakeRange(0, parsedStringLength)];
  91. if (numberOfUrlMatches == 0) {
  92. parsedString = [@"http://" stringByAppendingString:parsedString];
  93. }
  94. NSURL *targetUrl = [NSURL URLWithString:parsedString];
  95. return targetUrl;
  96. }
  97. }
  98. return url;
  99. }
  100. - (void)applicationWillResignActive:(UIApplication *)application
  101. {
  102. [[MLMediaLibrary sharedMediaLibrary] applicationWillExit];
  103. }
  104. - (void)applicationDidBecomeActive:(UIApplication *)application
  105. {
  106. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  107. [self updateMediaList];
  108. }
  109. - (void)applicationDidEnterBackground:(UIApplication *)application
  110. {
  111. [self validatePasscode]; // Lock library when going to background
  112. }
  113. #pragma mark - properties
  114. - (VLCDropboxTableViewController *)dropboxTableViewController
  115. {
  116. if (_dropboxTableViewController == nil) {
  117. _dropboxTableViewController = [[VLCDropboxTableViewController alloc] initWithNibName:nil bundle:nil];
  118. }
  119. return _dropboxTableViewController;
  120. }
  121. #pragma mark - media discovering
  122. - (void)mediaFileAdded:(NSString *)fileName loading:(BOOL)isLoading {
  123. if (!isLoading) {
  124. MLMediaLibrary *sharedLibrary = [MLMediaLibrary sharedMediaLibrary];
  125. [sharedLibrary addFilePaths:@[fileName]];
  126. /* exclude media files from backup (QA1719) */
  127. NSURL *excludeURL = [NSURL fileURLWithPath:fileName];
  128. [excludeURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  129. // TODO Should we update media db after adding new files?
  130. [sharedLibrary updateMediaDatabase];
  131. [_playlistViewController updateViewContents];
  132. }
  133. }
  134. - (void)mediaFileDeleted:(NSString *)name {
  135. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  136. [_playlistViewController updateViewContents];
  137. }
  138. #pragma mark - media list methods
  139. - (NSString *)directoryPath
  140. {
  141. #define LOCAL_PLAYBACK_HACK 0
  142. #if LOCAL_PLAYBACK_HACK && TARGET_IPHONE_SIMULATOR
  143. NSString *directoryPath = @"/Users/fkuehne/Desktop/VideoLAN docs/Clips/sel/";
  144. #else
  145. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  146. NSString *directoryPath = searchPaths[0];
  147. #endif
  148. return directoryPath;
  149. }
  150. - (void)updateMediaList
  151. {
  152. NSString *directoryPath = [self directoryPath];
  153. NSArray *foundFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil];
  154. NSMutableArray *filePaths = [NSMutableArray arrayWithCapacity:[foundFiles count]];
  155. NSURL *fileURL;
  156. for (NSString *fileName in foundFiles) {
  157. if ([fileName isSupportedMediaFormat]) {
  158. [filePaths addObject:[directoryPath stringByAppendingPathComponent:fileName]];
  159. /* exclude media files from backup (QA1719) */
  160. fileURL = [NSURL fileURLWithPath:[directoryPath stringByAppendingPathComponent:fileName]];
  161. [fileURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  162. }
  163. }
  164. [[MLMediaLibrary sharedMediaLibrary] addFilePaths:filePaths];
  165. [_playlistViewController updateViewContents];
  166. }
  167. #pragma mark - pass code validation
  168. - (void)validatePasscode
  169. {
  170. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  171. NSString *passcode = [defaults objectForKey:kVLCSettingPasscodeKey];
  172. if ([passcode isEqualToString:@""] || ![[defaults objectForKey:kVLCSettingPasscodeOnKey] boolValue]) {
  173. self.passcodeValidated = YES;
  174. return;
  175. }
  176. if (!self.passcodeValidated) {
  177. if ([self.nextPasscodeCheckDate earlierDate:[NSDate date]] == self.nextPasscodeCheckDate) {
  178. _passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionEnter];
  179. _passcodeLockController.delegate = self;
  180. _passcodeLockController.passcode = passcode;
  181. self.window.rootViewController = _passcodeLockController;
  182. } else
  183. self.passcodeValidated = YES;
  184. }
  185. }
  186. - (void)PAPasscodeViewControllerDidEnterPasscode:(PAPasscodeViewController *)controller
  187. {
  188. // TODO add transition animation, i.e. fade
  189. self.nextPasscodeCheckDate = [NSDate dateWithTimeIntervalSinceNow:300];
  190. self.window.rootViewController = self.navigationController;
  191. }
  192. - (void)PAPasscodeViewController:(PAPasscodeViewController *)controller didFailToEnterPasscode:(NSInteger)attempts
  193. {
  194. // TODO handle error attempts
  195. }
  196. @end