-
-
Notifications
You must be signed in to change notification settings - Fork 275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add check internet connectivity immediately #90
Merged
pwittchen
merged 2 commits into
pwittchen:master
from
Kisty:add_check_internet_connectivity_immediately
Oct 15, 2016
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ public class ReactiveNetwork { | |
private static final String DEFAULT_PING_HOST = "www.google.com"; | ||
private static final int DEFAULT_PING_PORT = 80; | ||
private static final int DEFAULT_PING_INTERVAL_IN_MS = 2000; | ||
private static final int DEFAULT_INITIAL_PING_INTERVAL_IN_MS = DEFAULT_PING_INTERVAL_IN_MS; | ||
private static final int DEFAULT_PING_TIMEOUT_IN_MS = 2000; | ||
|
||
protected ReactiveNetwork() { | ||
|
@@ -101,10 +102,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_INITIAL_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 | ||
* | ||
|
@@ -117,23 +133,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 positive number nor a positive 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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, remember to update documentation in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, ok! |
||
.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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could use
Preconditions.checkPositive(int,String)
method in the same way as it's used below. It does logically the same thing as this conditional statement.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried doing that but positive checking throws for 0 or lower so had to mirror the preconditions code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And 0 is what we want for an immediate check.