VLCAppDelegate.m 12 KB

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