Android: whisper to channels, clients and whisper lists

The channel and client sheets whisper to them or add them to a whisper
list. The top bar's whisper menu picks a list, replies to the last
whisper, stops whispering and edits the lists; a banner over the voice
bar says who the microphone whispers to.

Core keeps the whisper on the session, finding its channels by path and
its clients by unique id again as the server changes, and the saved lists
in whisperlists.properties. ts3j now sends whisper packets.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-09 22:19:52 +00:00
co-authored by Claude Opus 5.5
parent eed8049100
commit a334173fcc
11 changed files with 646 additions and 6 deletions
@@ -0,0 +1,40 @@
package com.ts3client.config;
import java.util.ArrayList;
import java.util.List;
/** A saved set of channels and clients to whisper to, wherever they are on the server we are on. */
public final class WhisperList {
/** A client by unique id, so the list finds them under any client id; the nickname is for showing. */
public record Client(String uniqueId, String nickname) {
}
public String name = "";
/** Channel references as {@link Bookmark#channel} holds them: "/"-separated paths or "/&lt;id&gt;". */
public final List<String> channels = new ArrayList<>();
public final List<Client> clients = new ArrayList<>();
public WhisperList() {
}
public WhisperList(String name) {
this.name = name;
}
public WhisperList copy() {
WhisperList c = new WhisperList(name);
c.channels.addAll(channels);
c.clients.addAll(clients);
return c;
}
public boolean isEmpty() {
return channels.isEmpty() && clients.isEmpty();
}
@Override
public String toString() {
return name;
}
}
@@ -0,0 +1,93 @@
package com.ts3client.config;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/** The saved {@link WhisperList}s, shared by every server. */
public final class WhisperLists {
private static File file() {
return AppDirs.file("whisperlists.properties");
}
private final List<WhisperList> entries = new ArrayList<>();
public List<WhisperList> all() {
return entries;
}
/** The list with this name, ignoring case, or null. */
public WhisperList find(String name) {
for (WhisperList l : entries) {
if (l.name.equalsIgnoreCase(name)) return l;
}
return null;
}
public static WhisperLists load() {
WhisperLists lists = new WhisperLists();
if (!file().isFile()) return lists;
Properties p = new Properties();
try (FileInputStream in = new FileInputStream(file())) {
p.load(in);
} catch (Exception e) {
return lists;
}
int count = parseInt(p.getProperty("count"));
for (int i = 0; i < count; i++) {
String prefix = "list." + i + ".";
WhisperList list = new WhisperList(p.getProperty(prefix + "name", ""));
int channels = parseInt(p.getProperty(prefix + "channels"));
for (int j = 0; j < channels; j++) {
String channel = p.getProperty(prefix + "channel." + j, "");
if (!channel.isBlank()) list.channels.add(channel);
}
int clients = parseInt(p.getProperty(prefix + "clients"));
for (int j = 0; j < clients; j++) {
String uid = p.getProperty(prefix + "client." + j + ".uid", "");
if (!uid.isBlank()) {
list.clients.add(new WhisperList.Client(uid, p.getProperty(prefix + "client." + j + ".name", "")));
}
}
lists.entries.add(list);
}
return lists;
}
public void save() {
Properties p = new Properties();
p.setProperty("count", Integer.toString(entries.size()));
for (int i = 0; i < entries.size(); i++) {
WhisperList list = entries.get(i);
String prefix = "list." + i + ".";
p.setProperty(prefix + "name", list.name);
p.setProperty(prefix + "channels", Integer.toString(list.channels.size()));
for (int j = 0; j < list.channels.size(); j++) {
p.setProperty(prefix + "channel." + j, list.channels.get(j));
}
p.setProperty(prefix + "clients", Integer.toString(list.clients.size()));
for (int j = 0; j < list.clients.size(); j++) {
WhisperList.Client c = list.clients.get(j);
p.setProperty(prefix + "client." + j + ".uid", c.uniqueId());
p.setProperty(prefix + "client." + j + ".name", c.nickname() == null ? "" : c.nickname());
}
}
try {
AppDirs.createProfile();
ProfileFiles.write(file(), out -> p.store(out, "TS3J client whisper lists"));
} catch (Exception ignored) {
}
}
private static int parseInt(String v) {
if (v == null) return 0;
try {
return Integer.parseInt(v.trim());
} catch (NumberFormatException e) {
return 0;
}
}
}
@@ -9,6 +9,7 @@ import com.github.manevolent.ts3j.event.*;
import com.github.manevolent.ts3j.identity.LocalIdentity;
import com.github.manevolent.ts3j.identity.Uid;
import com.github.manevolent.ts3j.protocol.ProtocolRole;
import com.github.manevolent.ts3j.protocol.packet.PacketBody1VoiceWhisper;
import com.github.manevolent.ts3j.protocol.socket.client.LocalTeamspeakClientSocket;
import com.github.manevolent.ts3j.util.Ts3Crypt;
import com.ts3client.audio.AudioBackend;
@@ -121,6 +122,8 @@ public final class TeamspeakConnection implements TS3Listener {
*/
/** Package-private: read directly by {@link ConnectionStatsCollector}. */
volatile LocalTeamspeakClientSocket client;
/** The client whose whisper we heard last, to reply to; 0 for none yet. */
private volatile int lastWhisperer;
private volatile VoiceInput microphone;
private volatile VoiceOutput playback;
private volatile LocalIdentity identity;
@@ -291,6 +294,7 @@ public final class TeamspeakConnection implements TS3Listener {
serverPort = port;
model.clear();
lastWhisperer = 0;
playback = audio.createOutput(settings);
playback.setMasterVolume(settings.effectiveOutputVolume());
playback.setReceiveBuffer(settings.receiveBuffer);
@@ -336,7 +340,9 @@ public final class TeamspeakConnection implements TS3Listener {
client.addListener(events);
client.setVoiceHandler(playback::handleVoice);
client.setWhisperHandler(whisper -> {
if (isWhisperAllowed(whisper.getClientId())) playback.handleWhisper(whisper);
if (!isWhisperAllowed(whisper.getClientId())) return;
lastWhisperer = whisper.getClientId();
playback.handleWhisper(whisper);
});
client.setExceptionHandler(t -> {
// A late packet dispatched after close() rejects on the shut-down
@@ -1623,6 +1629,28 @@ public final class TeamspeakConnection implements TS3Listener {
return ft.downloadToMemory(0, "", "/" + name, AvatarAdmin.MAX_AVATAR_DOWNLOAD_BYTES);
}
// ---- whisper ----
/** Sends the microphone's voice to {@code targets} as a whisper, or to our channel again with null. */
public void setWhisperTargets(Whisper.Targets targets) {
LocalTeamspeakClientSocket sock = client;
if (sock == null) return;
if (targets == null) {
sock.setWhisperTarget(null);
return;
}
PacketBody1VoiceWhisper.WhisperTargetMultiple target = new PacketBody1VoiceWhisper.WhisperTargetMultiple();
target.setChannelIds(targets.channelIds());
target.setClientIds(targets.clientIds());
sock.setWhisperTarget(target);
}
/** The client who whispered to us last, if still on the server. */
public ClientEntry lastWhisperer() {
int id = lastWhisperer;
return id == 0 ? null : model.getClient(id);
}
// ---- file transfers ----
/** Whether a live connection capable of file transfers is available. */
@@ -0,0 +1,76 @@
package com.ts3client.net;
import com.ts3client.config.WhisperList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
/**
* Where the microphone whispers to instead of talking in our channel: channels and clients,
* found on the server by reference so they follow renames, moves and reconnects.
*/
public final class Whisper {
/** What the whisper is shown as: the list's, the channel's or the client's name. */
public final String label;
/** Channel references as {@link ServerModel#findChannelByPath} reads them. */
private final List<String> channels;
private final List<String> clientUniqueIds;
private Whisper(String label, List<String> channels, List<String> clientUniqueIds) {
this.label = label;
this.channels = channels;
this.clientUniqueIds = clientUniqueIds;
}
public static Whisper toChannel(ChannelNode channel) {
return new Whisper(channel.name, List.of("/" + channel.id), List.of());
}
public static Whisper toClient(ClientEntry client) {
return new Whisper(client.nickname, List.of(), List.of(client.uniqueId));
}
public static Whisper of(WhisperList list) {
List<String> uids = new ArrayList<>();
for (WhisperList.Client c : list.clients) uids.add(c.uniqueId());
return new Whisper(list.name, List.copyOf(list.channels), uids);
}
/** Who hears it on the server as it is now; what is not there is left out. */
public Targets resolve(ServerModel model) {
Set<Integer> channelIds = new LinkedHashSet<>();
for (String ref : channels) {
ChannelNode c = model.findChannelByPath(ref);
if (c != null) channelIds.add(c.id);
}
Set<Integer> clientIds = new LinkedHashSet<>();
for (String uid : clientUniqueIds) {
ClientEntry c = model.findClientByUniqueId(uid);
if (c != null) clientIds.add(c.id);
}
return new Targets(channelIds.stream().mapToInt(Integer::intValue).toArray(),
clientIds.stream().mapToInt(Integer::intValue).toArray());
}
/** The channel and client ids a whisper goes to. */
public record Targets(int[] channelIds, int[] clientIds) {
public boolean isEmpty() {
return channelIds.length == 0 && clientIds.length == 0;
}
@Override
public boolean equals(Object o) {
return o instanceof Targets t && Arrays.equals(channelIds, t.channelIds) && Arrays.equals(clientIds, t.clientIds);
}
@Override
public int hashCode() {
return 31 * Arrays.hashCode(channelIds) + Arrays.hashCode(clientIds);
}
}
}
@@ -14,6 +14,7 @@ import com.ts3client.net.ClientEntry;
import com.ts3client.net.ConnectionListener;
import com.ts3client.net.ServerModel;
import com.ts3client.net.TeamspeakConnection;
import com.ts3client.net.Whisper;
import com.ts3client.recording.VoiceRecorder;
import com.ts3client.sound.SoundNotifier;
@@ -95,6 +96,10 @@ public final class ServerSession {
/** Away message currently published, empty when away carries no message. */
private volatile String awayMessage = "";
private volatile boolean commander;
/** Where the microphone whispers to; null when it talks in our channel. */
private volatile Whisper whisper;
/** {@link #whisper} as last resolved on the server, to resend only when it changes. */
private volatile Whisper.Targets whisperTargets;
public ServerSession(Settings settings, IdentityStore identities, AudioBackend audio,
SoundNotifier sounds, ContactStore contacts, Listener listener) {
@@ -388,6 +393,44 @@ public final class ServerSession {
if (conn.isConnected()) conn.setPhoneticNickname(phoneticNickname);
}
// ---- whisper ----
public Whisper whisper() {
return whisper;
}
/** Whispers to {@code whisper} from now on, or talks in our channel again with null. */
public void setWhisper(Whisper whisper) {
this.whisper = whisper;
whisperTargets = null;
if (whisper == null) conn.setWhisperTargets(null);
else updateWhisperTargets();
listener.onSessionChanged();
}
/** Whether whispering finds nobody there now, so nobody hears it. */
public boolean isWhisperingToNobody() {
Whisper.Targets targets = whisperTargets;
return whisper != null && targets != null && targets.isEmpty();
}
/** The client who whispered to us last, to whisper back to; null when none is on. */
public ClientEntry lastWhisperer() {
return conn.isConnected() ? conn.lastWhisperer() : null;
}
/** Follows the whisper's channels and clients around the server. */
private void updateWhisperTargets() {
Whisper w = whisper;
if (w == null || !conn.isConnected()) return;
Whisper.Targets targets = w.resolve(conn.getModel());
if (targets.equals(whisperTargets)) return;
boolean wasEmpty = isWhisperingToNobody();
whisperTargets = targets;
conn.setWhisperTargets(targets);
if (targets.isEmpty() != wasEmpty) listener.onSessionChanged();
}
/** What the local client looks like on this server. */
public SelfState selfState() {
if (!conn.isConnected()) return SelfState.DISCONNECTED;
@@ -715,6 +758,9 @@ public final class ServerSession {
@Override
public void onDisconnected(String reason) {
connecting = false;
// Channels and clients are this server's; another one has its own.
whisper = null;
whisperTargets = null;
serverIconId = -1;
conn.getModel().clear();
listener.onDisconnected(reason);
@@ -733,6 +779,7 @@ public final class ServerSession {
listener.onSessionChanged();
}
checkServerIcon();
updateWhisperTargets();
listener.onModelChanged();
}