summaryrefslogtreecommitdiff
path: root/SmartDeviceLink_Example/Classes/ProxyManager.m
blob: 355e913400f923dfc9d6876c3fc665d10c370ebd (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
//
//  ProxyManager.m
//  SmartDeviceLink-iOS

@import SmartDeviceLink;

#import "ProxyManager.h"

#import "Preferences.h"


NSString *const SDLAppName = @"SDL Test";
NSString *const SDLAppId = @"9999";


@interface ProxyManager () <SDLProxyListener>

@property (strong, nonatomic) SDLProxy *proxy;
@property (assign, nonatomic, readwrite) ProxyState state;
@property (assign, nonatomic) BOOL isFirstHMIFull;
@property (assign, nonatomic) ProxyTransportType currentTransportType;
@end


@implementation ProxyManager

#pragma mark - Initialization

+ (instancetype)sharedManager {
    static ProxyManager *sharedManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedManager = [[ProxyManager alloc] init];
    });
    
    return sharedManager;
}

- (instancetype)init {
    self = [super init];
    if (self == nil) {
        return nil;
    }
    
    _proxy = nil;
    _state = ProxyStateStopped;
    _isFirstHMIFull = NO;
    _currentTransportType = ProxyTransportTypeUnknown;
    
    return self;
}


#pragma mark - Public Proxy Setup

- (void)resetProxyWithTransportType:(ProxyTransportType)transportType {
    [self stopProxy];
    [self startProxyWithTransportType:transportType];
}


#pragma mark - Private Proxy Setup

- (void)startProxyWithTransportType:(ProxyTransportType)transportType {
    if (self.proxy != nil) {
        return;
    }

    self.currentTransportType = transportType;
    self.isFirstHMIFull = YES;
    self.state = ProxyStateSearchingForConnection;
    
    switch (transportType) {
        case ProxyTransportTypeTCP: {
            self.proxy = [SDLProxyFactory buildSDLProxyWithListener:self tcpIPAddress:[Preferences sharedPreferences].ipAddress tcpPort:[Preferences sharedPreferences].port];
        } break;
        case ProxyTransportTypeIAP: {
            self.proxy = [SDLProxyFactory buildSDLProxyWithListener:self];
        } break;
        default: NSAssert(NO, @"Unknown transport setup: %@", @(transportType));
    }
}

- (void)stopProxy {
    self.state = ProxyStateStopped;
    
    if (self.proxy != nil) {
        [self.proxy dispose];
        self.proxy = nil;
    }
}

- (void)showInitialData {
    SDLShow *showRPC = [SDLRPCRequestFactory buildShowWithMainField1:@"SDL" mainField2:@"Test" alignment:[SDLTextAlignment CENTERED] correlationID:[self nextCorrelationID]];
    [self.proxy sendRPC:showRPC];
}


#pragma mark - Private Proxy Helpers

- (NSNumber *)nextCorrelationID {
    static NSInteger _correlationID = 1;
    return @(_correlationID++);
}

- (UInt32)nextMessageNumber {
    static UInt32 _messageNumber = 1;
    return _messageNumber++;
}


#pragma mark - SDLProxyListner delegate methods

- (void)onProxyOpened {
    self.state = ProxyStateConnected;
    
    SDLRegisterAppInterface *registerRequest = [SDLRPCRequestFactory buildRegisterAppInterfaceWithAppName:SDLAppName languageDesired:[SDLLanguage EN_US] appID:SDLAppId];
    registerRequest.appHMIType = [NSMutableArray arrayWithObjects:[SDLAppHMIType MEDIA], nil];
    [self.proxy sendRPC:registerRequest];
}

- (void)onProxyClosed {
    [self resetProxyWithTransportType:self.currentTransportType];
}

- (void)onOnDriverDistraction:(SDLOnDriverDistraction *)notification {
    
}

- (void)onOnHMIStatus:(SDLOnHMIStatus *)notification {
    if ((notification.hmiLevel == [SDLHMILevel FULL]) && self.isFirstHMIFull) {
        [self showInitialData];
        self.isFirstHMIFull = NO;
    }
}

- (void)onReceivedLockScreenIcon:(UIImage *)icon {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Lock Screen Icon" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:icon];
    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
        [alert setValue:imageView forKey:@"accessoryView"];
    }else{
        [alert addSubview:imageView];
    }
    [alert show];
}

@end