For issue #15934 fix ConcurrentModificationException

on WifiConnectionMonitor
upstream-sync
Arturo Mejia 4 years ago
parent 1dca411515
commit 50d7792330

@ -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)

Loading…
Cancel
Save