diff --git a/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/helpers/NetworkObserver.kt b/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/helpers/NetworkObserver.kt index ab34eeb2fa..13e16787e8 100644 --- a/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/helpers/NetworkObserver.kt +++ b/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/helpers/NetworkObserver.kt @@ -16,18 +16,18 @@ import java.util.concurrent.atomic.AtomicBoolean class NetworkObserver { private var prevInfo: UserNetworkInfo? = null // The core reconnects to the servers on every online network event, so the event is also sent - // when the network type is the same, but it is a different network or the addresses changed - // (e.g., another Wi-Fi network or a new IP address) - connections via the previous network are dead. + // when the network type is the same, but it is a different network or an address was removed + // (e.g., another Wi-Fi network or a lost IP address) - connections via the previous network are dead. private var prevNetwork: Network? = null private var prevAddresses: Set? = null // the last network info the core accepted and the network it was for (null - it was not accepted, or // nothing was sent to this chat controller and the core is online with the default network type) @Volatile private var sentInfo: UserNetworkInfo? = null @Volatile private var sentNetwork: Network? = null - // incremented when the chat controller is created anew - the reports for the previous one are dropped + // incremented when the chat controller is created anew - the reports for the previous one are not recorded @Volatile private var ctrlGeneration = 0 private val sendMutex = Mutex() - // the numbers of removed addresses of the current network and of those reported to the core: while they + // the numbers of removed addresses and of those reported to the core: while they // differ the next online info must be sent even for the same network - the addresses cannot be compared // instead, as an address removed and assigned again also leaves the connections made via it dead @Volatile private var addressRemovals = 0 @@ -112,8 +112,8 @@ class NetworkObserver { } @Synchronized private fun networkLost(network: Network? = null) { - // the network that is replaced by another one is lost after it becomes the current network, - // and the connections are made via it, so its loss is not reported + // on some devices the network replaced by another one is reported lost after the other one becomes + // the current network, and the connections are made via that one, so this loss is not reported if (network != null && prevNetwork != null && network != prevNetwork) return Log.d(TAG, "Network changed: lost") val none = UserNetworkInfo(networkType = UserNetworkType.NONE, false) @@ -143,8 +143,7 @@ class NetworkObserver { withBGApi { delay(3000) // the report is not cancelled once it started, so that the core is not left with the info - // the app did not record - the online event that cancelled it while it was resuming is - // reported instead of it + // the app did not record - the online event that cancels it after this check is reported after it val cancelled = !isActive withContext(NonCancellable) { sendMutex.withLock { @@ -153,7 +152,7 @@ class NetworkObserver { } }.also { noNetworkJob = it } } - // the job that is cancelled before it starts does not run its body; when the network info is reported + // the job that is cancelled before it starts does not run its body; when the online network info is reported // the wake lock is held until it times out, so that the device does not sleep while the core reconnects job.invokeOnCompletion { if (!sent.get()) releaseWakeLock() } } @@ -161,7 +160,7 @@ class NetworkObserver { private suspend fun sendNetworkInfo(info: UserNetworkInfo, network: Network?, removals: Int, ctrlGen: Int): Boolean { if (ctrlGen != ctrlGeneration) return false val ctrl = controller.currentCtrl() ?: return false.also { reportFailed(ctrlGen) } - // a failed command must not be recorded as sent, and it throws when the response cannot be parsed + // a failed command must not be recorded as sent, and it can throw when the response is not valid JSON val sent = try { controller.apiSetNetworkInfo(info, ctrl) } catch (e: Throwable) { Log.e(TAG, "Error reporting network info: ${e.stackTraceToString()}"); false } if (sent) setSentInfo(info, network, removals, ctrlGen) else reportFailed(ctrlGen) return sent