VLCAppDelegate.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. [_playlistViewController openMovieFromURL:parsedUrl];
  81. }
  82. return YES;
  83. }
  84. return NO;
  85. }
  86. - (NSURL *)parseOpenURL:(NSURL *)url
  87. {
  88. NSString *receivedUrl = [url absoluteString];
  89. if ([receivedUrl length] > 6) {
  90. NSString *verifyVlcUrl = [receivedUrl substringToIndex:6];
  91. if ([verifyVlcUrl isEqualToString:@"vlc://"]) {
  92. NSString *parsedString = [receivedUrl substringFromIndex:6];
  93. // "url decode" so we can parse http:// links
  94. parsedString = [parsedString stringByReplacingOccurrencesOfString:@"+"withString:@" "];
  95. parsedString = [parsedString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  96. // add http:// if nothing is there
  97. NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:(NSTextCheckingTypes)NSTextCheckingTypeLink error:nil];
  98. NSUInteger parsedStringLength = [parsedString length];
  99. NSUInteger numberOfUrlMatches = [detector numberOfMatchesInString:parsedString options:0 range:NSMakeRange(0, parsedStringLength)];
  100. if (numberOfUrlMatches == 0) {
  101. parsedString = [@"http://" stringByAppendingString:parsedString];
  102. }
  103. NSURL *targetUrl = [NSURL URLWithString:parsedString];
  104. return targetUrl;
  105. }
  106. }
  107. return url;
  108. }
  109. - (void)applicationWillEnterForeground:(UIApplication *)application
  110. {
  111. [[MLMediaLibrary sharedMediaLibrary] applicationWillStart];
  112. }
  113. - (void)applicationWillResignActive:(UIApplication *)application
  114. {
  115. [[MLMediaLibrary sharedMediaLibrary] applicationWillExit];
  116. }
  117. - (void)applicationDidBecomeActive:(UIApplication *)application
  118. {
  119. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  120. [self updateMediaList];
  121. }
  122. - (void)applicationDidEnterBackground:(UIApplication *)application
  123. {
  124. [self validatePasscode]; // Lock library when going to background
  125. }
  126. - (void)applicationWillTerminate:(UIApplication *)application
  127. {
  128. [[NSUserDefaults standardUserDefaults] synchronize];
  129. }
  130. #pragma mark - properties
  131. - (VLCDropboxTableViewController *)dropboxTableViewController
  132. {
  133. if (_dropboxTableViewController == nil) {
  134. _dropboxTableViewController = [[VLCDropboxTableViewController alloc] initWithNibName:nil bundle:nil];
  135. }
  136. return _dropboxTableViewController;
  137. }
  138. #pragma mark - media discovering
  139. - (void)mediaFileAdded:(NSString *)fileName loading:(BOOL)isLoading {
  140. if (!isLoading) {
  141. MLMediaLibrary *sharedLibrary = [MLMediaLibrary sharedMediaLibrary];
  142. [sharedLibrary addFilePaths:@[fileName]];
  143. /* exclude media files from backup (QA1719) */
  144. NSURL *excludeURL = [NSURL fileURLWithPath:fileName];
  145. [excludeURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  146. // TODO Should we update media db after adding new files?
  147. [sharedLibrary updateMediaDatabase];
  148. [_playlistViewController updateViewContents];
  149. }
  150. }
  151. - (void)mediaFileDeleted:(NSString *)name {
  152. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  153. [_playlistViewController updateViewContents];
  154. }
  155. #pragma mark - media list methods
  156. - (NSString *)directoryPath
  157. {
  158. #define LOCAL_PLAYBACK_HACK 0
  159. #if LOCAL_PLAYBACK_HACK && TARGET_IPHONE_SIMULATOR
  160. NSString *directoryPath = @"/Users/fkuehne/Desktop/VideoLAN docs/Clips/sel/";
  161. #else
  162. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  163. NSString *directoryPath = searchPaths[0];
  164. #endif
  165. return directoryPath;
  166. }
  167. - (void)updateMediaList
  168. {
  169. NSString *directoryPath = [self directoryPath];
  170. NSArray *foundFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil];
  171. NSMutableArray *filePaths = [NSMutableArray arrayWithCapacity:[foundFiles count]];
  172. NSURL *fileURL;
  173. for (NSString *fileName in foundFiles) {
  174. if ([fileName isSupportedMediaFormat] || [fileName isSupportedAudioMediaFormat]) {
  175. [filePaths addObject:[directoryPath stringByAppendingPathComponent:fileName]];
  176. /* exclude media files from backup (QA1719) */
  177. fileURL = [NSURL fileURLWithPath:[directoryPath stringByAppendingPathComponent:fileName]];
  178. [fileURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  179. }
  180. }
  181. [[MLMediaLibrary sharedMediaLibrary] addFilePaths:filePaths];
  182. [_playlistViewController updateViewContents];
  183. }
  184. #pragma mark - pass code validation
  185. - (void)validatePasscode
  186. {
  187. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  188. NSString *passcode = [defaults objectForKey:kVLCSettingPasscodeKey];
  189. if ([passcode isEqualToString:@""] || ![[defaults objectForKey:kVLCSettingPasscodeOnKey] boolValue]) {
  190. self.passcodeValidated = YES;
  191. return;
  192. }
  193. if (!self.passcodeValidated) {
  194. if ([self.nextPasscodeCheckDate earlierDate:[NSDate date]] == self.nextPasscodeCheckDate) {
  195. _passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionEnter];
  196. _passcodeLockController.delegate = self;
  197. _passcodeLockController.passcode = passcode;
  198. self.window.rootViewController = _passcodeLockController;
  199. } else
  200. self.passcodeValidated = YES;
  201. }
  202. }
  203. - (void)PAPasscodeViewControllerDidEnterPasscode:(PAPasscodeViewController *)controller
  204. {
  205. // TODO add transition animation, i.e. fade
  206. self.nextPasscodeCheckDate = [NSDate dateWithTimeIntervalSinceNow:300];
  207. self.window.rootViewController = self.revealController;
  208. }
  209. - (void)PAPasscodeViewController:(PAPasscodeViewController *)controller didFailToEnterPasscode:(NSInteger)attempts
  210. {
  211. // TODO handle error attempts
  212. }
  213. #pragma mark - idle timer preventer
  214. - (void)disableIdleTimer
  215. {
  216. _idleCounter++;
  217. if ([UIApplication sharedApplication].idleTimerDisabled == NO)
  218. [UIApplication sharedApplication].idleTimerDisabled = YES;
  219. }
  220. - (void)activateIdleTimer
  221. {
  222. _idleCounter--;
  223. if (_idleCounter < 1)
  224. [UIApplication sharedApplication].idleTimerDisabled = NO;
  225. }
  226. @end