fix(ui): статистика и статус туннеля в WG-режиме
isActive в нотификации сравнивался со строкой статуса прокси - при поднятом WG статус другой, поэтому потоки и скорость не показывались. Флаг теперь явный, а статус ушёл в заголовок уведомления, освободив строку под цифры.
This commit is contained in:
1 parent
d52cceb90c
commit
009baaeabc
8 files changed
+51
-21
No files matched your search
@@ -42,6 +42,10 @@ object ProxyServiceState {
|
||||
private val _connectionStats = MutableStateFlow(ConnectionStats.IDLE)
|
||||
val connectionStats: StateFlow<ConnectionStats> = _connectionStats.asStateFlow()
|
||||
|
||||
/** WG поднят поверх прокси: UI и нотификация говорят про туннель, а не про прокси. */
|
||||
private val _tunnelActive = MutableStateFlow(false)
|
||||
val tunnelActive: StateFlow<Boolean> = _tunnelActive.asStateFlow()
|
||||
|
||||
/**
|
||||
* Момент первого подключения (SystemClock.elapsedRealtime()).
|
||||
* При watchdog-рестарте не сбрасывается.
|
||||
@@ -99,6 +103,10 @@ object ProxyServiceState {
|
||||
_connectionStats.value = stats
|
||||
}
|
||||
|
||||
fun setTunnelActive(value: Boolean) {
|
||||
_tunnelActive.value = value
|
||||
}
|
||||
|
||||
/** Запомнить момент первого подключения сессии. Повторные вызовы игнорируются. */
|
||||
fun markConnectedIfAbsent(nowElapsed: Long) {
|
||||
_connectedSince.compareAndSet(null, nowElapsed)
|
||||
|
||||
@@ -63,6 +63,7 @@ class WireGuardTunnelManager(context: Context) {
|
||||
stopLocked()
|
||||
throw e
|
||||
}
|
||||
ProxyServiceState.setTunnelActive(true)
|
||||
ProxyServiceState.addLog("WireGuard: туннель $name поднят через $endpoint")
|
||||
}
|
||||
}
|
||||
@@ -70,6 +71,7 @@ class WireGuardTunnelManager(context: Context) {
|
||||
suspend fun stop() = mutex.withLock { stopLocked() }
|
||||
|
||||
private fun stopLocked() {
|
||||
ProxyServiceState.setTunnelActive(false)
|
||||
val tunnel = tunnelRef.getAndSet(null) ?: return
|
||||
try {
|
||||
backend.setState(tunnel, Tunnel.State.DOWN, null)
|
||||
|
||||
@@ -242,7 +242,8 @@ class CoreProcessController(
|
||||
ProxyServiceState.markConnectedIfAbsent(SystemClock.elapsedRealtime())
|
||||
notifier.setStatus(
|
||||
if (wireGuardStarted) context.getString(R.string.proxy_active_wireguard)
|
||||
else context.getString(R.string.proxy_active)
|
||||
else context.getString(R.string.proxy_active),
|
||||
active = true
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
val message = e.message ?: e.javaClass.simpleName
|
||||
|
||||
@@ -16,7 +16,7 @@ import java.util.Locale
|
||||
/**
|
||||
* Нотификации прокси-сервиса: foreground-статус (+скорость/число потоков) и
|
||||
* отдельный алерт ручной капчи. Статус и скорость держит внутри, перерисовывает
|
||||
* сам. Скорость/счётчик дописываются только когда статус == "активно" (без WG).
|
||||
* сам. Скорость/счётчик показываются только при "активных" статусах.
|
||||
*/
|
||||
class ProxyNotifier(private val service: Service) {
|
||||
|
||||
@@ -36,8 +36,7 @@ class ProxyNotifier(private val service: Service) {
|
||||
private var baseStatus = ""
|
||||
private var speedText = ""
|
||||
private var captchaActive = false
|
||||
|
||||
private val isActive get() = baseStatus == service.getString(R.string.proxy_active)
|
||||
private var isActive = false
|
||||
|
||||
fun createChannels() {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return
|
||||
@@ -61,10 +60,13 @@ class ProxyNotifier(private val service: Service) {
|
||||
/** Базовый статус "Подключение..." для самого первого startForeground. */
|
||||
fun prepareConnecting() {
|
||||
baseStatus = service.getString(R.string.notif_proxy_connecting)
|
||||
isActive = false
|
||||
}
|
||||
|
||||
fun setStatus(text: String) {
|
||||
/** [active] - соединение установлено (прокси или прокси+WG): показываем потоки и скорость. */
|
||||
fun setStatus(text: String, active: Boolean = false) {
|
||||
baseStatus = text
|
||||
isActive = active
|
||||
show()
|
||||
}
|
||||
|
||||
@@ -79,20 +81,25 @@ class ProxyNotifier(private val service: Service) {
|
||||
}
|
||||
|
||||
fun build(): Notification {
|
||||
var text = baseStatus
|
||||
val stats = ProxyServiceState.connectionStats.value
|
||||
// Активное соединение: статус уезжает в заголовок, строка текста целиком под
|
||||
// потоки и скорость - вместе они в одну строку не влезают.
|
||||
val title: String
|
||||
val text: String
|
||||
if (isActive) {
|
||||
if (stats.total > 0) {
|
||||
text = String.format(
|
||||
title = baseStatus
|
||||
text = listOfNotNull(
|
||||
if (stats.total > 0) String.format(
|
||||
Locale.US,
|
||||
service.getString(R.string.notif_proxy_status_format),
|
||||
service.getString(R.string.notif_proxy_threads_format),
|
||||
stats.active,
|
||||
stats.total,
|
||||
speedText
|
||||
)
|
||||
} else if (speedText.isNotEmpty()) {
|
||||
text += " • $speedText"
|
||||
}
|
||||
stats.total
|
||||
) else null,
|
||||
speedText.takeIf { it.isNotEmpty() }
|
||||
).joinToString(" • ")
|
||||
} else {
|
||||
title = service.getString(R.string.notif_proxy_title)
|
||||
text = baseStatus
|
||||
}
|
||||
|
||||
val stopIntent = Intent(service, ProxyReceiver::class.java).apply {
|
||||
@@ -103,7 +110,7 @@ class ProxyNotifier(private val service: Service) {
|
||||
)
|
||||
|
||||
return NotificationCompat.Builder(service, CHANNEL_PROXY)
|
||||
.setContentTitle(service.getString(R.string.notif_proxy_title))
|
||||
.setContentTitle(title)
|
||||
.setContentText(text)
|
||||
.setSmallIcon(android.R.drawable.ic_menu_preferences)
|
||||
.setOngoing(true)
|
||||
|
||||
@@ -70,6 +70,7 @@ import kotlin.math.ceil
|
||||
internal fun ConnectionHero(
|
||||
state: ProxyState,
|
||||
uptimeText: String?,
|
||||
tunnelActive: Boolean,
|
||||
onToggle: () -> Unit,
|
||||
modifier: Modifier = Modifier
|
||||
) {
|
||||
@@ -82,13 +83,14 @@ internal fun ConnectionHero(
|
||||
) {
|
||||
HeroToggleButton(
|
||||
kind = kind,
|
||||
tunnelActive = tunnelActive,
|
||||
reducedMotion = reducedMotion,
|
||||
onClick = onToggle
|
||||
)
|
||||
|
||||
Spacer(Modifier.height(20.dp))
|
||||
|
||||
StatusLabel(state = state, reducedMotion = reducedMotion)
|
||||
StatusLabel(state = state, tunnelActive = tunnelActive, reducedMotion = reducedMotion)
|
||||
|
||||
Spacer(Modifier.height(10.dp))
|
||||
|
||||
@@ -110,13 +112,16 @@ private fun ProxyState.heroKind(): HeroKind = when (this) {
|
||||
@Composable
|
||||
private fun HeroToggleButton(
|
||||
kind: HeroKind,
|
||||
tunnelActive: Boolean,
|
||||
reducedMotion: Boolean,
|
||||
onClick: () -> Unit
|
||||
) {
|
||||
val extended = MaterialTheme.extendedColorScheme
|
||||
val buttonLabel = when (kind) {
|
||||
HeroKind.Busy -> stringResource(R.string.proxy_connecting)
|
||||
HeroKind.Running -> stringResource(R.string.proxy_active_stop)
|
||||
HeroKind.Running -> stringResource(
|
||||
if (tunnelActive) R.string.tunnel_active_stop else R.string.proxy_active_stop
|
||||
)
|
||||
HeroKind.Error -> stringResource(R.string.proxy_error_restart)
|
||||
HeroKind.Idle -> stringResource(R.string.start_proxy)
|
||||
}
|
||||
@@ -245,9 +250,11 @@ private fun HeroIcon(kind: HeroKind, tint: Color) {
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun StatusLabel(state: ProxyState, reducedMotion: Boolean) {
|
||||
private fun StatusLabel(state: ProxyState, tunnelActive: Boolean, reducedMotion: Boolean) {
|
||||
val label = when (state) {
|
||||
is ProxyState.Running -> stringResource(R.string.proxy_active)
|
||||
is ProxyState.Running -> stringResource(
|
||||
if (tunnelActive) R.string.tunnel_active else R.string.proxy_active
|
||||
)
|
||||
is ProxyState.Starting, is ProxyState.Connecting -> stringResource(R.string.proxy_connecting)
|
||||
is ProxyState.Error -> state.message
|
||||
is ProxyState.CaptchaRequired -> stringResource(R.string.proxy_captcha_required)
|
||||
|
||||
@@ -58,6 +58,7 @@ fun HomeScreen(
|
||||
val proxyState by proxyViewModel.proxyState.collectAsStateWithLifecycle()
|
||||
val connectedSince by proxyViewModel.connectedSince.collectAsStateWithLifecycle()
|
||||
val uptimeText = rememberProxyUptime(connectedSince)
|
||||
val tunnelActive by proxyViewModel.tunnelActive.collectAsStateWithLifecycle()
|
||||
val clientConfig by settingsViewModel.clientConfig.collectAsStateWithLifecycle()
|
||||
val updateState by settingsViewModel.updateState.collectAsStateWithLifecycle()
|
||||
val suppressUpdatePrompt by settingsViewModel.suppressUpdatePrompt.collectAsStateWithLifecycle()
|
||||
@@ -156,6 +157,7 @@ fun HomeScreen(
|
||||
ConnectionHero(
|
||||
state = proxyState,
|
||||
uptimeText = uptimeText,
|
||||
tunnelActive = tunnelActive,
|
||||
onToggle = {
|
||||
when (proxyState) {
|
||||
is ProxyState.Idle, is ProxyState.Error -> {
|
||||
|
||||
@@ -18,6 +18,7 @@ class ProxyViewModel(
|
||||
|
||||
val proxyState: StateFlow<ProxyState> = proxyManager.proxyState
|
||||
val connectedSince: StateFlow<Long?> = ProxyServiceState.connectedSince
|
||||
val tunnelActive: StateFlow<Boolean> = ProxyServiceState.tunnelActive
|
||||
val logs: StateFlow<List<LogEntry>> = ProxyServiceState.logs
|
||||
|
||||
fun startProxy() {
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
<string name="nav_home">Главная</string>
|
||||
<string name="turn_proxy_title">Free Turn</string>
|
||||
<string name="proxy_active">Прокси активен</string>
|
||||
<string name="tunnel_active">Туннель активен</string>
|
||||
<string name="proxy_connecting">Подключение…</string>
|
||||
<string name="proxy_press_to_start">Нажмите для запуска</string>
|
||||
<string name="home_empty_title">Нет серверов</string>
|
||||
@@ -169,6 +170,7 @@
|
||||
<string name="server_connection_lost_title">Нет подключения к серверу</string>
|
||||
<string name="server_connection_lost_desc">SSH-сессия не активна. Повторите сопряжение, чтобы управлять сервером.</string>
|
||||
<string name="proxy_active_stop">Прокси активен - нажмите для остановки</string>
|
||||
<string name="tunnel_active_stop">Туннель активен - нажмите для остановки</string>
|
||||
<string name="proxy_error_restart">Ошибка - нажмите для повторного запуска</string>
|
||||
<string name="start_proxy">Запустить прокси</string>
|
||||
<string name="android_client">Android-клиент</string>
|
||||
@@ -344,7 +346,7 @@
|
||||
|
||||
<!-- Notification & Tile -->
|
||||
<string name="notif_proxy_stop_action">Отключить</string>
|
||||
<string name="notif_proxy_status_format">Потоки: %1$d/%2$d • %3$s</string>
|
||||
<string name="notif_proxy_threads_format">Потоки: %1$d/%2$d</string>
|
||||
<string name="tile_service_label">Free Turn</string>
|
||||
<string name="proxy_active_wireguard">Прокси и WireGuard активны</string>
|
||||
|
||||
|
||||
Reference in new issue
Block a user