VLCAppDelegate.m 11 KB

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