VLCAppDelegate.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. //
  2. // VLCAppDelegate.m
  3. // VLC for iOS
  4. //
  5. // Created by Felix Paul Kühne on 27.02.13.
  6. // Copyright (c) 2013 VideoLAN. All rights reserved.
  7. //
  8. // Refer to the COPYING file of the official project for license.
  9. //
  10. #import "VLCAppDelegate.h"
  11. #import "VLCMediaFileDiscoverer.h"
  12. #import "NSString+SupportedMedia.h"
  13. #import "UIDevice+SpeedCategory.h"
  14. #import "VLCPlaylistViewController.h"
  15. #import "VLCMovieViewController.h"
  16. #import "PAPasscodeViewController.h"
  17. #import "UINavigationController+Theme.h"
  18. #import "VLCHTTPUploaderController.h"
  19. #import "VLCMenuTableViewController.h"
  20. #define PASSCODE_CHECK_INTERVAL 300
  21. @interface VLCAppDelegate () <PAPasscodeViewControllerDelegate, VLCMediaFileDiscovererDelegate> {
  22. PAPasscodeViewController *_passcodeLockController;
  23. VLCDropboxTableViewController *_dropboxTableViewController;
  24. VLCGoogleDriveTableViewController *_googleDriveTableViewController;
  25. VLCDownloadViewController *_downloadViewController;
  26. int _idleCounter;
  27. VLCMovieViewController *_movieViewController;
  28. }
  29. @property (nonatomic) BOOL passcodeValidated;
  30. @end
  31. @implementation VLCAppDelegate
  32. + (void)initialize
  33. {
  34. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  35. NSNumber *skipLoopFilterDefaultValue;
  36. int deviceSpeedCategory = [[UIDevice currentDevice] speedCategory];
  37. if (deviceSpeedCategory < 3)
  38. skipLoopFilterDefaultValue = kVLCSettingSkipLoopFilterNonKey;
  39. else
  40. skipLoopFilterDefaultValue = kVLCSettingSkipLoopFilterNonRef;
  41. NSDictionary *appDefaults = @{kVLCSettingPasscodeKey : @"", kVLCSettingPasscodeOnKey : @(NO), kVLCSettingContinueAudioInBackgroundKey : @(YES), kVLCSettingStretchAudio : @(NO), kVLCSettingTextEncoding : kVLCSettingTextEncodingDefaultValue, kVLCSettingSkipLoopFilter : skipLoopFilterDefaultValue, kVLCSettingSubtitlesFont : kVLCSettingSubtitlesFontDefaultValue, kVLCSettingSubtitlesFontColor : kVLCSettingSubtitlesFontColorDefaultValue, kVLCSettingSubtitlesFontSize : kVLCSettingSubtitlesFontSizeDefaultValue, kVLCSettingDeinterlace : kVLCSettingDeinterlaceDefaultValue};
  42. [defaults registerDefaults:appDefaults];
  43. }
  44. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  45. {
  46. // Init the HTTP Server
  47. self.uploadController = [[VLCHTTPUploaderController alloc] init];
  48. // enable crash preventer
  49. [[MLMediaLibrary sharedMediaLibrary] applicationWillStart];
  50. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  51. _playlistViewController = [[VLCPlaylistViewController alloc] init];
  52. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_playlistViewController];
  53. [navCon loadTheme];
  54. _revealController = [[GHRevealViewController alloc] initWithNibName:nil bundle:nil];
  55. _revealController.wantsFullScreenLayout = YES;
  56. _menuViewController = [[VLCMenuTableViewController alloc] initWithNibName:nil bundle:nil];
  57. _revealController.sidebarViewController = _menuViewController;
  58. _revealController.contentViewController = navCon;
  59. self.window.rootViewController = self.revealController;
  60. // necessary to avoid navbar blinking in VLCOpenNetworkStreamViewController & VLCDownloadViewController
  61. _revealController.contentViewController.view.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  62. [self.window makeKeyAndVisible];
  63. VLCMediaFileDiscoverer *discoverer = [VLCMediaFileDiscoverer sharedInstance];
  64. [discoverer addObserver:self];
  65. [discoverer startDiscovering:[self directoryPath]];
  66. [self validatePasscode];
  67. return YES;
  68. }
  69. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
  70. {
  71. if ([[DBSession sharedSession] handleOpenURL:url]) {
  72. [self.dropboxTableViewController updateViewAfterSessionChange];
  73. return YES;
  74. }
  75. if (_playlistViewController && url != nil) {
  76. APLog(@"%@ requested %@ to be opened", sourceApplication, url);
  77. if (url.isFileURL) {
  78. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  79. NSString *directoryPath = searchPaths[0];
  80. NSURL *destinationURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", directoryPath, url.lastPathComponent]];
  81. NSError *theError;
  82. [[NSFileManager defaultManager] moveItemAtURL:url toURL:destinationURL error:&theError];
  83. if (theError.code != noErr)
  84. APLog(@"saving the file failed (%i): %@", theError.code, theError.localizedDescription);
  85. [self updateMediaList];
  86. } else {
  87. NSURL *parsedUrl = [self parseOpenURL:url];
  88. [self.menuViewController selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
  89. [self.playlistViewController performSelector:@selector(openMovieFromURL:) withObject:parsedUrl afterDelay:kGHRevealSidebarDefaultAnimationDuration];
  90. }
  91. return YES;
  92. }
  93. return NO;
  94. }
  95. - (NSURL *)parseOpenURL:(NSURL *)url
  96. {
  97. NSString *receivedUrl = [url absoluteString];
  98. if ([receivedUrl length] > 6) {
  99. NSString *verifyVlcUrl = [receivedUrl substringToIndex:6];
  100. if ([verifyVlcUrl isEqualToString:@"vlc://"]) {
  101. NSString *parsedString = [receivedUrl substringFromIndex:6];
  102. // "url decode" so we can parse http:// links
  103. parsedString = [parsedString stringByReplacingOccurrencesOfString:@"+"withString:@" "];
  104. parsedString = [parsedString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  105. // add http:// if nothing is there
  106. NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:(NSTextCheckingTypes)NSTextCheckingTypeLink error:nil];
  107. NSUInteger parsedStringLength = [parsedString length];
  108. NSUInteger numberOfUrlMatches = [detector numberOfMatchesInString:parsedString options:0 range:NSMakeRange(0, parsedStringLength)];
  109. if (numberOfUrlMatches == 0) {
  110. parsedString = [@"http://" stringByAppendingString:parsedString];
  111. }
  112. NSURL *targetUrl = [NSURL URLWithString:parsedString];
  113. return targetUrl;
  114. }
  115. }
  116. return url;
  117. }
  118. - (void)applicationWillEnterForeground:(UIApplication *)application
  119. {
  120. [[MLMediaLibrary sharedMediaLibrary] applicationWillStart];
  121. [self validatePasscode];
  122. }
  123. - (void)applicationWillResignActive:(UIApplication *)application
  124. {
  125. self.nextPasscodeCheckDate = [NSDate dateWithTimeIntervalSinceNow:PASSCODE_CHECK_INTERVAL];
  126. [[MLMediaLibrary sharedMediaLibrary] applicationWillExit];
  127. }
  128. - (void)applicationDidBecomeActive:(UIApplication *)application
  129. {
  130. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  131. [self updateMediaList];
  132. }
  133. - (void)applicationWillTerminate:(UIApplication *)application
  134. {
  135. [[NSUserDefaults standardUserDefaults] synchronize];
  136. }
  137. #pragma mark - properties
  138. - (VLCDropboxTableViewController *)dropboxTableViewController
  139. {
  140. if (_dropboxTableViewController == nil) {
  141. _dropboxTableViewController = [[VLCDropboxTableViewController alloc] initWithNibName:nil bundle:nil];
  142. }
  143. return _dropboxTableViewController;
  144. }
  145. - (VLCGoogleDriveTableViewController *)googleDriveTableViewController
  146. {
  147. if (_googleDriveTableViewController == nil) {
  148. _googleDriveTableViewController = [[VLCGoogleDriveTableViewController alloc] initWithNibName:nil bundle:nil];
  149. }
  150. return _googleDriveTableViewController;
  151. }
  152. - (VLCDownloadViewController *)downloadViewController
  153. {
  154. if (_downloadViewController == nil) {
  155. _downloadViewController = [[VLCDownloadViewController alloc] init];
  156. }
  157. return _downloadViewController;
  158. }
  159. #pragma mark - media discovering
  160. - (void)mediaFileAdded:(NSString *)fileName loading:(BOOL)isLoading {
  161. if (!isLoading) {
  162. MLMediaLibrary *sharedLibrary = [MLMediaLibrary sharedMediaLibrary];
  163. [sharedLibrary addFilePaths:@[fileName]];
  164. /* exclude media files from backup (QA1719) */
  165. NSURL *excludeURL = [NSURL fileURLWithPath:fileName];
  166. [excludeURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  167. // TODO Should we update media db after adding new files?
  168. [sharedLibrary updateMediaDatabase];
  169. [_playlistViewController updateViewContents];
  170. }
  171. }
  172. - (void)mediaFileDeleted:(NSString *)name {
  173. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  174. [_playlistViewController updateViewContents];
  175. }
  176. #pragma mark - media list methods
  177. - (NSString *)directoryPath
  178. {
  179. #define LOCAL_PLAYBACK_HACK 0
  180. #if LOCAL_PLAYBACK_HACK && TARGET_IPHONE_SIMULATOR
  181. NSString *directoryPath = @"/Users/fkuehne/Desktop/VideoLAN docs/Clips/sel/";
  182. #else
  183. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  184. NSString *directoryPath = searchPaths[0];
  185. #endif
  186. return directoryPath;
  187. }
  188. - (void)updateMediaList
  189. {
  190. NSString *directoryPath = [self directoryPath];
  191. NSArray *foundFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil];
  192. NSMutableArray *filePaths = [NSMutableArray arrayWithCapacity:[foundFiles count]];
  193. NSURL *fileURL;
  194. for (NSString *fileName in foundFiles) {
  195. if ([fileName isSupportedMediaFormat] || [fileName isSupportedAudioMediaFormat]) {
  196. [filePaths addObject:[directoryPath stringByAppendingPathComponent:fileName]];
  197. /* exclude media files from backup (QA1719) */
  198. fileURL = [NSURL fileURLWithPath:[directoryPath stringByAppendingPathComponent:fileName]];
  199. [fileURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  200. }
  201. }
  202. [[MLMediaLibrary sharedMediaLibrary] addFilePaths:filePaths];
  203. [_playlistViewController updateViewContents];
  204. }
  205. #pragma mark - pass code validation
  206. - (void)validatePasscode
  207. {
  208. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  209. NSString *passcode = [defaults objectForKey:kVLCSettingPasscodeKey];
  210. if ([passcode isEqualToString:@""] || ![[defaults objectForKey:kVLCSettingPasscodeOnKey] boolValue]) {
  211. self.passcodeValidated = YES;
  212. return;
  213. }
  214. if (!self.passcodeValidated) {
  215. if ([self.nextPasscodeCheckDate earlierDate:[NSDate date]] == self.nextPasscodeCheckDate) {
  216. _passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionEnter];
  217. _passcodeLockController.delegate = self;
  218. _passcodeLockController.passcode = passcode;
  219. self.window.rootViewController = _passcodeLockController;
  220. } else
  221. self.passcodeValidated = YES;
  222. }
  223. }
  224. - (void)PAPasscodeViewControllerDidEnterPasscode:(PAPasscodeViewController *)controller
  225. {
  226. // TODO add transition animation, i.e. fade
  227. self.nextPasscodeCheckDate = [NSDate dateWithTimeIntervalSinceNow:PASSCODE_CHECK_INTERVAL];
  228. self.window.rootViewController = self.revealController;
  229. }
  230. - (void)PAPasscodeViewController:(PAPasscodeViewController *)controller didFailToEnterPasscode:(NSInteger)attempts
  231. {
  232. // TODO handle error attempts
  233. }
  234. #pragma mark - idle timer preventer
  235. - (void)disableIdleTimer
  236. {
  237. _idleCounter++;
  238. if ([UIApplication sharedApplication].idleTimerDisabled == NO)
  239. [UIApplication sharedApplication].idleTimerDisabled = YES;
  240. }
  241. - (void)activateIdleTimer
  242. {
  243. _idleCounter--;
  244. if (_idleCounter < 1)
  245. [UIApplication sharedApplication].idleTimerDisabled = NO;
  246. }
  247. #pragma mark - playback view handling
  248. - (void)openMediaFromManagedObject:(NSManagedObject *)mediaObject
  249. {
  250. if (!_movieViewController)
  251. _movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  252. if ([mediaObject isKindOfClass:[MLFile class]])
  253. _movieViewController.mediaItem = (MLFile *)mediaObject;
  254. else if ([mediaObject isKindOfClass:[MLAlbumTrack class]])
  255. _movieViewController.mediaItem = [(MLAlbumTrack*)mediaObject files].anyObject;
  256. else if ([mediaObject isKindOfClass:[MLShowEpisode class]])
  257. _movieViewController.mediaItem = [(MLShowEpisode*)mediaObject files].anyObject;
  258. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_movieViewController];
  259. navCon.modalPresentationStyle = UIModalPresentationFullScreen;
  260. [self.window.rootViewController presentViewController:navCon animated:YES completion:nil];
  261. }
  262. - (void)openMovieFromURL:(NSURL *)url
  263. {
  264. if (!_movieViewController)
  265. _movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  266. _movieViewController.url = url;
  267. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_movieViewController];
  268. navCon.modalPresentationStyle = UIModalPresentationFullScreen;
  269. [self.window.rootViewController presentViewController:navCon animated:YES completion:nil];
  270. }
  271. @end