|
56 | 56 |
|
57 | 57 | abstract class BaseVoiceCommandManager extends BaseSubManager { |
58 | 58 | private static final String TAG = "BaseVoiceCommandManager"; |
59 | | - List<VoiceCommand> voiceCommands, currentVoiceCommands; |
| 59 | + List<VoiceCommand> voiceCommands, currentVoiceCommands, originalVoiceCommands; |
60 | 60 |
|
61 | 61 | int lastVoiceCommandId; |
62 | 62 | private static final int voiceCommandIdMin = 1900000000; |
@@ -132,21 +132,29 @@ private void updateTransactionQueueSuspended() { |
132 | 132 | public void setVoiceCommands(List<VoiceCommand> voiceCommands) { |
133 | 133 |
|
134 | 134 | // we actually need voice commands to set. |
135 | | - if (voiceCommands == null || voiceCommands.equals(this.voiceCommands)) { |
| 135 | + if (voiceCommands == null || voiceCommands.equals(this.originalVoiceCommands)) { |
136 | 136 | DebugTool.logInfo(TAG, "Voice commands list was null or matches the current voice commands"); |
137 | 137 | return; |
138 | 138 | } |
139 | 139 |
|
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)) { |
141 | 148 | DebugTool.logError(TAG, "Not all voice command strings are unique across all voice commands. Voice commands will not be set."); |
142 | 149 | return; |
143 | 150 | } |
144 | 151 |
|
145 | | - updateIdsOnVoiceCommands(voiceCommands); |
146 | | - this.voiceCommands = new ArrayList<>(voiceCommands); |
| 152 | + this.originalVoiceCommands = new ArrayList<>(voiceCommands); |
| 153 | + this.voiceCommands = validatedVoiceCommands; |
| 154 | + updateIdsOnVoiceCommands(this.voiceCommands); |
147 | 155 |
|
148 | 156 | cleanTransactionQueue(); |
149 | | - updateOperation = new VoiceCommandUpdateOperation(internalInterface, currentVoiceCommands, voiceCommands, new VoiceCommandUpdateOperation.VoiceCommandChangesListener() { |
| 157 | + updateOperation = new VoiceCommandUpdateOperation(internalInterface, currentVoiceCommands, this.voiceCommands, new VoiceCommandUpdateOperation.VoiceCommandChangesListener() { |
150 | 158 | @Override |
151 | 159 | public void updateVoiceCommands(List<VoiceCommand> newCurrentVoiceCommands, HashMap<RPCRequest, String> errorObject) { |
152 | 160 | DebugTool.logInfo(TAG, "The updated list of VoiceCommands: " + newCurrentVoiceCommands); |
@@ -194,6 +202,30 @@ private void updateIdsOnVoiceCommands(List<VoiceCommand> voiceCommands) { |
194 | 202 | } |
195 | 203 | } |
196 | 204 |
|
| 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 | + |
197 | 229 | // LISTENERS |
198 | 230 |
|
199 | 231 | private void addListeners() { |
@@ -243,6 +275,9 @@ private boolean isVoiceCommandsUnique(List<VoiceCommand> voiceCommands) { |
243 | 275 | int voiceCommandCount = 0; |
244 | 276 |
|
245 | 277 | for (VoiceCommand voiceCommand : voiceCommands) { |
| 278 | + if (voiceCommand == null) { |
| 279 | + continue; |
| 280 | + } |
246 | 281 | voiceCommandHashSet.addAll(voiceCommand.getVoiceCommands()); |
247 | 282 | voiceCommandCount += voiceCommand.getVoiceCommands().size(); |
248 | 283 | } |
|
0 commit comments