From 8cc09b57854369a72c860c1105c480aa9258d0a4 Mon Sep 17 00:00:00 2001 From: Arturo Mejia Date: Wed, 4 Nov 2020 14:34:48 -0500 Subject: [PATCH] [fenix] For issue https://github.com/mozilla-mobile/fenix/issues/15934 fix ConcurrentModificationException on WifiConnectionMonitor --- .../org/mozilla/fenix/wifi/WifiConnectionMonitor.kt | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/wifi/WifiConnectionMonitor.kt b/app/src/main/java/org/mozilla/fenix/wifi/WifiConnectionMonitor.kt index 34a6e5f7a5..4db646e128 100644 --- a/app/src/main/java/org/mozilla/fenix/wifi/WifiConnectionMonitor.kt +++ b/app/src/main/java/org/mozilla/fenix/wifi/WifiConnectionMonitor.kt @@ -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)