VLCAppDelegate.m 15 KB

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