VLCAppDelegate.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 colorWithWhite:.122 alpha:1.];
  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. #define LOCAL_PLAYBACK_HACK 0
  210. #if LOCAL_PLAYBACK_HACK && TARGET_IPHONE_SIMULATOR
  211. NSString *directoryPath = @"/Users/fkuehne/Desktop/VideoLAN docs/Clips/sel/";
  212. #else
  213. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  214. NSString *directoryPath = searchPaths[0];
  215. #endif
  216. return directoryPath;
  217. }
  218. - (void)updateMediaList
  219. {
  220. NSString *directoryPath = [self directoryPath];
  221. NSArray *foundFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil];
  222. NSMutableArray *filePaths = [NSMutableArray arrayWithCapacity:[foundFiles count]];
  223. NSURL *fileURL;
  224. for (NSString *fileName in foundFiles) {
  225. if ([fileName isSupportedMediaFormat] || [fileName isSupportedAudioMediaFormat]) {
  226. [filePaths addObject:[directoryPath stringByAppendingPathComponent:fileName]];
  227. /* exclude media files from backup (QA1719) */
  228. fileURL = [NSURL fileURLWithPath:[directoryPath stringByAppendingPathComponent:fileName]];
  229. [fileURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  230. }
  231. }
  232. [[MLMediaLibrary sharedMediaLibrary] addFilePaths:filePaths];
  233. [_playlistViewController updateViewContents];
  234. }
  235. #pragma mark - pass code validation
  236. - (void)validatePasscode
  237. {
  238. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  239. NSString *passcode = [defaults objectForKey:kVLCSettingPasscodeKey];
  240. if ([passcode isEqualToString:@""] || ![[defaults objectForKey:kVLCSettingPasscodeOnKey] boolValue]) {
  241. _passcodeValidated = YES;
  242. return;
  243. }
  244. if (!_passcodeValidated) {
  245. _passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionEnter];
  246. _passcodeLockController.delegate = self;
  247. _passcodeLockController.passcode = passcode;
  248. if (self.window.rootViewController.presentedViewController)
  249. [self.window.rootViewController dismissViewControllerAnimated:NO completion:nil];
  250. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_passcodeLockController];
  251. navCon.modalPresentationStyle = UIModalPresentationFullScreen;
  252. [self.window.rootViewController presentViewController:navCon animated:NO completion:nil];
  253. }
  254. }
  255. - (void)PAPasscodeViewControllerDidEnterPasscode:(PAPasscodeViewController *)controller
  256. {
  257. [self.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
  258. }
  259. - (void)PAPasscodeViewController:(PAPasscodeViewController *)controller didFailToEnterPasscode:(NSInteger)attempts
  260. {
  261. // FIXME: handle countless failed passcode attempts
  262. }
  263. #pragma mark - idle timer preventer
  264. - (void)disableIdleTimer
  265. {
  266. _idleCounter++;
  267. if ([UIApplication sharedApplication].idleTimerDisabled == NO)
  268. [UIApplication sharedApplication].idleTimerDisabled = YES;
  269. }
  270. - (void)activateIdleTimer
  271. {
  272. _idleCounter--;
  273. if (_idleCounter < 1)
  274. [UIApplication sharedApplication].idleTimerDisabled = NO;
  275. }
  276. - (void)networkActivityStarted
  277. {
  278. _networkActivityCounter++;
  279. if ([UIApplication sharedApplication].networkActivityIndicatorVisible == NO)
  280. [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
  281. }
  282. - (BOOL)haveNetworkActivity
  283. {
  284. return _networkActivityCounter >= 1;
  285. }
  286. - (void)networkActivityStopped
  287. {
  288. _networkActivityCounter--;
  289. if (_networkActivityCounter < 1)
  290. [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
  291. }
  292. #pragma mark - playback view handling
  293. - (void)openMediaFromManagedObject:(NSManagedObject *)mediaObject
  294. {
  295. if (!_movieViewController)
  296. _movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  297. if ([mediaObject isKindOfClass:[MLFile class]])
  298. _movieViewController.fileFromMediaLibrary = (MLFile *)mediaObject;
  299. else if ([mediaObject isKindOfClass:[MLAlbumTrack class]])
  300. _movieViewController.fileFromMediaLibrary = [(MLAlbumTrack*)mediaObject files].anyObject;
  301. else if ([mediaObject isKindOfClass:[MLShowEpisode class]])
  302. _movieViewController.fileFromMediaLibrary = [(MLShowEpisode*)mediaObject files].anyObject;
  303. [(MLFile *)_movieViewController.fileFromMediaLibrary setUnread:@(NO)];
  304. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_movieViewController];
  305. navCon.modalPresentationStyle = UIModalPresentationFullScreen;
  306. [self.window.rootViewController presentViewController:navCon animated:YES completion:nil];
  307. }
  308. - (void)openMovieFromURL:(NSURL *)url
  309. {
  310. if (!_movieViewController)
  311. _movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  312. _movieViewController.url = url;
  313. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_movieViewController];
  314. navCon.modalPresentationStyle = UIModalPresentationFullScreen;
  315. [self.window.rootViewController presentViewController:navCon animated:YES completion:nil];
  316. }
  317. - (void)openMediaList:(VLCMediaList *)list atIndex:(int)index
  318. {
  319. if (!_movieViewController)
  320. _movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  321. _movieViewController.mediaList = list;
  322. _movieViewController.itemInMediaListToBePlayedFirst = index;
  323. _movieViewController.pathToExternalSubtitlesFile = nil;
  324. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_movieViewController];
  325. navCon.modalPresentationStyle = UIModalPresentationFullScreen;
  326. [self.window.rootViewController presentViewController:navCon animated:YES completion:nil];
  327. }
  328. - (void)openMovieWithExternalSubtitleFromURL:(NSURL *)url externalSubURL:(NSString *)SubtitlePath
  329. {
  330. if (!_movieViewController)
  331. _movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  332. _movieViewController.url = url;
  333. _movieViewController.pathToExternalSubtitlesFile = SubtitlePath;
  334. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_movieViewController];
  335. navCon.modalPresentationStyle = UIModalPresentationFullScreen;
  336. [self.window.rootViewController presentViewController:navCon animated:YES completion:nil];
  337. }
  338. @end