Skip to content

Commit 24ac364

Browse files
committed
Restore NetworkBlockedChecker so we can initialize DefaultNetworkMonitor.isNetworkBlocked with its contents
We use a separate component to avoid propagating the `@file:Suppress("DEPRECATION")` to the whole `DefaultNetworkMonitor` file
1 parent a3afd98 commit 24ac364

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

features/networkmonitor/impl/src/main/kotlin/io/element/android/features/networkmonitor/impl/DefaultNetworkMonitor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class DefaultNetworkMonitor(
4747
) : NetworkMonitor {
4848
private val connectivityManager: ConnectivityManager = context.getSystemService(ConnectivityManager::class.java)
4949

50-
override val isNetworkBlocked = MutableStateFlow(false)
50+
override val isNetworkBlocked = MutableStateFlow(NetworkBlockedChecker(connectivityManager).isNetworkBlocked())
5151
override val isInAirGappedEnvironment = MutableStateFlow(false)
5252

5353
override val connectivity: StateFlow<NetworkStatus> = callbackFlow {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2026 Element Creations Ltd.
3+
*
4+
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
5+
* Please see LICENSE files in the repository root for full details.
6+
*/
7+
8+
@file:Suppress("DEPRECATION")
9+
10+
package io.element.android.features.networkmonitor.impl
11+
12+
import android.annotation.SuppressLint
13+
import android.net.ConnectivityManager
14+
import android.net.NetworkInfo
15+
16+
/**
17+
* Helper to synchronously check if the active network in [ConnectivityManager] is blocked.
18+
*
19+
* This is extracted to its own class because it uses deprecated APIs (but the only ones that are reliable)
20+
* and we don't want to suppress deprecations everywhere in the file this would be called.
21+
*/
22+
class NetworkBlockedChecker(
23+
private val connectivityManager: ConnectivityManager,
24+
) {
25+
// The permission is granted by the manifest, false positive
26+
@SuppressLint("MissingPermission")
27+
fun isNetworkBlocked(): Boolean {
28+
// This call is deprecated, but it seems like it's the only reliable way to tell if doze has blocked network access
29+
return connectivityManager.activeNetworkInfo?.detailedState == NetworkInfo.DetailedState.BLOCKED
30+
}
31+
}

0 commit comments

Comments
 (0)