VLCAppDelegate.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. @interface VLCAppDelegate () <PAPasscodeViewControllerDelegate, VLCMediaFileDiscovererDelegate> {
  27. PAPasscodeViewController *_passcodeLockController;
  28. VLCDropboxTableViewController *_dropboxTableViewController;
  29. VLCGoogleDriveTableViewController *_googleDriveTableViewController;
  30. VLCDownloadViewController *_downloadViewController;
  31. int _idleCounter;
  32. VLCMovieViewController *_movieViewController;
  33. }
  34. @property (nonatomic) BOOL passcodeValidated;
  35. @end
  36. @implementation VLCAppDelegate
  37. + (void)initialize
  38. {
  39. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  40. NSNumber *skipLoopFilterDefaultValue;
  41. int deviceSpeedCategory = [[UIDevice currentDevice] speedCategory];
  42. if (deviceSpeedCategory < 3)
  43. skipLoopFilterDefaultValue = kVLCSettingSkipLoopFilterNonKey;
  44. else
  45. skipLoopFilterDefaultValue = kVLCSettingSkipLoopFilterNonRef;
  46. NSDictionary *appDefaults = @{kVLCSettingPasscodeKey : @"", kVLCSettingPasscodeOnKey : @(NO), kVLCSettingContinueAudioInBackgroundKey : @(YES), kVLCSettingStretchAudio : @(NO), kVLCSettingTextEncoding : kVLCSettingTextEncodingDefaultValue, kVLCSettingSkipLoopFilter : skipLoopFilterDefaultValue, kVLCSettingSubtitlesFont : kVLCSettingSubtitlesFontDefaultValue, kVLCSettingSubtitlesFontColor : kVLCSettingSubtitlesFontColorDefaultValue, kVLCSettingSubtitlesFontSize : kVLCSettingSubtitlesFontSizeDefaultValue, kVLCSettingDeinterlace : kVLCSettingDeinterlaceDefaultValue};
  47. [defaults registerDefaults:appDefaults];
  48. }
  49. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  50. {
  51. [[BWQuincyManager sharedQuincyManager] setSubmissionURL:@"http://yourserver.com/crash_v200.php"];
  52. /* clean caches on launch (since those are used for wifi upload only) */
  53. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  54. NSString* uploadDirPath = [searchPaths[0] stringByAppendingPathComponent:@"Upload"];
  55. NSFileManager *fileManager = [NSFileManager defaultManager];
  56. if ([fileManager fileExistsAtPath:uploadDirPath])
  57. [fileManager removeItemAtPath:uploadDirPath error:nil];
  58. // Init the HTTP Server
  59. self.uploadController = [[VLCHTTPUploaderController alloc] init];
  60. // enable crash preventer
  61. [[MLMediaLibrary sharedMediaLibrary] applicationWillStart];
  62. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  63. _playlistViewController = [[VLCPlaylistViewController alloc] init];
  64. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_playlistViewController];
  65. [navCon loadTheme];
  66. _revealController = [[GHRevealViewController alloc] initWithNibName:nil bundle:nil];
  67. _revealController.wantsFullScreenLayout = YES;
  68. _menuViewController = [[VLCMenuTableViewController alloc] initWithNibName:nil bundle:nil];
  69. _revealController.sidebarViewController = _menuViewController;
  70. _revealController.contentViewController = navCon;
  71. self.window.rootViewController = self.revealController;
  72. // necessary to avoid navbar blinking in VLCOpenNetworkStreamViewController & VLCDownloadViewController
  73. _revealController.contentViewController.view.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  74. [self.window makeKeyAndVisible];
  75. VLCMediaFileDiscoverer *discoverer = [VLCMediaFileDiscoverer sharedInstance];
  76. [discoverer addObserver:self];
  77. [discoverer startDiscovering:[self directoryPath]];
  78. [self validatePasscode];
  79. return YES;
  80. }
  81. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
  82. {
  83. if ([[DBSession sharedSession] handleOpenURL:url]) {
  84. [self.dropboxTableViewController updateViewAfterSessionChange];
  85. return YES;
  86. }
  87. if (_playlistViewController && url != nil) {
  88. APLog(@"%@ requested %@ to be opened", sourceApplication, url);
  89. if (url.isFileURL) {
  90. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  91. NSString *directoryPath = searchPaths[0];
  92. NSURL *destinationURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", directoryPath, url.lastPathComponent]];
  93. NSError *theError;
  94. [[NSFileManager defaultManager] moveItemAtURL:url toURL:destinationURL error:&theError];
  95. if (theError.code != noErr)
  96. APLog(@"saving the file failed (%i): %@", theError.code, theError.localizedDescription);
  97. [self updateMediaList];
  98. } else {
  99. NSString *receivedUrl = [url absoluteString];
  100. if ([receivedUrl length] > 6) {
  101. NSString *verifyVlcUrl = [receivedUrl substringToIndex:6];
  102. if ([verifyVlcUrl isEqualToString:@"vlc://"]) {
  103. NSString *parsedString = [receivedUrl substringFromIndex:6];
  104. NSUInteger location = [parsedString rangeOfString:@"//"].location;
  105. /* Safari & al mangle vlc://http:// so fix this */
  106. if (location != NSNotFound && [parsedString characterAtIndex:location - 1] != 0x3a) { // :
  107. parsedString = [NSString stringWithFormat:@"%@://%@", [parsedString substringToIndex:location], [parsedString substringFromIndex:location+2]];
  108. } else
  109. parsedString = [@"http://" stringByAppendingString:[receivedUrl substringFromIndex:6]];
  110. url = [NSURL URLWithString:parsedString];
  111. }
  112. }
  113. [self.menuViewController selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
  114. [self openMovieFromURL:url];
  115. }
  116. return YES;
  117. }
  118. return NO;
  119. }
  120. - (void)applicationWillEnterForeground:(UIApplication *)application
  121. {
  122. [[MLMediaLibrary sharedMediaLibrary] applicationWillStart];
  123. [self validatePasscode];
  124. }
  125. - (void)applicationWillResignActive:(UIApplication *)application
  126. {
  127. self.passcodeValidated = NO;
  128. [self validatePasscode];
  129. [[MLMediaLibrary sharedMediaLibrary] applicationWillExit];
  130. }
  131. - (void)applicationDidBecomeActive:(UIApplication *)application
  132. {
  133. [self validatePasscode];
  134. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  135. [self updateMediaList];
  136. }
  137. - (void)applicationWillTerminate:(UIApplication *)application
  138. {
  139. self.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. 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. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  180. [_playlistViewController updateViewContents];
  181. }
  182. #pragma mark - media list methods
  183. - (NSString *)directoryPath
  184. {
  185. #define LOCAL_PLAYBACK_HACK 0
  186. #if LOCAL_PLAYBACK_HACK && TARGET_IPHONE_SIMULATOR
  187. NSString *directoryPath = @"/Users/fkuehne/Desktop/VideoLAN docs/Clips/sel/";
  188. #else
  189. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  190. NSString *directoryPath = searchPaths[0];
  191. #endif
  192. return directoryPath;
  193. }
  194. - (void)updateMediaList
  195. {
  196. NSString *directoryPath = [self directoryPath];
  197. NSArray *foundFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil];
  198. NSMutableArray *filePaths = [NSMutableArray arrayWithCapacity:[foundFiles count]];
  199. NSURL *fileURL;
  200. for (NSString *fileName in foundFiles) {
  201. if ([fileName isSupportedMediaFormat] || [fileName isSupportedAudioMediaFormat]) {
  202. [filePaths addObject:[directoryPath stringByAppendingPathComponent:fileName]];
  203. /* exclude media files from backup (QA1719) */
  204. fileURL = [NSURL fileURLWithPath:[directoryPath stringByAppendingPathComponent:fileName]];
  205. [fileURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  206. }
  207. }
  208. [[MLMediaLibrary sharedMediaLibrary] addFilePaths:filePaths];
  209. [_playlistViewController updateViewContents];
  210. }
  211. #pragma mark - pass code validation
  212. - (void)validatePasscode
  213. {
  214. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  215. NSString *passcode = [defaults objectForKey:kVLCSettingPasscodeKey];
  216. if ([passcode isEqualToString:@""] || ![[defaults objectForKey:kVLCSettingPasscodeOnKey] boolValue]) {
  217. self.passcodeValidated = YES;
  218. return;
  219. }
  220. if (!self.passcodeValidated) {
  221. _passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionEnter];
  222. _passcodeLockController.delegate = self;
  223. _passcodeLockController.passcode = passcode;
  224. self.window.rootViewController = _passcodeLockController;
  225. }
  226. }
  227. - (void)PAPasscodeViewControllerDidEnterPasscode:(PAPasscodeViewController *)controller
  228. {
  229. // TODO add transition animation, i.e. fade
  230. self.window.rootViewController = self.revealController;
  231. }
  232. - (void)PAPasscodeViewController:(PAPasscodeViewController *)controller didFailToEnterPasscode:(NSInteger)attempts
  233. {
  234. // TODO handle error attempts
  235. }
  236. #pragma mark - idle timer preventer
  237. - (void)disableIdleTimer
  238. {
  239. _idleCounter++;
  240. if ([UIApplication sharedApplication].idleTimerDisabled == NO)
  241. [UIApplication sharedApplication].idleTimerDisabled = YES;
  242. }
  243. - (void)activateIdleTimer
  244. {
  245. _idleCounter--;
  246. if (_idleCounter < 1)
  247. [UIApplication sharedApplication].idleTimerDisabled = NO;
  248. }
  249. #pragma mark - playback view handling
  250. - (void)openMediaFromManagedObject:(NSManagedObject *)mediaObject
  251. {
  252. if (!_movieViewController)
  253. _movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  254. if ([mediaObject isKindOfClass:[MLFile class]])
  255. _movieViewController.mediaItem = (MLFile *)mediaObject;
  256. else if ([mediaObject isKindOfClass:[MLAlbumTrack class]])
  257. _movieViewController.mediaItem = [(MLAlbumTrack*)mediaObject files].anyObject;
  258. else if ([mediaObject isKindOfClass:[MLShowEpisode class]])
  259. _movieViewController.mediaItem = [(MLShowEpisode*)mediaObject files].anyObject;
  260. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_movieViewController];
  261. navCon.modalPresentationStyle = UIModalPresentationFullScreen;
  262. [self.window.rootViewController presentViewController:navCon animated:YES completion:nil];
  263. }
  264. - (void)openMovieFromURL:(NSURL *)url
  265. {
  266. if (!_movieViewController)
  267. _movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  268. _movieViewController.url = url;
  269. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_movieViewController];
  270. navCon.modalPresentationStyle = UIModalPresentationFullScreen;
  271. [self.window.rootViewController presentViewController:navCon animated:YES completion:nil];
  272. }
  273. - (void)openMediaList:(VLCMediaList*)list atIndex:(NSUInteger)index
  274. {
  275. if (!_movieViewController)
  276. _movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  277. _movieViewController.mediaList = list;
  278. _movieViewController.itemInMediaListToBePlayedFirst = index;
  279. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_movieViewController];
  280. navCon.modalPresentationStyle = UIModalPresentationFullScreen;
  281. [self.window.rootViewController presentViewController:navCon animated:YES completion:nil];
  282. }
  283. @end