2
0
mirror of https://github.com/fork-maintainers/iceraven-browser synced 2024-11-19 09:25:34 +00:00

[fenix] For issue https://github.com/mozilla-mobile/fenix/issues/15934 fix ConcurrentModificationException

on WifiConnectionMonitor
This commit is contained in:
Arturo Mejia 2020-11-04 14:34:48 -05:00
parent 84770f1f64
commit 8cc09b5785

View File

@ -26,7 +26,7 @@ import android.net.NetworkRequest
* ```
*/
class WifiConnectionMonitor(app: Application) {
private val callbacks = mutableSetOf<(Boolean) -> Unit>()
private val callbacks = mutableListOf<(Boolean) -> Unit>()
private val connectivityManager = app.getSystemService(Context.CONNECTIVITY_SERVICE) as
ConnectivityManager
@ -35,16 +35,21 @@ class WifiConnectionMonitor(app: Application) {
private val frameworkListener = object : ConnectivityManager.NetworkCallback() {
override fun onLost(network: Network?) {
callbacks.forEach { it(false) }
notifyListeners(false)
lastKnownStateWasAvailable = false
}
override fun onAvailable(network: Network?) {
callbacks.forEach { it(true) }
notifyListeners(true)
lastKnownStateWasAvailable = true
}
}
internal fun notifyListeners(value: Boolean) {
val items = ArrayList(callbacks)
items.forEach { it(value) }
}
/**
* Attaches the [WifiConnectionMonitor] to the application. After this has been called, callbacks
* added via [addOnWifiConnectedChangedListener] will be called until either the app exits, or
@ -65,7 +70,7 @@ class WifiConnectionMonitor(app: Application) {
val noCallbacksReceivedYet = lastKnownStateWasAvailable == null
if (noCallbacksReceivedYet) {
lastKnownStateWasAvailable = false
callbacks.forEach { it(false) }
notifyListeners(false)
}
connectivityManager.registerNetworkCallback(request, frameworkListener)