VLCAppDelegate.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. [self validatePasscode];
  131. [[MLMediaLibrary sharedMediaLibrary] applicationWillExit];
  132. }
  133. - (void)applicationDidBecomeActive:(UIApplication *)application
  134. {
  135. [self validatePasscode];
  136. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  137. [self updateMediaList];
  138. }
  139. - (void)applicationWillTerminate:(UIApplication *)application
  140. {
  141. self.passcodeValidated = NO;
  142. [[NSUserDefaults standardUserDefaults] synchronize];
  143. }
  144. #pragma mark - properties
  145. - (VLCDropboxTableViewController *)dropboxTableViewController
  146. {
  147. if (_dropboxTableViewController == nil)
  148. _dropboxTableViewController = [[VLCDropboxTableViewController alloc] initWithNibName:nil bundle:nil];
  149. return _dropboxTableViewController;
  150. }
  151. - (VLCGoogleDriveTableViewController *)googleDriveTableViewController
  152. {
  153. if (_googleDriveTableViewController == nil)
  154. _googleDriveTableViewController = [[VLCGoogleDriveTableViewController alloc] initWithNibName:nil bundle:nil];
  155. return _googleDriveTableViewController;
  156. }
  157. - (VLCDownloadViewController *)downloadViewController
  158. {
  159. if (_downloadViewController == nil)
  160. _downloadViewController = [[VLCDownloadViewController alloc] init];
  161. return _downloadViewController;
  162. }
  163. #pragma mark - media discovering
  164. - (void)mediaFileAdded:(NSString *)fileName loading:(BOOL)isLoading {
  165. if (!isLoading) {
  166. MLMediaLibrary *sharedLibrary = [MLMediaLibrary sharedMediaLibrary];
  167. [sharedLibrary addFilePaths:@[fileName]];
  168. /* exclude media files from backup (QA1719) */
  169. NSURL *excludeURL = [NSURL fileURLWithPath:fileName];
  170. [excludeURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  171. // TODO Should we update media db after adding new files?
  172. [sharedLibrary updateMediaDatabase];
  173. [_playlistViewController updateViewContents];
  174. }
  175. }
  176. - (void)mediaFileDeleted:(NSString *)name {
  177. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  178. [_playlistViewController updateViewContents];
  179. }
  180. #pragma mark - media list methods
  181. - (NSString *)directoryPath
  182. {
  183. #define LOCAL_PLAYBACK_HACK 0
  184. #if LOCAL_PLAYBACK_HACK && TARGET_IPHONE_SIMULATOR
  185. NSString *directoryPath = @"/Users/fkuehne/Desktop/VideoLAN docs/Clips/sel/";
  186. #else
  187. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  188. NSString *directoryPath = searchPaths[0];
  189. #endif
  190. return directoryPath;
  191. }
  192. - (void)updateMediaList
  193. {
  194. NSString *directoryPath = [self directoryPath];
  195. NSArray *foundFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil];
  196. NSMutableArray *filePaths = [NSMutableArray arrayWithCapacity:[foundFiles count]];
  197. NSURL *fileURL;
  198. for (NSString *fileName in foundFiles) {
  199. if ([fileName isSupportedMediaFormat] || [fileName isSupportedAudioMediaFormat]) {
  200. [filePaths addObject:[directoryPath stringByAppendingPathComponent:fileName]];
  201. /* exclude media files from backup (QA1719) */
  202. fileURL = [NSURL fileURLWithPath:[directoryPath stringByAppendingPathComponent:fileName]];
  203. [fileURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  204. }
  205. }
  206. [[MLMediaLibrary sharedMediaLibrary] addFilePaths:filePaths];
  207. [_playlistViewController updateViewContents];
  208. }
  209. #pragma mark - pass code validation
  210. - (void)validatePasscode
  211. {
  212. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  213. NSString *passcode = [defaults objectForKey:kVLCSettingPasscodeKey];
  214. if ([passcode isEqualToString:@""] || ![[defaults objectForKey:kVLCSettingPasscodeOnKey] boolValue]) {
  215. self.passcodeValidated = YES;
  216. return;
  217. }
  218. if (!self.passcodeValidated) {
  219. _passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionEnter];
  220. _passcodeLockController.delegate = self;
  221. _passcodeLockController.passcode = passcode;
  222. self.window.rootViewController = _passcodeLockController;
  223. }
  224. }
  225. - (void)PAPasscodeViewControllerDidEnterPasscode:(PAPasscodeViewController *)controller
  226. {
  227. // TODO add transition animation, i.e. fade
  228. self.window.rootViewController = self.revealController;
  229. }
  230. - (void)PAPasscodeViewController:(PAPasscodeViewController *)controller didFailToEnterPasscode:(NSInteger)attempts
  231. {
  232. // TODO handle error attempts
  233. }
  234. #pragma mark - idle timer preventer
  235. - (void)disableIdleTimer
  236. {
  237. _idleCounter++;
  238. if ([UIApplication sharedApplication].idleTimerDisabled == NO)
  239. [UIApplication sharedApplication].idleTimerDisabled = YES;
  240. }
  241. - (void)activateIdleTimer
  242. {
  243. _idleCounter--;
  244. if (_idleCounter < 1)
  245. [UIApplication sharedApplication].idleTimerDisabled = NO;
  246. }
  247. #pragma mark - playback view handling
  248. - (void)openMediaFromManagedObject:(NSManagedObject *)mediaObject
  249. {
  250. if (!_movieViewController)
  251. _movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  252. if ([mediaObject isKindOfClass:[MLFile class]])
  253. _movieViewController.mediaItem = (MLFile *)mediaObject;
  254. else if ([mediaObject isKindOfClass:[MLAlbumTrack class]])
  255. _movieViewController.mediaItem = [(MLAlbumTrack*)mediaObject files].anyObject;
  256. else if ([mediaObject isKindOfClass:[MLShowEpisode class]])
  257. _movieViewController.mediaItem = [(MLShowEpisode*)mediaObject files].anyObject;
  258. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_movieViewController];
  259. navCon.modalPresentationStyle = UIModalPresentationFullScreen;
  260. [self.window.rootViewController presentViewController:navCon animated:YES completion:nil];
  261. }
  262. - (void)openMovieFromURL:(NSURL *)url
  263. {
  264. if (!_movieViewController)
  265. _movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  266. _movieViewController.url = url;
  267. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_movieViewController];
  268. navCon.modalPresentationStyle = UIModalPresentationFullScreen;
  269. [self.window.rootViewController presentViewController:navCon animated:YES completion:nil];
  270. }
  271. @end