Skip to content

Commit

Permalink
Add check internet connectivity immediately
Browse files Browse the repository at this point in the history
  • Loading branch information
Kisty committed Oct 7, 2016
1 parent 9fb03cc commit 91671ec
Showing 1 changed file with 48 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,25 @@ public static Observable<Connectivity> observeNetworkConnectivity(final Context
* and false if not
*/
public static Observable<Boolean> observeInternetConnectivity() {
return observeInternetConnectivity(DEFAULT_PING_INTERVAL_IN_MS, DEFAULT_PING_HOST,
return observeInternetConnectivity(DEFAULT_PING_INTERVAL_IN_MS, DEFAULT_PING_INTERVAL_IN_MS, DEFAULT_PING_HOST,
DEFAULT_PING_PORT, DEFAULT_PING_TIMEOUT_IN_MS);
}

/**
* Observes connectivity with the Internet <i>immediately</i> with default settings. It pings remote host
* (www.google.com) at port 80 every 2 seconds with 2 seconds of timeout. This operation is used
* for determining if device is connected to the Internet or not. Please note that this method is
* less efficient than {@link #observeNetworkConnectivity(Context)} method and consumes data
* transfer, but it gives you actual information if device is connected to the Internet or not.
*
* @return RxJava Observable with Boolean - true, when we have an access to the Internet
* and false if not
*/
public static Observable<Boolean> observeInternetConnectivityImmediately() {
return observeInternetConnectivity(0, DEFAULT_PING_INTERVAL_IN_MS, DEFAULT_PING_HOST,
DEFAULT_PING_PORT, DEFAULT_PING_TIMEOUT_IN_MS);
}

/**
* Observes connectivity with the Internet by opening socket connection with remote host
*
Expand All @@ -117,23 +132,42 @@ public static Observable<Boolean> observeInternetConnectivity() {
*/
public static Observable<Boolean> observeInternetConnectivity(final int intervalInMs,
final String host, final int port, final int timeoutInMs) {
return observeInternetConnectivity(intervalInMs, intervalInMs, host, port, timeoutInMs);
}

/**
* Observes connectivity with the Internet by opening socket connection with remote host
*
* @param initialIntervalInMs in milliseconds determining the delay of the first connectivity check
* @param intervalInMs in milliseconds determining how often we want to check connectivity
* @param host for checking Internet connectivity
* @param port for checking Internet connectivity
* @param timeoutInMs for pinging remote host in milliseconds
* @return RxJava Observable with Boolean - true, when we have connection with host and false if
* not
*/
public static Observable<Boolean> observeInternetConnectivity(final int initialIntervalInMs, final int intervalInMs,
final String host, final int port, final int timeoutInMs) {
if (initialIntervalInMs < 0) {
throw new IllegalArgumentException("initialIntervalInMs is not a non-zero number");
}
Preconditions.checkPositive(intervalInMs, "intervalInMs is not positive number");
Preconditions.checkNotNullOrEmpty(host, "host is null or empty");
Preconditions.checkPositive(port, "port is not positive number");
Preconditions.checkPositive(timeoutInMs, "timeoutInMs is not positive number");

return Observable.interval(intervalInMs, TimeUnit.MILLISECONDS, Schedulers.io())
.map(new Func1<Long, Boolean>() {
@Override public Boolean call(Long tick) {
try {
Socket socket = new Socket();
socket.connect(new InetSocketAddress(host, port), timeoutInMs);
return socket.isConnected();
} catch (IOException e) {
return Boolean.FALSE;
}
}
})
.distinctUntilChanged();
return Observable.interval(initialIntervalInMs, intervalInMs, TimeUnit.MILLISECONDS, Schedulers.io())
.map(new Func1<Long, Boolean>() {
@Override public Boolean call(Long tick) {
try {
Socket socket = new Socket();
socket.connect(new InetSocketAddress(host, port), timeoutInMs);
return socket.isConnected();
} catch (IOException e) {
return Boolean.FALSE;
}
}
})
.distinctUntilChanged();
}
}

0 comments on commit 91671ec

Please sign in to comment.