package com.demo.mylibrary.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.os.Build;
import android.widget.Toast;

public class NetworkUtils {

    public static String getNetInfo(Context context) {
        String netInfo = null;
        ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (Build.VERSION.SDK_INT < 21) {
            if (cm != null) {
                NetworkInfo wifiNetworkInfo = cm.getNetworkInfo(1);
                boolean wiFiConnected = wifiNetworkInfo.isConnected();
                NetworkInfo networkInfo = cm.getNetworkInfo(0);
                boolean netConnected = networkInfo.isConnected();
                if (wiFiConnected) {
                    netInfo = "WIFI";
                } else if (netConnected) {
                    netInfo = "MOBILE";
                } else {
                    Toast.makeText(context, "请检查网络是否连接", 0).show();
                }
            }
        } else {
            Network[] networks = cm.getAllNetworks();
            Network[] var13 = networks;
            int var14 = networks.length;

            for(int var7 = 0; var7 < var14; ++var7) {
                Network net = var13[var7];
                NetworkInfo networkInfo = cm.getNetworkInfo(net);
                boolean connected = networkInfo.isConnected();
                String type = networkInfo.getTypeName();
                if (connected) {
                    if (type.equals("WIFI")) {
                        netInfo = "WIFI";
                    } else if (type.equals("MOBILE")) {
                        netInfo = "MOBILE";
                    } else {
                        Toast.makeText(context, "请检查网络是否连接", 0).show();
                    }
                }
            }
        }

        return netInfo;
    }

    /**
     * 判断wifi是否连接
     * @param context
     * @return
     */
    public static boolean isWiFiConnected(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            //获取WIFI连接的信息
            NetworkInfo wifiNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            return wifiNetworkInfo.isConnected();
        }
        return false;
    }

    /**
     * 判断移动网络是否连接
     * @param context
     * @return
     */
    public static boolean isNetConnected(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            //获取移动数据连接的信息
            NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            return networkInfo.isConnected();
        }
        return false;
    }

    /**
     * 是否联网
     */
    public static boolean isNetworkConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager =
                    (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }
}
