@@ -18,22 +18,21 @@ public class NetworkInspector {
18
18
/**
19
19
* Constructor.
20
20
*
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 .
22
22
*/
23
23
public NetworkInspector (@ NonNull Context context ) {
24
24
this .context = context ;
25
25
}
26
26
27
27
/**
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.
30
29
*
31
30
* @return true iff the active network is connected and working.
32
31
* @see #isActiveNetworkConnected()
33
32
*/
34
33
public boolean isActiveNetworkConnectedAndWorking () {
35
34
return isActiveNetworkConnected ()
36
- && (isGoogleReachableWithPing () || isGoogleReachableWithInetAddress ());
35
+ && (isGoogleReachableWithInetAddress () || isGoogleReachableWithPing ());
37
36
}
38
37
39
38
/**
@@ -44,18 +43,15 @@ public boolean isActiveNetworkConnected() {
44
43
ConnectivityManager connectivityManager = (ConnectivityManager ) context .getSystemService (Context .CONNECTIVITY_SERVICE );
45
44
NetworkInfo networkInfo = connectivityManager .getActiveNetworkInfo ();
46
45
47
- return networkInfo != null
48
- && networkInfo .isAvailable ()
49
- && networkInfo .isConnected ();
46
+ return networkInfo != null && networkInfo .isAvailable () && networkInfo .isConnected ();
50
47
}
51
48
52
49
/**
53
50
* @return true iff a ping to www.google.com succeeded.
54
51
*/
55
52
boolean isGoogleReachableWithPing () {
56
53
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" );
59
55
60
56
int exitValue = process .waitFor ();
61
57
@@ -70,9 +66,7 @@ boolean isGoogleReachableWithPing() {
70
66
*/
71
67
boolean isGoogleReachableWithInetAddress () {
72
68
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 );
76
70
} catch (Exception ex ) {
77
71
return false ;
78
72
}
0 commit comments