Skip to content

Commit

Permalink
observe state of metered network.
Browse files Browse the repository at this point in the history
  • Loading branch information
StageGuard committed Oct 19, 2024
1 parent 077112c commit 3477193
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ data class AnitorrentConfig(
* * Android 移动流量
* * Windows 计费 Wi-Fi
*/
val limitUploadOnMeteredNetwork: Boolean = false,
val limitUploadOnMeteredNetwork: Boolean = true,
@Transient private val _placeholder: Int = 0,
) {
companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import android.net.Network
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow


@SuppressLint("MissingPermission")
private class AndroidMeteredNetworkDetector(
private val context: Context
) : MeteredNetworkDetector {

private val flow = MutableSharedFlow<Boolean>(extraBufferCapacity = 1)
private val flow = MutableStateFlow(false)
override val isMeteredNetworkFlow: Flow<Boolean> = flow

private val connectivityManager by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface MeteredNetworkDetector {
/**
* Dispose listeners or callbacks which may be created at initializing detector.
*/
fun dispose() { }
fun dispose()
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ private class WindowsMeteredNetworkDetector : MeteredNetworkDetector {
private val flow = flow {
while (true) {
emit(getIsMetered())
kotlinx.coroutines.delay(3000)
kotlinx.coroutines.delay(60000)
}
}
override val isMeteredNetworkFlow: Flow<Boolean> = flow

private fun getIsMetered(): Boolean {
var networkConnectionCost: INetworkConnectionCost? = null
var networkCostManager: INetworkCostManager? = null

try {
val coInitializeHResult = Ole32.INSTANCE.CoInitializeEx(null, Ole32.COINIT_MULTITHREADED)
Expand All @@ -55,23 +55,27 @@ private class WindowsMeteredNetworkDetector : MeteredNetworkDetector {
)
COMUtils.checkRC(coCreateInstanceHResult)

networkConnectionCost = INetworkConnectionCost(pNetworkCostManager)
networkCostManager = INetworkCostManager(pNetworkCostManager)
val pCost = IntByReference()

val getCostResult = networkConnectionCost.GetCost(pCost)
val getCostResult = networkCostManager.GetCost(pCost)
COMUtils.checkRC(getCostResult)

return (pCost.value and NLM_CONNECTION_COST_FIXED) != 0
} catch (ex: COMException) {
logger.warn(ex) { "Failed to get network status." }
return false
} finally {
networkConnectionCost?.Release()
networkCostManager?.Release()
Ole32.INSTANCE.CoUninitialize()
}
}

override fun dispose() {

}

private class INetworkConnectionCost(pointer: PointerByReference) : Unknown(pointer.value) {
private class INetworkCostManager(pointer: PointerByReference) : Unknown(pointer.value) {
@Suppress("FunctionName")
fun GetCost(cost: IntByReference): HRESULT {
return _invokeNativeObject(3, arrayOf(pointer, cost, null), HRESULT::class.java) as HRESULT
Expand Down Expand Up @@ -99,6 +103,7 @@ actual fun createMeteredNetworkDetector(context: Context): MeteredNetworkDetecto
is Platform.Windows -> WindowsMeteredNetworkDetector()
else -> object : MeteredNetworkDetector {
override val isMeteredNetworkFlow: Flow<Boolean> = flowOf(false)
override fun dispose() { }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (C) 2024 OpenAni and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license, which can be found at the following link.
*
* https://github.com/open-ani/ani/blob/main/LICENSE
*/

actual fun createMeteredNetworkDetector(context: Context): MeteredNetworkDetector {
return object : MeteredNetworkDetector {
override val isMeteredNetworkFlow: Flow<Boolean> = flowOf(false)
override fun dispose() { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ internal fun SettingsScope.TorrentEngineGroup(
checked = torrentSettings.limitUploadOnMeteredNetwork,
onCheckedChange = { torrentSettingsState.update(torrentSettings.copy(limitUploadOnMeteredNetwork = it)) },
title = { Text("计费网络限制上传") },
description = { Text("在 Android 移动流量或 Windows 计费网络环境下限制 BT 上传为 1 KB/s") }
description = { Text("在计费网络环境下限制上传速度为 1 KB/s") }
)
}
val navigator by rememberUpdatedState(LocalNavigator.current)
Expand Down

0 comments on commit 3477193

Please sign in to comment.