From 7a7364839bf8b306273317c92edfab4fb3105f22 Mon Sep 17 00:00:00 2001 From: Denny Sheirer Date: Tue, 17 Nov 2026 03:52:59 -0500 Subject: [PATCH] #2680 Alias List optionally auto-adds/upates radio ID to talker alias value. Enable the feature in the playlist editor in the channel config for protocols that produce talker alias (P25, NXDN, DMR). (#2682) Co-authored-by: denny --- .../dsheirer/alias/TalkerAliasAutoAdder.java | 455 ++++++++++++++++++ .../dsheirer/controller/channel/Channel.java | 47 ++ .../channel/ChannelConfigurationEditor.java | 42 ++ .../identifier/alias/TalkerAliasManager.java | 19 + .../module/decode/DecoderFactory.java | 29 ++ .../dsheirer/module/decode/DecoderType.java | 13 + .../decode/dmr/DMRTrafficChannelManager.java | 1 + .../nxdn/NXDNTrafficChannelManager.java | 1 + .../decode/p25/P25TrafficChannelManager.java | 1 + .../decode/traffic/TrafficChannelManager.java | 10 + .../alias/TalkerAliasAutoAdderTest.java | 215 +++++++++ .../ChannelAutoAddTalkerAliasesTest.java | 49 ++ 12 files changed, 882 insertions(+) create mode 100644 src/main/java/io/github/dsheirer/alias/TalkerAliasAutoAdder.java create mode 100644 src/test/java/io/github/dsheirer/alias/TalkerAliasAutoAdderTest.java create mode 100644 src/test/java/io/github/dsheirer/controller/channel/ChannelAutoAddTalkerAliasesTest.java diff --git a/src/main/java/io/github/dsheirer/alias/TalkerAliasAutoAdder.java b/src/main/java/io/github/dsheirer/alias/TalkerAliasAutoAdder.java new file mode 100644 index 00000000..d7974548 --- /dev/null +++ b/src/main/java/io/github/dsheirer/alias/TalkerAliasAutoAdder.java @@ -0,0 +1,455 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ + +package io.github.dsheirer.alias; + +import com.google.common.annotations.VisibleForTesting; +import io.github.dsheirer.alias.id.AliasID; +import io.github.dsheirer.alias.id.radio.NXDNTypeDRadio; +import io.github.dsheirer.alias.id.radio.P25FullyQualifiedRadio; +import io.github.dsheirer.alias.id.radio.Radio; +import io.github.dsheirer.identifier.alias.TalkerAliasIdentifier; +import io.github.dsheirer.identifier.radio.FullyQualifiedRadioIdentifier; +import io.github.dsheirer.identifier.radio.RadioIdentifier; +import io.github.dsheirer.module.decode.nxdn.identifier.NXDNRadioIdentifier; +import io.github.dsheirer.protocol.Protocol; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.BiConsumer; +import javafx.application.Platform; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Automatically adds decoded talker aliases to an alias list as radio aliases, using the talker alias as the alias name, + * and renames a stored radio alias when the radio's talker alias changes. + * + * Talker aliases span up to 4 messages, so a decoded talker alias can be a fragment (the leading characters) of the + * full alias, even when the decoder reports it as complete. To avoid replacing a full alias with a fragment: + *
    + *
  • A decoded alias that is the start of the stored alias name is a fragment and never replaces it.
  • + *
  • A decoded alias that starts with the stored alias name is a longer version of a stored fragment and + * replaces it.
  • + *
  • Any other decoded alias that differs from the stored alias name replaces it only after it is decoded + * {@link #CONFIRMATIONS_REQUIRED} times in a row for the radio.
  • + *
+ * + * Only aliases whose only identifier is the radio are created or renamed. An alias that combines the radio with other + * identifiers (e.g. several radios or talkgroups) is left alone. + * + * This class is thread safe. Talker aliases are received on decoder threads and the alias model is updated on the + * JavaFX application thread. + */ +public class TalkerAliasAutoAdder implements BiConsumer +{ + private static final Logger mLog = LoggerFactory.getLogger(TalkerAliasAutoAdder.class); + + /** + * Group assigned to aliases created from talker aliases. + */ + public static final String ALIAS_GROUP = "Talker Alias"; + + /** + * Number of consecutive times a different talker alias must be decoded for a radio before it replaces the stored + * alias name. + */ + public static final int CONFIRMATIONS_REQUIRED = 2; + + /** + * Interval for re-checking the stored alias for a talker alias that was already checked, so that a renamed or + * deleted alias is restored without checking the alias list for every decoded talker alias. + */ + private static final long RECHECK_INTERVAL_MS = 30_000; + + /** + * Outcome of comparing a decoded talker alias with the stored alias name. + */ + public enum Decision + { + /** + * The stored alias name matches the talker alias. + */ + UNCHANGED, + /** + * The talker alias is a fragment of the stored alias name. + */ + FRAGMENT, + /** + * The talker alias differs from the stored alias name and is waiting for more decodes. + */ + AWAIT_CONFIRMATION, + /** + * The talker alias replaces the stored alias name. + */ + UPDATE + } + + private final AliasModel mAliasModel; + private final String mAliasListName; + //Guarded by this + private final Map mCandidates = new HashMap<>(); + private final Map mChecked = new HashMap<>(); + + /** + * Constructs an instance + * @param aliasModel to add aliases to + * @param aliasListName of the alias list to add aliases to + */ + public TalkerAliasAutoAdder(AliasModel aliasModel, String aliasListName) + { + mAliasModel = aliasModel; + mAliasListName = aliasListName; + } + + /** + * Name of the alias list that talker aliases are added to. + */ + public String getAliasListName() + { + return mAliasListName; + } + + /** + * Processes a talker alias that is assigned to a radio. + * @param radio that the talker alias belongs to + * @param talkerAlias for the radio + */ + @Override + public void accept(RadioIdentifier radio, TalkerAliasIdentifier talkerAlias) + { + if(radio == null || talkerAlias == null || mAliasListName == null || mAliasListName.isEmpty()) + { + return; + } + + String value = normalize(talkerAlias.getValue()); + Radio aliasId = createAliasId(radio); + + if(value == null || aliasId == null || !aliasId.isValid()) + { + return; + } + + String key = getKey(aliasId); + long now = System.currentTimeMillis(); + int confirmations; + + synchronized(this) + { + Checked checked = mChecked.get(key); + + if(checked != null && checked.value().equals(value) && (now - checked.timestamp()) < RECHECK_INTERVAL_MS) + { + return; + } + + confirmations = confirm(key, value); + } + + runOnFxThread(() -> apply(radio, aliasId, key, value, confirmations)); + } + + /** + * Counts the consecutive decodes of the talker alias for the radio. A fragment of the talker alias that is + * waiting for confirmation doesn't interrupt the count, because fragments are decoded while the full alias is + * assembled. + * @return number of consecutive decodes of the value, including this one. + */ + private int confirm(String key, String value) + { + Candidate candidate = mCandidates.get(key); + + if(candidate != null && candidate.value().equals(value)) + { + candidate = new Candidate(value, candidate.count() + 1); + mCandidates.put(key, candidate); + return candidate.count(); + } + + if(candidate != null && isFragment(value, candidate.value())) + { + return 1; + } + + mCandidates.put(key, new Candidate(value, 1)); + return 1; + } + + /** + * Creates or updates the alias for the radio. Runs on the JavaFX application thread. + */ + private synchronized void apply(RadioIdentifier radio, Radio aliasId, String key, String value, int confirmations) + { + AliasList aliasList = mAliasModel.getAliasList(mAliasListName); + Alias existing = findExactMatch(aliasList.getAliases(radio), aliasId); + + if(existing == null) + { + Alias alias = new Alias(value); + alias.setAliasListName(mAliasListName); + alias.setGroup(ALIAS_GROUP); + alias.addAliasID(aliasId); + mAliasModel.addAlias(alias); + markChecked(key, value); + mLog.info("Talker alias [" + value + "] added for radio [" + aliasId + "] to alias list [" + + mAliasListName + "]"); + return; + } + + if(getIdentityCount(existing) > 1) + { + //The alias also identifies other radios or talkgroups - leave it alone. + markChecked(key, value); + return; + } + + String stored = normalize(existing.getName()); + + switch(decide(stored, value, confirmations)) + { + case UPDATE: + existing.setName(value); + markChecked(key, value); + mLog.info("Talker alias for radio [" + aliasId + "] in alias list [" + mAliasListName + + "] changed from [" + stored + "] to [" + value + "]"); + break; + case UNCHANGED: + case FRAGMENT: + markChecked(key, value); + break; + case AWAIT_CONFIRMATION: + default: + break; + } + } + + private void markChecked(String key, String value) + { + mChecked.put(key, new Checked(value, System.currentTimeMillis())); + mCandidates.remove(key); + } + + /** + * Compares the decoded talker alias with the stored alias name. + * @param stored alias name, normalized, or null + * @param decoded talker alias, normalized + * @param confirmations number of consecutive decodes of the talker alias + * @return decision + */ + @VisibleForTesting + static Decision decide(String stored, String decoded, int confirmations) + { + if(stored == null || stored.isEmpty()) + { + return Decision.UPDATE; + } + + if(stored.equals(decoded)) + { + return Decision.UNCHANGED; + } + + if(isFragment(decoded, stored)) + { + return Decision.FRAGMENT; + } + + //A longer version of a stored fragment. + if(isFragment(stored, decoded) && !hasUndecodableCharacters(decoded)) + { + return Decision.UPDATE; + } + + //A different alias that may be a corrupted or mis-assembled alias. It's replaced only once confirmed, and an + //alias that has undecodable characters (e.g. a partial multi-byte character) never replaces a stored alias. + if(confirmations >= CONFIRMATIONS_REQUIRED && !hasUndecodableCharacters(decoded)) + { + return Decision.UPDATE; + } + + return Decision.AWAIT_CONFIRMATION; + } + + /** + * Indicates if the value is a fragment (the leading characters) of the full value. + */ + @VisibleForTesting + static boolean isFragment(String value, String full) + { + return value != null && full != null && value.length() < full.length() && full.startsWith(value); + } + + private static boolean hasUndecodableCharacters(String value) + { + return value.indexOf('�') >= 0; + } + + /** + * Removes padding, control characters and trailing undecodable characters (e.g. from a partial multi-byte + * character at the end of an alias fragment) from the talker alias. + * @param value to normalize + * @return normalized value or null if the value is empty. + */ + @VisibleForTesting + static String normalize(String value) + { + if(value == null) + { + return null; + } + + StringBuilder sb = new StringBuilder(value.length()); + + for(int i = 0; i < value.length(); i++) + { + char c = value.charAt(i); + + if(!Character.isISOControl(c)) + { + sb.append(c); + } + } + + int end = sb.length(); + + while(end > 0 && (sb.charAt(end - 1) == '�' || Character.isWhitespace(sb.charAt(end - 1)))) + { + end--; + } + + String normalized = sb.substring(0, end).trim(); + return normalized.isEmpty() ? null : normalized; + } + + /** + * Creates a radio alias identifier that exactly matches the radio. + * @param radio identifier + * @return alias identifier or null if the radio protocol isn't known. + */ + @VisibleForTesting + static Radio createAliasId(RadioIdentifier radio) + { + if(radio instanceof FullyQualifiedRadioIdentifier fqri && fqri.getProtocol() == Protocol.APCO25) + { + return new P25FullyQualifiedRadio(fqri.getWacn(), fqri.getSystem(), fqri.getRadio()); + } + + if(radio instanceof NXDNRadioIdentifier nxdn && nxdn.isTypeD()) + { + return new NXDNTypeDRadio(nxdn.getTypeDHomeRepeater(), nxdn.getTypeDRadio()); + } + + Protocol protocol = radio.getProtocol(); + + if(protocol == null || protocol == Protocol.UNKNOWN || radio.getValue() == null) + { + return null; + } + + return new Radio(protocol, radio.getValue()); + } + + /** + * Finds the alias that has an identifier that exactly matches the radio (i.e. not a radio range). + */ + @VisibleForTesting + static Alias findExactMatch(List aliases, Radio aliasId) + { + if(aliases != null) + { + for(Alias alias: aliases) + { + for(AliasID id: alias.getAliasIdentifiers()) + { + if(isExactMatch(id, aliasId)) + { + return alias; + } + } + } + } + + return null; + } + + private static boolean isExactMatch(AliasID id, Radio aliasId) + { + if(aliasId instanceof P25FullyQualifiedRadio target) + { + return id instanceof P25FullyQualifiedRadio fqr && fqr.getHashKey().equals(target.getHashKey()); + } + + return id instanceof Radio radio && !(id instanceof P25FullyQualifiedRadio) && + radio.getProtocol() == aliasId.getProtocol() && radio.getValue() == aliasId.getValue(); + } + + /** + * Number of identifiers (e.g. radios, talkgroups) in the alias, excluding audio settings like priority, record and + * streaming. + */ + private static int getIdentityCount(Alias alias) + { + int count = 0; + + for(AliasID id: alias.getAliasIdentifiers()) + { + if(!id.isAudioIdentifier()) + { + count++; + } + } + + return count; + } + + private static String getKey(Radio aliasId) + { + if(aliasId instanceof P25FullyQualifiedRadio fqr) + { + return aliasId.getProtocol() + ":" + fqr.getHashKey(); + } + + return aliasId.getProtocol() + ":" + aliasId.getValue(); + } + + /** + * Runs on the FX thread, or on this thread when the FX toolkit isn't running (headless). + */ + private static void runOnFxThread(Runnable runnable) + { + if(Platform.isFxApplicationThread()) + { + runnable.run(); + return; + } + + try + { + Platform.runLater(runnable); + } + catch(IllegalStateException ise) + { + runnable.run(); + } + } + + private record Candidate(String value, int count) {} + private record Checked(String value, long timestamp) {} +} diff --git a/src/main/java/io/github/dsheirer/controller/channel/Channel.java b/src/main/java/io/github/dsheirer/controller/channel/Channel.java index ff40fe8f..252af614 100644 --- a/src/main/java/io/github/dsheirer/controller/channel/Channel.java +++ b/src/main/java/io/github/dsheirer/controller/channel/Channel.java @@ -18,6 +18,7 @@ */ package io.github.dsheirer.controller.channel; +import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; @@ -83,6 +84,8 @@ public class Channel extends Configuration implements Listener private BooleanProperty mProcessing = new SimpleBooleanProperty(); private BooleanProperty mAutoStart = new SimpleBooleanProperty(); + //Enabled for new channels. Channels loaded from a playlist without the setting keep it disabled, see fromXml(). + private BooleanProperty mAutoAddTalkerAliases = new SimpleBooleanProperty(true); private IntegerProperty mAutoStartOrder = new SimpleIntegerProperty(); private boolean mSelected; private List mTunerChannels = null; @@ -123,6 +126,19 @@ public class Channel extends Configuration implements Listener mChannelID = UNIQUE_ID++; } + /** + * Creates a channel for deserialization from a playlist. Settings added after a channel was saved take their + * legacy (disabled) value when the playlist doesn't specify them, instead of the default for new channels, so that + * existing channels don't change behavior. + */ + @JsonCreator + private static Channel fromXml() + { + Channel channel = new Channel(); + channel.setAutoAddTalkerAliases(false); + return channel; + } + /** * Creates a (new) deep copy of this channel */ @@ -134,6 +150,7 @@ public class Channel extends Configuration implements Listener channel.setAliasListName(mAliasListName.get()); channel.setAutoStart(mAutoStart.get()); channel.setAutoStartOrder(mAutoStartOrder.get()); + channel.setAutoAddTalkerAliases(mAutoAddTalkerAliases.get()); AuxDecodeConfiguration auxCopy = new AuxDecodeConfiguration(); @@ -509,6 +526,36 @@ public class Channel extends Configuration implements Listener mAutoStart.set(autoStart); } + /** + * Auto-add talker aliases property + */ + public BooleanProperty autoAddTalkerAliasesProperty() + { + return mAutoAddTalkerAliases; + } + + /** + * Indicates if decoded talker aliases are automatically added to the channel's alias list as radio aliases, using + * the talker alias as the alias name. + * + * @return true if talker aliases are automatically added to the alias list + */ + @JacksonXmlProperty(isAttribute = true, localName = "auto_add_talker_aliases") + public boolean isAutoAddTalkerAliases() + { + return mAutoAddTalkerAliases.get(); + } + + /** + * Sets automatically adding decoded talker aliases to the channel's alias list. + * + * @param autoAdd true to automatically add talker aliases to the alias list + */ + public void setAutoAddTalkerAliases(boolean autoAdd) + { + mAutoAddTalkerAliases.set(autoAdd); + } + /** * Auto-start order. Specifies an optional ordering for channels so that when the application starts up, * the channels can be started in a user-specified order. diff --git a/src/main/java/io/github/dsheirer/gui/playlist/channel/ChannelConfigurationEditor.java b/src/main/java/io/github/dsheirer/gui/playlist/channel/ChannelConfigurationEditor.java index f3dc7a95..9deabec9 100644 --- a/src/main/java/io/github/dsheirer/gui/playlist/channel/ChannelConfigurationEditor.java +++ b/src/main/java/io/github/dsheirer/gui/playlist/channel/ChannelConfigurationEditor.java @@ -96,6 +96,7 @@ public abstract class ChannelConfigurationEditor extends Editor private TextField mNameField; private ComboBox mAliasListComboBox; private Button mNewAliasListButton; + private CheckBox mAutoAddTalkerAliasesCheckBox; private GridPane mTextFieldPane; private Button mSaveButton; private Button mResetButton; @@ -187,6 +188,7 @@ public abstract class ChannelConfigurationEditor extends Editor getNameField().setDisable(disable); getAliasListComboBox().setDisable(disable); getNewAliasListButton().setDisable(disable); + getAutoAddTalkerAliasesCheckBox().setDisable(disable); getAutoStartSwitch().setDisable(disable); if(channel != null) @@ -210,6 +212,7 @@ public abstract class ChannelConfigurationEditor extends Editor getAliasListComboBox().getSelectionModel().select(null); } + getAutoAddTalkerAliasesCheckBox().setSelected(channel.isAutoAddTalkerAliases()); getAutoStartSwitch().selectedProperty().set(channel.isAutoStart()); getAutoStartOrderSpinner().setDisable(!channel.isAutoStart()); Integer order = channel.getAutoStartOrder(); @@ -251,6 +254,7 @@ public abstract class ChannelConfigurationEditor extends Editor getSiteField().setText(null); getNameField().setText(null); getAliasListComboBox().getSelectionModel().select(null); + getAutoAddTalkerAliasesCheckBox().setSelected(false); getAutoStartSwitch().selectedProperty().set(false); getAutoStartOrderSpinner().setDisable(true); getAutoStartOrderSpinner().getValueFactory().setValue(0); @@ -277,6 +281,12 @@ public abstract class ChannelConfigurationEditor extends Editor getItem().setName(" "); getItem().setName(getNameField().getText()); getItem().setAliasListName(getAliasListComboBox().getSelectionModel().getSelectedItem()); + + if(providesTalkerAlias()) + { + getItem().setAutoAddTalkerAliases(getAutoAddTalkerAliasesCheckBox().isSelected()); + } + getItem().setAutoStart(getAutoStartSwitch().isSelected()); Integer order = getAutoStartOrderSpinner().getValue(); @@ -598,6 +608,12 @@ public abstract class ChannelConfigurationEditor extends Editor GridPane.setConstraints(getNewAliasListButton(), 4, row); mTextFieldPane.getChildren().add(getNewAliasListButton()); + + if(providesTalkerAlias()) + { + GridPane.setConstraints(getAutoAddTalkerAliasesCheckBox(), 3, ++row, 2, 1); + mTextFieldPane.getChildren().add(getAutoAddTalkerAliasesCheckBox()); + } } return mTextFieldPane; @@ -660,6 +676,32 @@ public abstract class ChannelConfigurationEditor extends Editor return mAliasListComboBox; } + /** + * Indicates if the decoder for this editor produces talker aliases. + */ + private boolean providesTalkerAlias() + { + return getDecoderType() != null && getDecoderType().providesTalkerAlias(); + } + + /** + * Check box to automatically add decoded talker aliases to the selected alias list. + */ + private CheckBox getAutoAddTalkerAliasesCheckBox() + { + if(mAutoAddTalkerAliasesCheckBox == null) + { + mAutoAddTalkerAliasesCheckBox = new CheckBox("Auto-Add Talker Aliases"); + mAutoAddTalkerAliasesCheckBox.setDisable(true); + mAutoAddTalkerAliasesCheckBox.setTooltip(new Tooltip("Adds a radio alias for each talker alias decoded " + + "on this system, named with the talker alias, and renames the radio alias when its talker alias " + + "changes. Applies when the channel is (re)started.")); + mAutoAddTalkerAliasesCheckBox.setOnAction(event -> modifiedProperty().set(true)); + } + + return mAutoAddTalkerAliasesCheckBox; + } + private Button getNewAliasListButton() { if(mNewAliasListButton == null) diff --git a/src/main/java/io/github/dsheirer/identifier/alias/TalkerAliasManager.java b/src/main/java/io/github/dsheirer/identifier/alias/TalkerAliasManager.java index cd987084..613b5a77 100644 --- a/src/main/java/io/github/dsheirer/identifier/alias/TalkerAliasManager.java +++ b/src/main/java/io/github/dsheirer/identifier/alias/TalkerAliasManager.java @@ -31,6 +31,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.function.BiConsumer; /** * Talker alias cache manager. Collects observed talker aliases and inserts them into an identifier collection when @@ -41,6 +42,7 @@ import java.util.concurrent.ConcurrentHashMap; public class TalkerAliasManager { private Map mAliasMap = new ConcurrentHashMap<>(); + private volatile BiConsumer mTalkerAliasListener; /** * Constructs an instance @@ -49,6 +51,16 @@ public class TalkerAliasManager { } + /** + * Sets an optional listener to receive each talker alias that is assigned to a radio, e.g. to automatically add + * talker aliases to an alias list. The listener is invoked on the calling decoder thread. + * @param listener to receive radio and talker alias updates, or null to clear the listener. + */ + public void setTalkerAliasListener(BiConsumer listener) + { + mTalkerAliasListener = listener; + } + /** * Updates the alias for the radio identifier * @@ -60,6 +72,13 @@ public class TalkerAliasManager if(identifier.getRole() == Role.FROM) { mAliasMap.put(identifier.getValue(), alias); + + BiConsumer listener = mTalkerAliasListener; + + if(listener != null && alias != null) + { + listener.accept(identifier, alias); + } } } diff --git a/src/main/java/io/github/dsheirer/module/decode/DecoderFactory.java b/src/main/java/io/github/dsheirer/module/decode/DecoderFactory.java index cd92d35a..afc24aa0 100644 --- a/src/main/java/io/github/dsheirer/module/decode/DecoderFactory.java +++ b/src/main/java/io/github/dsheirer/module/decode/DecoderFactory.java @@ -20,6 +20,7 @@ package io.github.dsheirer.module.decode; import io.github.dsheirer.alias.AliasList; import io.github.dsheirer.alias.AliasModel; +import io.github.dsheirer.alias.TalkerAliasAutoAdder; import io.github.dsheirer.alias.action.AliasActionManager; import io.github.dsheirer.audio.*; import io.github.dsheirer.audio.codec.mbe.JmbeAudioModule; @@ -210,9 +211,37 @@ public class DecoderFactory throw new IllegalArgumentException("Unknown decoder type [" + decodeConfig.getDecoderType().toString() + "]"); } + configureTalkerAliasAutoAdd(channel, modules, aliasModel); + return modules; } + /** + * Configures the traffic channel manager of a standard (control) channel to automatically add talker aliases to + * the channel's alias list, when the channel is configured for it. Traffic channels share the talker alias manager + * of their control channel, so they use the control channel's setting. + * @param channel configuration + * @param modules for the channel + * @param aliasModel to add aliases to + */ + private static void configureTalkerAliasAutoAdd(Channel channel, List modules, AliasModel aliasModel) + { + if(channel.isStandardChannel() && channel.isAutoAddTalkerAliases() && + channel.getDecodeConfiguration().getDecoderType().providesTalkerAlias() && + channel.getAliasListName() != null && !channel.getAliasListName().isEmpty()) + { + TalkerAliasAutoAdder autoAdder = new TalkerAliasAutoAdder(aliasModel, channel.getAliasListName()); + + for(Module module: modules) + { + if(module instanceof TrafficChannelManager tcm && tcm.getTalkerAliasManager() != null) + { + tcm.getTalkerAliasManager().setTalkerAliasListener(autoAdder); + } + } + } + } + /** * Creates decoder modules for APCO-25 Phase 2 decoder * @param channel configuration diff --git a/src/main/java/io/github/dsheirer/module/decode/DecoderType.java b/src/main/java/io/github/dsheirer/module/decode/DecoderType.java index 8df22320..e5e15779 100644 --- a/src/main/java/io/github/dsheirer/module/decode/DecoderType.java +++ b/src/main/java/io/github/dsheirer/module/decode/DecoderType.java @@ -78,6 +78,11 @@ public enum DecoderType */ public static final EnumSet MBE_AUDIO_CODEC_DECODERS = EnumSet.of(DMR, NXDN, P25_PHASE1, P25_PHASE2); + /** + * Decoders that produce talker aliases + */ + public static final EnumSet TALKER_ALIAS_DECODERS = EnumSet.of(DMR, NXDN, P25_PHASE1, P25_PHASE2); + public Protocol getProtocol() { return mProtocol; @@ -114,4 +119,12 @@ public enum DecoderType { return MBE_AUDIO_CODEC_DECODERS.contains(this); } + + /** + * Indicates if the decoder type produces talker aliases + */ + public boolean providesTalkerAlias() + { + return TALKER_ALIAS_DECODERS.contains(this); + } } diff --git a/src/main/java/io/github/dsheirer/module/decode/dmr/DMRTrafficChannelManager.java b/src/main/java/io/github/dsheirer/module/decode/dmr/DMRTrafficChannelManager.java index f5467547..e83f1d11 100644 --- a/src/main/java/io/github/dsheirer/module/decode/dmr/DMRTrafficChannelManager.java +++ b/src/main/java/io/github/dsheirer/module/decode/dmr/DMRTrafficChannelManager.java @@ -135,6 +135,7 @@ public class DMRTrafficChannelManager extends TrafficChannelManager implements I /** * Talker alias manager */ + @Override public TalkerAliasManager getTalkerAliasManager() { return mTalkerAliasManager; diff --git a/src/main/java/io/github/dsheirer/module/decode/nxdn/NXDNTrafficChannelManager.java b/src/main/java/io/github/dsheirer/module/decode/nxdn/NXDNTrafficChannelManager.java index ac677ec2..18a991d6 100644 --- a/src/main/java/io/github/dsheirer/module/decode/nxdn/NXDNTrafficChannelManager.java +++ b/src/main/java/io/github/dsheirer/module/decode/nxdn/NXDNTrafficChannelManager.java @@ -148,6 +148,7 @@ public class NXDNTrafficChannelManager extends TrafficChannelManager implements /** * Talker alias manager */ + @Override public TalkerAliasManager getTalkerAliasManager() { return mTalkerAliasManager; diff --git a/src/main/java/io/github/dsheirer/module/decode/p25/P25TrafficChannelManager.java b/src/main/java/io/github/dsheirer/module/decode/p25/P25TrafficChannelManager.java index 3b5800c3..0873a416 100644 --- a/src/main/java/io/github/dsheirer/module/decode/p25/P25TrafficChannelManager.java +++ b/src/main/java/io/github/dsheirer/module/decode/p25/P25TrafficChannelManager.java @@ -165,6 +165,7 @@ public class P25TrafficChannelManager extends TrafficChannelManager implements I * Talker alias manager * @return manager */ + @Override public TalkerAliasManager getTalkerAliasManager() { return mTalkerAliasManager; diff --git a/src/main/java/io/github/dsheirer/module/decode/traffic/TrafficChannelManager.java b/src/main/java/io/github/dsheirer/module/decode/traffic/TrafficChannelManager.java index 0adf1b07..55de9dff 100644 --- a/src/main/java/io/github/dsheirer/module/decode/traffic/TrafficChannelManager.java +++ b/src/main/java/io/github/dsheirer/module/decode/traffic/TrafficChannelManager.java @@ -20,6 +20,7 @@ package io.github.dsheirer.module.decode.traffic; import io.github.dsheirer.controller.channel.Channel; +import io.github.dsheirer.identifier.alias.TalkerAliasManager; import io.github.dsheirer.module.Module; /** @@ -36,6 +37,15 @@ public abstract class TrafficChannelManager extends Module { } + /** + * Talker alias manager shared by the control and traffic channels of the system. + * @return talker alias manager, or null if the protocol doesn't have talker aliases. + */ + public TalkerAliasManager getTalkerAliasManager() + { + return null; + } + /** * Current control frequency * @return frequency in hertz diff --git a/src/test/java/io/github/dsheirer/alias/TalkerAliasAutoAdderTest.java b/src/test/java/io/github/dsheirer/alias/TalkerAliasAutoAdderTest.java new file mode 100644 index 00000000..ae793963 --- /dev/null +++ b/src/test/java/io/github/dsheirer/alias/TalkerAliasAutoAdderTest.java @@ -0,0 +1,215 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ + +package io.github.dsheirer.alias; + +import io.github.dsheirer.alias.TalkerAliasAutoAdder.Decision; +import io.github.dsheirer.alias.id.radio.P25FullyQualifiedRadio; +import io.github.dsheirer.alias.id.radio.Radio; +import io.github.dsheirer.alias.id.talkgroup.Talkgroup; +import io.github.dsheirer.identifier.alias.P25TalkerAliasIdentifier; +import io.github.dsheirer.identifier.alias.TalkerAliasManager; +import io.github.dsheirer.identifier.radio.RadioIdentifier; +import io.github.dsheirer.module.decode.p25.identifier.radio.APCO25FullyQualifiedRadioIdentifier; +import io.github.dsheirer.module.decode.p25.identifier.radio.APCO25RadioIdentifier; +import io.github.dsheirer.protocol.Protocol; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Tests for automatically adding talker aliases to an alias list. The JavaFX toolkit isn't running, so the alias model + * updates run on the calling thread. + */ +public class TalkerAliasAutoAdderTest +{ + private static final String LIST = "Test List"; + private static final RadioIdentifier RADIO = (RadioIdentifier)APCO25RadioIdentifier.createFrom(1234); + + private AliasModel mAliasModel; + private TalkerAliasManager mManager; + + @BeforeEach + public void setup() + { + mAliasModel = new AliasModel(); + mManager = new TalkerAliasManager(); + mManager.setTalkerAliasListener(new TalkerAliasAutoAdder(mAliasModel, LIST)); + } + + private void decode(RadioIdentifier radio, String alias) + { + mManager.update(radio, P25TalkerAliasIdentifier.create(alias)); + } + + private List aliases() + { + return mAliasModel.getAliases().stream().filter(a -> LIST.equals(a.getAliasListName())).toList(); + } + + private Alias alias(RadioIdentifier radio) + { + return TalkerAliasAutoAdder.findExactMatch(aliases(), TalkerAliasAutoAdder.createAliasId(radio)); + } + + @Test + public void createsAliasForRadio() + { + decode(RADIO, "ENGINE 12"); + assertEquals(1, aliases().size()); + Alias alias = alias(RADIO); + assertEquals("ENGINE 12", alias.getName()); + assertEquals(TalkerAliasAutoAdder.ALIAS_GROUP, alias.getGroup()); + assertTrue(alias.getAliasIdentifiers().get(0) instanceof Radio radio && radio.getValue() == 1234 && + radio.getProtocol() == Protocol.APCO25); + + decode(RADIO, "ENGINE 12"); + assertEquals(1, aliases().size(), "no duplicate alias"); + } + + @Test + public void fragmentNeverReplacesFullAlias() + { + decode(RADIO, "ENGINE 12 OFFICER"); + + for(int x = 0; x < 5; x++) + { + decode(RADIO, "ENGINE 12 OFF"); + decode(RADIO, "ENG"); + } + + assertEquals("ENGINE 12 OFFICER", alias(RADIO).getName()); + } + + @Test + public void fullAliasReplacesStoredFragment() + { + decode(RADIO, "ENGINE 1"); + assertEquals("ENGINE 1", alias(RADIO).getName()); + decode(RADIO, "ENGINE 12 OFFICER"); + assertEquals("ENGINE 12 OFFICER", alias(RADIO).getName()); + } + + @Test + public void differentAliasRequiresConfirmation() + { + decode(RADIO, "ENGINE 12"); + decode(RADIO, "LADDER 3"); + assertEquals("ENGINE 12", alias(RADIO).getName(), "a single decode doesn't rename"); + decode(RADIO, "LADDER 3"); + assertEquals("LADDER 3", alias(RADIO).getName(), "confirmed decode renames"); + } + + @Test + public void fragmentsOfNewAliasDontInterruptConfirmation() + { + decode(RADIO, "ENGINE 12"); + decode(RADIO, "LADDER 3 CAPTAIN"); + decode(RADIO, "LADDER"); + decode(RADIO, "LADDER 3 CAPTAIN"); + assertEquals("LADDER 3 CAPTAIN", alias(RADIO).getName()); + } + + @Test + public void interleavedDifferentAliasesDontConfirm() + { + decode(RADIO, "ENGINE 12"); + decode(RADIO, "LADDER 3"); + decode(RADIO, "RESCUE 5"); + decode(RADIO, "LADDER 3"); + assertEquals("ENGINE 12", alias(RADIO).getName()); + } + + @Test + public void multiIdentifierAliasIsLeftAlone() + { + Alias alias = new Alias("Portables"); + alias.setAliasListName(LIST); + alias.addAliasID(new Radio(Protocol.APCO25, 1234)); + alias.addAliasID(new Radio(Protocol.APCO25, 5678)); + mAliasModel.addAlias(alias); + + decode(RADIO, "ENGINE 12"); + decode(RADIO, "ENGINE 12"); + assertEquals(1, aliases().size()); + assertEquals("Portables", alias.getName()); + } + + @Test + public void userAliasForRadioIsRenamed() + { + Alias alias = new Alias("Old Name"); + alias.setAliasListName(LIST); + alias.addAliasID(new Radio(Protocol.APCO25, 1234)); + alias.addAliasID(new io.github.dsheirer.alias.id.priority.Priority(1)); + mAliasModel.addAlias(alias); + + decode(RADIO, "ENGINE 12"); + decode(RADIO, "ENGINE 12"); + assertEquals(1, aliases().size()); + assertEquals("ENGINE 12", alias.getName()); + } + + @Test + public void talkgroupAliasWithSameValueIsIgnored() + { + Alias alias = new Alias("Dispatch"); + alias.setAliasListName(LIST); + alias.addAliasID(new Talkgroup(Protocol.APCO25, 1234)); + mAliasModel.addAlias(alias); + + decode(RADIO, "ENGINE 12"); + assertEquals(2, aliases().size()); + assertEquals("Dispatch", alias.getName()); + } + + @Test + public void fullyQualifiedRadio() + { + RadioIdentifier roaming = APCO25FullyQualifiedRadioIdentifier.createFrom(1234, 0xBEE00, 0x123, 777); + decode(roaming, "ROAMER"); + Alias alias = alias(roaming); + assertTrue(alias.getAliasIdentifiers().get(0) instanceof P25FullyQualifiedRadio); + assertNull(alias(RADIO), "local radio with the same value isn't aliased"); + } + + @Test + public void decide() + { + assertEquals(Decision.UPDATE, TalkerAliasAutoAdder.decide(null, "A", 1)); + assertEquals(Decision.UNCHANGED, TalkerAliasAutoAdder.decide("ABC", "ABC", 1)); + assertEquals(Decision.FRAGMENT, TalkerAliasAutoAdder.decide("ABCDEF", "ABC", 5)); + assertEquals(Decision.UPDATE, TalkerAliasAutoAdder.decide("ABC", "ABCDEF", 1)); + assertEquals(Decision.AWAIT_CONFIRMATION, TalkerAliasAutoAdder.decide("ABC", "XYZ", 1)); + assertEquals(Decision.UPDATE, TalkerAliasAutoAdder.decide("ABC", "XYZ", 2)); + assertEquals(Decision.AWAIT_CONFIRMATION, TalkerAliasAutoAdder.decide("ABC", "X�Z", 5)); + } + + @Test + public void normalize() + { + assertEquals("JOS", TalkerAliasAutoAdder.normalize("JOS�")); + assertEquals("ENGINE 12", TalkerAliasAutoAdder.normalize(" ENGINE 12\u0000\u0000 ")); + assertNull(TalkerAliasAutoAdder.normalize("\u0000 ")); + } +} diff --git a/src/test/java/io/github/dsheirer/controller/channel/ChannelAutoAddTalkerAliasesTest.java b/src/test/java/io/github/dsheirer/controller/channel/ChannelAutoAddTalkerAliasesTest.java new file mode 100644 index 00000000..0137983b --- /dev/null +++ b/src/test/java/io/github/dsheirer/controller/channel/ChannelAutoAddTalkerAliasesTest.java @@ -0,0 +1,49 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.controller.channel; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule; +import com.fasterxml.jackson.dataformat.xml.XmlMapper; +import io.github.dsheirer.playlist.PlaylistV2; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +/** + * Tests channel auto-add talker aliases default and playlist persistence. + */ +public class ChannelAutoAddTalkerAliasesTest { + @Test public void newChannelsDefaultOnLegacyChannelsStayOff() throws Exception { + JacksonXmlModule m = new JacksonXmlModule(); m.setDefaultUseWrapper(false); + XmlMapper mapper = new XmlMapper(m); + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + assertTrue(new Channel().isAutoAddTalkerAliases(), "new channel default on"); + assertTrue(new Channel("n").isAutoAddTalkerAliases(), "new named channel default on"); + Channel off = new Channel("x"); off.setAutoAddTalkerAliases(false); + assertFalse(mapper.readValue(mapper.writeValueAsString(off), Channel.class).isAutoAddTalkerAliases()); + assertTrue(mapper.readValue(mapper.writeValueAsString(new Channel("y")), Channel.class).isAutoAddTalkerAliases()); + String legacy = mapper.writeValueAsString(new Channel("z")).replace(" auto_add_talker_aliases=\"true\"", ""); + assertFalse(legacy.contains("auto_add")); + Channel legacyChannel = mapper.readValue(legacy, Channel.class); + assertFalse(legacyChannel.isAutoAddTalkerAliases(), "legacy channel stays off"); + assertEquals("z", legacyChannel.getName()); + PlaylistV2 pl = new PlaylistV2(); pl.getChannels().add(new Channel("p")); + String plXml = mapper.writeValueAsString(pl).replace(" auto_add_talker_aliases=\"true\"", ""); + assertFalse(mapper.readValue(plXml, PlaylistV2.class).getChannels().get(0).isAutoAddTalkerAliases(), "legacy playlist off"); + off.setAutoAddTalkerAliases(false); assertFalse(off.copyOf().isAutoAddTalkerAliases()); + } +}