Skip to content

NetStatusBus 1.1.0

Latest

Choose a tag to compare

@sunyatas sunyatas released this 30 Oct 03:14
· 11 commits to master since this release

Added

  • NetStatusBus.addListener(OnNetTypeChangeListener) / removeListener(...): an annotation-free, reflection-free callback for Java and Kotlin. It receives the current type right away and then every change on the main thread, including while the UI is in the background (unlike LiveData). addListener(listener, LifecycleOwner) removes it automatically on DESTROYED. A listener that throws is logged and does not affect other listeners or subscribers.

  • NetworkUtils.getNetType(Context), isNetworkAvailable(Context), isVpnActive(Context) and getMobileGeneration(Context): the same checks without calling init first (for example in a BroadcastReceiver, a WorkManager worker, or another library). Only the application context is kept.

  • NetStatusBus.getCurrentNetType(): the type the bus last delivered, read from a cached field without querying the system (any thread). NetStatusBus.isInitialized() tells whether init has been called.

Changed

  • Using the bus or a no-argument NetworkUtils method before init now throws IllegalStateException with a message that explains the fix, instead of a bare RuntimeException("application is empty"); register and addListener called before init now throw the same IllegalStateException instead of IllegalArgumentException. IllegalStateException is a RuntimeException, so existing catch blocks still work.
  • unregisterAllObserver() also removes listeners added with addListener.

Fixed

  • Registering the same subscriber from several threads at the same time could add it twice, so every network change was delivered to it twice. Registration and unregistration now share a lock; dispatch stays lock-free.
  • Calling register or addListener off the main thread while the network was changing could deliver the "current type" callback after a newer change had already been delivered, so the subscriber or listener ended up on an outdated type until the next change (getNetTypeLiveData() was not affected). The initial callback is now skipped when a newer change has already been delivered.