VLCAppDelegate.m 17 KB

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