VLCAppDelegate.m 14 KB

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