-
- All Implemented Interfaces:
-
com.appsflyer.internal.network.NetworkStateObserver
public final class NetworkStateObserverImpl implements NetworkStateObserver
Observes device connectivity and notifies registered listeners on online/offline transitions.
Platform-specific observation is encapsulated behind ConnectivityStrategy (Strategy pattern). A concrete strategy is chosen once in createStrategy based on the device API level:
API 24+: NetworkCallbackStrategy — uses ConnectivityManager.registerDefaultNetworkCallback
API < 24: BroadcastReceiverStrategy — listens for ConnectivityManager.CONNECTIVITY_ACTION
isOnline reads a
@Volatilefield — safe from any thread without locking.handleConnectivityChange is
@Synchronizedto serialize state transitions, preventing interleaved or out-of-order listener notifications from concurrent callbacks.CopyOnWriteArrayList for listeners — optimized for frequent iteration with rare mutation.
start → creates and activates the strategy → stop tears it down. Both are idempotent, guarded by an AtomicBoolean.
-
-
Constructor Summary
Constructors Constructor Description NetworkStateObserverImpl(Context appContext, ConnectivityManager connectivityManagerOverride)NetworkStateObserverImpl(Context appContext)
-
Method Summary
Modifier and Type Method Description BooleanisOnline()Returns the current connectivity state. Unitstart()Begins observing connectivity changes. Unitstop()Stops observing and releases all system callbacks/receivers. UnitaddListener(NetworkStateListener listener)UnitremoveListener(NetworkStateListener listener)-
-
Constructor Detail
-
NetworkStateObserverImpl
NetworkStateObserverImpl(Context appContext, ConnectivityManager connectivityManagerOverride)
-
NetworkStateObserverImpl
NetworkStateObserverImpl(Context appContext)
-
-
Method Detail
-
isOnline
Boolean isOnline()
Returns the current connectivity state. After start is called, this value is kept up-to-date reactively.
-
start
Unit start()
Begins observing connectivity changes. Idempotent — subsequent calls are no-ops. Must be paired with stop to prevent resource leaks.
-
stop
Unit stop()
Stops observing and releases all system callbacks/receivers. Idempotent — safe to call even if not started.
-
addListener
Unit addListener(NetworkStateListener listener)
-
removeListener
Unit removeListener(NetworkStateListener listener)
-
-
-
-