VLCAppDelegate.m 10 KB

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