VLCAppDelegate.m 11 KB

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