MarshmallowNetworkObservingStrategy implements NetworkObservingStrategy { protected Publisher<Connectivity> propagateAnyConnectedState(final Connectivity last, final Connectivity current) { final boolean typeChanged = last.type() != current.type(); final boolean wasConnected = last.state() == NetworkInfo.State.CONNECTED; final boolean isDisconnected = current.state() == NetworkInfo.State.DISCONNECTED; final boolean isNotIdle = current.detailedState() != NetworkInfo.DetailedState.IDLE; if (typeChanged && wasConnected && isDisconnected && isNotIdle) { return Flowable.fromArray(current, last); } else { return Flowable.fromArray(current); } } @SuppressWarnings("NullAway") // networkCallback cannot be initialized here  MarshmallowNetworkObservingStrategy(); @Override Observable<Connectivity> observeNetworkConnectivity(final Context context); @Override void onError(final String message, final Exception exception);  }
@Test public void shouldNotPropagateLastConnectivityEventWhenTypeIsNotChanged() { final Connectivity last = new Connectivity.Builder() .type(ConnectivityManager.TYPE_WIFI) .state(NetworkInfo.State.CONNECTED) .build(); final Connectivity current = new Connectivity.Builder() .type(ConnectivityManager.TYPE_WIFI) .state(NetworkInfo.State.DISCONNECTED) .detailedState(NetworkInfo.DetailedState.CONNECTED) .build(); final Publisher<Connectivity> publisher = strategy.propagateAnyConnectedState(last, current); final TestSubscriber<Object> testSubscriber = new TestSubscriber<>(); publisher.subscribe(testSubscriber); testSubscriber.assertValueCount(1); testSubscriber.assertValues(current); }
@Test public void shouldNotPropagateLastConnectivityWhenWasNotConnected() { final Connectivity last = new Connectivity.Builder() .type(ConnectivityManager.TYPE_WIFI) .state(NetworkInfo.State.DISCONNECTED) .build(); final Connectivity current = new Connectivity.Builder() .type(ConnectivityManager.TYPE_MOBILE) .state(NetworkInfo.State.CONNECTED) .detailedState(NetworkInfo.DetailedState.CONNECTED) .build(); final Publisher<Connectivity> publisher = strategy.propagateAnyConnectedState(last, current); final TestSubscriber<Object> testSubscriber = new TestSubscriber<>(); publisher.subscribe(testSubscriber); testSubscriber.assertValueCount(1); testSubscriber.assertValues(current); }
@Test public void shouldNotPropagateLastConnectivityWhenIsConnected() { final Connectivity last = new Connectivity.Builder() .type(ConnectivityManager.TYPE_WIFI) .state(NetworkInfo.State.CONNECTED) .build(); final Connectivity current = new Connectivity.Builder() .type(ConnectivityManager.TYPE_MOBILE) .state(NetworkInfo.State.CONNECTED) .detailedState(NetworkInfo.DetailedState.CONNECTED) .build(); final Publisher<Connectivity> publisher = strategy.propagateAnyConnectedState(last, current); final TestSubscriber<Object> testSubscriber = new TestSubscriber<>(); publisher.subscribe(testSubscriber); testSubscriber.assertValueCount(1); testSubscriber.assertValues(current); }
@Test public void shouldNotPropagateLastConnectivityWhenIsIdle() { final Connectivity last = new Connectivity.Builder() .type(ConnectivityManager.TYPE_WIFI) .state(NetworkInfo.State.CONNECTED) .build(); final Connectivity current = new Connectivity.Builder() .type(ConnectivityManager.TYPE_MOBILE) .state(NetworkInfo.State.DISCONNECTED) .detailedState(NetworkInfo.DetailedState.IDLE) .build(); final Publisher<Connectivity> publisher = strategy.propagateAnyConnectedState(last, current); final TestSubscriber<Object> testSubscriber = new TestSubscriber<>(); publisher.subscribe(testSubscriber); testSubscriber.assertValueCount(1); testSubscriber.assertValues(current); }