package com.angcyo.uiview.net.base;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.util.Log;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * Copyright (C) 2016,深圳市红鸟网络科技股份有限公司 All rights reserved.
 * 项目名称：
 * 类的描述：网络状态检查, 网络类别工具
 * 创建人员：Robi
 * 创建时间：2016/11/30 8:34
 * 修改人员：Robi
 * 修改时间：2016/11/30 8:34
 * 修改备注：
 * Version: 1.0.0
 */
public class Network {

    /**
     * 判断网络是否连接
     */
    public static boolean isConnected(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm == null)
            return false;
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo == null) {
            return false;
        }
        if (netInfo.isConnected()) {
            return true;
        }
        return false;
    }

    /**
     * 获取网络类型
     */
    public static String getNetTypeName(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            NetworkInfo activeNetworkInfo = connectivityManager
                    .getActiveNetworkInfo();
            if (activeNetworkInfo == null) {
                return "NoNet";
            }
            if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                return activeNetworkInfo.getTypeName();
            } else {
                String typeName = activeNetworkInfo.getSubtypeName();
                //Log.i("网络名称:", typeName);
                if (typeName == null || typeName.length() == 0) {
                    return "Unknown";
                } else if (typeName.length() > 3) {
                    return activeNetworkInfo.getSubtypeName().substring(0, 4);
                } else {
                    return activeNetworkInfo.getSubtypeName().substring(0, typeName.length());
                }
            }
        } else {
            return "NoNet";
        }
    }

    /**
     * 获取手机号码(双卡手机未测试)
     */
    public static String getTelNumber(Context context) {
        TelephonyManager tm = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        return tm.getLine1Number();
    }

    /**
     * 获取WIFI网络的IP地址
     */
    public static String getWifiIp(Context context) {
        WifiManager wifiManager = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        int ipAddress = wifiInfo.getIpAddress();

        // 格式化IP address，例如：格式化前：1828825280，格式化后：192.168.1.109
        String ip = String.format("%d.%d.%d.%d", (ipAddress & 0xff),
                (ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff),
                (ipAddress >> 24 & 0xff));
        return ip;
    }

    /**
     * 获取GSM网络的IP地址
     */
    public static String getMobileIP() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = (NetworkInterface) en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) {
                        String ipaddress = inetAddress.getHostAddress().toString();
                        return ipaddress;
                    }
                }
            }
        } catch (SocketException ex) {
            Log.e("getMobileIP", "Exception in Get IP Address: " + ex.toString());
        }
        return "";
    }
}

@@ -4011,11 +4011,1497 @@
 %22;%0A    %7D
+%0A%0A    /**%0A     * %E8%8E%B7%E5%8F%96IP%E5%9C%B0%E5%9D%80%0A     * %3Cp%3E%E9%9C%80%E6%B7%BB%E5%8A%A0%E6%9D%83%E9%99%90 %7B@code %3Cuses-permission android:name=%22android.permission.INTERNET%22/%3E%7D%3C/p%3E%0A     *%0A     * @param useIPv4 %E6%98%AF%E5%90%A6%E7%94%A8IPv4%0A     * @return IP%E5%9C%B0%E5%9D%80%0A     */%0A    public static String getIPAddress(boolean useIPv4) %7B%0A        try %7B%0A            for (Enumeration%3CNetworkInterface%3E nis = NetworkInterface.getNetworkInterfaces(); nis.hasMoreElements(); ) %7B%0A                NetworkInterface ni = nis.nextElement();%0A                // %E9%98%B2%E6%AD%A2%E5%B0%8F%E7%B1%B3%E6%89%8B%E6%9C%BA%E8%BF%94%E5%9B%9E10.0.2.15%0A                if (!ni.isUp()) continue;%0A                for (Enumeration%3CInetAddress%3E addresses = ni.getInetAddresses(); addresses.hasMoreElements(); ) %7B%0A                    InetAddress inetAddress = addresses.nextElement();%0A                    if (!inetAddress.isLoopbackAddress()) %7B%0A                        String hostAddress = inetAddress.getHostAddress();%0A                        boolean isIPv4 = hostAddress.indexOf(':') %3C 0;%0A                        if (useIPv4) %7B%0A                            if (isIPv4) return hostAddress;%0A                        %7D else %7B%0A                            if (!isIPv4) %7B%0A                                int index = hostAddress.indexOf('%25');%0A                                return index %3C 0 ? hostAddress.toUpperCase() : hostAddress.substring(0, index).toUpperCase();%0A                            %7D%0A                        %7D%0A                    %7D%0A                %7D%0A            %7D%0A        %7D catch (SocketException e) %7B%0A            e.printStackTrace();%0A        %7D%0A        return null;%0A    %7D
 %0A%7D%0A

add network