VLCAppDelegate.m 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*****************************************************************************
  2. * VLCAppDelegate.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013-2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. * Gleb Pinigin <gpinigin # gmail.com>
  10. * Jean-Romain Prévost <jr # 3on.fr>
  11. * Luis Fernandes <zipleen # gmail.com>
  12. * Carola Nitz <nitz.carola # googlemail.com>
  13. * Tamas Timar <ttimar.vlc # gmail.com>
  14. * Tobias Conradi <videolan # tobias-conradi.de>
  15. * Soomin Lee <TheHungryBu # gmail.com>
  16. *
  17. * Refer to the COPYING file of the official project for license.
  18. *****************************************************************************/
  19. #import "VLCAppDelegate.h"
  20. #import "VLCMediaFileDiscoverer.h"
  21. #import "NSString+SupportedMedia.h"
  22. #import "UIDevice+VLC.h"
  23. #import "VLCLibraryViewController.h"
  24. #import "VLCHTTPUploaderController.h"
  25. #import "VLCMigrationViewController.h"
  26. #import <BoxSDK/BoxSDK.h>
  27. #import "VLCPlaybackController.h"
  28. #import "VLCPlaybackController+MediaLibrary.h"
  29. #import "VLCPlayerDisplayController.h"
  30. #import <MediaPlayer/MediaPlayer.h>
  31. #import <HockeySDK/HockeySDK.h>
  32. #import "VLCSidebarController.h"
  33. #import "VLCKeychainCoordinator.h"
  34. #import "VLCActivityManager.h"
  35. #import "VLCDropboxConstants.h"
  36. #import <ObjectiveDropboxOfficial/ObjectiveDropboxOfficial.h>
  37. #import "VLCPlaybackNavigationController.h"
  38. #import "PAPasscodeViewController.h"
  39. NSString *const VLCDropboxSessionWasAuthorized = @"VLCDropboxSessionWasAuthorized";
  40. #define BETA_DISTRIBUTION 1
  41. @interface VLCAppDelegate () <VLCMediaFileDiscovererDelegate>
  42. {
  43. BOOL _isRunningMigration;
  44. BOOL _isComingFromHandoff;
  45. VLCWatchCommunication *_watchCommunication;
  46. }
  47. @end
  48. @implementation VLCAppDelegate
  49. + (void)initialize
  50. {
  51. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  52. NSDictionary *appDefaults = @{kVLCSettingPasscodeAllowTouchID : @(1),
  53. kVLCSettingContinueAudioInBackgroundKey : @(YES),
  54. kVLCSettingStretchAudio : @(NO),
  55. kVLCSettingTextEncoding : kVLCSettingTextEncodingDefaultValue,
  56. kVLCSettingSkipLoopFilter : kVLCSettingSkipLoopFilterNonRef,
  57. kVLCSettingSubtitlesFont : kVLCSettingSubtitlesFontDefaultValue,
  58. kVLCSettingSubtitlesFontColor : kVLCSettingSubtitlesFontColorDefaultValue,
  59. kVLCSettingSubtitlesFontSize : kVLCSettingSubtitlesFontSizeDefaultValue,
  60. kVLCSettingSubtitlesBoldFont: kVLCSettingSubtitlesBoldFontDefaultValue,
  61. kVLCSettingDeinterlace : kVLCSettingDeinterlaceDefaultValue,
  62. kVLCSettingHardwareDecoding : kVLCSettingHardwareDecodingDefault,
  63. kVLCSettingNetworkCaching : kVLCSettingNetworkCachingDefaultValue,
  64. kVLCSettingVolumeGesture : @(YES),
  65. kVLCSettingPlayPauseGesture : @(YES),
  66. kVLCSettingBrightnessGesture : @(YES),
  67. kVLCSettingSeekGesture : @(YES),
  68. kVLCSettingCloseGesture : @(YES),
  69. kVLCSettingVariableJumpDuration : @(NO),
  70. kVLCSettingVideoFullscreenPlayback : @(YES),
  71. kVLCSettingContinuePlayback : @(1),
  72. kVLCSettingContinueAudioPlayback : @(1),
  73. kVLCSettingFTPTextEncoding : kVLCSettingFTPTextEncodingDefaultValue,
  74. kVLCSettingWiFiSharingIPv6 : kVLCSettingWiFiSharingIPv6DefaultValue,
  75. kVLCSettingEqualizerProfile : kVLCSettingEqualizerProfileDefaultValue,
  76. kVLCSettingPlaybackForwardSkipLength : kVLCSettingPlaybackForwardSkipLengthDefaultValue,
  77. kVLCSettingPlaybackBackwardSkipLength : kVLCSettingPlaybackBackwardSkipLengthDefaultValue,
  78. kVLCSettingOpenAppForPlayback : kVLCSettingOpenAppForPlaybackDefaultValue,
  79. kVLCAutomaticallyPlayNextItem : @(YES)};
  80. [defaults registerDefaults:appDefaults];
  81. }
  82. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  83. {
  84. BITHockeyManager *hockeyManager = [BITHockeyManager sharedHockeyManager];
  85. [hockeyManager configureWithBetaIdentifier:@"0114ca8e265244ce588d2ebd035c3577"
  86. liveIdentifier:@"c95f4227dff96c61f8b3a46a25edc584"
  87. delegate:nil];
  88. [hockeyManager startManager];
  89. // Configure Dropbox
  90. [DBClientsManager setupWithAppKey:kVLCDropboxAppKey];
  91. [self setupAppearence];
  92. // Init the HTTP Server and clean its cache
  93. [[VLCHTTPUploaderController sharedInstance] cleanCache];
  94. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  95. // enable crash preventer
  96. void (^setupBlock)() = ^{
  97. __weak typeof(self) weakSelf = self;
  98. void (^setupLibraryBlock)() = ^{
  99. _libraryViewController = [[VLCLibraryViewController alloc] init];
  100. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_libraryViewController];
  101. VLCSidebarController *sidebarVC = [VLCSidebarController sharedInstance];
  102. sidebarVC.contentViewController = navCon;
  103. VLCPlayerDisplayController *playerDisplayController = [VLCPlayerDisplayController sharedInstance];
  104. playerDisplayController.childViewController = sidebarVC.fullViewController;
  105. weakSelf.window.rootViewController = playerDisplayController;
  106. };
  107. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:[[UIViewController alloc] init]];
  108. self.window.rootViewController = navCon;
  109. [self.window makeKeyAndVisible];
  110. [self validatePasscodeIfNeededWithCompletion:setupLibraryBlock];
  111. BOOL spotlightEnabled = ![[VLCKeychainCoordinator defaultCoordinator] passcodeLockEnabled];
  112. [[MLMediaLibrary sharedMediaLibrary] setSpotlightIndexingEnabled:spotlightEnabled];
  113. [[MLMediaLibrary sharedMediaLibrary] applicationWillStart];
  114. VLCMediaFileDiscoverer *discoverer = [VLCMediaFileDiscoverer sharedInstance];
  115. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  116. discoverer.directoryPath = [searchPaths firstObject];
  117. [discoverer addObserver:self];
  118. [discoverer startDiscovering];
  119. };
  120. NSError *error = nil;
  121. if ([[MLMediaLibrary sharedMediaLibrary] libraryMigrationNeeded]){
  122. _isRunningMigration = YES;
  123. VLCMigrationViewController *migrationController = [[VLCMigrationViewController alloc] initWithNibName:@"VLCMigrationViewController" bundle:nil];
  124. migrationController.completionHandler = ^{
  125. //migrate
  126. setupBlock();
  127. _isRunningMigration = NO;
  128. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  129. [[VLCMediaFileDiscoverer sharedInstance] updateMediaList];
  130. };
  131. self.window.rootViewController = migrationController;
  132. [self.window makeKeyAndVisible];
  133. } else {
  134. if (error != nil) {
  135. APLog(@"removed persistentStore since it was corrupt");
  136. NSURL *storeURL = ((MLMediaLibrary *)[MLMediaLibrary sharedMediaLibrary]).persistentStoreURL;
  137. [[NSFileManager defaultManager] removeItemAtURL:storeURL error:&error];
  138. }
  139. setupBlock();
  140. }
  141. if ([VLCWatchCommunication isSupported]) {
  142. _watchCommunication = [VLCWatchCommunication sharedInstance];
  143. // TODO: push DB changes instead
  144. // [_watchCommunication startRelayingNotificationName:NSManagedObjectContextDidSaveNotification object:nil];
  145. [_watchCommunication startRelayingNotificationName:VLCPlaybackControllerPlaybackMetadataDidChange object:nil];
  146. }
  147. /* add our static shortcut items the dynamic way to ease l10n and dynamic elements to be introduced later */
  148. if ([UIApplicationShortcutItem class] != nil) {
  149. if (application.shortcutItems == nil || application.shortcutItems.count < 4) {
  150. UIApplicationShortcutItem *localLibraryItem = [[UIApplicationShortcutItem alloc] initWithType:kVLCApplicationShortcutLocalLibrary
  151. localizedTitle:NSLocalizedString(@"SECTION_HEADER_LIBRARY",nil)
  152. localizedSubtitle:nil
  153. icon:[UIApplicationShortcutIcon iconWithTemplateImageName:@"AllFiles"]
  154. userInfo:nil];
  155. UIApplicationShortcutItem *localServerItem = [[UIApplicationShortcutItem alloc] initWithType:kVLCApplicationShortcutLocalServers
  156. localizedTitle:NSLocalizedString(@"LOCAL_NETWORK",nil)
  157. localizedSubtitle:nil
  158. icon:[UIApplicationShortcutIcon iconWithTemplateImageName:@"Local"]
  159. userInfo:nil];
  160. UIApplicationShortcutItem *openNetworkStreamItem = [[UIApplicationShortcutItem alloc] initWithType:kVLCApplicationShortcutOpenNetworkStream
  161. localizedTitle:NSLocalizedString(@"OPEN_NETWORK",nil)
  162. localizedSubtitle:nil
  163. icon:[UIApplicationShortcutIcon iconWithTemplateImageName:@"OpenNetStream"]
  164. userInfo:nil];
  165. UIApplicationShortcutItem *cloudsItem = [[UIApplicationShortcutItem alloc] initWithType:kVLCApplicationShortcutClouds
  166. localizedTitle:NSLocalizedString(@"CLOUD_SERVICES",nil)
  167. localizedSubtitle:nil
  168. icon:[UIApplicationShortcutIcon iconWithTemplateImageName:@"iCloudIcon"]
  169. userInfo:nil];
  170. application.shortcutItems = @[localLibraryItem, localServerItem, openNetworkStreamItem, cloudsItem];
  171. }
  172. }
  173. return YES;
  174. }
  175. - (void)setupAppearence
  176. {
  177. UIColor *vlcOrange = [UIColor VLCOrangeTintColor];
  178. // Change the keyboard for UISearchBar
  179. [[UITextField appearance] setKeyboardAppearance:UIKeyboardAppearanceDark];
  180. // For the cursor
  181. [[UITextField appearance] setTintColor:vlcOrange];
  182. // Don't override the 'Cancel' button color in the search bar with the previous UITextField call. Use the default blue color
  183. [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitleTextAttributes:@{[UIColor whiteColor] : NSForegroundColorAttributeName} forState:UIControlStateNormal];
  184. [[UINavigationBar appearance] setBarTintColor:vlcOrange];
  185. [[UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[[VLCPlaybackNavigationController class]]] setBarTintColor: nil];
  186. [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
  187. [[UINavigationBar appearance] setTitleTextAttributes: @{ NSForegroundColorAttributeName : [UIColor whiteColor] }];
  188. // For the edit selection indicators
  189. [[UITableView appearance] setTintColor:vlcOrange];
  190. [[UISwitch appearance] setOnTintColor:vlcOrange];
  191. [[UISearchBar appearance] setBarTintColor:vlcOrange];
  192. [[UISearchBar appearance] setTintColor:[UIColor whiteColor]];
  193. }
  194. - (void)dealloc
  195. {
  196. [[NSNotificationCenter defaultCenter] removeObserver:self];
  197. }
  198. #pragma mark - Handoff
  199. - (BOOL)application:(UIApplication *)application willContinueUserActivityWithType:(NSString *)userActivityType
  200. {
  201. if ([userActivityType isEqualToString:kVLCUserActivityLibraryMode] ||
  202. [userActivityType isEqualToString:kVLCUserActivityPlaying] ||
  203. [userActivityType isEqualToString:kVLCUserActivityLibrarySelection])
  204. return YES;
  205. return NO;
  206. }
  207. - (BOOL)application:(UIApplication *)application
  208. continueUserActivity:(NSUserActivity *)userActivity
  209. restorationHandler:(void (^)(NSArray *))restorationHandler
  210. {
  211. NSString *userActivityType = userActivity.activityType;
  212. NSDictionary *dict = userActivity.userInfo;
  213. if([userActivityType isEqualToString:kVLCUserActivityLibraryMode] ||
  214. [userActivityType isEqualToString:kVLCUserActivityLibrarySelection]) {
  215. VLCLibraryMode libraryMode = (VLCLibraryMode)[(NSNumber *)dict[@"state"] integerValue];
  216. if (libraryMode <= VLCLibraryModeAllSeries) {
  217. [[VLCSidebarController sharedInstance] selectRowAtIndexPath:[NSIndexPath indexPathForRow:libraryMode inSection:0]
  218. scrollPosition:UITableViewScrollPositionTop];
  219. [self.libraryViewController setLibraryMode:libraryMode];
  220. }
  221. [self.libraryViewController restoreUserActivityState:userActivity];
  222. _isComingFromHandoff = YES;
  223. return YES;
  224. } else {
  225. NSURL *uriRepresentation = nil;
  226. if ([userActivityType isEqualToString:CSSearchableItemActionType]) {
  227. uriRepresentation = [NSURL URLWithString:dict[CSSearchableItemActivityIdentifier]];
  228. } else {
  229. uriRepresentation = dict[@"playingmedia"];
  230. }
  231. if (!uriRepresentation) {
  232. return NO;
  233. }
  234. NSManagedObject *managedObject = [[MLMediaLibrary sharedMediaLibrary] objectForURIRepresentation:uriRepresentation];
  235. if (managedObject == nil) {
  236. APLog(@"%s file not found: %@",__PRETTY_FUNCTION__,userActivity);
  237. return NO;
  238. }
  239. [[VLCPlaybackController sharedInstance] openMediaLibraryObject:managedObject];
  240. return YES;
  241. }
  242. return NO;
  243. }
  244. - (void)application:(UIApplication *)application
  245. didFailToContinueUserActivityWithType:(NSString *)userActivityType
  246. error:(NSError *)error
  247. {
  248. if (error.code != NSUserCancelledError){
  249. //TODO: present alert
  250. }
  251. }
  252. - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
  253. {
  254. //Handles Dropbox Authorization flow.
  255. DBOAuthResult *authResult = [DBClientsManager handleRedirectURL:url];
  256. if (authResult != nil) {
  257. if ([authResult isSuccess]) {
  258. return YES;
  259. }
  260. }
  261. //Handles Google Authorization flow.
  262. if ([_currentGoogleAuthorizationFlow resumeAuthorizationFlowWithURL:url]) {
  263. _currentGoogleAuthorizationFlow = nil;
  264. return YES;
  265. }
  266. if (_libraryViewController && url != nil) {
  267. APLog(@"requested %@ to be opened", url);
  268. if (url.isFileURL) {
  269. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  270. NSString *directoryPath = searchPaths.firstObject;
  271. NSURL *destinationURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", directoryPath, url.lastPathComponent]];
  272. NSError *theError;
  273. NSFileManager *manager = [NSFileManager defaultManager];
  274. [[NSFileManager defaultManager] moveItemAtURL:url toURL:destinationURL error:&theError];
  275. if (theError.code != noErr)
  276. APLog(@"saving the file failed (%li): %@", (long)theError.code, theError.localizedDescription);
  277. [[VLCMediaFileDiscoverer sharedInstance] updateMediaList];
  278. NSURLRelationship relationship;
  279. [manager getRelationship:&relationship ofDirectoryAtURL:[NSURL fileURLWithPath:directoryPath] toItemAtURL:url error:&theError];
  280. if (relationship == NSURLRelationshipContains) {
  281. [self playWithURL:url successCallback:nil errorCallback:nil];
  282. }
  283. } else if ([url.scheme isEqualToString:@"vlc-x-callback"] || [url.host isEqualToString:@"x-callback-url"]) {
  284. // URL confirmes to the x-callback-url specification
  285. // vlc-x-callback://x-callback-url/action?param=value&x-success=callback
  286. APLog(@"x-callback-url with host '%@' path '%@' parameters '%@'", url.host, url.path, url.query);
  287. NSString *action = [url.path stringByReplacingOccurrencesOfString:@"/" withString:@""];
  288. NSURL *movieURL;
  289. NSURL *successCallback;
  290. NSURL *errorCallback;
  291. NSString *fileName;
  292. for (NSString *entry in [url.query componentsSeparatedByString:@"&"]) {
  293. NSArray *keyvalue = [entry componentsSeparatedByString:@"="];
  294. if (keyvalue.count < 2) continue;
  295. NSString *key = keyvalue[0];
  296. NSString *value = [keyvalue[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  297. if ([key isEqualToString:@"url"])
  298. movieURL = [NSURL URLWithString:value];
  299. else if ([key isEqualToString:@"filename"])
  300. fileName = value;
  301. else if ([key isEqualToString:@"x-success"])
  302. successCallback = [NSURL URLWithString:value];
  303. else if ([key isEqualToString:@"x-error"])
  304. errorCallback = [NSURL URLWithString:value];
  305. }
  306. if ([action isEqualToString:@"stream"] && movieURL) {
  307. [self playWithURL:movieURL successCallback:successCallback errorCallback:errorCallback];
  308. }
  309. else if ([action isEqualToString:@"download"] && movieURL) {
  310. [self downloadMovieFromURL:movieURL fileNameOfMedia:fileName];
  311. }
  312. } else {
  313. NSString *receivedUrl = [url absoluteString];
  314. if ([receivedUrl length] > 6) {
  315. NSString *verifyVlcUrl = [receivedUrl substringToIndex:6];
  316. if ([verifyVlcUrl isEqualToString:@"vlc://"]) {
  317. NSString *parsedString = [receivedUrl substringFromIndex:6];
  318. NSUInteger location = [parsedString rangeOfString:@"//"].location;
  319. /* Safari & al mangle vlc://http:// so fix this */
  320. if (location != NSNotFound && [parsedString characterAtIndex:location - 1] != 0x3a) { // :
  321. parsedString = [NSString stringWithFormat:@"%@://%@", [parsedString substringToIndex:location], [parsedString substringFromIndex:location+2]];
  322. } else {
  323. parsedString = [receivedUrl substringFromIndex:6];
  324. if (![parsedString hasPrefix:@"http://"] && ![parsedString hasPrefix:@"https://"] && ![parsedString hasPrefix:@"ftp://"]) {
  325. parsedString = [@"http://" stringByAppendingString:[receivedUrl substringFromIndex:6]];
  326. }
  327. }
  328. url = [NSURL URLWithString:parsedString];
  329. }
  330. }
  331. [[VLCSidebarController sharedInstance] selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
  332. scrollPosition:UITableViewScrollPositionNone];
  333. NSString *scheme = url.scheme;
  334. if ([scheme isEqualToString:@"http"] || [scheme isEqualToString:@"https"] || [scheme isEqualToString:@"ftp"]) {
  335. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"OPEN_STREAM_OR_DOWNLOAD", nil) message:url.absoluteString cancelButtonTitle:NSLocalizedString(@"BUTTON_DOWNLOAD", nil) otherButtonTitles:@[NSLocalizedString(@"PLAY_BUTTON", nil)]];
  336. alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
  337. if (cancelled)
  338. [self downloadMovieFromURL:url fileNameOfMedia:nil];
  339. else {
  340. [self playWithURL:url successCallback:nil errorCallback:nil];
  341. }
  342. };
  343. [alert show];
  344. } else {
  345. [self playWithURL:url successCallback:nil errorCallback:nil];
  346. }
  347. }
  348. return YES;
  349. }
  350. return NO;
  351. }
  352. - (void)applicationWillEnterForeground:(UIApplication *)application
  353. {
  354. [[MLMediaLibrary sharedMediaLibrary] applicationWillStart];
  355. }
  356. - (void)applicationWillResignActive:(UIApplication *)application
  357. {
  358. //Touch ID is shown
  359. if ([_window.rootViewController.presentedViewController isKindOfClass:[UINavigationController class]]){
  360. UINavigationController *navCon = (UINavigationController *)_window.rootViewController.presentedViewController;
  361. if ([navCon.topViewController isKindOfClass:[PAPasscodeViewController class]]){
  362. return;
  363. }
  364. }
  365. __weak typeof(self) weakself = self;
  366. [self validatePasscodeIfNeededWithCompletion:^{
  367. [weakself.libraryViewController updateViewContents];
  368. if ([VLCPlaybackController sharedInstance].isPlaying){
  369. [[VLCPlayerDisplayController sharedInstance] pushPlaybackView];
  370. }
  371. }];
  372. [[MLMediaLibrary sharedMediaLibrary] applicationWillExit];
  373. }
  374. - (void)applicationDidBecomeActive:(UIApplication *)application
  375. {
  376. if (!_isRunningMigration && !_isComingFromHandoff) {
  377. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  378. [[VLCMediaFileDiscoverer sharedInstance] updateMediaList];
  379. } else if(_isComingFromHandoff) {
  380. _isComingFromHandoff = NO;
  381. }
  382. }
  383. - (void)applicationWillTerminate:(UIApplication *)application
  384. {
  385. [[NSUserDefaults standardUserDefaults] synchronize];
  386. }
  387. - (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
  388. {
  389. [[VLCSidebarController sharedInstance] performActionForShortcutItem:shortcutItem];
  390. }
  391. #pragma mark - media discovering
  392. - (void)mediaFileAdded:(NSString *)fileName loading:(BOOL)isLoading
  393. {
  394. if (!isLoading) {
  395. MLMediaLibrary *sharedLibrary = [MLMediaLibrary sharedMediaLibrary];
  396. [sharedLibrary addFilePaths:@[fileName]];
  397. /* exclude media files from backup (QA1719) */
  398. NSURL *excludeURL = [NSURL fileURLWithPath:fileName];
  399. [excludeURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  400. // TODO Should we update media db after adding new files?
  401. [sharedLibrary updateMediaDatabase];
  402. [_libraryViewController updateViewContents];
  403. }
  404. }
  405. - (void)mediaFileDeleted:(NSString *)name
  406. {
  407. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  408. [_libraryViewController updateViewContents];
  409. }
  410. - (void)mediaFilesFoundRequiringAdditionToStorageBackend:(NSArray<NSString *> *)foundFiles
  411. {
  412. [[MLMediaLibrary sharedMediaLibrary] addFilePaths:foundFiles];
  413. [[(VLCAppDelegate *)[UIApplication sharedApplication].delegate libraryViewController] updateViewContents];
  414. }
  415. #pragma mark - pass code validation
  416. - (void)validatePasscodeIfNeededWithCompletion:(void(^)(void))completion
  417. {
  418. VLCKeychainCoordinator *keychainCoordinator = [VLCKeychainCoordinator defaultCoordinator];
  419. if ([keychainCoordinator passcodeLockEnabled]) {
  420. [[VLCPlayerDisplayController sharedInstance] dismissPlaybackView];
  421. [keychainCoordinator validatePasscodeWithCompletion:completion];
  422. } else {
  423. completion();
  424. }
  425. }
  426. #pragma mark - download handling
  427. - (void)downloadMovieFromURL:(NSURL *)url
  428. fileNameOfMedia:(NSString *)fileName
  429. {
  430. [[VLCDownloadViewController sharedInstance] addURLToDownloadList:url fileNameOfMedia:fileName];
  431. [[VLCSidebarController sharedInstance] selectRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:1]
  432. scrollPosition:UITableViewScrollPositionNone];
  433. }
  434. #pragma mark - playback
  435. - (void)playWithURL:(NSURL *)url successCallback:(NSURL *)successCallback errorCallback:(NSURL *)errorCallback
  436. {
  437. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  438. vpc.fullscreenSessionRequested = YES;
  439. vpc.successCallback = successCallback;
  440. vpc.errorCallback = errorCallback;
  441. VLCMediaList *mediaList = [[VLCMediaList alloc] initWithArray:@[[VLCMedia mediaWithURL:url]]];
  442. [vpc playMediaList:mediaList firstIndex:0 subtitlesFilePath:nil];
  443. }
  444. #pragma mark - watch stuff
  445. - (void)application:(UIApplication *)application
  446. handleWatchKitExtensionRequest:(NSDictionary *)userInfo
  447. reply:(void (^)(NSDictionary *))reply
  448. {
  449. if ([VLCWatchCommunication isSupported]) {
  450. [self.watchCommunication session:[WCSession defaultSession] didReceiveMessage:userInfo replyHandler:reply];
  451. }
  452. }
  453. @end