Skip to content

Commit a9fd1e8

Browse files
author
Adil Hussain
committed
Some small tweaks in the NetworkInspector class.
1 parent 6455b91 commit a9fd1e8

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

library/src/main/java/com/tazkiyatech/utils/network/NetworkInspector.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,21 @@ public class NetworkInspector {
1818
/**
1919
* Constructor.
2020
*
21-
* @param context the {@link Context} used to get the {@link ConnectivityManager}.
21+
* @param context The {@link Context} to use in order to get system services.
2222
*/
2323
public NetworkInspector(@NonNull Context context) {
2424
this.context = context;
2525
}
2626

2727
/**
28-
* This method should not be called in the main/UI thread
29-
* as it makes a network call.
28+
* This method should not be called in the main/UI thread as it makes a network call.
3029
*
3130
* @return true iff the active network is connected and working.
3231
* @see #isActiveNetworkConnected()
3332
*/
3433
public boolean isActiveNetworkConnectedAndWorking() {
3534
return isActiveNetworkConnected()
36-
&& (isGoogleReachableWithPing() || isGoogleReachableWithInetAddress());
35+
&& (isGoogleReachableWithInetAddress() || isGoogleReachableWithPing());
3736
}
3837

3938
/**
@@ -44,18 +43,15 @@ public boolean isActiveNetworkConnected() {
4443
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
4544
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
4645

47-
return networkInfo != null
48-
&& networkInfo.isAvailable()
49-
&& networkInfo.isConnected();
46+
return networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected();
5047
}
5148

5249
/**
5350
* @return true iff a ping to www.google.com succeeded.
5451
*/
5552
boolean isGoogleReachableWithPing() {
5653
try {
57-
Runtime runtime = Runtime.getRuntime();
58-
Process process = runtime.exec("/system/bin/ping -c 1 www.google.com");
54+
Process process = Runtime.getRuntime().exec("/system/bin/ping -c 1 google.com");
5955

6056
int exitValue = process.waitFor();
6157

@@ -70,9 +66,7 @@ boolean isGoogleReachableWithPing() {
7066
*/
7167
boolean isGoogleReachableWithInetAddress() {
7268
try {
73-
InetAddress inetAddress = InetAddress.getByName("www.google.com");
74-
75-
return inetAddress != null && !inetAddress.toString().equals("");
69+
return InetAddress.getByName("google.com").isReachable(5_000);
7670
} catch (Exception ex) {
7771
return false;
7872
}

0 commit comments

Comments
 (0)