Touchgui/plugins/org.apache.cordova.splashscreen/src/ios/CDVSplashScreen.m

changeset 0
e8ccd40d0ef6
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Touchgui/plugins/org.apache.cordova.splashscreen/src/ios/CDVSplashScreen.m	Thu Jun 04 14:50:33 2015 +0200
     1.3 @@ -0,0 +1,307 @@
     1.4 +/*
     1.5 + Licensed to the Apache Software Foundation (ASF) under one
     1.6 + or more contributor license agreements.  See the NOTICE file
     1.7 + distributed with this work for additional information
     1.8 + regarding copyright ownership.  The ASF licenses this file
     1.9 + to you under the Apache License, Version 2.0 (the
    1.10 + "License"); you may not use this file except in compliance
    1.11 + with the License.  You may obtain a copy of the License at
    1.12 +
    1.13 + http://www.apache.org/licenses/LICENSE-2.0
    1.14 +
    1.15 + Unless required by applicable law or agreed to in writing,
    1.16 + software distributed under the License is distributed on an
    1.17 + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    1.18 + KIND, either express or implied.  See the License for the
    1.19 + specific language governing permissions and limitations
    1.20 + under the License.
    1.21 + */
    1.22 +
    1.23 +#import "CDVSplashScreen.h"
    1.24 +#import <Cordova/CDVViewController.h>
    1.25 +#import <Cordova/CDVScreenOrientationDelegate.h>
    1.26 +
    1.27 +#define kSplashScreenDurationDefault 0.25f
    1.28 +
    1.29 +
    1.30 +@implementation CDVSplashScreen
    1.31 +
    1.32 +- (void)pluginInitialize
    1.33 +{
    1.34 +    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pageDidLoad) name:CDVPageDidLoadNotification object:self.webView];
    1.35 +
    1.36 +    [self setVisible:YES];
    1.37 +}
    1.38 +
    1.39 +- (void)show:(CDVInvokedUrlCommand*)command
    1.40 +{
    1.41 +    [self setVisible:YES];
    1.42 +}
    1.43 +
    1.44 +- (void)hide:(CDVInvokedUrlCommand*)command
    1.45 +{
    1.46 +    [self setVisible:NO];
    1.47 +}
    1.48 +
    1.49 +- (void)pageDidLoad
    1.50 +{
    1.51 +    id autoHideSplashScreenValue = [self.commandDelegate.settings objectForKey:[@"AutoHideSplashScreen" lowercaseString]];
    1.52 +
    1.53 +    // if value is missing, default to yes
    1.54 +    if ((autoHideSplashScreenValue == nil) || [autoHideSplashScreenValue boolValue]) {
    1.55 +        [self setVisible:NO];
    1.56 +    }
    1.57 +}
    1.58 +
    1.59 +- (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context
    1.60 +{
    1.61 +    [self updateImage];
    1.62 +}
    1.63 +
    1.64 +- (void)createViews
    1.65 +{
    1.66 +    /*
    1.67 +     * The Activity View is the top spinning throbber in the status/battery bar. We init it with the default Grey Style.
    1.68 +     *
    1.69 +     *     whiteLarge = UIActivityIndicatorViewStyleWhiteLarge
    1.70 +     *     white      = UIActivityIndicatorViewStyleWhite
    1.71 +     *     gray       = UIActivityIndicatorViewStyleGray
    1.72 +     *
    1.73 +     */
    1.74 +    NSString* topActivityIndicator = [self.commandDelegate.settings objectForKey:[@"TopActivityIndicator" lowercaseString]];
    1.75 +    UIActivityIndicatorViewStyle topActivityIndicatorStyle = UIActivityIndicatorViewStyleGray;
    1.76 +
    1.77 +    if ([topActivityIndicator isEqualToString:@"whiteLarge"]) {
    1.78 +        topActivityIndicatorStyle = UIActivityIndicatorViewStyleWhiteLarge;
    1.79 +    } else if ([topActivityIndicator isEqualToString:@"white"]) {
    1.80 +        topActivityIndicatorStyle = UIActivityIndicatorViewStyleWhite;
    1.81 +    } else if ([topActivityIndicator isEqualToString:@"gray"]) {
    1.82 +        topActivityIndicatorStyle = UIActivityIndicatorViewStyleGray;
    1.83 +    }
    1.84 +
    1.85 +    UIView* parentView = self.viewController.view;
    1.86 +    parentView.userInteractionEnabled = NO;  // disable user interaction while splashscreen is shown
    1.87 +    _activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:topActivityIndicatorStyle];
    1.88 +    _activityView.center = CGPointMake(parentView.bounds.size.width / 2, parentView.bounds.size.height / 2);
    1.89 +    _activityView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin
    1.90 +        | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin;
    1.91 +    [_activityView startAnimating];
    1.92 +
    1.93 +    // Set the frame & image later.
    1.94 +    _imageView = [[UIImageView alloc] init];
    1.95 +    [parentView addSubview:_imageView];
    1.96 +
    1.97 +    id showSplashScreenSpinnerValue = [self.commandDelegate.settings objectForKey:[@"ShowSplashScreenSpinner" lowercaseString]];
    1.98 +    // backwards compatibility - if key is missing, default to true
    1.99 +    if ((showSplashScreenSpinnerValue == nil) || [showSplashScreenSpinnerValue boolValue]) {
   1.100 +        [parentView addSubview:_activityView];
   1.101 +    }
   1.102 +
   1.103 +    // Frame is required when launching in portrait mode.
   1.104 +    // Bounds for landscape since it captures the rotation.
   1.105 +    [parentView addObserver:self forKeyPath:@"frame" options:0 context:nil];
   1.106 +    [parentView addObserver:self forKeyPath:@"bounds" options:0 context:nil];
   1.107 +
   1.108 +    [self updateImage];
   1.109 +}
   1.110 +
   1.111 +- (void)destroyViews
   1.112 +{
   1.113 +    [_imageView removeFromSuperview];
   1.114 +    [_activityView removeFromSuperview];
   1.115 +    _imageView = nil;
   1.116 +    _activityView = nil;
   1.117 +    _curImageName = nil;
   1.118 +
   1.119 +    self.viewController.view.userInteractionEnabled = YES;  // re-enable user interaction upon completion
   1.120 +    [self.viewController.view removeObserver:self forKeyPath:@"frame"];
   1.121 +    [self.viewController.view removeObserver:self forKeyPath:@"bounds"];
   1.122 +}
   1.123 +
   1.124 +- (CDV_iOSDevice) getCurrentDevice
   1.125 +{
   1.126 +    CDV_iOSDevice device;
   1.127 +    
   1.128 +    UIScreen* mainScreen = [UIScreen mainScreen];
   1.129 +    CGFloat mainScreenHeight = mainScreen.bounds.size.height;
   1.130 +    CGFloat mainScreenWidth = mainScreen.bounds.size.width;
   1.131 +    
   1.132 +    int limit = MAX(mainScreenHeight,mainScreenWidth);
   1.133 +    
   1.134 +    device.iPad = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
   1.135 +    device.iPhone = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone);
   1.136 +    device.retina = ([mainScreen scale] == 2.0);
   1.137 +    device.iPhone5 = (device.iPhone && limit == 568.0);
   1.138 +    // note these below is not a true device detect, for example if you are on an
   1.139 +    // iPhone 6/6+ but the app is scaled it will prob set iPhone5 as true, but
   1.140 +    // this is appropriate for detecting the runtime screen environment
   1.141 +    device.iPhone6 = (device.iPhone && limit == 667.0);
   1.142 +    device.iPhone6Plus = (device.iPhone && limit == 736.0);
   1.143 +    
   1.144 +    return device;
   1.145 +}
   1.146 +
   1.147 +- (NSString*)getImageName:(UIInterfaceOrientation)currentOrientation delegate:(id<CDVScreenOrientationDelegate>)orientationDelegate device:(CDV_iOSDevice)device
   1.148 +{
   1.149 +    // Use UILaunchImageFile if specified in plist.  Otherwise, use Default.
   1.150 +    NSString* imageName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UILaunchImageFile"];
   1.151 +    
   1.152 +    NSUInteger supportedOrientations = [orientationDelegate supportedInterfaceOrientations];
   1.153 +    
   1.154 +    // Checks to see if the developer has locked the orientation to use only one of Portrait or Landscape
   1.155 +    BOOL supportsLandscape = (supportedOrientations & UIInterfaceOrientationMaskLandscape);
   1.156 +    BOOL supportsPortrait = (supportedOrientations & UIInterfaceOrientationMaskPortrait || supportedOrientations & UIInterfaceOrientationMaskPortraitUpsideDown);
   1.157 +    // this means there are no mixed orientations in there
   1.158 +    BOOL isOrientationLocked = !(supportsPortrait && supportsLandscape);
   1.159 +    
   1.160 +    if (imageName) {
   1.161 +        imageName = [imageName stringByDeletingPathExtension];
   1.162 +    } else {
   1.163 +        imageName = @"Default";
   1.164 +    }
   1.165 +    
   1.166 +    if (device.iPhone5) { // does not support landscape
   1.167 +        imageName = [imageName stringByAppendingString:@"-568h"];
   1.168 +    } else if (device.iPhone6) { // does not support landscape
   1.169 +        imageName = [imageName stringByAppendingString:@"-667h"];
   1.170 +    } else if (device.iPhone6Plus) { // supports landscape
   1.171 +        if (isOrientationLocked) {
   1.172 +            imageName = [imageName stringByAppendingString:(supportsLandscape ? @"-Landscape" : @"")];
   1.173 +        } else {
   1.174 +            switch (currentOrientation) {
   1.175 +                case UIInterfaceOrientationLandscapeLeft:
   1.176 +                case UIInterfaceOrientationLandscapeRight:
   1.177 +                        imageName = [imageName stringByAppendingString:@"-Landscape"];
   1.178 +                    break;
   1.179 +                default:
   1.180 +                    break;
   1.181 +            }
   1.182 +        }
   1.183 +        imageName = [imageName stringByAppendingString:@"-736h"];
   1.184 +
   1.185 +    } else if (device.iPad) { // supports landscape
   1.186 +        if (isOrientationLocked) {
   1.187 +            imageName = [imageName stringByAppendingString:(supportsLandscape ? @"-Landscape" : @"-Portrait")];
   1.188 +        } else {
   1.189 +            switch (currentOrientation) {
   1.190 +                case UIInterfaceOrientationLandscapeLeft:
   1.191 +                case UIInterfaceOrientationLandscapeRight:
   1.192 +                    imageName = [imageName stringByAppendingString:@"-Landscape"];
   1.193 +                    break;
   1.194 +                    
   1.195 +                case UIInterfaceOrientationPortrait:
   1.196 +                case UIInterfaceOrientationPortraitUpsideDown:
   1.197 +                default:
   1.198 +                    imageName = [imageName stringByAppendingString:@"-Portrait"];
   1.199 +                    break;
   1.200 +            }
   1.201 +        }
   1.202 +    }
   1.203 +    
   1.204 +    return imageName;
   1.205 +}
   1.206 +
   1.207 +// Sets the view's frame and image.
   1.208 +- (void)updateImage
   1.209 +{
   1.210 +    NSString* imageName = [self getImageName:self.viewController.interfaceOrientation delegate:(id<CDVScreenOrientationDelegate>)self.viewController device:[self getCurrentDevice]];
   1.211 +
   1.212 +    if (![imageName isEqualToString:_curImageName]) {
   1.213 +        UIImage* img = [UIImage imageNamed:imageName];
   1.214 +        _imageView.image = img;
   1.215 +        _curImageName = imageName;
   1.216 +    }
   1.217 +
   1.218 +    // Check that splash screen's image exists before updating bounds
   1.219 +    if (_imageView.image) {
   1.220 +        [self updateBounds];
   1.221 +    } else {
   1.222 +        NSLog(@"WARNING: The splashscreen image named %@ was not found", imageName);
   1.223 +    }
   1.224 +}
   1.225 +
   1.226 +- (void)updateBounds
   1.227 +{
   1.228 +    UIImage* img = _imageView.image;
   1.229 +    CGRect imgBounds = (img) ? CGRectMake(0, 0, img.size.width, img.size.height) : CGRectZero;
   1.230 +
   1.231 +    CGSize screenSize = [self.viewController.view convertRect:[UIScreen mainScreen].bounds fromView:nil].size;
   1.232 +    UIInterfaceOrientation orientation = self.viewController.interfaceOrientation;
   1.233 +    CGAffineTransform imgTransform = CGAffineTransformIdentity;
   1.234 +
   1.235 +    /* If and only if an iPhone application is landscape-only as per
   1.236 +     * UISupportedInterfaceOrientations, the view controller's orientation is
   1.237 +     * landscape. In this case the image must be rotated in order to appear
   1.238 +     * correctly.
   1.239 +     */
   1.240 +    if (UIInterfaceOrientationIsLandscape(orientation) && !CDV_IsIPad()) {
   1.241 +        imgTransform = CGAffineTransformMakeRotation(M_PI / 2);
   1.242 +        imgBounds.size = CGSizeMake(imgBounds.size.height, imgBounds.size.width);
   1.243 +    }
   1.244 +
   1.245 +    // There's a special case when the image is the size of the screen.
   1.246 +    if (CGSizeEqualToSize(screenSize, imgBounds.size)) {
   1.247 +        CGRect statusFrame = [self.viewController.view convertRect:[UIApplication sharedApplication].statusBarFrame fromView:nil];
   1.248 +        if (!(IsAtLeastiOSVersion(@"7.0"))) {
   1.249 +            imgBounds.origin.y -= statusFrame.size.height;
   1.250 +        }
   1.251 +    } else if (imgBounds.size.width > 0) {
   1.252 +        CGRect viewBounds = self.viewController.view.bounds;
   1.253 +        CGFloat imgAspect = imgBounds.size.width / imgBounds.size.height;
   1.254 +        CGFloat viewAspect = viewBounds.size.width / viewBounds.size.height;
   1.255 +        // This matches the behaviour of the native splash screen.
   1.256 +        CGFloat ratio;
   1.257 +        if (viewAspect > imgAspect) {
   1.258 +            ratio = viewBounds.size.width / imgBounds.size.width;
   1.259 +        } else {
   1.260 +            ratio = viewBounds.size.height / imgBounds.size.height;
   1.261 +        }
   1.262 +        imgBounds.size.height *= ratio;
   1.263 +        imgBounds.size.width *= ratio;
   1.264 +    }
   1.265 +
   1.266 +    _imageView.transform = imgTransform;
   1.267 +    _imageView.frame = imgBounds;
   1.268 +}
   1.269 +
   1.270 +- (void)setVisible:(BOOL)visible
   1.271 +{
   1.272 +    if (visible == _visible) {
   1.273 +        return;
   1.274 +    }
   1.275 +    _visible = visible;
   1.276 +
   1.277 +    id fadeSplashScreenValue = [self.commandDelegate.settings objectForKey:[@"FadeSplashScreen" lowercaseString]];
   1.278 +    id fadeSplashScreenDuration = [self.commandDelegate.settings objectForKey:[@"FadeSplashScreenDuration" lowercaseString]];
   1.279 +
   1.280 +    float fadeDuration = fadeSplashScreenDuration == nil ? kSplashScreenDurationDefault : [fadeSplashScreenDuration floatValue];
   1.281 +
   1.282 +    if ((fadeSplashScreenValue == nil) || ![fadeSplashScreenValue boolValue]) {
   1.283 +        fadeDuration = 0;
   1.284 +    }
   1.285 +
   1.286 +    // Never animate the showing of the splash screen.
   1.287 +    if (visible) {
   1.288 +        if (_imageView == nil) {
   1.289 +            [self createViews];
   1.290 +        }
   1.291 +    } else if (fadeDuration == 0) {
   1.292 +        [self destroyViews];
   1.293 +    } else {
   1.294 +        [UIView transitionWithView:self.viewController.view
   1.295 +                          duration:fadeDuration
   1.296 +                           options:UIViewAnimationOptionTransitionNone
   1.297 +                        animations:^(void) {
   1.298 +                            [_imageView setAlpha:0];
   1.299 +                            [_activityView setAlpha:0];
   1.300 +                        }
   1.301 +                        completion:^(BOOL finished) {
   1.302 +                            if (finished) {
   1.303 +                                [self destroyViews];
   1.304 +                            }
   1.305 +                        }
   1.306 +        ];
   1.307 +    }
   1.308 +}
   1.309 +
   1.310 +@end

mercurial