Skip to content

Commit 7f84950

Browse files
author
Julian Kast
committed
Merge branch 'develop' into bugfix/issue_1676
2 parents 7882136 + be4e58b commit 7f84950

2 files changed

Lines changed: 84 additions & 6 deletions

File tree

android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/screen/menu/VoiceCommandManagerTests.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,47 @@ public void testUniqueStrings() {
214214
assertNull(voiceCommandManager.voiceCommands);
215215
}
216216

217+
/**
218+
* Test trim whitespace from voice commands and not uploading empty strings.
219+
*/
220+
@Test
221+
public void testEmptyStringsAndWhiteSpaceRemoval() {
222+
List<VoiceCommand> voiceCommandList = new ArrayList<>();
223+
VoiceCommand command1 = new VoiceCommand(Arrays.asList(" Command one "), null);
224+
225+
voiceCommandList.add(command1);
226+
voiceCommandManager.currentHMILevel = HMILevel.HMI_NONE;
227+
voiceCommandManager.setVoiceCommands(voiceCommandList);
228+
assertEquals(voiceCommandManager.voiceCommands.get(0).getVoiceCommands().get(0), "Command one");
229+
230+
voiceCommandManager.voiceCommands = null;
231+
voiceCommandList = new ArrayList<>();
232+
command1 = new VoiceCommand(Arrays.asList(" "), null);
233+
234+
voiceCommandList.add(command1);
235+
voiceCommandManager.setVoiceCommands(voiceCommandList);
236+
assertNull(voiceCommandManager.voiceCommands);
237+
238+
VoiceCommand command2 = new VoiceCommand(Arrays.asList("Command two"), null);
239+
voiceCommandList.add(command2);
240+
voiceCommandManager.setVoiceCommands(voiceCommandList);
241+
assertEquals(voiceCommandManager.voiceCommands.size(), 1);
242+
243+
voiceCommandManager.setVoiceCommands(voiceCommandList);
244+
assertEquals(voiceCommandManager.voiceCommands.size(), 1);
245+
246+
voiceCommandManager.voiceCommands = null;
247+
voiceCommandManager.setVoiceCommands(null);
248+
assertNull(voiceCommandManager.voiceCommands);
249+
250+
voiceCommandList = new ArrayList<>();
251+
VoiceCommand command3 = new VoiceCommand(Arrays.asList("Command three", null, " "), null);
252+
voiceCommandList.add(command3);
253+
voiceCommandList.add(null);
254+
voiceCommandManager.setVoiceCommands(voiceCommandList);
255+
assertEquals(voiceCommandManager.voiceCommands.size(), 1);
256+
257+
}
258+
259+
217260
}

base/src/main/java/com/smartdevicelink/managers/screen/menu/BaseVoiceCommandManager.java

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757
abstract class BaseVoiceCommandManager extends BaseSubManager {
5858
private static final String TAG = "BaseVoiceCommandManager";
59-
List<VoiceCommand> voiceCommands, currentVoiceCommands;
59+
List<VoiceCommand> voiceCommands, currentVoiceCommands, originalVoiceCommands;
6060

6161
int lastVoiceCommandId;
6262
private static final int voiceCommandIdMin = 1900000000;
@@ -132,21 +132,29 @@ private void updateTransactionQueueSuspended() {
132132
public void setVoiceCommands(List<VoiceCommand> voiceCommands) {
133133

134134
// we actually need voice commands to set.
135-
if (voiceCommands == null || voiceCommands.equals(this.voiceCommands)) {
135+
if (voiceCommands == null || voiceCommands.equals(this.originalVoiceCommands)) {
136136
DebugTool.logInfo(TAG, "Voice commands list was null or matches the current voice commands");
137137
return;
138138
}
139139

140-
if (!isVoiceCommandsUnique(voiceCommands)) {
140+
List<VoiceCommand> validatedVoiceCommands = removeEmptyVoiceCommands(voiceCommands);
141+
142+
if (validatedVoiceCommands.size() == 0) {
143+
DebugTool.logError(TAG, "New voice commands are invalid, skipping...");
144+
return;
145+
}
146+
147+
if (!isVoiceCommandsUnique(validatedVoiceCommands)) {
141148
DebugTool.logError(TAG, "Not all voice command strings are unique across all voice commands. Voice commands will not be set.");
142149
return;
143150
}
144151

145-
updateIdsOnVoiceCommands(voiceCommands);
146-
this.voiceCommands = new ArrayList<>(voiceCommands);
152+
this.originalVoiceCommands = new ArrayList<>(voiceCommands);
153+
this.voiceCommands = validatedVoiceCommands;
154+
updateIdsOnVoiceCommands(this.voiceCommands);
147155

148156
cleanTransactionQueue();
149-
updateOperation = new VoiceCommandUpdateOperation(internalInterface, currentVoiceCommands, voiceCommands, new VoiceCommandUpdateOperation.VoiceCommandChangesListener() {
157+
updateOperation = new VoiceCommandUpdateOperation(internalInterface, currentVoiceCommands, this.voiceCommands, new VoiceCommandUpdateOperation.VoiceCommandChangesListener() {
150158
@Override
151159
public void updateVoiceCommands(List<VoiceCommand> newCurrentVoiceCommands, HashMap<RPCRequest, String> errorObject) {
152160
DebugTool.logInfo(TAG, "The updated list of VoiceCommands: " + newCurrentVoiceCommands);
@@ -194,6 +202,30 @@ private void updateIdsOnVoiceCommands(List<VoiceCommand> voiceCommands) {
194202
}
195203
}
196204

205+
List<VoiceCommand> removeEmptyVoiceCommands(List<VoiceCommand> voiceCommands) {
206+
List<VoiceCommand> validatedVoiceCommands = new ArrayList<>();
207+
for (VoiceCommand voiceCommand : voiceCommands) {
208+
if (voiceCommand == null) {
209+
continue;
210+
}
211+
List<String> voiceCommandStrings = new ArrayList<>();
212+
for (String voiceCommandString : voiceCommand.getVoiceCommands()) {
213+
if (voiceCommandString == null) {
214+
continue;
215+
}
216+
String trimmedString = voiceCommandString.trim();
217+
if (trimmedString.length() > 0) {
218+
voiceCommandStrings.add(trimmedString);
219+
}
220+
}
221+
if (voiceCommandStrings.size() > 0) {
222+
voiceCommand.setVoiceCommands(voiceCommandStrings);
223+
validatedVoiceCommands.add(voiceCommand);
224+
}
225+
}
226+
return validatedVoiceCommands;
227+
}
228+
197229
// LISTENERS
198230

199231
private void addListeners() {
@@ -243,6 +275,9 @@ private boolean isVoiceCommandsUnique(List<VoiceCommand> voiceCommands) {
243275
int voiceCommandCount = 0;
244276

245277
for (VoiceCommand voiceCommand : voiceCommands) {
278+
if (voiceCommand == null) {
279+
continue;
280+
}
246281
voiceCommandHashSet.addAll(voiceCommand.getVoiceCommands());
247282
voiceCommandCount += voiceCommand.getVoiceCommands().size();
248283
}

0 commit comments

Comments
 (0)