VLCAppDelegate.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. /* clean caches on launch (since those are used for wifi upload only) */
  52. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  53. NSString* uploadDirPath = searchPaths[0];
  54. NSFileManager *fileManager = [NSFileManager defaultManager];
  55. if ([fileManager fileExistsAtPath:uploadDirPath])
  56. [fileManager removeItemAtPath:uploadDirPath error:nil];
  57. // Init the HTTP Server
  58. self.uploadController = [[VLCHTTPUploaderController alloc] init];
  59. // enable crash preventer
  60. [[MLMediaLibrary sharedMediaLibrary] applicationWillStart];
  61. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  62. _playlistViewController = [[VLCPlaylistViewController alloc] init];
  63. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_playlistViewController];
  64. [navCon loadTheme];
  65. _revealController = [[GHRevealViewController alloc] initWithNibName:nil bundle:nil];
  66. _revealController.wantsFullScreenLayout = YES;
  67. _menuViewController = [[VLCMenuTableViewController alloc] initWithNibName:nil bundle:nil];
  68. _revealController.sidebarViewController = _menuViewController;
  69. _revealController.contentViewController = navCon;
  70. self.window.rootViewController = self.revealController;
  71. // necessary to avoid navbar blinking in VLCOpenNetworkStreamViewController & VLCDownloadViewController
  72. _revealController.contentViewController.view.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  73. [self.window makeKeyAndVisible];
  74. VLCMediaFileDiscoverer *discoverer = [VLCMediaFileDiscoverer sharedInstance];
  75. [discoverer addObserver:self];
  76. [discoverer startDiscovering:[self directoryPath]];
  77. [self validatePasscode];
  78. return YES;
  79. }
  80. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
  81. {
  82. if ([[DBSession sharedSession] handleOpenURL:url]) {
  83. [self.dropboxTableViewController updateViewAfterSessionChange];
  84. return YES;
  85. }
  86. if (_playlistViewController && url != nil) {
  87. APLog(@"%@ requested %@ to be opened", sourceApplication, url);
  88. if (url.isFileURL) {
  89. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  90. NSString *directoryPath = searchPaths[0];
  91. NSURL *destinationURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", directoryPath, url.lastPathComponent]];
  92. NSError *theError;
  93. [[NSFileManager defaultManager] moveItemAtURL:url toURL:destinationURL error:&theError];
  94. if (theError.code != noErr)
  95. APLog(@"saving the file failed (%i): %@", theError.code, theError.localizedDescription);
  96. [self updateMediaList];
  97. } else {
  98. NSString *receivedUrl = [url absoluteString];
  99. if ([receivedUrl length] > 6) {
  100. NSString *verifyVlcUrl = [receivedUrl substringToIndex:6];
  101. if ([verifyVlcUrl isEqualToString:@"vlc://"]) {
  102. NSString *parsedString = [receivedUrl substringFromIndex:6];
  103. NSUInteger location = [parsedString rangeOfString:@"//"].location;
  104. /* Safari & al mangle vlc://http:// so fix this */
  105. if (location != NSNotFound && [parsedString characterAtIndex:location - 1] != 0x3a) { // :
  106. parsedString = [NSString stringWithFormat:@"%@://%@", [parsedString substringToIndex:location], [parsedString substringFromIndex:location+2]];
  107. } else
  108. parsedString = [@"http://" stringByAppendingString:[receivedUrl substringFromIndex:6]];
  109. url = [NSURL URLWithString:parsedString];
  110. }
  111. }
  112. [self.menuViewController selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
  113. [self openMovieFromURL:url];
  114. }
  115. return YES;
  116. }
  117. return NO;
  118. }
  119. - (void)applicationWillEnterForeground:(UIApplication *)application
  120. {
  121. [[MLMediaLibrary sharedMediaLibrary] applicationWillStart];
  122. [self validatePasscode];
  123. }
  124. - (void)applicationWillResignActive:(UIApplication *)application
  125. {
  126. self.passcodeValidated = NO;
  127. [self validatePasscode];
  128. [[MLMediaLibrary sharedMediaLibrary] applicationWillExit];
  129. }
  130. - (void)applicationDidBecomeActive:(UIApplication *)application
  131. {
  132. [self validatePasscode];
  133. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  134. [self updateMediaList];
  135. }
  136. - (void)applicationWillTerminate:(UIApplication *)application
  137. {
  138. self.passcodeValidated = NO;
  139. [[NSUserDefaults standardUserDefaults] synchronize];
  140. }
  141. #pragma mark - properties
  142. - (VLCDropboxTableViewController *)dropboxTableViewController
  143. {
  144. if (_dropboxTableViewController == nil)
  145. _dropboxTableViewController = [[VLCDropboxTableViewController alloc] initWithNibName:@"VLCCloudStorageTableViewController" bundle:nil];
  146. return _dropboxTableViewController;
  147. }
  148. - (VLCGoogleDriveTableViewController *)googleDriveTableViewController
  149. {
  150. if (_googleDriveTableViewController == nil)
  151. _googleDriveTableViewController = [[VLCGoogleDriveTableViewController alloc] initWithNibName:@"VLCCloudStorageTableViewController" bundle:nil];
  152. return _googleDriveTableViewController;
  153. }
  154. - (VLCDownloadViewController *)downloadViewController
  155. {
  156. if (_downloadViewController == nil)
  157. _downloadViewController = [[VLCDownloadViewController alloc] init];
  158. return _downloadViewController;
  159. }
  160. #pragma mark - media discovering
  161. - (void)mediaFileAdded:(NSString *)fileName loading:(BOOL)isLoading {
  162. if (!isLoading) {
  163. MLMediaLibrary *sharedLibrary = [MLMediaLibrary sharedMediaLibrary];
  164. [sharedLibrary addFilePaths:@[fileName]];
  165. /* exclude media files from backup (QA1719) */
  166. NSURL *excludeURL = [NSURL fileURLWithPath:fileName];
  167. [excludeURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  168. // TODO Should we update media db after adding new files?
  169. [sharedLibrary updateMediaDatabase];
  170. [_playlistViewController updateViewContents];
  171. }
  172. }
  173. - (void)mediaFileDeleted:(NSString *)name {
  174. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  175. [_playlistViewController updateViewContents];
  176. }
  177. #pragma mark - media list methods
  178. - (NSString *)directoryPath
  179. {
  180. #define LOCAL_PLAYBACK_HACK 0
  181. #if LOCAL_PLAYBACK_HACK && TARGET_IPHONE_SIMULATOR
  182. NSString *directoryPath = @"/Users/fkuehne/Desktop/VideoLAN docs/Clips/sel/";
  183. #else
  184. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  185. NSString *directoryPath = searchPaths[0];
  186. #endif
  187. return directoryPath;
  188. }
  189. - (void)updateMediaList
  190. {
  191. NSString *directoryPath = [self directoryPath];
  192. NSArray *foundFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil];
  193. NSMutableArray *filePaths = [NSMutableArray arrayWithCapacity:[foundFiles count]];
  194. NSURL *fileURL;
  195. for (NSString *fileName in foundFiles) {
  196. if ([fileName isSupportedMediaFormat] || [fileName isSupportedAudioMediaFormat]) {
  197. [filePaths addObject:[directoryPath stringByAppendingPathComponent:fileName]];
  198. /* exclude media files from backup (QA1719) */
  199. fileURL = [NSURL fileURLWithPath:[directoryPath stringByAppendingPathComponent:fileName]];
  200. [fileURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  201. }
  202. }
  203. [[MLMediaLibrary sharedMediaLibrary] addFilePaths:filePaths];
  204. [_playlistViewController updateViewContents];
  205. }
  206. #pragma mark - pass code validation
  207. - (void)validatePasscode
  208. {
  209. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  210. NSString *passcode = [defaults objectForKey:kVLCSettingPasscodeKey];
  211. if ([passcode isEqualToString:@""] || ![[defaults objectForKey:kVLCSettingPasscodeOnKey] boolValue]) {
  212. self.passcodeValidated = YES;
  213. return;
  214. }
  215. if (!self.passcodeValidated) {
  216. _passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionEnter];
  217. _passcodeLockController.delegate = self;
  218. _passcodeLockController.passcode = passcode;
  219. self.window.rootViewController = _passcodeLockController;
  220. }
  221. }
  222. - (void)PAPasscodeViewControllerDidEnterPasscode:(PAPasscodeViewController *)controller
  223. {
  224. // TODO add transition animation, i.e. fade
  225. self.window.rootViewController = self.revealController;
  226. }
  227. - (void)PAPasscodeViewController:(PAPasscodeViewController *)controller didFailToEnterPasscode:(NSInteger)attempts
  228. {
  229. // TODO handle error attempts
  230. }
  231. #pragma mark - idle timer preventer
  232. - (void)disableIdleTimer
  233. {
  234. _idleCounter++;
  235. if ([UIApplication sharedApplication].idleTimerDisabled == NO)
  236. [UIApplication sharedApplication].idleTimerDisabled = YES;
  237. }
  238. - (void)activateIdleTimer
  239. {
  240. _idleCounter--;
  241. if (_idleCounter < 1)
  242. [UIApplication sharedApplication].idleTimerDisabled = NO;
  243. }
  244. #pragma mark - playback view handling
  245. - (void)openMediaFromManagedObject:(NSManagedObject *)mediaObject
  246. {
  247. if (!_movieViewController)
  248. _movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  249. if ([mediaObject isKindOfClass:[MLFile class]])
  250. _movieViewController.mediaItem = (MLFile *)mediaObject;
  251. else if ([mediaObject isKindOfClass:[MLAlbumTrack class]])
  252. _movieViewController.mediaItem = [(MLAlbumTrack*)mediaObject files].anyObject;
  253. else if ([mediaObject isKindOfClass:[MLShowEpisode class]])
  254. _movieViewController.mediaItem = [(MLShowEpisode*)mediaObject files].anyObject;
  255. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_movieViewController];
  256. navCon.modalPresentationStyle = UIModalPresentationFullScreen;
  257. [self.window.rootViewController presentViewController:navCon animated:YES completion:nil];
  258. }
  259. - (void)openMovieFromURL:(NSURL *)url
  260. {
  261. if (!_movieViewController)
  262. _movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  263. _movieViewController.url = url;
  264. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_movieViewController];
  265. navCon.modalPresentationStyle = UIModalPresentationFullScreen;
  266. [self.window.rootViewController presentViewController:navCon animated:YES completion:nil];
  267. }
  268. @end