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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
//
// ProxyManager.swift
// SmartDeviceLink-ExampleSwift
//
// Copyright © 2017 smartdevicelink. All rights reserved.
//
import UIKit
import SmartDeviceLink
enum ProxyState {
case stopped
case searching
case connected
}
weak var delegate:ProxyManagerDelegate?
fileprivate var firstHMIFull = true
let appIcon = UIImage(named: "AppIcon60x60")
protocol ProxyManagerDelegate: class {
func didChangeProxyState(_ newState: ProxyState)
}
class ProxyManager: NSObject {
fileprivate var sdlManager: SDLManager!
// Singleton
static let sharedManager = ProxyManager()
private override init() {
super.init()
}
// MARK: - SDL Setup
func startIAP() {
delegate?.didChangeProxyState(ProxyState.searching)
let lifecycleConfiguration = setLifecycleConfigurationPropertiesOnConfiguration(SDLLifecycleConfiguration.defaultConfiguration(withAppName: AppConstants.sdlAppName, appId: AppConstants.sdlAppID))
startSDLManager(lifecycleConfiguration)
}
func startTCP() {
delegate?.didChangeProxyState(ProxyState.searching)
let defaultIP = UserDefaults.standard.string(forKey: "ipAddress")!
let defaultPort = UInt16(UserDefaults.standard.string(forKey: "port")!)!
let lifecycleConfiguration = setLifecycleConfigurationPropertiesOnConfiguration(SDLLifecycleConfiguration.debugConfiguration(withAppName: AppConstants.sdlAppName, appId: AppConstants.sdlAppID, ipAddress: defaultIP, port: defaultPort))
startSDLManager(lifecycleConfiguration)
}
private func startSDLManager(_ lifecycleConfiguration: SDLLifecycleConfiguration) {
// Configure the proxy handling RPC calls between the SDL Core and the app
let configuration: SDLConfiguration = SDLConfiguration(lifecycle: lifecycleConfiguration, lockScreen: SDLLockScreenConfiguration.enabledConfiguration(withAppIcon: appIcon!, backgroundColor: nil))
self.sdlManager = SDLManager(configuration: configuration, delegate: self)
// Start watching for a connection with a SDL Core
self.sdlManager?.start(readyHandler: { [unowned self] (success, error) in
if success {
delegate?.didChangeProxyState(ProxyState.connected)
print("SDL start file manager storage: \(self.sdlManager!.fileManager.bytesAvailable / 1024 / 1024) mb")
}
if let error = error {
print("Error starting SDL: \(error)")
}
})
}
private func setLifecycleConfigurationPropertiesOnConfiguration(_ configuration: SDLLifecycleConfiguration) -> SDLLifecycleConfiguration {
configuration.shortAppName = AppConstants.sdlShortAppName
configuration.appType = SDLAppHMIType.media()
configuration.appIcon = SDLArtwork.persistentArtwork(with: appIcon!, name: AppConstants.appIconName, as: .PNG)
return configuration
}
func send(request: SDLRPCRequest, responseHandler: SDLResponseHandler? = nil) {
guard sdlManager.hmiLevel != .none() else {
return
}
sdlManager.send(request, withResponseHandler: responseHandler)
}
func reset() {
sdlManager?.stop()
delegate?.didChangeProxyState(ProxyState.stopped)
}
}
// MARK: SDLManagerDelegate
extension ProxyManager: SDLManagerDelegate {
func managerDidDisconnect() {
delegate?.didChangeProxyState(ProxyState.stopped)
}
func hmiLevel(_ oldLevel: SDLHMILevel, didChangeTo newLevel: SDLHMILevel) {
// On our first HMI level that isn't none, do some setup
if newLevel != .none() && firstHMIFull == true {
firstHMIFull = false
}
// HMI state is changing from NONE or BACKGROUND to FULL or LIMITED
if (oldLevel == .none() || oldLevel == .background())
&& (newLevel == .full() || newLevel == .limited()) {
prepareRemoteSystem(overwrite: true) { [unowned self] in
self.showMainImage()
self.prepareButtons()
self.addSpeakMenuCommand()
self.addperformInteractionMenuCommand()
self.setText()
self.setDisplayLayout()
}
} else if (oldLevel == .full() || oldLevel == .limited())
&& (newLevel == .none() || newLevel == .background()) {
// HMI state changing from FULL or LIMITED to NONE or BACKGROUND
}
}
}
// MARK: - Prepare Remote System
extension ProxyManager {
func prepareRemoteSystem(overwrite: Bool = false, completionHandler: @escaping (Void) -> (Void)) {
let group = DispatchGroup()
group.enter()
group.notify(queue: .main) {
completionHandler()
}
// Send images
if !sdlManager.fileManager.remoteFileNames.contains(AppConstants.mainArtwork) {
let artwork = SDLArtwork(image: #imageLiteral(resourceName: "sdl_logo_green"), name: AppConstants.mainArtwork, persistent: true, as: .PNG)
group.enter()
sdlManager.fileManager.uploadFile(artwork, completionHandler: { (_, _, error) in
group.leave()
if let error = error {
print("Error uploading default artwork \(artwork) with error \(error)")
}
})
}
if !sdlManager.fileManager.remoteFileNames.contains(AppConstants.PointingSoftButtonArtworkName) {
let buttonIconPoint = SDLArtwork(image: #imageLiteral(resourceName: "sdl_softbutton_icon"), name: AppConstants.PointingSoftButtonArtworkName, persistent: true, as: .PNG)
group.enter()
sdlManager.fileManager.uploadFile(buttonIconPoint, completionHandler: { (_, _, error) in
group.leave()
if let error = error {
print("Error uploading default artwork \(buttonIconPoint) with error \(error)")
}
})
}
let choice = SDLChoice(id: 113, menuName: AppConstants.menuNameOnlyChoice, vrCommands: [AppConstants.menuNameOnlyChoice])!
let createRequest = SDLCreateInteractionChoiceSet(id: 113, choiceSet: [choice])!
group.enter()
sdlManager.send(createRequest) { (request, response, error) in
group.leave()
if response?.resultCode == .success() {
}
}
group.leave()
}
}
// MARK: - RPCs
extension ProxyManager {
// MARK: Show Requests
// Set Text
func setText(){
let show = SDLShow(mainField1: AppConstants.sdl, mainField2: AppConstants.testApp, alignment: .centered())
send(request: show!)
}
// Set Display Layout
func setDisplayLayout(){
let display = SDLSetDisplayLayout(predefinedLayout: .non_MEDIA())!
send(request: display)
}
// Show Main Image
func showMainImage(){
let sdlImage = SDLImage(name: AppConstants.mainArtwork, of: .dynamic())
let show = SDLShow()!
show.graphic = sdlImage
send(request: show)
}
// MARK: Buttons
func prepareButtons(){
let softButton = SDLSoftButton()!
softButton.softButtonID = 100
softButton.handler = { (notification) in
if let onButtonPress = notification as? SDLOnButtonPress {
if onButtonPress.buttonPressMode.isEqual(to: SDLButtonPressMode.short()) {
let alert = SDLAlert()!
alert.alertText1 = AppConstants.pushButtonText
self.send(request: alert)
}
}
}
softButton.type = .both()
softButton.text = AppConstants.buttonText
softButton.image = SDLImage(name: AppConstants.PointingSoftButtonArtworkName, of: .dynamic())
let show = SDLShow()!
show.softButtons = [softButton]
send(request: show)
}
// MARK: Menu Items
func addSpeakMenuCommand(){
let menuParameters = SDLMenuParams(menuName: AppConstants.speakAppNameText, parentId: 0, position: 0)!
let menuItem = SDLAddCommand(id: 111, vrCommands: [AppConstants.speakAppNameText]) { (notification) in
guard let onCommand = notification as? SDLOnCommand else {
return
}
if onCommand.triggerSource == .menu() {
self.send(request: self.appNameSpeak())
}
}!
menuItem.menuParams = menuParameters
send(request: menuItem)
}
// MARK: Perform Interaction Functions
func addperformInteractionMenuCommand(){
let menuParameters = SDLMenuParams(menuName: AppConstants.performInteractionText, parentId: 0, position: 1)!
let menuItem = SDLAddCommand(id: 112, vrCommands: [AppConstants.performInteractionText]) { (notification) in
guard let onCommand = notification as? SDLOnCommand else {
return
}
if onCommand.triggerSource == .menu() {
self.createPerformInteraction()
}
}!
menuItem.menuParams = menuParameters
send(request: menuItem)
}
func createPerformInteraction(){
let performInteraction = SDLPerformInteraction(initialPrompt: nil, initialText: AppConstants.menuNameOnlyChoice, interactionChoiceSetID: 113)!
performInteraction.interactionMode = .manual_ONLY()
performInteraction.interactionLayout = .list_ONLY()
performInteraction.initialPrompt = SDLTTSChunk.textChunks(from: AppConstants.chooseOneTTS)
performInteraction.initialText = AppConstants.initialTextInteraction
performInteraction.helpPrompt = SDLTTSChunk.textChunks(from: AppConstants.doItText)
performInteraction.timeoutPrompt = SDLTTSChunk.textChunks(from: AppConstants.tooLateText)
performInteraction.timeout = 5000 // 5 seconds
self.sdlManager.send(performInteraction) { (request, response, error) in
guard let performInteractionResponse = response as? SDLPerformInteractionResponse else {
return;
}
// Wait for user's selection or for timeout
if performInteractionResponse.resultCode == .timed_OUT() {
self.send(request: self.youMissedItSpeak())
} else if performInteractionResponse.resultCode == .success() {
self.send(request: self.goodJobSpeak())
}
}
}
//MARK: Speak Functions
func appNameSpeak() -> SDLSpeak {
let speak = SDLSpeak()
speak?.ttsChunks = SDLTTSChunk.textChunks(from: AppConstants.sdlTTS)
return speak!
}
func goodJobSpeak() -> SDLSpeak {
let speak = SDLSpeak()
speak?.ttsChunks = SDLTTSChunk.textChunks(from: AppConstants.goodJobTTS)
return speak!
}
func youMissedItSpeak() -> SDLSpeak {
let speak = SDLSpeak()
speak?.ttsChunks = SDLTTSChunk.textChunks(from: AppConstants.missedItTTS)
return speak!
}
}
|