summaryrefslogtreecommitdiff
path: root/base/src/main/java/com/smartdevicelink/managers/screen/menu/BaseVoiceCommandManager.java
blob: f96452bb2bd6511d511c90261d187e5627b5ffb6 (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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
 * Copyright (c) 2019 Livio, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided with the
 * distribution.
 *
 * Neither the name of the Livio Inc. nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package com.smartdevicelink.managers.screen.menu;

import androidx.annotation.NonNull;

import com.smartdevicelink.managers.BaseSubManager;
import com.smartdevicelink.managers.CompletionListener;
import com.smartdevicelink.protocol.enums.FunctionID;
import com.smartdevicelink.proxy.RPCNotification;
import com.smartdevicelink.proxy.RPCResponse;
import com.smartdevicelink.proxy.interfaces.ISdl;
import com.smartdevicelink.proxy.rpc.AddCommand;
import com.smartdevicelink.proxy.rpc.DeleteCommand;
import com.smartdevicelink.proxy.rpc.OnCommand;
import com.smartdevicelink.proxy.rpc.OnHMIStatus;
import com.smartdevicelink.proxy.rpc.enums.HMILevel;
import com.smartdevicelink.proxy.rpc.enums.PredefinedWindows;
import com.smartdevicelink.proxy.rpc.listeners.OnMultipleRequestListener;
import com.smartdevicelink.proxy.rpc.listeners.OnRPCNotificationListener;
import com.smartdevicelink.util.DebugTool;

import java.util.ArrayList;
import java.util.List;

abstract class BaseVoiceCommandManager extends BaseSubManager {
	private static final String TAG = "BaseVoiceCommandManager";
	List<VoiceCommand> voiceCommands, oldVoiceCommands;

	List<AddCommand> inProgressUpdate;

	int lastVoiceCommandId;
	private static final int voiceCommandIdMin = 1900000000;

	boolean waitingOnHMIUpdate;
	boolean hasQueuedUpdate;

	HMILevel currentHMILevel;
	OnRPCNotificationListener hmiListener;
	OnRPCNotificationListener commandListener;

	// CONSTRUCTORS

	BaseVoiceCommandManager(@NonNull ISdl internalInterface) {
		super(internalInterface);

		currentHMILevel = HMILevel.HMI_NONE;
		addListeners();
		lastVoiceCommandId = voiceCommandIdMin;
	}

	@Override
	public void start(CompletionListener listener) {
		transitionToState(READY);
		super.start(listener);
	}

	@Override
	public void dispose(){

		lastVoiceCommandId = voiceCommandIdMin;
		voiceCommands = null;
		oldVoiceCommands = null;

		waitingOnHMIUpdate = false;
		currentHMILevel = null;
		inProgressUpdate = null;
		hasQueuedUpdate = false;

		// remove listeners
		internalInterface.removeOnRPCNotificationListener(FunctionID.ON_HMI_STATUS, hmiListener);
		internalInterface.removeOnRPCNotificationListener(FunctionID.ON_COMMAND, commandListener);

		super.dispose();
	}

	// SETTERS

	public void setVoiceCommands(List<VoiceCommand> voiceCommands){

		// we actually need voice commands to set.
		if (voiceCommands == null || voiceCommands.size() == 0){
			DebugTool.logInfo(TAG, "Trying to set empty list of voice commands, returning");
			return;
		}

		// make sure hmi is not none
		if (currentHMILevel == null || currentHMILevel == HMILevel.HMI_NONE){
			// Trying to send on HMI_NONE, waiting for full
			this.voiceCommands = new ArrayList<>(voiceCommands);
			waitingOnHMIUpdate = true;
			return;
		}

		waitingOnHMIUpdate = false;
		lastVoiceCommandId = voiceCommandIdMin;
		updateIdsOnVoiceCommands(voiceCommands);
		this.oldVoiceCommands = new ArrayList<>();
		if (this.voiceCommands != null && !this.voiceCommands.isEmpty()) {
			this.oldVoiceCommands.addAll(this.voiceCommands);
		}
		this.voiceCommands = new ArrayList<>(voiceCommands);

		update();
	}

	public List<VoiceCommand> getVoiceCommands(){
		return voiceCommands;
	}

	// UPDATING SYSTEM

	private void update(){

		if (currentHMILevel == null || currentHMILevel.equals(HMILevel.HMI_NONE)){
			waitingOnHMIUpdate = true;
			return;
		}

		if (inProgressUpdate != null){
			// There's an in-progress update, put this on hold
			hasQueuedUpdate = true;
			return;
		}

		sendDeleteCurrentVoiceCommands(new CompletionListener() {
			@Override
			public void onComplete(boolean success) {
				// we don't care about errors from deleting, send new add commands
				sendCurrentVoiceCommands(new CompletionListener() {
					@Override
					public void onComplete(boolean success2) {
						inProgressUpdate = null;

						if (hasQueuedUpdate){
							update();
							hasQueuedUpdate = false;
						}

						if (!success2){
							DebugTool.logError(TAG, "Error sending voice commands");
						}
					}
				});
			}
		});

	}

	// DELETING OLD MENU ITEMS

	private void sendDeleteCurrentVoiceCommands(final CompletionListener listener){

		if (oldVoiceCommands == null || oldVoiceCommands.size() == 0){
			if (listener != null){
				listener.onComplete(true);
			}
			return;
		}

		List<DeleteCommand> deleteVoiceCommands = deleteCommandsForVoiceCommands(oldVoiceCommands);
		oldVoiceCommands.clear();
		internalInterface.sendRequests(deleteVoiceCommands, new OnMultipleRequestListener() {
			@Override
			public void onUpdate(int remainingRequests) {

			}

			@Override
			public void onFinished() {
				DebugTool.logInfo(TAG, "Successfully deleted old voice commands");
				if (listener != null){
					listener.onComplete(true);
				}
			}

			@Override
			public void onResponse(int correlationId, RPCResponse response) {}
		});

	}

	// SEND NEW MENU ITEMS

	private void sendCurrentVoiceCommands(final CompletionListener listener){

		if (voiceCommands == null || voiceCommands.size() == 0){
			if (listener != null){
				listener.onComplete(true); // no voice commands to send doesnt mean that its an error
			}
			return;
		}

		inProgressUpdate = addCommandsForVoiceCommands(voiceCommands);

		internalInterface.sendRequests(inProgressUpdate, new OnMultipleRequestListener() {
			@Override
			public void onUpdate(int remainingRequests) {

			}

			@Override
			public void onFinished() {
				DebugTool.logInfo(TAG, "Sending Voice Commands Complete");
				if (listener != null){
					listener.onComplete(true);
				}
				oldVoiceCommands = voiceCommands;
			}

			@Override
			public void onResponse(int correlationId, RPCResponse response) {
			}
		});
	}

	// DELETES

	List<DeleteCommand> deleteCommandsForVoiceCommands(List<VoiceCommand> voiceCommands){
		List<DeleteCommand> deleteCommandList = new ArrayList<>();
		for (VoiceCommand command : voiceCommands){
			DeleteCommand delete = new DeleteCommand(command.getCommandId());
			deleteCommandList.add(delete);
		}
		return deleteCommandList;
	}

	// COMMANDS

	List<AddCommand> addCommandsForVoiceCommands(List<VoiceCommand> voiceCommands){
		List<AddCommand> addCommandList = new ArrayList<>();
		for (VoiceCommand command : voiceCommands){
			addCommandList.add(commandForVoiceCommand(command));
		}
		return addCommandList;
	}

	private AddCommand commandForVoiceCommand(VoiceCommand voiceCommand){
		AddCommand command = new AddCommand(voiceCommand.getCommandId());
		command.setVrCommands(voiceCommand.getVoiceCommands());
		return command;
	}

	// HELPERS

	private void updateIdsOnVoiceCommands(List<VoiceCommand> voiceCommands){
		for (VoiceCommand command : voiceCommands){
			command.setCommandId(++lastVoiceCommandId);
		}
	}

	// LISTENERS

	private void addListeners(){

		// HMI UPDATES
		hmiListener = new OnRPCNotificationListener() {
			@Override
			public void onNotified(RPCNotification notification) {
				OnHMIStatus onHMIStatus = (OnHMIStatus)notification;
				if (onHMIStatus.getWindowID() != null && onHMIStatus.getWindowID() != PredefinedWindows.DEFAULT_WINDOW.getValue()) {
					return;
				}
				HMILevel oldHMILevel = currentHMILevel;
				currentHMILevel = onHMIStatus.getHmiLevel();
				// Auto-send an update if we were in NONE and now we are not
				if (oldHMILevel == HMILevel.HMI_NONE && currentHMILevel != HMILevel.HMI_NONE){
					if (waitingOnHMIUpdate){
						setVoiceCommands(voiceCommands);
					}
				}
			}
		};
		internalInterface.addOnRPCNotificationListener(FunctionID.ON_HMI_STATUS, hmiListener);

		// COMMANDS
		commandListener = new OnRPCNotificationListener() {
			@Override
			public void onNotified(RPCNotification notification) {
				OnCommand onCommand = (OnCommand) notification;
				if (voiceCommands != null && voiceCommands.size() > 0){
					for (VoiceCommand command : voiceCommands){
						if (onCommand.getCmdID() == command.getCommandId()){
							if (command.getVoiceCommandSelectionListener() != null) {
								command.getVoiceCommandSelectionListener().onVoiceCommandSelected();
								break;
							}
						}
					}
				}
			}
		};
		internalInterface.addOnRPCNotificationListener(FunctionID.ON_COMMAND, commandListener);
	}
}