-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move NetworkUtils to WPUtils subtree
- Loading branch information
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
WordPressUtils/src/main/java/org/wordpress/android/util/NetworkUtils.java
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 |
---|---|---|
@@ -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; | ||
} | ||
} |
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 |
---|---|---|
@@ -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> |