Check if network access if blocked when fetching notifications (#6247)
* Add `NetworkMonitor.isNetworkBlocked()`, use it to check if Doze prevented us from loading notifications * Only check if network is blocked after checking if we have a network available, otherwise it's always `true` * Extract `NetworkBlockedChecker` to handle deprecations more carefully
This commit is contained in:
committed by
GitHub
parent
dc11430a73
commit
fe4554703c
@@ -43,6 +43,9 @@ class DefaultNetworkMonitor(
|
||||
appCoroutineScope: CoroutineScope,
|
||||
) : NetworkMonitor {
|
||||
private val connectivityManager: ConnectivityManager = context.getSystemService(ConnectivityManager::class.java)
|
||||
private val blockedNetworkBlockedChecker = NetworkBlockedChecker(connectivityManager)
|
||||
|
||||
override fun isNetworkBlocked(): Boolean = blockedNetworkBlockedChecker.isNetworkBlocked()
|
||||
|
||||
override val connectivity: StateFlow<NetworkStatus> = callbackFlow {
|
||||
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (c) 2026 Element Creations Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
|
||||
* Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
@file:Suppress("DEPRECATION")
|
||||
|
||||
package io.element.android.features.networkmonitor.impl
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.net.ConnectivityManager
|
||||
import android.net.NetworkInfo
|
||||
|
||||
/**
|
||||
* Helper to check if the active network in [ConnectivityManager] is blocked.
|
||||
*
|
||||
* This is extracted to its own class because it uses deprecated APIs (but the only ones that are reliable)
|
||||
* and we don't want to suppress deprecations everywhere.
|
||||
*/
|
||||
class NetworkBlockedChecker(
|
||||
private val connectivityManager: ConnectivityManager,
|
||||
) {
|
||||
// The permission is granted by the manifest, false positive
|
||||
@SuppressLint("MissingPermission")
|
||||
fun isNetworkBlocked(): Boolean {
|
||||
// This call is deprecated, but it seems like it's the only reliable way to tell if doze has blocked network access
|
||||
return connectivityManager.activeNetworkInfo?.detailedState == NetworkInfo.DetailedState.BLOCKED
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user