VLCAppDelegate.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*****************************************************************************
  2. * VLCAppDelegate.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013-2014 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. #import "BWQuincyManager.h"
  27. @interface VLCAppDelegate () <PAPasscodeViewControllerDelegate, VLCMediaFileDiscovererDelegate, BWQuincyManagerDelegate> {
  28. PAPasscodeViewController *_passcodeLockController;
  29. VLCDropboxTableViewController *_dropboxTableViewController;
  30. VLCGoogleDriveTableViewController *_googleDriveTableViewController;
  31. VLCDownloadViewController *_downloadViewController;
  32. int _idleCounter;
  33. VLCMovieViewController *_movieViewController;
  34. BOOL _passcodeValidated;
  35. }
  36. @end
  37. @implementation VLCAppDelegate
  38. + (void)initialize
  39. {
  40. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  41. NSNumber *skipLoopFilterDefaultValue;
  42. int deviceSpeedCategory = [[UIDevice currentDevice] speedCategory];
  43. if (deviceSpeedCategory < 3)
  44. skipLoopFilterDefaultValue = kVLCSettingSkipLoopFilterNonKey;
  45. else
  46. skipLoopFilterDefaultValue = kVLCSettingSkipLoopFilterNonRef;
  47. NSDictionary *appDefaults = @{kVLCSettingPasscodeKey : @"", kVLCSettingPasscodeOnKey : @(NO), kVLCSettingContinueAudioInBackgroundKey : @(YES), kVLCSettingStretchAudio : @(NO), kVLCSettingTextEncoding : kVLCSettingTextEncodingDefaultValue, kVLCSettingSkipLoopFilter : skipLoopFilterDefaultValue, kVLCSettingSubtitlesFont : kVLCSettingSubtitlesFontDefaultValue, kVLCSettingSubtitlesFontColor : kVLCSettingSubtitlesFontColorDefaultValue, kVLCSettingSubtitlesFontSize : kVLCSettingSubtitlesFontSizeDefaultValue, kVLCSettingSubtitlesBoldFont: kVLCSettingSubtitlesBoldFontDefaulValue, kVLCSettingDeinterlace : kVLCSettingDeinterlaceDefaultValue, kVLCSettingNetworkCaching : kVLCSettingNetworkCachingDefaultValue, kVLCSettingPlaybackGestures : [NSNumber numberWithBool:YES]};
  48. [defaults registerDefaults:appDefaults];
  49. }
  50. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  51. {
  52. BWQuincyManager *quincyManager = [BWQuincyManager sharedQuincyManager];
  53. [quincyManager setSubmissionURL:@"http://crash.videolan.org/crash_v200.php"];
  54. [quincyManager setDelegate:self];
  55. [quincyManager setShowAlwaysButton:YES];
  56. [quincyManager startManager];
  57. /* clean caches on launch (since those are used for wifi upload only) */
  58. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  59. NSString* uploadDirPath = [searchPaths[0] stringByAppendingPathComponent:@"Upload"];
  60. NSFileManager *fileManager = [NSFileManager defaultManager];
  61. if ([fileManager fileExistsAtPath:uploadDirPath])
  62. [fileManager removeItemAtPath:uploadDirPath error:nil];
  63. // Init the HTTP Server
  64. self.uploadController = [[VLCHTTPUploaderController alloc] init];
  65. // enable crash preventer
  66. [[MLMediaLibrary sharedMediaLibrary] applicationWillStart];
  67. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  68. _playlistViewController = [[VLCPlaylistViewController alloc] init];
  69. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_playlistViewController];
  70. [navCon loadTheme];
  71. _revealController = [[GHRevealViewController alloc] initWithNibName:nil bundle:nil];
  72. _revealController.wantsFullScreenLayout = YES;
  73. _menuViewController = [[VLCMenuTableViewController alloc] initWithNibName:nil bundle:nil];
  74. _revealController.sidebarViewController = _menuViewController;
  75. _revealController.contentViewController = navCon;
  76. self.window.rootViewController = self.revealController;
  77. // necessary to avoid navbar blinking in VLCOpenNetworkStreamViewController & VLCDownloadViewController
  78. _revealController.contentViewController.view.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  79. [self.window makeKeyAndVisible];
  80. VLCMediaFileDiscoverer *discoverer = [VLCMediaFileDiscoverer sharedInstance];
  81. [discoverer addObserver:self];
  82. [discoverer startDiscovering:[self directoryPath]];
  83. [self validatePasscode];
  84. return YES;
  85. }
  86. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
  87. {
  88. if ([[DBSession sharedSession] handleOpenURL:url]) {
  89. [self.dropboxTableViewController updateViewAfterSessionChange];
  90. return YES;
  91. }
  92. if (_playlistViewController && url != nil) {
  93. APLog(@"%@ requested %@ to be opened", sourceApplication, url);
  94. if (url.isFileURL) {
  95. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  96. NSString *directoryPath = searchPaths[0];
  97. NSURL *destinationURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", directoryPath, url.lastPathComponent]];
  98. NSError *theError;
  99. [[NSFileManager defaultManager] moveItemAtURL:url toURL:destinationURL error:&theError];
  100. if (theError.code != noErr)
  101. APLog(@"saving the file failed (%li): %@", theError.code, theError.localizedDescription);
  102. [self updateMediaList];
  103. } else {
  104. NSString *receivedUrl = [url absoluteString];
  105. if ([receivedUrl length] > 6) {
  106. NSString *verifyVlcUrl = [receivedUrl substringToIndex:6];
  107. if ([verifyVlcUrl isEqualToString:@"vlc://"]) {
  108. NSString *parsedString = [receivedUrl substringFromIndex:6];
  109. NSUInteger location = [parsedString rangeOfString:@"//"].location;
  110. /* Safari & al mangle vlc://http:// so fix this */
  111. if (location != NSNotFound && [parsedString characterAtIndex:location - 1] != 0x3a) { // :
  112. parsedString = [NSString stringWithFormat:@"%@://%@", [parsedString substringToIndex:location], [parsedString substringFromIndex:location+2]];
  113. } else
  114. parsedString = [@"http://" stringByAppendingString:[receivedUrl substringFromIndex:6]];
  115. url = [NSURL URLWithString:parsedString];
  116. }
  117. }
  118. [self.menuViewController selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
  119. [self openMovieFromURL:url];
  120. }
  121. return YES;
  122. }
  123. return NO;
  124. }
  125. - (void)applicationWillEnterForeground:(UIApplication *)application
  126. {
  127. [[MLMediaLibrary sharedMediaLibrary] applicationWillStart];
  128. }
  129. - (void)applicationWillResignActive:(UIApplication *)application
  130. {
  131. _passcodeValidated = NO;
  132. [self validatePasscode];
  133. [[MLMediaLibrary sharedMediaLibrary] applicationWillExit];
  134. }
  135. - (void)applicationDidBecomeActive:(UIApplication *)application
  136. {
  137. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  138. [self updateMediaList];
  139. }
  140. - (void)applicationWillTerminate:(UIApplication *)application
  141. {
  142. _passcodeValidated = NO;
  143. [[NSUserDefaults standardUserDefaults] synchronize];
  144. }
  145. #pragma mark - properties
  146. - (VLCDropboxTableViewController *)dropboxTableViewController
  147. {
  148. if (_dropboxTableViewController == nil)
  149. _dropboxTableViewController = [[VLCDropboxTableViewController alloc] initWithNibName:@"VLCCloudStorageTableViewController" bundle:nil];
  150. return _dropboxTableViewController;
  151. }
  152. - (VLCGoogleDriveTableViewController *)googleDriveTableViewController
  153. {
  154. if (_googleDriveTableViewController == nil)
  155. _googleDriveTableViewController = [[VLCGoogleDriveTableViewController alloc] initWithNibName:@"VLCCloudStorageTableViewController" bundle:nil];
  156. return _googleDriveTableViewController;
  157. }
  158. - (VLCDownloadViewController *)downloadViewController
  159. {
  160. if (_downloadViewController == nil) {
  161. if (SYSTEM_RUNS_IOS7_OR_LATER)
  162. _downloadViewController = [[VLCDownloadViewController alloc] initWithNibName:@"VLCFutureDownloadViewController" bundle:nil];
  163. else
  164. _downloadViewController = [[VLCDownloadViewController alloc] initWithNibName:@"VLCDownloadViewController" bundle:nil];
  165. }
  166. return _downloadViewController;
  167. }
  168. #pragma mark - media discovering
  169. - (void)mediaFileAdded:(NSString *)fileName loading:(BOOL)isLoading
  170. {
  171. if (!isLoading) {
  172. MLMediaLibrary *sharedLibrary = [MLMediaLibrary sharedMediaLibrary];
  173. [sharedLibrary addFilePaths:@[fileName]];
  174. /* exclude media files from backup (QA1719) */
  175. NSURL *excludeURL = [NSURL fileURLWithPath:fileName];
  176. [excludeURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  177. // TODO Should we update media db after adding new files?
  178. [sharedLibrary updateMediaDatabase];
  179. [_playlistViewController updateViewContents];
  180. }
  181. }
  182. - (void)mediaFileDeleted:(NSString *)name
  183. {
  184. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  185. [_playlistViewController updateViewContents];
  186. }
  187. #pragma mark - media list methods
  188. - (NSString *)directoryPath
  189. {
  190. #define LOCAL_PLAYBACK_HACK 0
  191. #if LOCAL_PLAYBACK_HACK && TARGET_IPHONE_SIMULATOR
  192. NSString *directoryPath = @"/Users/fkuehne/Desktop/VideoLAN docs/Clips/sel/";
  193. #else
  194. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  195. NSString *directoryPath = searchPaths[0];
  196. #endif
  197. return directoryPath;
  198. }
  199. - (void)updateMediaList
  200. {
  201. NSString *directoryPath = [self directoryPath];
  202. NSArray *foundFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil];
  203. NSMutableArray *filePaths = [NSMutableArray arrayWithCapacity:[foundFiles count]];
  204. NSURL *fileURL;
  205. for (NSString *fileName in foundFiles) {
  206. if ([fileName isSupportedMediaFormat] || [fileName isSupportedAudioMediaFormat]) {
  207. [filePaths addObject:[directoryPath stringByAppendingPathComponent:fileName]];
  208. /* exclude media files from backup (QA1719) */
  209. fileURL = [NSURL fileURLWithPath:[directoryPath stringByAppendingPathComponent:fileName]];
  210. [fileURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  211. }
  212. }
  213. [[MLMediaLibrary sharedMediaLibrary] addFilePaths:filePaths];
  214. [_playlistViewController updateViewContents];
  215. }
  216. #pragma mark - pass code validation
  217. - (void)validatePasscode
  218. {
  219. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  220. NSString *passcode = [defaults objectForKey:kVLCSettingPasscodeKey];
  221. if ([passcode isEqualToString:@""] || ![[defaults objectForKey:kVLCSettingPasscodeOnKey] boolValue]) {
  222. _passcodeValidated = YES;
  223. return;
  224. }
  225. if (!_passcodeValidated) {
  226. _passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionEnter];
  227. _passcodeLockController.delegate = self;
  228. _passcodeLockController.passcode = passcode;
  229. if (self.window.rootViewController.presentedViewController)
  230. [self.window.rootViewController dismissViewControllerAnimated:NO completion:nil];
  231. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_passcodeLockController];
  232. navCon.modalPresentationStyle = UIModalPresentationFullScreen;
  233. [self.window.rootViewController presentViewController:navCon animated:NO completion:nil];
  234. }
  235. }
  236. - (void)PAPasscodeViewControllerDidEnterPasscode:(PAPasscodeViewController *)controller
  237. {
  238. [self.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
  239. }
  240. - (void)PAPasscodeViewController:(PAPasscodeViewController *)controller didFailToEnterPasscode:(NSInteger)attempts
  241. {
  242. // FIXME: handle countless failed passcode attempts
  243. }
  244. #pragma mark - idle timer preventer
  245. - (void)disableIdleTimer
  246. {
  247. _idleCounter++;
  248. if ([UIApplication sharedApplication].idleTimerDisabled == NO)
  249. [UIApplication sharedApplication].idleTimerDisabled = YES;
  250. }
  251. - (void)activateIdleTimer
  252. {
  253. _idleCounter--;
  254. if (_idleCounter < 1)
  255. [UIApplication sharedApplication].idleTimerDisabled = NO;
  256. }
  257. #pragma mark - playback view handling
  258. - (void)openMediaFromManagedObject:(NSManagedObject *)mediaObject
  259. {
  260. if (!_movieViewController)
  261. _movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  262. if ([mediaObject isKindOfClass:[MLFile class]])
  263. _movieViewController.mediaItem = (MLFile *)mediaObject;
  264. else if ([mediaObject isKindOfClass:[MLAlbumTrack class]])
  265. _movieViewController.mediaItem = [(MLAlbumTrack*)mediaObject files].anyObject;
  266. else if ([mediaObject isKindOfClass:[MLShowEpisode class]])
  267. _movieViewController.mediaItem = [(MLShowEpisode*)mediaObject files].anyObject;
  268. [(MLFile *)_movieViewController.mediaItem setUnread:@(NO)];
  269. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_movieViewController];
  270. navCon.modalPresentationStyle = UIModalPresentationFullScreen;
  271. [self.window.rootViewController presentViewController:navCon animated:YES completion:nil];
  272. }
  273. - (void)openMovieFromURL:(NSURL *)url
  274. {
  275. if (!_movieViewController)
  276. _movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  277. _movieViewController.url = url;
  278. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_movieViewController];
  279. navCon.modalPresentationStyle = UIModalPresentationFullScreen;
  280. [self.window.rootViewController presentViewController:navCon animated:YES completion:nil];
  281. }
  282. - (void)openMediaList:(VLCMediaList*)list atIndex:(int)index
  283. {
  284. if (!_movieViewController)
  285. _movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  286. _movieViewController.mediaList = list;
  287. _movieViewController.itemInMediaListToBePlayedFirst = index;
  288. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_movieViewController];
  289. navCon.modalPresentationStyle = UIModalPresentationFullScreen;
  290. [self.window.rootViewController presentViewController:navCon animated:YES completion:nil];
  291. }
  292. @end