summaryrefslogtreecommitdiff
path: root/SmartDeviceLink/SDLLockScreenViewController.m
blob: 181cbc1c9072d8f2d14370622070b295711e4ea9 (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
//
//  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"

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;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    BOOL useWhiteIcon = [self.class 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.view.backgroundColor = _backgroundColor;
    [self sdl_layoutViews];
}


#pragma mark - Layout

- (void)sdl_layoutViews {
    BOOL useWhiteIcon = [self.class shouldUseWhiteForegroundForBackgroundColor:self.backgroundColor];

    UIImage *sdlLogo = [self.class sdl_logoImageWithColor:useWhiteIcon];
    self.sdlIconImageView.image = sdlLogo;

    self.arrowUpImageView.image = [self.class sdl_arrowUpImageWithColor:useWhiteIcon];
    self.arrowDownImageView.image = [self.class sdl_arrowDownImageWithColor:useWhiteIcon];

    self.lockedLabel.textColor = useWhiteIcon ? [UIColor whiteColor] : [UIColor blackColor];

    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];
    }

    [self.view layoutIfNeeded];
}

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

    self.backupImageView.image = 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.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.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.arrowUpImageView.alpha = 0.0;
    self.arrowDownImageView.alpha = 0.0;

    self.sdlIconImageView.alpha = 0.0;
}


#pragma mark - Private Image

// TODO: (Joel F.)[2016-08-22] When moved to iOS 7+, use `imageWithRenderingMode:`
+ (UIImage *)sdl_logoImageWithColor:(BOOL)white {
    return [self sdl_imageWithName:[NSString stringWithFormat:@"sdl_logo_%@", white ? @"white" : @"black"]];
}

+ (UIImage *)sdl_arrowUpImageWithColor:(BOOL)white {
    return [self sdl_imageWithName:[NSString stringWithFormat:@"lock_arrow_up_%@", white ? @"white" : @"black"]];
}

+ (UIImage *)sdl_arrowDownImageWithColor:(BOOL)white {
    return [self sdl_imageWithName:[NSString stringWithFormat:@"lock_arrow_down_%@", white ? @"white" : @"black"]];
}

+ (UIImage*)sdl_imageWithName:(NSString*)name {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
    return [UIImage imageNamed:name inBundle:[NSBundle sdlBundle] compatibleWithTraitCollection:nil];
#else
    NSString* bundlePath = [[NSBundle sdlBundle] bundlePath];
    return [UIImage imageNamed:[NSString stringWithFormat:@"%@/%@", bundlePath, name]];
#endif
}

+ (BOOL)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