VLCAppDelegate.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. //
  2. // VLCAppDelegate.m
  3. // AspenProject
  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 "VLCMenuViewController.h"
  16. #import "VLCMovieViewController.h"
  17. #import "PAPasscodeViewController.h"
  18. #import "UINavigationController+Theme.h"
  19. #import "VLCHTTPUploaderController.h"
  20. #import "VLCMenuTableViewController.h"
  21. @interface VLCAppDelegate () <PAPasscodeViewControllerDelegate, VLCMediaFileDiscovererDelegate> {
  22. PAPasscodeViewController *_passcodeLockController;
  23. VLCDropboxTableViewController *_dropboxTableViewController;
  24. int _idleCounter;
  25. }
  26. @property (nonatomic) BOOL passcodeValidated;
  27. @end
  28. @implementation VLCAppDelegate
  29. + (void)initialize
  30. {
  31. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  32. NSNumber *skipLoopFilterDefaultValue;
  33. int deviceSpeedCategory = [[UIDevice currentDevice] speedCategory];
  34. if (deviceSpeedCategory < 3)
  35. skipLoopFilterDefaultValue = kVLCSettingSkipLoopFilterNonKey;
  36. else
  37. skipLoopFilterDefaultValue = kVLCSettingSkipLoopFilterNonRef;
  38. NSDictionary *appDefaults = @{kVLCSettingPasscodeKey : @"", kVLCSettingPasscodeOnKey : @(NO), kVLCSettingContinueAudioInBackgroundKey : @(YES), kVLCSettingStretchAudio : @(NO), kVLCSettingTextEncoding : kVLCSettingTextEncodingDefaultValue, kVLCSettingSkipLoopFilter : skipLoopFilterDefaultValue, kVLCSettingSubtitlesFont : kVLCSettingSubtitlesFontDefaultValue, kVLCSettingSubtitlesFontColor : kVLCSettingSubtitlesFontColorDefaultValue, kVLCSettingSubtitlesFontSize : kVLCSettingSubtitlesFontSizeDefaultValue, kVLCSettingDeinterlace : kVLCSettingDeinterlaceDefaultValue};
  39. [defaults registerDefaults:appDefaults];
  40. }
  41. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  42. {
  43. // Init the HTTP Server
  44. self.uploadController = [[VLCHTTPUploaderController alloc] init];
  45. // enable crash preventer
  46. [[MLMediaLibrary sharedMediaLibrary] applicationWillStart];
  47. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  48. _playlistViewController = [[VLCPlaylistViewController alloc] init];
  49. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_playlistViewController];
  50. [navCon loadTheme];
  51. _revealController = [[GHRevealViewController alloc] initWithNibName:nil bundle:nil];
  52. _revealController.sidebarViewController = [[VLCMenuTableViewController alloc] initWithNibName:nil bundle:nil];
  53. _revealController.contentViewController = navCon;
  54. self.window.rootViewController = self.revealController;
  55. [self.window makeKeyAndVisible];
  56. VLCMediaFileDiscoverer *discoverer = [VLCMediaFileDiscoverer sharedInstance];
  57. [discoverer addObserver:self];
  58. [discoverer startDiscovering:[self directoryPath]];
  59. [self validatePasscode];
  60. return YES;
  61. }
  62. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
  63. {
  64. if ([[DBSession sharedSession] handleOpenURL:url]) {
  65. [self.dropboxTableViewController updateViewAfterSessionChange];
  66. return YES;
  67. }
  68. if (_playlistViewController && url != nil) {
  69. APLog(@"%@ requested %@ to be opened", sourceApplication, url);
  70. if (url.isFileURL) {
  71. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  72. NSString *directoryPath = searchPaths[0];
  73. NSURL *destinationURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", directoryPath, url.lastPathComponent]];
  74. NSError *theError;
  75. [[NSFileManager defaultManager] moveItemAtURL:url toURL:destinationURL error:&theError];
  76. if (theError.code != noErr)
  77. APLog(@"saving the file failed (%i): %@", theError.code, theError.localizedDescription);
  78. [self updateMediaList];
  79. } else {
  80. NSURL *parsedUrl = [self parseOpenURL:url];
  81. UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.playlistViewController];
  82. [navController loadTheme];
  83. self.revealController.contentViewController = navController;
  84. [self.revealController toggleSidebar:NO duration:kGHRevealSidebarDefaultAnimationDuration];
  85. [self.playlistViewController performSelector:@selector(openMovieFromURL:) withObject:parsedUrl afterDelay:kGHRevealSidebarDefaultAnimationDuration];
  86. }
  87. return YES;
  88. }
  89. return NO;
  90. }
  91. - (NSURL *)parseOpenURL:(NSURL *)url
  92. {
  93. NSString *receivedUrl = [url absoluteString];
  94. if ([receivedUrl length] > 6) {
  95. NSString *verifyVlcUrl = [receivedUrl substringToIndex:6];
  96. if ([verifyVlcUrl isEqualToString:@"vlc://"]) {
  97. NSString *parsedString = [receivedUrl substringFromIndex:6];
  98. // "url decode" so we can parse http:// links
  99. parsedString = [parsedString stringByReplacingOccurrencesOfString:@"+"withString:@" "];
  100. parsedString = [parsedString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  101. // add http:// if nothing is there
  102. NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:(NSTextCheckingTypes)NSTextCheckingTypeLink error:nil];
  103. NSUInteger parsedStringLength = [parsedString length];
  104. NSUInteger numberOfUrlMatches = [detector numberOfMatchesInString:parsedString options:0 range:NSMakeRange(0, parsedStringLength)];
  105. if (numberOfUrlMatches == 0) {
  106. parsedString = [@"http://" stringByAppendingString:parsedString];
  107. }
  108. NSURL *targetUrl = [NSURL URLWithString:parsedString];
  109. return targetUrl;
  110. }
  111. }
  112. return url;
  113. }
  114. - (void)applicationWillEnterForeground:(UIApplication *)application
  115. {
  116. [[MLMediaLibrary sharedMediaLibrary] applicationWillStart];
  117. }
  118. - (void)applicationWillResignActive:(UIApplication *)application
  119. {
  120. [[MLMediaLibrary sharedMediaLibrary] applicationWillExit];
  121. }
  122. - (void)applicationDidBecomeActive:(UIApplication *)application
  123. {
  124. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  125. [self updateMediaList];
  126. }
  127. - (void)applicationDidEnterBackground:(UIApplication *)application
  128. {
  129. [self validatePasscode]; // Lock library when going to background
  130. }
  131. - (void)applicationWillTerminate:(UIApplication *)application
  132. {
  133. [[NSUserDefaults standardUserDefaults] synchronize];
  134. }
  135. #pragma mark - properties
  136. - (VLCDropboxTableViewController *)dropboxTableViewController
  137. {
  138. if (_dropboxTableViewController == nil) {
  139. _dropboxTableViewController = [[VLCDropboxTableViewController alloc] initWithNibName:nil bundle:nil];
  140. }
  141. return _dropboxTableViewController;
  142. }
  143. #pragma mark - media discovering
  144. - (void)mediaFileAdded:(NSString *)fileName loading:(BOOL)isLoading {
  145. if (!isLoading) {
  146. MLMediaLibrary *sharedLibrary = [MLMediaLibrary sharedMediaLibrary];
  147. [sharedLibrary addFilePaths:@[fileName]];
  148. /* exclude media files from backup (QA1719) */
  149. NSURL *excludeURL = [NSURL fileURLWithPath:fileName];
  150. [excludeURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  151. // TODO Should we update media db after adding new files?
  152. [sharedLibrary updateMediaDatabase];
  153. [_playlistViewController updateViewContents];
  154. }
  155. }
  156. - (void)mediaFileDeleted:(NSString *)name {
  157. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  158. [_playlistViewController updateViewContents];
  159. }
  160. #pragma mark - media list methods
  161. - (NSString *)directoryPath
  162. {
  163. #define LOCAL_PLAYBACK_HACK 0
  164. #if LOCAL_PLAYBACK_HACK && TARGET_IPHONE_SIMULATOR
  165. NSString *directoryPath = @"/Users/fkuehne/Desktop/VideoLAN docs/Clips/sel/";
  166. #else
  167. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  168. NSString *directoryPath = searchPaths[0];
  169. #endif
  170. return directoryPath;
  171. }
  172. - (void)updateMediaList
  173. {
  174. NSString *directoryPath = [self directoryPath];
  175. NSArray *foundFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil];
  176. NSMutableArray *filePaths = [NSMutableArray arrayWithCapacity:[foundFiles count]];
  177. NSURL *fileURL;
  178. for (NSString *fileName in foundFiles) {
  179. if ([fileName isSupportedMediaFormat] || [fileName isSupportedAudioMediaFormat]) {
  180. [filePaths addObject:[directoryPath stringByAppendingPathComponent:fileName]];
  181. /* exclude media files from backup (QA1719) */
  182. fileURL = [NSURL fileURLWithPath:[directoryPath stringByAppendingPathComponent:fileName]];
  183. [fileURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  184. }
  185. }
  186. [[MLMediaLibrary sharedMediaLibrary] addFilePaths:filePaths];
  187. [_playlistViewController updateViewContents];
  188. }
  189. #pragma mark - pass code validation
  190. - (void)validatePasscode
  191. {
  192. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  193. NSString *passcode = [defaults objectForKey:kVLCSettingPasscodeKey];
  194. if ([passcode isEqualToString:@""] || ![[defaults objectForKey:kVLCSettingPasscodeOnKey] boolValue]) {
  195. self.passcodeValidated = YES;
  196. return;
  197. }
  198. if (!self.passcodeValidated) {
  199. if ([self.nextPasscodeCheckDate earlierDate:[NSDate date]] == self.nextPasscodeCheckDate) {
  200. _passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionEnter];
  201. _passcodeLockController.delegate = self;
  202. _passcodeLockController.passcode = passcode;
  203. self.window.rootViewController = _passcodeLockController;
  204. } else
  205. self.passcodeValidated = YES;
  206. }
  207. }
  208. - (void)PAPasscodeViewControllerDidEnterPasscode:(PAPasscodeViewController *)controller
  209. {
  210. // TODO add transition animation, i.e. fade
  211. self.nextPasscodeCheckDate = [NSDate dateWithTimeIntervalSinceNow:300];
  212. self.window.rootViewController = self.revealController;
  213. }
  214. - (void)PAPasscodeViewController:(PAPasscodeViewController *)controller didFailToEnterPasscode:(NSInteger)attempts
  215. {
  216. // TODO handle error attempts
  217. }
  218. #pragma mark - idle timer preventer
  219. - (void)disableIdleTimer
  220. {
  221. _idleCounter++;
  222. if ([UIApplication sharedApplication].idleTimerDisabled == NO)
  223. [UIApplication sharedApplication].idleTimerDisabled = YES;
  224. }
  225. - (void)activateIdleTimer
  226. {
  227. _idleCounter--;
  228. if (_idleCounter < 1)
  229. [UIApplication sharedApplication].idleTimerDisabled = NO;
  230. }
  231. @end