summaryrefslogtreecommitdiff
path: root/SmartDeviceLink/private/SDLLockScreenStatusManager.m
blob: 402b8739b4fe566357411e8cffe4462cbddea233 (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
//
//  SDLLockScreenManager.m
//  SmartDeviceLink
//

#import "SDLLockScreenStatusManager.h"

#import "SDLLogMacros.h"
#import "SDLNotificationConstants.h"
#import "SDLNotificationDispatcher.h"
#import "SDLOnDriverDistraction.h"
#import "SDLOnHMIStatus.h"
#import "SDLLockScreenStatusInfo.h"
#import "SDLRPCNotificationNotification.h"

NS_ASSUME_NONNULL_BEGIN

@interface SDLLockScreenStatusManager ()

@property (assign, nonatomic) BOOL haveDriverDistractionStatus;
@property (weak, nonatomic) SDLNotificationDispatcher *notificationDispatcher;

@end


@implementation SDLLockScreenStatusManager

#pragma mark - Lifecycle

- (instancetype)initWithNotificationDispatcher:(SDLNotificationDispatcher *)dispatcher {
    self = [super init];
    if (!self) { return nil; }

    _userSelected = NO;
    _driverDistracted = NO;
    _haveDriverDistractionStatus = NO;

    return self;
}

#pragma mark - Component Updates

- (void)updateHMIStatus:(SDLOnHMIStatus *)hmiStatus {
    self.hmiLevel = hmiStatus.hmiLevel;
    [self sdl_postLockScreenStatus:self.lockScreenStatusNotification];
}

- (void)updateDriverDistractionState:(SDLOnDriverDistraction *)driverDistraction {
    self.driverDistracted = [driverDistraction.state isEqualToEnum:SDLDriverDistractionStateOn];
    [self sdl_postLockScreenStatus:self.lockScreenStatusNotification];
}

#pragma mark - Getters / Setters
#pragma mark Custom setters

- (void)setDriverDistracted:(BOOL)driverDistracted {
    _driverDistracted = driverDistracted;
    _haveDriverDistractionStatus = YES;
}

- (void)setHmiLevel:(nullable SDLHMILevel)hmiLevel {
    if (_hmiLevel != hmiLevel) {
        _hmiLevel = hmiLevel;
    }

    if ([hmiLevel isEqualToEnum:SDLHMILevelFull] || [hmiLevel isEqualToEnum:SDLHMILevelLimited]) {
        self.userSelected = YES;
    } else if ([hmiLevel isEqualToEnum:SDLHMILevelNone]) {
        self.userSelected = NO;
    }
}


#pragma mark Custom Getters

- (SDLLockScreenStatusInfo *)lockScreenStatusNotification {
    SDLLockScreenStatusInfo *notification = [[SDLLockScreenStatusInfo alloc] initWithDriverDistractionStatus:@(self.driverDistracted) userSelected:@(self.userSelected) lockScreenStatus:self.lockScreenStatus hmiLevel:self.hmiLevel];
    return notification;
}

- (SDLLockScreenStatus)lockScreenStatus {
    if (self.hmiLevel == nil || [self.hmiLevel isEqualToEnum:SDLHMILevelNone]) {
        // App is not active on the car
        return SDLLockScreenStatusOff;
    } else if ([self.hmiLevel isEqualToEnum:SDLHMILevelBackground]) {
        // App is in the background on the car
        if (self.userSelected) {
            // It was user selected
            if (self.haveDriverDistractionStatus && !self.driverDistracted) {
                // We have the distraction status, and the driver is not distracted
                return SDLLockScreenStatusOptional;
            } else {
                // We don't have the distraction status, and/or the driver is distracted
                return SDLLockScreenStatusRequired;
            }
        } else {
            return SDLLockScreenStatusOff;
        }
    } else if ([self.hmiLevel isEqualToEnum:SDLHMILevelFull] || [self.hmiLevel isEqualToEnum:SDLHMILevelLimited]) {
        // App is in the foreground on the car in some manner
        if (self.haveDriverDistractionStatus && !self.driverDistracted) {
            // We have the distraction status, and the driver is not distracted
            return SDLLockScreenStatusOptional;
        } else {
            // We don't have the distraction status, and/or the driver is distracted
            return SDLLockScreenStatusRequired;
        }
    } else {
        // This shouldn't be possible.
        return SDLLockScreenStatusOff;
    }
}

#pragma mark - Utilities

- (void)sdl_postLockScreenStatus:(SDLLockScreenStatusInfo *)statusNotification {
    SDLLogD(@"Lock screen status changed: %@", statusNotification);

    [self.notificationDispatcher postNotificationName:SDLDidChangeLockScreenStatusNotification infoObject:statusNotification];
}

@end

NS_ASSUME_NONNULL_END