diff --git a/ts3-client/core/src/main/java/eu/lixko/ts3j/client/session/Sessions.java b/ts3-client/core/src/main/java/eu/lixko/ts3j/client/session/Sessions.java index 8d8d33d..405451f 100644 --- a/ts3-client/core/src/main/java/eu/lixko/ts3j/client/session/Sessions.java +++ b/ts3-client/core/src/main/java/eu/lixko/ts3j/client/session/Sessions.java @@ -6,6 +6,7 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.RejectedExecutionException; /** * Every server the client is on, and what they share. Only one of them can capture with the @@ -73,7 +74,7 @@ public final class Sessions { if (microphoneOwner == session) return; ServerSession previous = microphoneOwner; microphoneOwner = session; - handover.execute(() -> { + handOver(() -> { if (previous != null) previous.setMicrophoneActive(false); if (session != null) session.setMicrophoneActive(true); }); @@ -105,7 +106,7 @@ public final class Sessions { public void disconnected(ServerSession session) { if (microphoneOwner == session) { microphoneOwner = null; - handover.execute(() -> session.setMicrophoneActive(false)); + handOver(() -> session.setMicrophoneActive(false)); } assignMicrophoneIfFree(); } @@ -134,6 +135,18 @@ public final class Sessions { return session == null || !session.isConnected() ? null : session.microphone(); } + /** + * Runs a hand-over on {@link #handover}. Leaving every server on {@link #shutdown} reports + * each disconnect to the frontend, which may pass it on here after the thread is gone; + * with every server left, there is nothing to hand over. + */ + private void handOver(Runnable change) { + try { + handover.execute(change); + } catch (RejectedExecutionException ignored) { + } + } + // ---- the user's own state ---- public void setAwayEverywhere(boolean away, String message) { diff --git a/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/DesktopNotifier.java b/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/DesktopNotifier.java index 34fb7b8..bc9753f 100644 --- a/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/DesktopNotifier.java +++ b/ts3-client/swing/src/main/java/eu/lixko/ts3j/client/ui/DesktopNotifier.java @@ -19,6 +19,7 @@ import java.util.Locale; import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.RejectedExecutionException; /** * Tells the user about pokes and private messages through the desktop's notifications @@ -66,7 +67,7 @@ public final class DesktopNotifier { if (!wanted()) return; String body = BBCode.toPlain(message); Notice notice = new Notice(from + " poked you", body, () -> open(tab, null), null); - worker.execute(() -> { + later(() -> { DesktopNotifications out = backend(); if (out == null) return; int id = out.show(notice, 0); @@ -78,7 +79,7 @@ public final class DesktopNotifier { if (!wanted()) return; String text = ChatHtml.plainMessage(message); Conversation key = new Conversation(tab, clientId); - worker.execute(() -> { + later(() -> { DesktopNotifications out = backend(); if (out == null) return; Shown shown = conversations.computeIfAbsent(key, k -> new Shown()); @@ -92,7 +93,7 @@ public final class DesktopNotifier { /** The window is in front again: what was notified of is in plain sight. */ void windowActivated() { - worker.execute(() -> { + later(() -> { if (backend == null) return; conversations.values().forEach(shown -> backend.close(shown.id)); conversations.clear(); @@ -103,7 +104,7 @@ public final class DesktopNotifier { /** A private chat on a server that went away can neither be opened nor answered. */ void forget(ServerTab tab) { - worker.execute(() -> conversations.entrySet().removeIf(e -> { + later(() -> conversations.entrySet().removeIf(e -> { if (e.getKey().tab() != tab) return false; if (backend != null) backend.close(e.getValue().id); return true; @@ -111,12 +112,23 @@ public final class DesktopNotifier { } void shutdown() { - worker.execute(() -> { + later(() -> { if (backend != null) backend.shutdown(); }); worker.shutdown(); } + /** + * Runs on the worker. The frame may still pass on a server's disconnect after + * {@link #shutdown}, once the worker is gone; nothing is shown any more by then. + */ + private void later(Runnable task) { + try { + worker.execute(task); + } catch (RejectedExecutionException ignored) { + } + } + private boolean wanted() { // Some window of ours has the focus exactly when there is an active window. return settings.desktopNotifications.get()