VLCAppDelegate.m 9.9 KB

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