package me.weyye.todaynews.utils;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.annotation.Nullable;
import android.util.Log;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by Administrator on 2017/2/10 0010.
*/
public class NetworkUtil {
private static final String TAG = NetworkUtil.class.getSimpleName();
/**
* Returns true if device is connected to wifi or mobile network, false
* otherwise.
*
* @param context
* @return
*/
public static boolean isConnected(Context context) {
ConnectivityManager conMan = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo infoWifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (infoWifi != null) {
NetworkInfo.State wifi = infoWifi.getState();
if (wifi == NetworkInfo.State.CONNECTED) {
// EGOR logger.debug("Wifi is connected");
return true;
}
}
NetworkInfo infoMobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (infoMobile != null) {
NetworkInfo.State mobile = infoMobile.getState();
if (mobile == NetworkInfo.State.CONNECTED) {
// EGOR logger.debug("Mobile data is connected");
return true;
}
}
Log.d(TAG,"Network not available");
return false;
}
/**
* Check if there is any connectivity to a Wifi network
*
* @param context
* @return
*/
public static boolean isConnectedWifi(Context context) {
NetworkInfo info = getNetworkInfo(context);
return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
}
/**
* Check if there is any connectivity to a mobile network
*
* @param context
* @return
*/
public static boolean isConnectedMobile(Context context) {
NetworkInfo info = getNetworkInfo(context);
return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE);
}
/**
* Get the network info
*
* @param context
* @return
*/
@Nullable
public static NetworkInfo getNetworkInfo(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo();
}
/**
* 检查 URL 是否合法
* @param url
* @return true 合法,false 非法
*/
public static boolean isNetworkUrl(String url) {
String regex = "^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
Pattern patt = Pattern.compile(regex);
Matcher matcher = patt.matcher(url);
return matcher.matches();
}
}