summaryrefslogtreecommitdiff
path: root/SmartDeviceLink/SDLLockScreenViewController.m
blob: 291913aaab482389b0f13e45c3a7ffe2d808e02d (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//
//  SDLLockScreenViewController.m
//  SmartDeviceLink-iOS
//
//  Created by Joel Fischer on 10/7/15.
//  Copyright © 2015 smartdevicelink. All rights reserved.
//

#import "SDLLockScreenViewController.h"

#import "NSBundle+SDLBundle.h"
#import "SDLGlobals.h"

NS_ASSUME_NONNULL_BEGIN

@interface SDLLockScreenViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *primaryAppIconImageView;
@property (weak, nonatomic) IBOutlet UIImageView *primaryVehicleIconImageView;
@property (weak, nonatomic) IBOutlet UIImageView *sdlIconImageView;
@property (weak, nonatomic) IBOutlet UIImageView *backupImageView;

@property (weak, nonatomic) IBOutlet UILabel *lockedLabel;
@property (weak, nonatomic) IBOutlet UIImageView *arrowUpImageView;
@property (weak, nonatomic) IBOutlet UIImageView *arrowDownImageView;

@end


@implementation SDLLockScreenViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self sdl_layoutViews];
}

- (BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    BOOL useWhiteIcon = [self.class sdl_shouldUseWhiteForegroundForBackgroundColor:self.backgroundColor];

    return useWhiteIcon ? UIStatusBarStyleLightContent : UIStatusBarStyleDefault;
}


#pragma mark - Setters

- (void)setAppIcon:(UIImage *_Nullable)appIcon {
    _appIcon = appIcon;

    [self sdl_layoutViews];
}

- (void)setVehicleIcon:(UIImage *_Nullable)vehicleIcon {
    _vehicleIcon = vehicleIcon;

    [self sdl_layoutViews];
}

- (void)setBackgroundColor:(UIColor *_Nullable)backgroundColor {
    _backgroundColor = backgroundColor;

    [self sdl_layoutViews];
}


#pragma mark - Layout

- (void)sdl_layoutViews {
    dispatch_async(dispatch_get_main_queue(), ^{
        UIColor *iconColor = [self.class sdl_accentColorBasedOnColor:self.backgroundColor];

        self.sdlIconImageView.image = [self.class sdl_imageWithName:@"sdl_logo_black"];
        self.sdlIconImageView.tintColor = iconColor;

        self.arrowUpImageView.image = [self.class sdl_imageWithName:@"lock_arrow_up_black"];
        self.arrowUpImageView.tintColor = iconColor;

        self.arrowDownImageView.image = [self.class sdl_imageWithName:@"lock_arrow_down_black"];
        self.arrowDownImageView.tintColor = iconColor;

        self.lockedLabel.textColor = iconColor;

        self.view.backgroundColor = self.backgroundColor;

        if (self.vehicleIcon != nil && self.appIcon != nil) {
            [self sdl_setVehicleAndAppIconsLayout];
        } else if (self.vehicleIcon != nil) {
            [self sdl_setVehicleIconOnlyLayout];
        } else if (self.appIcon != nil) {
            [self sdl_setAppIconOnlyLayout];
        } else {
            [self sdl_setNoIconsLayout];
        }

        // HAX: The autolayout doesn't scale for 4s, so hide a view so it doesn't look like garbage.
        if (CGRectGetHeight([UIScreen mainScreen].bounds) == 480) {
            self.sdlIconImageView.hidden = YES;
        } else {
            self.sdlIconImageView.hidden = NO;
        }

        [self.view layoutIfNeeded];
    });
}

- (void)sdl_setVehicleAndAppIconsLayout {
    self.primaryAppIconImageView.image = self.appIcon;
    self.primaryVehicleIconImageView.image = self.vehicleIcon;

    self.backupImageView.image = nil;
    self.backupImageView.tintColor = nil;
    
    self.arrowUpImageView.alpha = 1.0;
    self.arrowDownImageView.alpha = 1.0;

    self.sdlIconImageView.alpha = 1.0;
}

- (void)sdl_setAppIconOnlyLayout {
    self.primaryAppIconImageView.image = nil;
    self.primaryVehicleIconImageView.image = nil;

    self.backupImageView.image = self.appIcon;
    self.backupImageView.tintColor = nil;
    
    self.arrowUpImageView.alpha = 0.0;
    self.arrowDownImageView.alpha = 0.0;

    self.sdlIconImageView.alpha = 1.0;
}

- (void)sdl_setVehicleIconOnlyLayout {
    self.primaryAppIconImageView.image = nil;
    self.primaryVehicleIconImageView.image = nil;

    self.backupImageView.image = self.vehicleIcon;
    self.backupImageView.tintColor = nil;
    
    self.arrowUpImageView.alpha = 0.0;
    self.arrowDownImageView.alpha = 0.0;

    self.sdlIconImageView.alpha = 1.0;
}

- (void)sdl_setNoIconsLayout {
    self.primaryAppIconImageView.image = nil;
    self.primaryVehicleIconImageView.image = nil;

    self.backupImageView.image = self.sdlIconImageView.image;
    self.backupImageView.tintColor = [self.class sdl_accentColorBasedOnColor:self.backgroundColor];

    self.arrowUpImageView.alpha = 0.0;
    self.arrowDownImageView.alpha = 0.0;

    self.sdlIconImageView.alpha = 0.0;
}


#pragma mark - Private Image

+ (UIColor *)sdl_accentColorBasedOnColor:(UIColor *)backgroundColor {
    return [self sdl_shouldUseWhiteForegroundForBackgroundColor:backgroundColor] ? [UIColor whiteColor] : [UIColor blackColor];
}

+ (UIImage *)sdl_imageWithName:(NSString *)name {
    UIImage* image = [UIImage imageNamed:name inBundle:[NSBundle sdlBundle] compatibleWithTraitCollection:nil];
    return [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
}

+ (BOOL)sdl_shouldUseWhiteForegroundForBackgroundColor:(UIColor *)backgroundColor {
    CGFloat red, green, blue;

    [backgroundColor getRed:&red green:&green blue:&blue alpha:nil];

    // http://stackoverflow.com/a/3943023
    red = (red <= 0.3928) ? (red / 12.92) : pow(((red + 0.055) / 1.055), 2.4);
    green = (green <= 0.3928) ? (green / 12.92) : pow(((green + 0.055) / 1.055), 2.4);
    blue = (blue <= 0.3928) ? (blue / 12.92) : pow(((blue + 0.055) / 1.055), 2.4);
    CGFloat luminescence = 0.2126 * red + 0.7152 * green + 0.0722 * blue;

    return (luminescence <= 0.179);
}

@end

NS_ASSUME_NONNULL_END