Android servers get their shared parts from ServerParts, not SessionController

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-14 18:25:51 +00:00
co-authored by Claude Opus 5.5
parent 382d76c3d8
commit 6070160fb6
4 changed files with 65 additions and 47 deletions
@@ -29,5 +29,7 @@ class AppParts(context: Context) {
val myTeamSpeak: MyTeamSpeakAccount = MyTeamSpeakAccount(context, pictures, val myTeamSpeak: MyTeamSpeakAccount = MyTeamSpeakAccount(context, pictures,
TeamSpeakImporter(identityStore, bookmarkStore, settings, contactStore), bookmarks, identities) { sessions.myTeamSpeakChanged() } 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))
} }
@@ -44,12 +44,12 @@ import kotlin.coroutines.suspendCoroutine
class ServerController internal constructor( class ServerController internal constructor(
/** Unique among the servers of this process run. */ /** Unique among the servers of this process run. */
val id: Int, val id: Int,
private val parts: ServerParts,
/** The owner, told of what happens to this server. */
private val app: SessionController, private val app: SessionController,
) { ) {
internal val session = ServerSession( internal val session = parts.session(Listener())
app.settings, app.identityStore, app.audio, app.sounds, app.contactStore, Listener() private val network = parts.network.follow(session, ::refresh, ::system)
)
private val network = app.network.follow(session, ::refresh, ::system)
/** Whether this server holds the microphone; set by [SessionController] as it moves. */ /** Whether this server holds the microphone; set by [SessionController] as it moves. */
@Volatile @Volatile
@@ -82,7 +82,7 @@ class ServerController internal constructor(
set(value) { set(value) {
field = value field = value
if (value != null) markRead(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. */ /** Counts the answers to info requests, for the details on screen to be read again. */
@@ -262,7 +262,7 @@ class ServerController internal constructor(
// ---- files ---- // ---- 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. */ /** 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<List<RemoteFile>> = suspend fun listFiles(channelId: Int, password: String, path: String): Result<List<RemoteFile>> =
@@ -343,7 +343,7 @@ class ServerController internal constructor(
suspend fun setAvatar(data: ByteArray): Result<Unit> { suspend fun setAvatar(data: ByteArray): Result<Unit> {
if (!session.isConnected) return Result.failure(RequestException.notConnected()) if (!session.isConnected) return Result.failure(RequestException.notConnected())
val file = withContext(Dispatchers.IO) { 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 { return try {
session.avatarAdmin().set(file).done() 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 channelDetails(channelId: Int): Details? = session.model().getChannel(channelId)?.let(Details::channel)
fun clientDetails(clientId: Int): Details? = session.model().getClient(clientId)?.let { 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. */ /** 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. */ /** Files a client on the server as a contact; a new entry takes the type's defaults. */
fun addContact(clientId: Int, type: ContactType) { fun addContact(clientId: Int, type: ContactType) {
val client = session.model().getClient(clientId) ?: return 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() { 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. */ /** 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) { suspend fun sendFile(key: ChatKey, uri: Uri) {
app.openShared(uri) parts.openShared(uri)
.onSuccess { session.sendAttachment(key, app.attachments, it.data, it.size, it.name) } .onSuccess { session.sendAttachment(key, parts.attachments, it.data, it.size, it.name) }
.onFailure { app.error(it.message ?: "Could not read the file") } .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. */ /** Answers a private message from its notification. */
internal fun reply(clientId: Int, text: String) { 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) { private fun addLine(key: ChatKey, title: String, html: String) {
@@ -456,7 +456,7 @@ class ServerController internal constructor(
away = session.isAway, away = session.isAway,
selfClientId = if (session.isConnected) session.selfClientId() else 0, selfClientId = if (session.isConnected) session.selfClientId() else 0,
channelId = if (session.isConnected) self?.channelId ?: 0 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, hostButton = if (session.isConnected) session.model().hostButton() else null,
self = session.selfState(), self = session.selfState(),
whisper = session.whisper()?.label, whisper = session.whisper()?.label,
@@ -479,7 +479,7 @@ class ServerController internal constructor(
treePending.set(false) treePending.set(false)
if (structureChanged.getAndSet(false)) roots = session.model().buildTree() if (structureChanged.getAndSet(false)) roots = session.model().buildTree()
_tree.value = ServerTree.of(roots, session.model(), session.contacts().store(), _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() refresh()
} }
} }
@@ -502,11 +502,11 @@ class ServerController internal constructor(
override fun closePrivateChats() = _chats.update { it.copy(private = emptyList()) } override fun closePrivateChats() = _chats.update { it.copy(private = emptyList()) }
override fun onPrivateMessage(fromClientId: Int, fromName: String, message: String) { 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) = 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() { override fun onConnected() {
network.onConnected() network.onConnected()
@@ -516,7 +516,7 @@ class ServerController internal constructor(
override fun onDisconnected(reason: String?) { override fun onDisconnected(reason: String?) {
network.onDisconnected() network.onDisconnected()
app.notifier.clearServer(id) parts.notifier.clearServer(id)
app.disconnected(this@ServerController) app.disconnected(this@ServerController)
scheduleTree(structure = true) scheduleTree(structure = true)
refresh() refresh()
@@ -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)
}
@@ -4,15 +4,12 @@ import android.Manifest
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.net.Uri
import android.os.Handler import android.os.Handler
import android.os.Looper import android.os.Looper
import eu.lixko.ts3j.client.config.Bookmark 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.Setting
import eu.lixko.ts3j.client.config.Settings import eu.lixko.ts3j.client.config.Settings
import eu.lixko.ts3j.client.net.HostButton 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.SelfState
import eu.lixko.ts3j.client.session.ServerLink import eu.lixko.ts3j.client.session.ServerLink
import eu.lixko.ts3j.client.session.ServerSession 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.SharedFlow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
import java.io.File
import java.util.concurrent.atomic.AtomicInteger import java.util.concurrent.atomic.AtomicInteger
/** What the app shows of a connection and the local client on it. */ /** What the app shows of a connection and the local client on it. */
@@ -67,24 +63,12 @@ sealed interface SessionEvent {
*/ */
class SessionController internal constructor( class SessionController internal constructor(
private val context: Context, private val context: Context,
internal val settings: Settings, private val settings: Settings,
internal val identityStore: IdentityStore,
private val identities: IdentityManager, private val identities: IdentityManager,
private val bookmarks: BookmarkManager, private val bookmarks: BookmarkManager,
private val contacts: ContactManager, private val parts: ServerParts,
private val audioSetup: AudioSetup, private val levels: LevelMonitor,
private val myTeamSpeak: MyTeamSpeakAccount,
) { ) {
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 main = Handler(Looper.getMainLooper())
private val nextId = AtomicInteger() private val nextId = AtomicInteger()
/** Touched on the main thread only, as [Sessions] wants. */ /** Touched on the main thread only, as [Sessions] wants. */
@@ -117,19 +101,18 @@ class SessionController internal constructor(
get() = _onScreen.value get() = _onScreen.value
set(value) { set(value) {
_onScreen.value = 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. // 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<MicLevel> = levels.level val micLevel: StateFlow<MicLevel> = levels.level
@Volatile @Volatile
private var watchingLevel = false private var watchingLevel = false
init { init {
// Mutations report on whichever thread made them, a connection thread included. // 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) Setting.onAnyChange(::applyOutputSettings, settings.outputDevice, settings.outputVolume, settings.receiveBuffer)
settings.countryFlags.addListener { servers.value.forEach { it.scheduleTree(structure = false) } } settings.countryFlags.addListener { servers.value.forEach { it.scheduleTree(structure = false) } }
} }
@@ -162,8 +145,7 @@ class SessionController internal constructor(
} }
private fun start(connect: (ServerController) -> Unit) { private fun start(connect: (ServerController) -> Unit) {
val server = ServerController(nextId.incrementAndGet(), this) val server = ServerController(nextId.incrementAndGet(), parts, this)
server.session.setMyTeamSpeakLogin(myTeamSpeak.login)
sessions.add(server.session) sessions.add(server.session)
_servers.update { it + server } _servers.update { it + server }
_current.value = server _current.value = server
@@ -220,8 +202,8 @@ class SessionController internal constructor(
val servers = _servers.value val servers = _servers.value
servers.forEach(ServerController::leaving) servers.forEach(ServerController::leaving)
ServerSession.shutdownAll(servers.map { it.session }) ServerSession.shutdownAll(servers.map { it.session })
contactStore.flush() parts.contactStore.flush()
attachments.close() parts.attachments.close()
} }
fun server(id: Int): ServerController? = _servers.value.firstOrNull { it.id == id } 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) { internal fun poked(from: String, message: String) {
_events.tryEmit(SessionEvent.Poke(from, message)) _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) internal fun talking(transmitting: Boolean) = levels.talking(transmitting)
@@ -370,7 +352,7 @@ class SessionController internal constructor(
/** Answers a private message from its notification. */ /** Answers a private message from its notification. */
fun reply(serverId: Int, clientId: Int, text: String) { 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 { companion object {