Skip to content

Commit f85a003

Browse files
committed
move NetworkUtils to WPUtils subtree
1 parent c955d26 commit f85a003

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package org.wordpress.android.util;
2+
3+
import android.content.Context;
4+
import android.net.ConnectivityManager;
5+
import android.net.NetworkInfo;
6+
import android.os.Build;
7+
import android.provider.Settings;
8+
9+
/**
10+
* requires android.permission.ACCESS_NETWORK_STATE
11+
*/
12+
public class NetworkUtils {
13+
public static final int TYPE_UNKNOWN = -1;
14+
15+
/**
16+
* returns information on the active network connection
17+
*/
18+
private static NetworkInfo getActiveNetworkInfo(Context context) {
19+
if (context == null) {
20+
return null;
21+
}
22+
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
23+
if (cm == null) {
24+
return null;
25+
}
26+
// note that this may return null if no network is currently active
27+
return cm.getActiveNetworkInfo();
28+
}
29+
30+
/**
31+
* returns the ConnectivityManager.TYPE_xxx if there's an active connection, otherwise
32+
* returns TYPE_UNKNOWN
33+
*/
34+
private static int getActiveNetworkType(Context context) {
35+
NetworkInfo info = getActiveNetworkInfo(context);
36+
if (info == null || !info.isConnected()) {
37+
return TYPE_UNKNOWN;
38+
}
39+
return info.getType();
40+
}
41+
42+
/**
43+
* returns true if a network connection is available
44+
*/
45+
public static boolean isNetworkAvailable(Context context) {
46+
NetworkInfo info = getActiveNetworkInfo(context);
47+
return (info != null && info.isConnected());
48+
}
49+
50+
/**
51+
* returns true if the user is connected to WiFi
52+
*/
53+
public static boolean isWiFiConnected(Context context) {
54+
return (getActiveNetworkType(context) == ConnectivityManager.TYPE_WIFI);
55+
}
56+
57+
/**
58+
* returns true if airplane mode has been enabled
59+
*/
60+
public static boolean isAirplaneModeOn(Context context) {
61+
// prior to JellyBean 4.2 this was Settings.System.AIRPLANE_MODE_ON, JellyBean 4.2
62+
// moved it to Settings.Global
63+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
64+
return Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) != 0;
65+
} else {
66+
return Settings.Global.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
67+
}
68+
}
69+
70+
/**
71+
* returns true if there's an active network connection, otherwise displays a toast error
72+
* and returns false
73+
*/
74+
public static boolean checkConnection(Context context) {
75+
if (isNetworkAvailable(context)) {
76+
return true;
77+
}
78+
ToastUtils.showToast(context, R.string.no_network_message);
79+
return false;
80+
}
81+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
33
<string name="ptr_tip_message">Tip: Pull down to refresh</string>
4+
<string name="no_network_message">There is no network available</string>
45
</resources>

0 commit comments

Comments
 (0)