VLCAppDelegate.m 17 KB

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