VLCAppDelegate.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. if (!isLoading) {
  173. MLMediaLibrary *sharedLibrary = [MLMediaLibrary sharedMediaLibrary];
  174. [sharedLibrary addFilePaths:@[fileName]];
  175. /* exclude media files from backup (QA1719) */
  176. NSURL *excludeURL = [NSURL fileURLWithPath:fileName];
  177. [excludeURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  178. // TODO Should we update media db after adding new files?
  179. [sharedLibrary updateMediaDatabase];
  180. [_playlistViewController updateViewContents];
  181. }
  182. }
  183. - (void)mediaFileDeleted:(NSString *)name {
  184. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  185. [_playlistViewController updateViewContents];
  186. }
  187. #pragma mark - media list methods
  188. - (NSString *)directoryPath
  189. {
  190. #define LOCAL_PLAYBACK_HACK 0
  191. #if LOCAL_PLAYBACK_HACK && TARGET_IPHONE_SIMULATOR
  192. NSString *directoryPath = @"/Users/fkuehne/Desktop/VideoLAN docs/Clips/sel/";
  193. #else
  194. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  195. NSString *directoryPath = searchPaths[0];
  196. #endif
  197. return directoryPath;
  198. }
  199. - (void)updateMediaList
  200. {
  201. NSString *directoryPath = [self directoryPath];
  202. NSArray *foundFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil];
  203. NSMutableArray *filePaths = [NSMutableArray arrayWithCapacity:[foundFiles count]];
  204. NSURL *fileURL;
  205. for (NSString *fileName in foundFiles) {
  206. if ([fileName isSupportedMediaFormat] || [fileName isSupportedAudioMediaFormat]) {
  207. [filePaths addObject:[directoryPath stringByAppendingPathComponent:fileName]];
  208. /* exclude media files from backup (QA1719) */
  209. fileURL = [NSURL fileURLWithPath:[directoryPath stringByAppendingPathComponent:fileName]];
  210. [fileURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  211. }
  212. }
  213. [[MLMediaLibrary sharedMediaLibrary] addFilePaths:filePaths];
  214. [_playlistViewController updateViewContents];
  215. }
  216. #pragma mark - pass code validation
  217. - (void)validatePasscode
  218. {
  219. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  220. NSString *passcode = [defaults objectForKey:kVLCSettingPasscodeKey];
  221. if ([passcode isEqualToString:@""] || ![[defaults objectForKey:kVLCSettingPasscodeOnKey] boolValue]) {
  222. self.passcodeValidated = YES;
  223. return;
  224. }
  225. if (!self.passcodeValidated) {
  226. _passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionEnter];
  227. _passcodeLockController.delegate = self;
  228. _passcodeLockController.passcode = passcode;
  229. self.window.rootViewController = _passcodeLockController;
  230. }
  231. }
  232. - (void)PAPasscodeViewControllerDidEnterPasscode:(PAPasscodeViewController *)controller
  233. {
  234. // TODO add transition animation, i.e. fade
  235. self.window.rootViewController = self.revealController;
  236. }
  237. - (void)PAPasscodeViewController:(PAPasscodeViewController *)controller didFailToEnterPasscode:(NSInteger)attempts
  238. {
  239. // TODO handle error attempts
  240. }
  241. #pragma mark - idle timer preventer
  242. - (void)disableIdleTimer
  243. {
  244. _idleCounter++;
  245. if ([UIApplication sharedApplication].idleTimerDisabled == NO)
  246. [UIApplication sharedApplication].idleTimerDisabled = YES;
  247. }
  248. - (void)activateIdleTimer
  249. {
  250. _idleCounter--;
  251. if (_idleCounter < 1)
  252. [UIApplication sharedApplication].idleTimerDisabled = NO;
  253. }
  254. #pragma mark - playback view handling
  255. - (void)openMediaFromManagedObject:(NSManagedObject *)mediaObject
  256. {
  257. if (!_movieViewController)
  258. _movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  259. if ([mediaObject isKindOfClass:[MLFile class]])
  260. _movieViewController.mediaItem = (MLFile *)mediaObject;
  261. else if ([mediaObject isKindOfClass:[MLAlbumTrack class]])
  262. _movieViewController.mediaItem = [(MLAlbumTrack*)mediaObject files].anyObject;
  263. else if ([mediaObject isKindOfClass:[MLShowEpisode class]])
  264. _movieViewController.mediaItem = [(MLShowEpisode*)mediaObject files].anyObject;
  265. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_movieViewController];
  266. navCon.modalPresentationStyle = UIModalPresentationFullScreen;
  267. [self.window.rootViewController presentViewController:navCon animated:YES completion:nil];
  268. }
  269. - (void)openMovieFromURL:(NSURL *)url
  270. {
  271. if (!_movieViewController)
  272. _movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  273. _movieViewController.url = url;
  274. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_movieViewController];
  275. navCon.modalPresentationStyle = UIModalPresentationFullScreen;
  276. [self.window.rootViewController presentViewController:navCon animated:YES completion:nil];
  277. }
  278. - (void)openMediaList:(VLCMediaList*)list atIndex:(int)index
  279. {
  280. if (!_movieViewController)
  281. _movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  282. _movieViewController.mediaList = list;
  283. _movieViewController.itemInMediaListToBePlayedFirst = index;
  284. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_movieViewController];
  285. navCon.modalPresentationStyle = UIModalPresentationFullScreen;
  286. [self.window.rootViewController presentViewController:navCon animated:YES completion:nil];
  287. }
  288. @end