package com.sobot.utils;

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

/**
 * @Description: 网络连接工具类
 * @Author: znw
 * @Date: 2021/7/16 上午10:00
 * @Version: 1.0
 */
public class SobotNetUtils {

    /**
     * 判断是否有网络连接
     *
     * @param context
     * @return boolean true 已连接  ；false 网络未连接（包含无网、需要认证）
     */
    public static boolean isConnected(Context context) {
        if (null == context) {
            return false;
        }
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Network[] networks = new Network[0];
            if (connectivity != null) {
                networks = connectivity.getAllNetworks();
            }
            for (Network network : networks) {
                NetworkCapabilities capabilities = connectivity.getNetworkCapabilities(network);
//                if (capabilities != null && (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) ||
//                        capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) ||
//                        capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN) ||
//                        capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET))) {
//                    return true;
//                }
                //当NetworkCapabilities的描述中有VALIDATED这个描述时，此网络是真正可用的
                if (capabilities != null && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) {
                    // Log.i("Avalible", "NetworkCapalbilities:"+capabilities.toString());
                    return true;
                }
            }
        } else {
            NetworkInfo info = null;
            if (connectivity != null) {
                info = connectivity.getActiveNetworkInfo();
            }
            if (info != null && info.isAvailable()) {
                return true;
            }

        }
        return false;
    }

    /**
     * 判断WIFI网络是否可用
     *
     * @param context
     * @return boolean
     */
    public static boolean isWifi(Context context) {
        if (null == context) {
            return false;
        }
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (connectivity != null) {
                Network networks = connectivity.getActiveNetwork();
                NetworkCapabilities networkCapabilities = connectivity.getNetworkCapabilities(networks);
                if (networkCapabilities != null) {
                    if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                        return true;
                    }
                }
            }
        } else {
            NetworkInfo networkInfo = connectivity.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.isConnected()) {
                if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                    return true;
                }
            }

        }

        return false;
    }

    /**
     * 判断MOBILE网络是否可用
     *
     * @param context
     * @return boolean
     */
    public static boolean isMobile(Context context) {
        if (null == context) {
            return false;
        }
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (connectivity != null) {
                Network networks = connectivity.getActiveNetwork();
                NetworkCapabilities networkCapabilities = connectivity.getNetworkCapabilities(networks);
                if (networkCapabilities != null) {
                    if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
                        return true;
                    }
                }
            }
        } else {
            NetworkInfo networkInfo = connectivity.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.isConnected()) {
                if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                    return true;
                }
            }

        }

        return false;
    }
}