VLCAppDelegate.m 13 KB

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