package com.hh.lib_common.connect;

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

import com.hh.lib_base.BuildConfig;


/**
 * Date: 2022/5/19 0019 14:40
 * Description:
 */
public class NetworkUtils {

    /**
     * 判断是否打开网络
     *
     * @param context
     * @return
     */
    public static boolean isNetworkAvaiable(Context context) {
        boolean isAvaiable = false;
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isAvailable()) {
            isAvaiable = true;
        }
        return isAvaiable;
    }


    /**
     * 跳转到网络设置页面
     * @param context
     */
    public static void goNetSettings(Context context) {
        Intent intent = null;
        //判断当前系统版本
        if (Build.VERSION.SDK_INT > 10) {
            intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
        } else {
            intent = new Intent();
            intent.setClassName("com.android.setting", "com.android.settings.WirelessSettings");
        }
        context.startActivity(intent);
    }


    public static String getNetType(Context context) {

        ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();

        if(activeNetwork == null){
            return "";
        }

        int type = activeNetwork.getType();

        if (type == ConnectivityManager.TYPE_WIFI) {
            // connected to wifi
            return "TYPE_WIFI";
        } else if (type == ConnectivityManager.TYPE_MOBILE) {
            // connected to mobile data
            return "TYPE_MOBILE";
        }else  if(type == ConnectivityManager.TYPE_ETHERNET){

            return "TYPE_ETHERNET";
        }
        return "";
    }


}
