VLCAppDelegate.m 14 KB

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