feat(notify): провайдер в уведомлении, без счётчика потоков в direct
This commit is contained in:
1 parent
deb37d6c2b
commit
1d73a97672
4 files changed
+31
-11
No files matched your search
@@ -11,6 +11,7 @@ import android.os.Build
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import com.freeturn.app.R
|
||||
import com.freeturn.app.data.config.Provider
|
||||
import com.freeturn.app.domain.proxy.ProxyPhase
|
||||
import com.freeturn.app.domain.proxy.ProxyStatus
|
||||
import java.util.Locale
|
||||
@@ -54,6 +55,7 @@ class ProxyNotifier(private val service: Service) {
|
||||
|
||||
private var shown: ProxyStatus? = null
|
||||
private var shownTunnelMode = false
|
||||
private var shownProvider: String? = null
|
||||
private var captchaShown = false
|
||||
|
||||
private val openApp: PendingIntent? by lazy {
|
||||
@@ -72,22 +74,24 @@ class ProxyNotifier(private val service: Service) {
|
||||
}
|
||||
|
||||
/** Первая нотификация для `startForeground` - до неё сессии ещё нет. */
|
||||
fun build(): Notification = build(ProxyStatus(phase = ProxyPhase.Connecting), tunnelMode = false)
|
||||
fun build(): Notification =
|
||||
build(ProxyStatus(phase = ProxyPhase.Connecting), tunnelMode = false, provider = null)
|
||||
|
||||
fun update(status: ProxyStatus, tunnelMode: Boolean) {
|
||||
fun update(status: ProxyStatus, tunnelMode: Boolean, provider: String?) {
|
||||
if (status.captchaUrl.isNotEmpty()) showCaptcha() else cancelCaptcha()
|
||||
val prev = shown
|
||||
if (prev != null && tunnelMode == shownTunnelMode &&
|
||||
if (prev != null && tunnelMode == shownTunnelMode && provider == shownProvider &&
|
||||
prev.phase == status.phase && prev.active == status.active && prev.total == status.total
|
||||
) {
|
||||
return
|
||||
}
|
||||
shown = status
|
||||
shownTunnelMode = tunnelMode
|
||||
notify(NOTIF_ID_FG, build(status, tunnelMode))
|
||||
shownProvider = provider
|
||||
notify(NOTIF_ID_FG, build(status, tunnelMode, provider))
|
||||
}
|
||||
|
||||
private fun build(status: ProxyStatus, tunnelMode: Boolean): Notification {
|
||||
private fun build(status: ProxyStatus, tunnelMode: Boolean, provider: String?): Notification {
|
||||
val connected = status.phase == ProxyPhase.Connected
|
||||
val title = when {
|
||||
connected && tunnelMode -> service.getString(R.string.tunnel_active)
|
||||
@@ -95,8 +99,11 @@ class ProxyNotifier(private val service: Service) {
|
||||
status.phase == ProxyPhase.Error -> service.getString(R.string.notif_proxy_connect_error)
|
||||
else -> service.getString(R.string.notif_proxy_connecting)
|
||||
}
|
||||
val details = streamsText(status)?.takeIf { connected }
|
||||
?: service.getString(R.string.notif_proxy_title)
|
||||
// direct - всегда один поток: счётчик там ничего не сообщает.
|
||||
val streams = streamsText(status)?.takeIf { connected && provider != Provider.DIRECT }
|
||||
val details = listOfNotNull(providerText(provider), streams)
|
||||
.joinToString(" · ")
|
||||
.ifEmpty { service.getString(R.string.notif_proxy_title) }
|
||||
return NotificationCompat.Builder(service, CHANNEL_PROXY)
|
||||
.setContentTitle(title)
|
||||
.setContentText(details)
|
||||
@@ -107,6 +114,12 @@ class ProxyNotifier(private val service: Service) {
|
||||
.build()
|
||||
}
|
||||
|
||||
private fun providerText(provider: String?): String? = when (provider) {
|
||||
Provider.RELAY -> service.getString(R.string.provider_relay)
|
||||
Provider.DIRECT -> service.getString(R.string.provider_direct)
|
||||
else -> null
|
||||
}
|
||||
|
||||
private fun streamsText(status: ProxyStatus): String? =
|
||||
if (status.total > 0) String.format(
|
||||
Locale.US,
|
||||
|
||||
@@ -65,6 +65,8 @@ class ProxyService : VpnService() {
|
||||
|
||||
// Нотификация должна сказать про туннель раньше, чем метрики его увидят.
|
||||
@Volatile private var tunnelMode = false
|
||||
// null - сессия ещё не прочла конфиг.
|
||||
@Volatile private var provider: String? = null
|
||||
// Остановка решена: всё, что поднимет хвост уже начатого старта, сворачиваем сразу.
|
||||
@Volatile private var stopping = false
|
||||
// Заявка ядру на текущую сессию: гасим по ней именно свою, а не следующую.
|
||||
@@ -213,6 +215,7 @@ class ProxyService : VpnService() {
|
||||
network.register()
|
||||
|
||||
tunnelMode = cfg.wireGuardActive
|
||||
provider = cfg.provider
|
||||
// Раздача только поверх туннеля: без tun сокеты сервера ушли бы напрямую.
|
||||
val hotspot = tunnelMode && prefs.hotspotProxyEnabledFlow.first()
|
||||
if (tunnelMode) {
|
||||
@@ -323,7 +326,7 @@ class ProxyService : VpnService() {
|
||||
store.status.collect { status ->
|
||||
// После решения об остановке молчим: нотификация уже снята.
|
||||
if (stopping) return@collect
|
||||
notifier.update(status, tunnelMode)
|
||||
notifier.update(status, tunnelMode, provider)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -73,6 +73,7 @@ import kotlin.math.ceil
|
||||
internal fun ConnectionHero(
|
||||
status: ProxyStatus,
|
||||
uptimeText: String?,
|
||||
showStreams: Boolean,
|
||||
decorEnabled: Boolean,
|
||||
onToggle: () -> Unit,
|
||||
modifier: Modifier = Modifier
|
||||
@@ -105,7 +106,7 @@ internal fun ConnectionHero(
|
||||
|
||||
Spacer(Modifier.height(10.dp))
|
||||
|
||||
StatsPill(status = status, kind = kind, uptimeText = uptimeText)
|
||||
StatsPill(status = status, kind = kind, uptimeText = uptimeText, showStreams = showStreams)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -371,9 +372,9 @@ private fun StatusLabel(status: ProxyStatus, reducedMotion: Boolean) {
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun StatsPill(status: ProxyStatus, kind: HeroKind, uptimeText: String?) {
|
||||
private fun StatsPill(status: ProxyStatus, kind: HeroKind, uptimeText: String?, showStreams: Boolean) {
|
||||
val counts = when {
|
||||
!status.busy -> null
|
||||
!showStreams || !status.busy -> null
|
||||
status.total > 0 -> "${status.active}/${status.total}"
|
||||
status.phase == ProxyPhase.Connected -> "${status.active}"
|
||||
else -> null
|
||||
|
||||
@@ -36,6 +36,7 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.freeturn.app.data.config.Provider
|
||||
import com.freeturn.app.data.config.SplitTunnelMode
|
||||
import com.freeturn.app.ui.components.SettingsContentMaxWidth
|
||||
import com.freeturn.app.ui.util.HapticUtil
|
||||
@@ -157,6 +158,8 @@ fun HomeScreen(
|
||||
ConnectionHero(
|
||||
status = status,
|
||||
uptimeText = uptimeText,
|
||||
// direct - всегда один поток, счётчик ничего не сообщает.
|
||||
showStreams = clientConfig.provider != Provider.DIRECT,
|
||||
decorEnabled = seasonalDecor,
|
||||
onToggle = {
|
||||
// Любая непокоящаяся фаза (включая капчу и старт) - остановка.
|
||||
|
||||
Reference in new issue
Block a user