Skip to content

Commit 6a6a45e

Browse files
author
Julian Kast
committed
Add clone method to VoiceCommand and update the handling of comparing voiceCommands in VoiceCommandManager
1 parent 7f84950 commit 6a6a45e

2 files changed

Lines changed: 32 additions & 7 deletions

File tree

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

Lines changed: 11 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, originalVoiceCommands;
59+
List<VoiceCommand> voiceCommands, currentVoiceCommands;
6060

6161
int lastVoiceCommandId;
6262
private static final int voiceCommandIdMin = 1900000000;
@@ -132,12 +132,18 @@ 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.originalVoiceCommands)) {
136-
DebugTool.logInfo(TAG, "Voice commands list was null or matches the current voice commands");
135+
if (voiceCommands == null) {
136+
DebugTool.logInfo(TAG, "Voice commands list was null");
137137
return;
138138
}
139139

140-
List<VoiceCommand> validatedVoiceCommands = removeEmptyVoiceCommands(voiceCommands);
140+
// Clone voice commands
141+
this.voiceCommands = new ArrayList<>();
142+
for (VoiceCommand voiceCommand : voiceCommands) {
143+
this.voiceCommands.add(voiceCommand.clone());
144+
}
145+
146+
List<VoiceCommand> validatedVoiceCommands = removeEmptyVoiceCommands(this.voiceCommands);
141147

142148
if (validatedVoiceCommands.size() == 0) {
143149
DebugTool.logError(TAG, "New voice commands are invalid, skipping...");
@@ -147,10 +153,9 @@ public void setVoiceCommands(List<VoiceCommand> voiceCommands) {
147153
if (!isVoiceCommandsUnique(validatedVoiceCommands)) {
148154
DebugTool.logError(TAG, "Not all voice command strings are unique across all voice commands. Voice commands will not be set.");
149155
return;
156+
150157
}
151158

152-
this.originalVoiceCommands = new ArrayList<>(voiceCommands);
153-
this.voiceCommands = validatedVoiceCommands;
154159
updateIdsOnVoiceCommands(this.voiceCommands);
155160

156161
cleanTransactionQueue();

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@
3636
import androidx.annotation.Nullable;
3737

3838

39+
import com.smartdevicelink.util.DebugTool;
40+
3941
import java.util.ArrayList;
4042
import java.util.HashSet;
4143
import java.util.List;
4244

43-
public class VoiceCommand {
45+
public class VoiceCommand implements Cloneable {
4446

4547
/**
4648
* The strings the user can say to activate this voice command
@@ -162,4 +164,22 @@ public boolean equals(Object o) {
162164
// return comparison
163165
return hashCode() == o.hashCode();
164166
}
167+
168+
/**
169+
* Creates a deep copy of the object
170+
*
171+
* @return deep copy of the object, null if an exception occurred
172+
*/
173+
@Override
174+
public VoiceCommand clone() {
175+
try {
176+
VoiceCommand clone = (VoiceCommand) super.clone();
177+
return clone;
178+
} catch (CloneNotSupportedException e) {
179+
if (DebugTool.isDebugEnabled()) {
180+
throw new RuntimeException("Clone not supported by super class");
181+
}
182+
}
183+
return null;
184+
}
165185
}

0 commit comments

Comments
 (0)