From 065c62118edfaf3483df1fed82f77893b70801b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Thu, 24 Aug 2023 23:25:35 +0800 Subject: [PATCH] Update API usage --- .../sfa/bg/PlatformInterfaceWrapper.kt | 3 + .../java/io/nekohasekai/sfa/ktx/Wrappers.kt | 11 +++ .../sfa/ui/dashboard/OverviewFragment.kt | 99 ++++++++++++------- .../io/nekohasekai/sfa/utils/CommandClient.kt | 17 +++- 4 files changed, 95 insertions(+), 35 deletions(-) create mode 100644 app/src/main/java/io/nekohasekai/sfa/ktx/Wrappers.kt diff --git a/app/src/main/java/io/nekohasekai/sfa/bg/PlatformInterfaceWrapper.kt b/app/src/main/java/io/nekohasekai/sfa/bg/PlatformInterfaceWrapper.kt index 9dff401..e502370 100644 --- a/app/src/main/java/io/nekohasekai/sfa/bg/PlatformInterfaceWrapper.kt +++ b/app/src/main/java/io/nekohasekai/sfa/bg/PlatformInterfaceWrapper.kt @@ -98,6 +98,9 @@ interface PlatformInterfaceWrapper : PlatformInterface { return false } + override fun clearDNSCache() { + } + private class InterfaceArray(private val iterator: Enumeration) : NetworkInterfaceIterator { diff --git a/app/src/main/java/io/nekohasekai/sfa/ktx/Wrappers.kt b/app/src/main/java/io/nekohasekai/sfa/ktx/Wrappers.kt new file mode 100644 index 0000000..9631a79 --- /dev/null +++ b/app/src/main/java/io/nekohasekai/sfa/ktx/Wrappers.kt @@ -0,0 +1,11 @@ +package io.nekohasekai.sfa.ktx + +import io.nekohasekai.libbox.StringIterator + +fun StringIterator.toList(): List { + return mutableListOf().apply { + while (hasNext()) { + add(next()) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/io/nekohasekai/sfa/ui/dashboard/OverviewFragment.kt b/app/src/main/java/io/nekohasekai/sfa/ui/dashboard/OverviewFragment.kt index 46fc4a5..0a6bd17 100644 --- a/app/src/main/java/io/nekohasekai/sfa/ui/dashboard/OverviewFragment.kt +++ b/app/src/main/java/io/nekohasekai/sfa/ui/dashboard/OverviewFragment.kt @@ -29,13 +29,15 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.launch import kotlinx.coroutines.withContext -class OverviewFragment : Fragment(), CommandClient.Handler { +class OverviewFragment : Fragment() { private val activity: MainActivity? get() = super.getActivity() as MainActivity? private var _binding: FragmentDashboardOverviewBinding? = null private val binding get() = _binding!! - private val commandClient = - CommandClient(lifecycleScope, CommandClient.ConnectionType.Status, this) + private val statusClient = + CommandClient(lifecycleScope, CommandClient.ConnectionType.Status, StatusClient()) + private val clashModeClient = + CommandClient(lifecycleScope, CommandClient.ConnectionType.ClashMode, ClashModeClient()) private var _adapter: Adapter? = null private val adapter get() = _adapter!! @@ -61,7 +63,8 @@ class OverviewFragment : Fragment(), CommandClient.Handler { activity.serviceStatus.observe(viewLifecycleOwner) { binding.statusContainer.isVisible = it == Status.Starting || it == Status.Started if (it == Status.Started) { - commandClient.connect() + statusClient.connect() +// clashModeClient.connect() } } ProfileManager.registerCallback(this::updateProfiles) @@ -71,46 +74,76 @@ class OverviewFragment : Fragment(), CommandClient.Handler { super.onDestroyView() _adapter = null _binding = null - commandClient.disconnect() + statusClient.disconnect() +// clashModeClient.disconnect() ProfileManager.unregisterCallback(this::updateProfiles) } + override fun onPause() { + super.onPause() + statusClient.disconnect() +// clashModeClient.disconnect() + } + + override fun onResume() { + super.onResume() + statusClient.connect() +// clashModeClient.connect() + } + private fun updateProfiles() { _adapter?.reload() } - override fun onConnected() { - val binding = _binding ?: return - lifecycleScope.launch(Dispatchers.Main) { - binding.memoryText.text = getString(R.string.loading) - binding.goroutinesText.text = getString(R.string.loading) - } - } - override fun onDisconnected() { - val binding = _binding ?: return - lifecycleScope.launch(Dispatchers.Main) { - binding.memoryText.text = getString(R.string.loading) - binding.goroutinesText.text = getString(R.string.loading) - } - } + inner class StatusClient : CommandClient.Handler { - override fun updateStatus(status: StatusMessage) { - val binding = _binding ?: return - lifecycleScope.launch(Dispatchers.Main) { - binding.memoryText.text = Libbox.formatBytes(status.memory) - binding.goroutinesText.text = status.goroutines.toString() - val trafficAvailable = status.trafficAvailable - binding.trafficContainer.isVisible = trafficAvailable - if (trafficAvailable) { - binding.inboundConnectionsText.text = status.connectionsIn.toString() - binding.outboundConnectionsText.text = status.connectionsOut.toString() - binding.uplinkText.text = Libbox.formatBytes(status.uplink) + "/s" - binding.downlinkText.text = Libbox.formatBytes(status.downlink) + "/s" - binding.uplinkTotalText.text = Libbox.formatBytes(status.uplinkTotal) - binding.downlinkTotalText.text = Libbox.formatBytes(status.downlinkTotal) + override fun onConnected() { + val binding = _binding ?: return + lifecycleScope.launch(Dispatchers.Main) { + binding.memoryText.text = getString(R.string.loading) + binding.goroutinesText.text = getString(R.string.loading) } } + + override fun onDisconnected() { + val binding = _binding ?: return + lifecycleScope.launch(Dispatchers.Main) { + binding.memoryText.text = getString(R.string.loading) + binding.goroutinesText.text = getString(R.string.loading) + } + } + + override fun updateStatus(status: StatusMessage) { + val binding = _binding ?: return + lifecycleScope.launch(Dispatchers.Main) { + binding.memoryText.text = Libbox.formatBytes(status.memory) + binding.goroutinesText.text = status.goroutines.toString() + val trafficAvailable = status.trafficAvailable + binding.trafficContainer.isVisible = trafficAvailable + if (trafficAvailable) { + binding.inboundConnectionsText.text = status.connectionsIn.toString() + binding.outboundConnectionsText.text = status.connectionsOut.toString() + binding.uplinkText.text = Libbox.formatBytes(status.uplink) + "/s" + binding.downlinkText.text = Libbox.formatBytes(status.downlink) + "/s" + binding.uplinkTotalText.text = Libbox.formatBytes(status.uplinkTotal) + binding.downlinkTotalText.text = Libbox.formatBytes(status.downlinkTotal) + } + } + } + + } + + inner class ClashModeClient : CommandClient.Handler { + + override fun initializeClashMode(modeList: List, currentMode: String) { + // TODO: initialize mode selector here + } + + override fun updateClashMode(newMode: String) { + // TODO: update mode here + } + } class Adapter( diff --git a/app/src/main/java/io/nekohasekai/sfa/utils/CommandClient.kt b/app/src/main/java/io/nekohasekai/sfa/utils/CommandClient.kt index 2f99426..18a71ef 100644 --- a/app/src/main/java/io/nekohasekai/sfa/utils/CommandClient.kt +++ b/app/src/main/java/io/nekohasekai/sfa/utils/CommandClient.kt @@ -8,20 +8,22 @@ import io.nekohasekai.libbox.Libbox import io.nekohasekai.libbox.OutboundGroup import io.nekohasekai.libbox.OutboundGroupIterator import io.nekohasekai.libbox.StatusMessage +import io.nekohasekai.libbox.StringIterator +import io.nekohasekai.sfa.ktx.toList import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.isActive import kotlinx.coroutines.launch -class CommandClient( +open class CommandClient( private val scope: CoroutineScope, private val connectionType: ConnectionType, private val handler: Handler ) { enum class ConnectionType { - Status, Groups, Log + Status, Groups, Log, ClashMode } interface Handler { @@ -31,6 +33,8 @@ class CommandClient( fun updateStatus(status: StatusMessage) {} fun updateGroups(groups: List) {} fun appendLog(message: String) {} + fun initializeClashMode(modeList: List, currentMode: String) {} + fun updateClashMode(newMode: String) {} } @@ -44,6 +48,7 @@ class CommandClient( ConnectionType.Status -> Libbox.CommandStatus ConnectionType.Groups -> Libbox.CommandGroup ConnectionType.Log -> Libbox.CommandLog + ConnectionType.ClashMode -> Libbox.CommandClashMode } options.statusInterval = 2 * 1000 * 1000 * 1000 val commandClient = CommandClient(clientHandler, options) @@ -115,6 +120,14 @@ class CommandClient( handler.updateStatus(message) } + override fun initializeClashMode(modeList: StringIterator, currentMode: String) { + handler.initializeClashMode(modeList.toList(), currentMode) + } + + override fun updateClashMode(newMode: String) { + handler.updateClashMode(newMode) + } + } } \ No newline at end of file