VLCAppDelegate.m 15 KB

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