VLCAppDelegate.m 14 KB

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