Skip to content

Commit

Permalink
explicitly fallback on macos
Browse files Browse the repository at this point in the history
  • Loading branch information
Him188 committed Oct 19, 2024
1 parent 3477193 commit 744033b
Showing 1 changed file with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ import me.him188.ani.utils.platform.currentPlatformDesktop

private class WindowsMeteredNetworkDetector : MeteredNetworkDetector {
private val logger = logger<MeteredNetworkDetector>()

private val flow = flow {
while (true) {
emit(getIsMetered())
kotlinx.coroutines.delay(60000)
}
}
override val isMeteredNetworkFlow: Flow<Boolean> = flow

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

Expand All @@ -54,13 +54,13 @@ private class WindowsMeteredNetworkDetector : MeteredNetworkDetector {
pNetworkCostManager,
)
COMUtils.checkRC(coCreateInstanceHResult)

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

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." }
Expand All @@ -81,12 +81,12 @@ private class WindowsMeteredNetworkDetector : MeteredNetworkDetector {
return _invokeNativeObject(3, arrayOf(pointer, cost, null), HRESULT::class.java) as HRESULT
}
}

@Suppress("unused")
companion object {
private val CLSID_NetworkListManager = Guid.CLSID("{DCB00C01-570F-4A9B-8D69-199FDBA5723B}")
private val IID_INetworkCostManager = Guid.IID("{DCB00008-570F-4A9B-8D69-199FDBA5723B}")

private const val NLM_CONNECTION_COST_UNKNOWN = 0
private const val NLM_CONNECTION_COST_UNRESTRICTED = 0x1
private const val NLM_CONNECTION_COST_FIXED = 0x2
Expand All @@ -101,6 +101,12 @@ private class WindowsMeteredNetworkDetector : MeteredNetworkDetector {
actual fun createMeteredNetworkDetector(context: Context): MeteredNetworkDetector {
return when (currentPlatformDesktop()) {
is Platform.Windows -> WindowsMeteredNetworkDetector()
is Platform.MacOS -> object : MeteredNetworkDetector {
// macos API 要用 swift 才能实现
override val isMeteredNetworkFlow: Flow<Boolean> = flowOf(false)
override fun dispose() { }
}

else -> object : MeteredNetworkDetector {
override val isMeteredNetworkFlow: Flow<Boolean> = flowOf(false)
override fun dispose() { }
Expand Down

0 comments on commit 744033b

Please sign in to comment.