VLCAppDelegate.m 13.1 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * VLCAppDelegate.m
 * VLC for iOS
 *****************************************************************************
5
 * Copyright (c) 2013-2019 VideoLAN. All rights reserved.
6 7 8 9 10 11 12
 * $Id$
 *
 * Authors: Felix Paul Kühne <fkuehne # videolan.org>
 *          Gleb Pinigin <gpinigin # gmail.com>
 *          Jean-Romain Prévost <jr # 3on.fr>
 *          Luis Fernandes <zipleen # gmail.com>
 *          Carola Nitz <nitz.carola # googlemail.com>
13
 *          Tamas Timar <ttimar.vlc # gmail.com>
Tobias's avatar
Tobias committed
14
 *          Tobias Conradi <videolan # tobias-conradi.de>
15
 *          Soomin Lee <TheHungryBu # gmail.com>
16 17 18
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/
Felix Paul Kühne's avatar
Felix Paul Kühne committed
19 20

#import "VLCAppDelegate.h"
21
#import "VLCMediaFileDiscoverer.h"
22
#import "NSString+SupportedMedia.h"
Felix Paul Kühne's avatar
Felix Paul Kühne committed
23
#import "UIDevice+VLC.h"
24
#import "VLCHTTPUploaderController.h"
25
#import "VLCPlaybackController.h"
26
#import "VLCPlaybackController+MediaLibrary.h"
Tobias's avatar
Tobias committed
27
#import <MediaPlayer/MediaPlayer.h>
Felix Paul Kühne's avatar
Felix Paul Kühne committed
28
#import <HockeySDK/HockeySDK.h>
29
#import "VLCActivityManager.h"
30
#import "VLCDropboxConstants.h"
31
#import "VLCPlaybackNavigationController.h"
32
#import "PAPasscodeViewController.h"
33
#import "VLC-Swift.h"
34 35
#import <OneDriveSDK.h>
#import "VLCOneDriveConstants.h"
36

Felix Paul Kühne's avatar
Felix Paul Kühne committed
37 38
#define BETA_DISTRIBUTION 1

39
@interface VLCAppDelegate ()
40
{
Carola Nitz's avatar
Carola Nitz committed
41
    BOOL _isComingFromHandoff;
42
    VLCKeychainCoordinator *_keychainCoordinator;
43
    AppCoordinator *appCoordinator;
44
    UITabBarController *rootViewController;
Gleb Pinigin's avatar
Gleb Pinigin committed
45
}
46 47

@end
Felix Paul Kühne's avatar
Felix Paul Kühne committed
48 49 50

@implementation VLCAppDelegate

51 52 53 54
+ (void)initialize
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

55 56
    NSDictionary *appDefaults = @{kVLCSettingPasscodeAllowFaceID : @(1),
                                  kVLCSettingPasscodeAllowTouchID : @(1),
57 58 59
                                  kVLCSettingContinueAudioInBackgroundKey : @(YES),
                                  kVLCSettingStretchAudio : @(NO),
                                  kVLCSettingTextEncoding : kVLCSettingTextEncodingDefaultValue,
60
                                  kVLCSettingSkipLoopFilter : kVLCSettingSkipLoopFilterNonRef,
61 62 63 64 65
                                  kVLCSettingSubtitlesFont : kVLCSettingSubtitlesFontDefaultValue,
                                  kVLCSettingSubtitlesFontColor : kVLCSettingSubtitlesFontColorDefaultValue,
                                  kVLCSettingSubtitlesFontSize : kVLCSettingSubtitlesFontSizeDefaultValue,
                                  kVLCSettingSubtitlesBoldFont: kVLCSettingSubtitlesBoldFontDefaultValue,
                                  kVLCSettingDeinterlace : kVLCSettingDeinterlaceDefaultValue,
66
                                  kVLCSettingHardwareDecoding : kVLCSettingHardwareDecodingDefault,
67
                                  kVLCSettingNetworkCaching : kVLCSettingNetworkCachingDefaultValue,
68 69 70 71 72 73
                                  kVLCSettingVolumeGesture : @(YES),
                                  kVLCSettingPlayPauseGesture : @(YES),
                                  kVLCSettingBrightnessGesture : @(YES),
                                  kVLCSettingSeekGesture : @(YES),
                                  kVLCSettingCloseGesture : @(YES),
                                  kVLCSettingVariableJumpDuration : @(NO),
74
                                  kVLCSettingVideoFullscreenPlayback : @(YES),
75
                                  kVLCSettingContinuePlayback : @(1),
76
                                  kVLCSettingContinueAudioPlayback : @(1),
77 78 79 80 81
                                  kVLCSettingFTPTextEncoding : kVLCSettingFTPTextEncodingDefaultValue,
                                  kVLCSettingWiFiSharingIPv6 : kVLCSettingWiFiSharingIPv6DefaultValue,
                                  kVLCSettingEqualizerProfile : kVLCSettingEqualizerProfileDefaultValue,
                                  kVLCSettingPlaybackForwardSkipLength : kVLCSettingPlaybackForwardSkipLengthDefaultValue,
                                  kVLCSettingPlaybackBackwardSkipLength : kVLCSettingPlaybackBackwardSkipLengthDefaultValue,
82 83
                                  kVLCSettingOpenAppForPlayback : kVLCSettingOpenAppForPlaybackDefaultValue,
                                  kVLCAutomaticallyPlayNextItem : @(YES)};
84 85 86
    [defaults registerDefaults:appDefaults];
}

87 88
- (void)setup
{
89
    void (^setupAppCoordinator)(void) = ^{
90
        self->appCoordinator = [[AppCoordinator alloc] initWithTabBarController:self->rootViewController];
91 92
        [self->appCoordinator start];
    };
93
    [self validatePasscodeIfNeededWithCompletion:setupAppCoordinator];
94 95 96 97 98 99

    BOOL spotlightEnabled = ![VLCKeychainCoordinator passcodeLockEnabled];
    [[MLMediaLibrary sharedMediaLibrary] setSpotlightIndexingEnabled:spotlightEnabled];
    [[MLMediaLibrary sharedMediaLibrary] applicationWillStart];
}

Felix Paul Kühne's avatar
Felix Paul Kühne committed
100 101
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
Felix Paul Kühne's avatar
Felix Paul Kühne committed
102
    BITHockeyManager *hockeyManager = [BITHockeyManager sharedHockeyManager];
103 104 105
    [hockeyManager configureWithBetaIdentifier:@"0114ca8e265244ce588d2ebd035c3577"
                                liveIdentifier:@"c95f4227dff96c61f8b3a46a25edc584"
                                      delegate:nil];
106
    [hockeyManager startManager];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
107

108 109 110
    // Configure Dropbox
    [DBClientsManager setupWithAppKey:kVLCDropboxAppKey];

111 112 113
    // Configure OneDrive
    [ODClient setMicrosoftAccountAppId:kVLCOneDriveClientID scopes:@[@"onedrive.readwrite", @"offline_access"]];

114
    [VLCApperanceManager setupAppearanceWithTheme:PresentationTheme.current];
115

Felix Paul Kühne's avatar
Felix Paul Kühne committed
116
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
117
    rootViewController = [UITabBarController new];
118 119
    self.window.rootViewController = rootViewController;
    [self.window makeKeyAndVisible];
120
    [self setup];
121

122
    /* add our static shortcut items the dynamic way to ease l10n and dynamic elements to be introduced later */
123
    if (@available(iOS 9, *)) {
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
        if (application.shortcutItems == nil || application.shortcutItems.count < 4) {
            UIApplicationShortcutItem *localLibraryItem = [[UIApplicationShortcutItem alloc] initWithType:kVLCApplicationShortcutLocalLibrary
                                                                                           localizedTitle:NSLocalizedString(@"SECTION_HEADER_LIBRARY",nil)
                                                                                        localizedSubtitle:nil
                                                                                                     icon:[UIApplicationShortcutIcon iconWithTemplateImageName:@"AllFiles"]
                                                                                                 userInfo:nil];
            UIApplicationShortcutItem *localServerItem = [[UIApplicationShortcutItem alloc] initWithType:kVLCApplicationShortcutLocalServers
                                                                                           localizedTitle:NSLocalizedString(@"LOCAL_NETWORK",nil)
                                                                                        localizedSubtitle:nil
                                                                                                     icon:[UIApplicationShortcutIcon iconWithTemplateImageName:@"Local"]
                                                                                                 userInfo:nil];
            UIApplicationShortcutItem *openNetworkStreamItem = [[UIApplicationShortcutItem alloc] initWithType:kVLCApplicationShortcutOpenNetworkStream
                                                                                           localizedTitle:NSLocalizedString(@"OPEN_NETWORK",nil)
                                                                                        localizedSubtitle:nil
                                                                                                     icon:[UIApplicationShortcutIcon iconWithTemplateImageName:@"OpenNetStream"]
                                                                                                 userInfo:nil];
            UIApplicationShortcutItem *cloudsItem = [[UIApplicationShortcutItem alloc] initWithType:kVLCApplicationShortcutClouds
                                                                                           localizedTitle:NSLocalizedString(@"CLOUD_SERVICES",nil)
                                                                                        localizedSubtitle:nil
                                                                                                     icon:[UIApplicationShortcutIcon iconWithTemplateImageName:@"iCloudIcon"]
                                                                                                 userInfo:nil];
            application.shortcutItems = @[localLibraryItem, localServerItem, openNetworkStreamItem, cloudsItem];
        }
    }

149 150
    return YES;
}
151

Carola Nitz's avatar
Carola Nitz committed
152 153
#pragma mark - Handoff

154 155
- (BOOL)application:(UIApplication *)application willContinueUserActivityWithType:(NSString *)userActivityType
{
156 157 158
    if ([userActivityType isEqualToString:kVLCUserActivityLibraryMode] ||
        [userActivityType isEqualToString:kVLCUserActivityPlaying] ||
        [userActivityType isEqualToString:kVLCUserActivityLibrarySelection])
Carola Nitz's avatar
Carola Nitz committed
159
        return YES;
160

Carola Nitz's avatar
Carola Nitz committed
161 162 163
    return NO;
}

164 165
- (BOOL)application:(UIApplication *)application
continueUserActivity:(NSUserActivity *)userActivity
166
 restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> *))restorationHandler
167 168
{
    NSString *userActivityType = userActivity.activityType;
169 170 171
    NSDictionary *dict = userActivity.userInfo;
    if([userActivityType isEqualToString:kVLCUserActivityLibraryMode] ||
       [userActivityType isEqualToString:kVLCUserActivityLibrarySelection]) {
172
        //TODO: Add restoreUserActivityState to the mediaviewcontroller
Carola Nitz's avatar
Carola Nitz committed
173
        _isComingFromHandoff = YES;
Carola Nitz's avatar
Carola Nitz committed
174
        return YES;
175
    } else {
Tobias's avatar
Tobias committed
176 177 178 179 180 181 182 183 184 185 186
        NSURL *uriRepresentation = nil;
        if ([userActivityType isEqualToString:CSSearchableItemActionType]) {
            uriRepresentation = [NSURL URLWithString:dict[CSSearchableItemActivityIdentifier]];
        } else {
            uriRepresentation = dict[@"playingmedia"];
        }

        if (!uriRepresentation) {
            return NO;
        }

187 188 189 190 191
        NSManagedObject *managedObject = [[MLMediaLibrary sharedMediaLibrary] objectForURIRepresentation:uriRepresentation];
        if (managedObject == nil) {
            APLog(@"%s file not found: %@",__PRETTY_FUNCTION__,userActivity);
            return NO;
        }
192 193 194
        [self validatePasscodeIfNeededWithCompletion:^{
            [[VLCPlaybackController sharedInstance] openMediaLibraryObject:managedObject];
        }];
195
        return YES;
Carola Nitz's avatar
Carola Nitz committed
196 197 198 199
    }
    return NO;
}

200 201 202
- (void)application:(UIApplication *)application
didFailToContinueUserActivityWithType:(NSString *)userActivityType
              error:(NSError *)error
Carola Nitz's avatar
Carola Nitz committed
203 204 205 206 207 208
{
    if (error.code != NSUserCancelledError){
        //TODO: present alert
    }
}

Carola Nitz's avatar
Carola Nitz committed
209
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
210
{
211 212 213 214 215
    for (id<VLCURLHandler> handler in URLHandlers.handlers) {
        if ([handler canHandleOpenWithUrl:url options:options]) {
            if ([handler performOpenWithUrl:url options:options]) {
                break;
            }
216
        }
217
    }
218 219 220
    return NO;
}

221 222 223 224 225
- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [[MLMediaLibrary sharedMediaLibrary] applicationWillStart];
}

Felix Paul Kühne's avatar
Felix Paul Kühne committed
226 227
- (void)applicationWillResignActive:(UIApplication *)application
{
228 229 230 231 232 233 234 235
    //Touch ID is shown 
    if ([_window.rootViewController.presentedViewController isKindOfClass:[UINavigationController class]]){
        UINavigationController *navCon = (UINavigationController *)_window.rootViewController.presentedViewController;
        if ([navCon.topViewController isKindOfClass:[PAPasscodeViewController class]]){
            return;
        }
    }
    [self validatePasscodeIfNeededWithCompletion:^{
236
        //TODO: handle updating the videoview and
237
        if ([VLCPlaybackController sharedInstance].isPlaying){
238
            //TODO: push playback
239 240
        }
    }];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
241 242 243 244 245
    [[MLMediaLibrary sharedMediaLibrary] applicationWillExit];
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
246
    if (!_isComingFromHandoff) {
247
        [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
248 249
      //  [[VLCMediaFileDiscoverer sharedInstance] updateMediaList];
        [[VLCPlaybackController sharedInstance] recoverDisplayedMetadata];
Carola Nitz's avatar
Carola Nitz committed
250 251
    } else if(_isComingFromHandoff) {
        _isComingFromHandoff = NO;
252
    }
Felix Paul Kühne's avatar
Felix Paul Kühne committed
253 254
}

255 256
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
257
    //TODO: shortcutItem should be implemented
258 259
}

260
#pragma mark - pass code validation
261 262 263 264 265 266 267
- (VLCKeychainCoordinator *)keychainCoordinator
{
    if (!_keychainCoordinator) {
        _keychainCoordinator = [[VLCKeychainCoordinator alloc] init];
    }
    return _keychainCoordinator;
}
268

269
- (void)validatePasscodeIfNeededWithCompletion:(void(^)(void))completion
270
{
271
    if ([VLCKeychainCoordinator passcodeLockEnabled]) {
272
        //TODO: Dimiss playback
273
        [self.keychainCoordinator validatePasscodeWithCompletion:completion];
274
    } else {
275
        completion();
276
    }
277 278
}

Felix Paul Kühne's avatar
Felix Paul Kühne committed
279
@end