Synchronize access to AndroidConnectivityMonitor.callbacks
@@ -56,8 +56,10 @@ public final class AndroidConnectivityMonitor implements ConnectivityMonitor {
@Override
public void addCallback(Consumer<NetworkStatus> callback) {
+ synchronized (callbacks) {
callbacks.add(callback);
}
+ }
@Override
public void shutdown() {
@@ -99,18 +101,22 @@ public final class AndroidConnectivityMonitor implements ConnectivityMonitor {
private class DefaultNetworkCallback extends ConnectivityManager.NetworkCallback {
@Override
public void onAvailable(Network network) {
+ synchronized (callbacks) {
for (Consumer<NetworkStatus> callback : callbacks) {
callback.accept(NetworkStatus.REACHABLE);
}
}
+ }
@Override
public void onLost(Network network) {
+ synchronized (callbacks) {
for (Consumer<NetworkStatus> callback : callbacks) {
callback.accept(NetworkStatus.UNREACHABLE);
}
}
}
+ }
/** Respond to network changes. Only used on API levels < 24. */
private class NetworkReceiver extends BroadcastReceiver {
@@ -124,10 +130,13 @@ public final class AndroidConnectivityMonitor implements ConnectivityMonitor {
boolean wasConnected = isConnected;
isConnected = networkInfo != null && networkInfo.isConnected();
if (isConnected && !wasConnected) {
+ synchronized (callbacks) {
for (Consumer<NetworkStatus> callback : callbacks) {
callback.accept(NetworkStatus.REACHABLE);
}
+ }
} else if (!isConnected && wasConnected) {
+ synchronized (callbacks) {
for (Consumer<NetworkStatus> callback : callbacks) {
callback.accept(NetworkStatus.UNREACHABLE);
}
@@ -135,3 +144,4 @@ public final class AndroidConnectivityMonitor implements ConnectivityMonitor {
}
}
}
+}
