aboutsummaryrefslogtreecommitdiffstats
path: root/iPhone/CordovaLib/Classes/CDVSplashScreen.m
blob: cba1b53f5a0292519faeaf7f0461b9123ebdc772 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
 */

#import "CDVSplashScreen.h"

#define kSplashScreenStateShow 0
#define kSplashScreenStateHide 1

#define kSplashScreenDurationDefault 0.25f

@implementation CDVSplashScreen

- (void)pluginInitialize
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pageDidLoad) name:CDVPageDidLoadNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onOrientationWillChange:) name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];

    [self show:nil];
}

- (void)show:(CDVInvokedUrlCommand*)command
{
    [self updateSplashScreenWithState:kSplashScreenStateShow];
}

- (void)hide:(CDVInvokedUrlCommand*)command
{
    [self updateSplashScreenWithState:kSplashScreenStateHide];
}

- (void)pageDidLoad
{
    id autoHideSplashScreenValue = [self.commandDelegate.settings objectForKey:@"AutoHideSplashScreen"];

    // if value is missing, default to yes
    if ((autoHideSplashScreenValue == nil) || [autoHideSplashScreenValue boolValue]) {
        [self hide:nil];
    }
}

- (void)onOrientationWillChange:(NSNotification*)notification
{
    if (_imageView != nil) {
        UIInterfaceOrientation orientation = [notification.userInfo[UIApplicationStatusBarOrientationUserInfoKey] intValue];
        [self updateSplashImageForOrientation:orientation];
    }
}

- (void)createViews
{
    /*
     * The Activity View is the top spinning throbber in the status/battery bar. We init it with the default Grey Style.
     *
     *     whiteLarge = UIActivityIndicatorViewStyleWhiteLarge
     *     white      = UIActivityIndicatorViewStyleWhite
     *     gray       = UIActivityIndicatorViewStyleGray
     *
     */
    NSString* topActivityIndicator = [self.commandDelegate.settings objectForKey:@"TopActivityIndicator"];
    UIActivityIndicatorViewStyle topActivityIndicatorStyle = UIActivityIndicatorViewStyleGray;

    if ([topActivityIndicator isEqualToString:@"whiteLarge"]) {
        topActivityIndicatorStyle = UIActivityIndicatorViewStyleWhiteLarge;
    } else if ([topActivityIndicator isEqualToString:@"white"]) {
        topActivityIndicatorStyle = UIActivityIndicatorViewStyleWhite;
    } else if ([topActivityIndicator isEqualToString:@"gray"]) {
        topActivityIndicatorStyle = UIActivityIndicatorViewStyleGray;
    }

    _activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:topActivityIndicatorStyle];
    _activityView.tag = 2;
    _activityView.center = self.viewController.view.center;
    [_activityView startAnimating];

    _imageView = [[UIImageView alloc] init];
    [self.viewController.view addSubview:_imageView];
    [self.viewController.view.superview addSubview:_activityView];
    [self.viewController.view.superview layoutSubviews];
}

- (void)updateSplashImageForOrientation:(UIInterfaceOrientation)orientation
{
    // IPHONE (default)
    NSString* imageName = @"Default";

    if (CDV_IsIPhone5()) {
        imageName = [imageName stringByAppendingString:@"-568h"];
    } else if (CDV_IsIPad()) {
        // set default to portrait upside down
        imageName = @"Default-Portrait"; // @"Default-PortraitUpsideDown.png";

        if (orientation == UIInterfaceOrientationLandscapeLeft) {
            imageName = @"Default-Landscape.png"; // @"Default-LandscapeLeft.png";
        } else if (orientation == UIInterfaceOrientationLandscapeRight) {
            imageName = @"Default-Landscape.png"; // @"Default-LandscapeRight.png";
        }
    }

    _imageView.image = [UIImage imageNamed:imageName];
    _imageView.frame = CGRectMake(0, 0, _imageView.image.size.width, _imageView.image.size.height);
}

- (void)updateSplashScreenWithState:(int)state
{
    float toAlpha = state == kSplashScreenStateShow ? 1.0f : 0.0f;
    BOOL hidden = state == kSplashScreenStateShow ? NO : YES;

    id fadeSplashScreenValue = [self.commandDelegate.settings objectForKey:@"FadeSplashScreen"];
    id fadeSplashScreenDuration = [self.commandDelegate.settings objectForKey:@"FadeSplashScreenDuration"];

    float fadeDuration = fadeSplashScreenDuration == nil ? kSplashScreenDurationDefault : [fadeSplashScreenDuration floatValue];

    if ((fadeSplashScreenValue == nil) || ![fadeSplashScreenValue boolValue]) {
        fadeDuration = 0;
    }
    if (hidden && (_imageView == nil)) {
        return;
    } else if (_imageView == nil) {
        [self createViews];
        fadeDuration = 0;
    }

    if (!hidden) {
        [self updateSplashImageForOrientation:self.viewController.interfaceOrientation];
    }

    if (fadeDuration == 0) {
        [_imageView setHidden:hidden];
        [_activityView setHidden:hidden];
    } else {
        if (state == kSplashScreenStateShow) {
            // reset states
            [_imageView setHidden:NO];
            [_activityView setHidden:NO];
            [_imageView setAlpha:0.0f];
            [_activityView setAlpha:0.0f];
        }

        [UIView transitionWithView:self.viewController.view
                          duration:fadeDuration
                           options:UIViewAnimationOptionTransitionNone
                        animations:^(void) {
                [_imageView setAlpha:toAlpha];
                [_activityView setAlpha:toAlpha];
            }
                        completion:^(BOOL finished) {
                if (state == kSplashScreenStateHide) {
                    // Clean-up resources.
                    [_imageView removeFromSuperview];
                    [_activityView removeFromSuperview];
                    _imageView = nil;
                    _activityView = nil;
                }
            }];
    }
}

@end