Skip to content

Commit be4e58b

Browse files
JulianKastJulian Kast
andauthored
Fix for voice command that contains no string should be removed (#1686)
* Update sending VoiceCommandManager to strip extra whiteSpace from voiceCommands and avoid sending empty strings in the update operation * Add unit test for empty strings and trimmed whiteSpace for voiceCommands * update unit test * Add null unit test check to voiceCommandManager * Implement suggestions from code review * Removed null voiceCommands from VoiceCommandList. Update unit test * Fix suggestions from code review * Update ID's for voice commands in proper list * Strip white Space before checking if commands are unique Co-authored-by: Julian Kast <julian@livio.com>
1 parent ad5fc1e commit be4e58b

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
@@ -210,4 +210,47 @@ public void testUniqueStrings() {
210210
assertNull(voiceCommandManager.voiceCommands);
211211
}
212212

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

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)