Skip to content

Commit

Permalink
move NetworkUtils to WPUtils subtree
Browse files Browse the repository at this point in the history
  • Loading branch information
maxme committed Aug 26, 2014
1 parent f0229fc commit 5a6e5cf
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.wordpress.android.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.provider.Settings;

/**
* requires android.permission.ACCESS_NETWORK_STATE
*/
public class NetworkUtils {
public static final int TYPE_UNKNOWN = -1;

/**
* returns information on the active network connection
*/
private static NetworkInfo getActiveNetworkInfo(Context context) {
if (context == null) {
return null;
}
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null) {
return null;
}
// note that this may return null if no network is currently active
return cm.getActiveNetworkInfo();
}

/**
* returns the ConnectivityManager.TYPE_xxx if there's an active connection, otherwise
* returns TYPE_UNKNOWN
*/
private static int getActiveNetworkType(Context context) {
NetworkInfo info = getActiveNetworkInfo(context);
if (info == null || !info.isConnected()) {
return TYPE_UNKNOWN;
}
return info.getType();
}

/**
* returns true if a network connection is available
*/
public static boolean isNetworkAvailable(Context context) {
NetworkInfo info = getActiveNetworkInfo(context);
return (info != null && info.isConnected());
}

/**
* returns true if the user is connected to WiFi
*/
public static boolean isWiFiConnected(Context context) {
return (getActiveNetworkType(context) == ConnectivityManager.TYPE_WIFI);
}

/**
* returns true if airplane mode has been enabled
*/
public static boolean isAirplaneModeOn(Context context) {
// prior to JellyBean 4.2 this was Settings.System.AIRPLANE_MODE_ON, JellyBean 4.2
// moved it to Settings.Global
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
return Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) != 0;
} else {
return Settings.Global.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
}
}

/**
* returns true if there's an active network connection, otherwise displays a toast error
* and returns false
*/
public static boolean checkConnection(Context context) {
if (isNetworkAvailable(context)) {
return true;
}
ToastUtils.showToast(context, R.string.no_network_message);
return false;
}
}
1 change: 1 addition & 0 deletions WordPressUtils/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="ptr_tip_message">Tip: Pull down to refresh</string>
<string name="no_network_message">There is no network available</string>
</resources>

0 comments on commit 5a6e5cf

Please sign in to comment.