From 6070160fb69090913201ecccbff95994c28bfb72 Mon Sep 17 00:00:00 2001 From: ericek111 Date: Wed, 14 Oct 2026 18:25:51 +0000 Subject: [PATCH] Android servers get their shared parts from ServerParts, not SessionController Co-Authored-By: Claude Opus 5.5 --- .../eu/lixko/ts3j/client/android/AppParts.kt | 4 +- .../ts3j/client/android/ServerController.kt | 34 ++++++++-------- .../lixko/ts3j/client/android/ServerParts.kt | 34 ++++++++++++++++ .../ts3j/client/android/SessionController.kt | 40 +++++-------------- 4 files changed, 65 insertions(+), 47 deletions(-) create mode 100644 android/app/src/main/java/eu/lixko/ts3j/client/android/ServerParts.kt diff --git a/android/app/src/main/java/eu/lixko/ts3j/client/android/AppParts.kt b/android/app/src/main/java/eu/lixko/ts3j/client/android/AppParts.kt index 609e4d2..34be9c3 100644 --- a/android/app/src/main/java/eu/lixko/ts3j/client/android/AppParts.kt +++ b/android/app/src/main/java/eu/lixko/ts3j/client/android/AppParts.kt @@ -29,5 +29,7 @@ class AppParts(context: Context) { val myTeamSpeak: MyTeamSpeakAccount = MyTeamSpeakAccount(context, pictures, TeamSpeakImporter(identityStore, bookmarkStore, settings, contactStore), bookmarks, identities) { sessions.myTeamSpeakChanged() } - val sessions: SessionController = SessionController(context, settings, identityStore, identities, bookmarks, contacts, audio, myTeamSpeak) + private val serverParts = ServerParts(context, settings, identityStore, audio, contactStore, myTeamSpeak) + val sessions: SessionController = SessionController(context, settings, identities, bookmarks, serverParts, + LevelMonitor(settings, audio.audio)) } diff --git a/android/app/src/main/java/eu/lixko/ts3j/client/android/ServerController.kt b/android/app/src/main/java/eu/lixko/ts3j/client/android/ServerController.kt index dba8358..3a02e09 100644 --- a/android/app/src/main/java/eu/lixko/ts3j/client/android/ServerController.kt +++ b/android/app/src/main/java/eu/lixko/ts3j/client/android/ServerController.kt @@ -44,12 +44,12 @@ import kotlin.coroutines.suspendCoroutine class ServerController internal constructor( /** Unique among the servers of this process run. */ val id: Int, + private val parts: ServerParts, + /** The owner, told of what happens to this server. */ private val app: SessionController, ) { - internal val session = ServerSession( - app.settings, app.identityStore, app.audio, app.sounds, app.contactStore, Listener() - ) - private val network = app.network.follow(session, ::refresh, ::system) + internal val session = parts.session(Listener()) + private val network = parts.network.follow(session, ::refresh, ::system) /** Whether this server holds the microphone; set by [SessionController] as it moves. */ @Volatile @@ -82,7 +82,7 @@ class ServerController internal constructor( set(value) { field = value if (value != null) markRead(value) - if (value != null && value.isPrivate) app.notifier.clear(id, value.clientId()) + if (value != null && value.isPrivate) parts.notifier.clear(id, value.clientId()) } /** Counts the answers to info requests, for the details on screen to be read again. */ @@ -262,7 +262,7 @@ class ServerController internal constructor( // ---- files ---- - val files = app.fileTransfers(session.files()) + val files = parts.fileTransfers(session.files()) /** The files in a directory of a channel's repository; empty for an empty one. */ suspend fun listFiles(channelId: Int, password: String, path: String): Result> = @@ -343,7 +343,7 @@ class ServerController internal constructor( suspend fun setAvatar(data: ByteArray): Result { if (!session.isConnected) return Result.failure(RequestException.notConnected()) val file = withContext(Dispatchers.IO) { - File.createTempFile("avatar", null, app.cacheDir).apply { writeBytes(data) } + File.createTempFile("avatar", null, parts.cacheDir).apply { writeBytes(data) } } return try { session.avatarAdmin().set(file).done() @@ -360,7 +360,7 @@ class ServerController internal constructor( fun channelDetails(channelId: Int): Details? = session.model().getChannel(channelId)?.let(Details::channel) fun clientDetails(clientId: Int): Details? = session.model().getClient(clientId)?.let { - Details.client(it, session.model(), app.contactStore.find(it.uniqueId)) + Details.client(it, session.model(), parts.contactStore.find(it.uniqueId)) } /** Asks the server for what the channel's details lack; they change once it answers. */ @@ -376,7 +376,7 @@ class ServerController internal constructor( /** Files a client on the server as a contact; a new entry takes the type's defaults. */ fun addContact(clientId: Int, type: ContactType) { val client = session.model().getClient(clientId) ?: return - app.contactStore.add(client.uniqueId, client.nickname, type, app.settings.contactDefaults) + parts.contactStore.add(client.uniqueId, client.nickname, type, parts.settings.contactDefaults) } internal fun contactsChanged() { @@ -405,8 +405,8 @@ class ServerController internal constructor( /** Shares the file at [uri] in a chat, through myTeamSpeak; failures are told in the chat or as errors. */ suspend fun sendFile(key: ChatKey, uri: Uri) { - app.openShared(uri) - .onSuccess { session.sendAttachment(key, app.attachments, it.data, it.size, it.name) } + parts.openShared(uri) + .onSuccess { session.sendAttachment(key, parts.attachments, it.data, it.size, it.name) } .onFailure { app.error(it.message ?: "Could not read the file") } } @@ -424,7 +424,7 @@ class ServerController internal constructor( /** Answers a private message from its notification. */ internal fun reply(clientId: Int, text: String) { - if (session.isConnected) send(ChatKey.privateWith(clientId), text) else app.notifier.clear(id, clientId) + if (session.isConnected) send(ChatKey.privateWith(clientId), text) else parts.notifier.clear(id, clientId) } private fun addLine(key: ChatKey, title: String, html: String) { @@ -456,7 +456,7 @@ class ServerController internal constructor( away = session.isAway, selfClientId = if (session.isConnected) session.selfClientId() else 0, channelId = if (session.isConnected) self?.channelId ?: 0 else 0, - inputMode = app.settings.inputMode.get(), + inputMode = parts.settings.inputMode.get(), hostButton = if (session.isConnected) session.model().hostButton() else null, self = session.selfState(), whisper = session.whisper()?.label, @@ -479,7 +479,7 @@ class ServerController internal constructor( treePending.set(false) if (structureChanged.getAndSet(false)) roots = session.model().buildTree() _tree.value = ServerTree.of(roots, session.model(), session.contacts().store(), - app.settings.colorContacts.get(), app.settings.countryFlags.get(), app.settings.clientRowColors) + parts.settings.colorContacts.get(), parts.settings.countryFlags.get(), parts.settings.clientRowColors) refresh() } } @@ -502,11 +502,11 @@ class ServerController internal constructor( override fun closePrivateChats() = _chats.update { it.copy(private = emptyList()) } override fun onPrivateMessage(fromClientId: Int, fromName: String, message: String) { - if (!app.foreground) app.notifier.received(id, fromClientId, fromName, ChatHtml.plainMessage(message)) + if (!app.foreground) parts.notifier.received(id, fromClientId, fromName, ChatHtml.plainMessage(message)) } override fun onPrivateMessageSent(toClientId: Int, message: String) = - app.notifier.sent(id, toClientId, ChatHtml.plainMessage(message)) + parts.notifier.sent(id, toClientId, ChatHtml.plainMessage(message)) override fun onConnected() { network.onConnected() @@ -516,7 +516,7 @@ class ServerController internal constructor( override fun onDisconnected(reason: String?) { network.onDisconnected() - app.notifier.clearServer(id) + parts.notifier.clearServer(id) app.disconnected(this@ServerController) scheduleTree(structure = true) refresh() diff --git a/android/app/src/main/java/eu/lixko/ts3j/client/android/ServerParts.kt b/android/app/src/main/java/eu/lixko/ts3j/client/android/ServerParts.kt new file mode 100644 index 0000000..9f4349e --- /dev/null +++ b/android/app/src/main/java/eu/lixko/ts3j/client/android/ServerParts.kt @@ -0,0 +1,34 @@ +package eu.lixko.ts3j.client.android + +import android.content.Context +import android.net.Uri +import eu.lixko.ts3j.client.config.IdentityStore +import eu.lixko.ts3j.client.config.Settings +import eu.lixko.ts3j.client.contacts.ContactStore +import eu.lixko.ts3j.client.net.ChannelFiles +import eu.lixko.ts3j.client.session.ServerSession +import java.io.File + +/** What every server the app is on runs on and reports to, shared by all of them. */ +class ServerParts internal constructor( + private val context: Context, + internal val settings: Settings, + private val identityStore: IdentityStore, + private val audio: AudioSetup, + internal val contactStore: ContactStore, + private val myTeamSpeak: MyTeamSpeakAccount, +) { + internal val notifier = MessageNotifier(context) + internal val network = NetworkFollower(context) + internal val cacheDir: File get() = context.cacheDir + internal val attachments get() = myTeamSpeak.attachments + + internal fun session(listener: ServerSession.Listener) = + ServerSession(settings, identityStore, audio.audio, audio.sounds, contactStore, listener).also { + it.setMyTeamSpeakLogin(myTeamSpeak.login) + } + + internal fun fileTransfers(files: ChannelFiles) = FileTransfers(context, files) + + internal suspend fun openShared(uri: Uri) = myTeamSpeak.openShared(uri) +} diff --git a/android/app/src/main/java/eu/lixko/ts3j/client/android/SessionController.kt b/android/app/src/main/java/eu/lixko/ts3j/client/android/SessionController.kt index ddcf30d..b34de07 100644 --- a/android/app/src/main/java/eu/lixko/ts3j/client/android/SessionController.kt +++ b/android/app/src/main/java/eu/lixko/ts3j/client/android/SessionController.kt @@ -4,15 +4,12 @@ import android.Manifest import android.content.Context import android.content.Intent import android.content.pm.PackageManager -import android.net.Uri import android.os.Handler import android.os.Looper import eu.lixko.ts3j.client.config.Bookmark -import eu.lixko.ts3j.client.config.IdentityStore import eu.lixko.ts3j.client.config.Setting import eu.lixko.ts3j.client.config.Settings import eu.lixko.ts3j.client.net.HostButton -import eu.lixko.ts3j.client.net.ChannelFiles import eu.lixko.ts3j.client.session.SelfState import eu.lixko.ts3j.client.session.ServerLink import eu.lixko.ts3j.client.session.ServerSession @@ -23,7 +20,6 @@ import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.SharedFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.update -import java.io.File import java.util.concurrent.atomic.AtomicInteger /** What the app shows of a connection and the local client on it. */ @@ -67,24 +63,12 @@ sealed interface SessionEvent { */ class SessionController internal constructor( private val context: Context, - internal val settings: Settings, - internal val identityStore: IdentityStore, + private val settings: Settings, private val identities: IdentityManager, private val bookmarks: BookmarkManager, - private val contacts: ContactManager, - private val audioSetup: AudioSetup, - private val myTeamSpeak: MyTeamSpeakAccount, + private val parts: ServerParts, + private val levels: LevelMonitor, ) { - internal val cacheDir: File get() = context.cacheDir - internal fun fileTransfers(files: ChannelFiles) = FileTransfers(context, files) - internal val audio get() = audioSetup.audio - internal val sounds get() = audioSetup.sounds - internal val notifier = MessageNotifier(context) - internal val contactStore get() = contacts.store - internal val attachments get() = myTeamSpeak.attachments - internal suspend fun openShared(uri: Uri) = myTeamSpeak.openShared(uri) - internal val network = NetworkFollower(context) - private val main = Handler(Looper.getMainLooper()) private val nextId = AtomicInteger() /** Touched on the main thread only, as [Sessions] wants. */ @@ -117,19 +101,18 @@ class SessionController internal constructor( get() = _onScreen.value set(value) { _onScreen.value = value - if (value) notifier.clearPokes() + if (value) parts.notifier.clearPokes() // The process may be let go of any time once out of sight. - else contactStore.flush() + else parts.contactStore.flush() } - private val levels = LevelMonitor(settings, audio) val micLevel: StateFlow = levels.level @Volatile private var watchingLevel = false init { // Mutations report on whichever thread made them, a connection thread included. - contactStore.addListener { _servers.value.forEach(ServerController::contactsChanged) } + parts.contactStore.addListener { _servers.value.forEach(ServerController::contactsChanged) } Setting.onAnyChange(::applyOutputSettings, settings.outputDevice, settings.outputVolume, settings.receiveBuffer) settings.countryFlags.addListener { servers.value.forEach { it.scheduleTree(structure = false) } } } @@ -162,8 +145,7 @@ class SessionController internal constructor( } private fun start(connect: (ServerController) -> Unit) { - val server = ServerController(nextId.incrementAndGet(), this) - server.session.setMyTeamSpeakLogin(myTeamSpeak.login) + val server = ServerController(nextId.incrementAndGet(), parts, this) sessions.add(server.session) _servers.update { it + server } _current.value = server @@ -220,8 +202,8 @@ class SessionController internal constructor( val servers = _servers.value servers.forEach(ServerController::leaving) ServerSession.shutdownAll(servers.map { it.session }) - contactStore.flush() - attachments.close() + parts.contactStore.flush() + parts.attachments.close() } fun server(id: Int): ServerController? = _servers.value.firstOrNull { it.id == id } @@ -263,7 +245,7 @@ class SessionController internal constructor( internal fun poked(from: String, message: String) { _events.tryEmit(SessionEvent.Poke(from, message)) - if (!foreground) notifier.poke(from, BBCode.toPlain(message)) + if (!foreground) parts.notifier.poke(from, BBCode.toPlain(message)) } internal fun talking(transmitting: Boolean) = levels.talking(transmitting) @@ -370,7 +352,7 @@ class SessionController internal constructor( /** Answers a private message from its notification. */ fun reply(serverId: Int, clientId: Int, text: String) { - server(serverId)?.reply(clientId, text) ?: notifier.clear(serverId, clientId) + server(serverId)?.reply(clientId, text) ?: parts.notifier.clear(serverId, clientId) } companion object {