VLCAppDelegate.m 13 KB

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