private void checkGeneralPreconditions(String host, int port, int timeoutInMs, ErrorHandler errorHandler) { Preconditions.checkNotNullOrEmpty(host, "host is null or empty"); Preconditions.checkGreaterThanZero(port, "port is not a positive number"); Preconditions.checkGreaterThanZero(timeoutInMs, "timeoutInMs is not a positive number"); Preconditions.checkNotNull(errorHandler, "errorHandler is null"); }
@Override public Observable<Boolean> observeInternetConnectivity(final int initialIntervalInMs, final int intervalInMs, final String host, final int port, final int timeoutInMs, final int httpResponse, final ErrorHandler errorHandler) { Preconditions.checkGreaterOrEqualToZero(initialIntervalInMs, "initialIntervalInMs is not a positive number"); Preconditions.checkGreaterThanZero(intervalInMs, "intervalInMs is not a positive number"); checkGeneralPreconditions(host, port, timeoutInMs, httpResponse, errorHandler); final String adjustedHost = adjustHost(host); return Observable.interval(initialIntervalInMs, intervalInMs, TimeUnit.MILLISECONDS, Schedulers.io()).map(new Function<Long, Boolean>() { @Override public Boolean apply(@NonNull Long tick) { return isConnected(adjustedHost, port, timeoutInMs, httpResponse, errorHandler); } }).distinctUntilChanged(); }
/** * Observes network connectivity. Information about network state, type and typeName are contained * in * observed Connectivity object. * * @param context Context of the activity or an application * @return RxJava Observable with Connectivity class containing information about network state, * type and typeName */ @RequiresPermission(Manifest.permission.ACCESS_NETWORK_STATE) public static Observable<Connectivity> observeNetworkConnectivity(final Context context) { final NetworkObservingStrategy strategy; if (Preconditions.isAtLeastAndroidMarshmallow()) { strategy = new MarshmallowNetworkObservingStrategy(); } else if (Preconditions.isAtLeastAndroidLollipop()) { strategy = new LollipopNetworkObservingStrategy(); } else { strategy = new PreLollipopNetworkObservingStrategy(); } return observeNetworkConnectivity(context, strategy); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowAnExceptionWhenValueLowerThanZero() { Preconditions.checkGreaterThanZero(-1, MSG_VALUE_IS_NOT_GREATER_THAN_ZERO); }
@Test public void shouldNotThrowAnythingWhenStringIsNotEmpty() { Preconditions.checkNotNullOrEmpty("notEmpty", MSG_STRING_IS_NULL); }
@Test public void shouldNotThrowAnythingWhenValueIsGreaterThanZeroForGreaterOrEqualCheck() { Preconditions.checkGreaterOrEqualToZero(1, MSG_VALUE_IS_NOT_GREATER_THAN_ZERO); }
@Test @Config(sdk = 22) public void shouldNotBeAtLeastAndroidMarshmallowForLowerApi() { boolean isAtLeastAndroidMarshmallow = Preconditions.isAtLeastAndroidMarshmallow(); assertThat(isAtLeastAndroidMarshmallow).isFalse(); }
@Test @Config(sdk = 21) public void shouldBeAtLeastAndroidLollipop() { boolean isAtLeastAndroidLollipop = Preconditions.isAtLeastAndroidLollipop(); assertThat(isAtLeastAndroidLollipop).isTrue(); }
private static void checkStrategyIsNotNull(InternetObservingStrategy strategy) { Preconditions.checkNotNull(strategy, "strategy == null"); } }
@Override public Observable<Boolean> observeInternetConnectivity(final int initialIntervalInMs, final int intervalInMs, final String host, final int port, final int timeoutInMs, final int httpResponse, final ErrorHandler errorHandler) { Preconditions.checkGreaterOrEqualToZero(initialIntervalInMs, "initialIntervalInMs is not a positive number"); Preconditions.checkGreaterThanZero(intervalInMs, "intervalInMs is not a positive number"); checkGeneralPreconditions(host, port, timeoutInMs, errorHandler); final String adjustedHost = adjustHost(host); return Observable.interval(initialIntervalInMs, intervalInMs, TimeUnit.MILLISECONDS, Schedulers.io()).map(new Function<Long, Boolean>() { @Override public Boolean apply(@NonNull Long tick) throws Exception { return isConnected(adjustedHost, port, timeoutInMs, errorHandler); } }).distinctUntilChanged(); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowAnExceptionWhenValueIsZero() { Preconditions.checkGreaterThanZero(0, MSG_VALUE_IS_NOT_GREATER_THAN_ZERO); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowAnExceptionWhenStringIsNull() { Preconditions.checkNotNullOrEmpty(null, MSG_STRING_IS_NULL); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowAnExceptionWhenValueLowerThanZeroForGreaterOrEqualCheck() { Preconditions.checkGreaterOrEqualToZero(-1, MSG_VALUE_IS_NOT_GREATER_THAN_ZERO); }
@Test @Config(sdk = 23) public void shouldBeAtLeastAndroidMarshmallow() { boolean isAtLeastAndroidMarshmallow = Preconditions.isAtLeastAndroidMarshmallow(); assertThat(isAtLeastAndroidMarshmallow).isTrue(); }
@Test @Config(sdk = 22) public void shouldBeAtLeastAndroidLollipopForHigherApi() { boolean isAtLeastAndroidLollipop = Preconditions.isAtLeastAndroidLollipop(); assertThat(isAtLeastAndroidLollipop).isTrue(); }
/** * Observes network connectivity. Information about network state, type and typeName are contained * in observed Connectivity object. Moreover, allows you to define NetworkObservingStrategy. * * @param context Context of the activity or an application * @param strategy NetworkObserving strategy to be applied - you can use one of the existing * strategies {@link PreLollipopNetworkObservingStrategy}, * {@link LollipopNetworkObservingStrategy} or create your own custom strategy * @return RxJava Observable with Connectivity class containing information about network state, * type and typeName */ @RequiresPermission(Manifest.permission.ACCESS_NETWORK_STATE) public static Observable<Connectivity> observeNetworkConnectivity(final Context context, final NetworkObservingStrategy strategy) { Preconditions.checkNotNull(context, "context == null"); Preconditions.checkNotNull(strategy, "strategy == null"); return strategy.observeNetworkConnectivity(context); }
private void checkGeneralPreconditions(final String host, final int port, final int timeoutInMs, final int httpResponse, final ErrorHandler errorHandler) { Preconditions.checkNotNullOrEmpty(host, "host is null or empty"); Preconditions.checkGreaterThanZero(port, "port is not a positive number"); Preconditions.checkGreaterThanZero(timeoutInMs, "timeoutInMs is not a positive number"); Preconditions.checkNotNull(errorHandler, "errorHandler is null"); Preconditions.checkNotNull(httpResponse, "httpResponse is null"); Preconditions.checkGreaterThanZero(httpResponse, "httpResponse is not a positive number"); }
@Test public void shouldNotThrowAnythingWhenValueIsGreaterThanZero() { Preconditions.checkGreaterThanZero(1, MSG_VALUE_IS_NOT_GREATER_THAN_ZERO); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowAnExceptionWhenStringIsEmpty() { Preconditions.checkNotNullOrEmpty("", MSG_STRING_IS_NULL); }
@Test public void shouldNotThrowAnythingWhenValueIsEqualToZero() { Preconditions.checkGreaterOrEqualToZero(0, MSG_VALUE_IS_NOT_GREATER_THAN_ZERO); }