/*
 * Copyright (C) 2015, 程序亦非猿
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.yifeiyuan.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.annotation.NonNull;
import android.telephony.TelephonyManager;

/**
 * Created by 程序亦非猿 on 15/12/26.
 *
 * 包含如下方法
 * 1.getDeviceId            获取IMEI
 * 2.isNetworkConnected     网络是否连接
 * 3.isWifiConnected        wifi是否可用
 * 4.getCurNetType          当前网络
 */
public class DeviceUtil {

    /**
     * 获取设备唯一标识
     * 例子:IMEI for GSM and the MEID or ESN for CDMA phones
     * 注意,需要添加权限: {@link android.Manifest.permission#READ_PHONE_STATE}
     *
     * @param context 上下文
     * @return eg.IMEI for GSM
     */
    public static String getDeviceId(@NonNull Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        return tm.getDeviceId();
    }

    /**
     * 网络是否连接
     *
     * @param context 上下文
     * @return true 如果网络连接着
     */
    public static boolean isNetworkConnected(Context context) {
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = manager.getActiveNetworkInfo();
        return null != info && info.isAvailable();
    }

    /**
     * wifi是否连接可用
     *
     * @param context
     * @return true 如果wifi可用
     */
    public static boolean isWifiConnected(Context context) {
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        return info != null && info.isConnected();
    }

    /**
     * 得到当前的手机网络类型
     *
     * @param context
     * @return 没网返回"" , 有网返回 2g / 3g /4g/wifi
     */
    public static String getCurNetType(Context context) {
        String type = "";
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getActiveNetworkInfo();
        if (info == null) {
            type = "";
        } else if (info.getType() == ConnectivityManager.TYPE_WIFI) {
            type = "wifi";
        } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
            int subType = info.getSubtype();
            if (subType == TelephonyManager.NETWORK_TYPE_CDMA || subType == TelephonyManager.NETWORK_TYPE_GPRS
                    || subType == TelephonyManager.NETWORK_TYPE_EDGE) {
                type = "2g";
            } else if (subType == TelephonyManager.NETWORK_TYPE_UMTS || subType == TelephonyManager.NETWORK_TYPE_HSDPA
                    || subType == TelephonyManager.NETWORK_TYPE_EVDO_A || subType == TelephonyManager.NETWORK_TYPE_EVDO_0
                    || subType == TelephonyManager.NETWORK_TYPE_EVDO_B) {
                type = "3g";
            } else if (subType == TelephonyManager.NETWORK_TYPE_LTE) {// LTE是3g到4g的过渡，是3.9G的全球标准
                type = "4g";
            }
        }
        return type;
    }
}

@@ -1481,32 +1481,111 @@
 * %E7%BD%91%E7%BB%9C%E6%98%AF%E5%90%A6%E8%BF%9E%E6%8E%A5%0A     *%0A
+     * %E6%B3%A8%E6%84%8F%E9%9C%80%E8%A6%81%E6%9D%83%E9%99%90: %7B@link android.Manifest.permission#ACCESS_NETWORK_STATE%7D%0A     *%0A
      * @param co
@@ -1586,32 +1586,32 @@
 ram context %E4%B8%8A%E4%B8%8B%E6%96%87%0A
-
      * @return t
@@ -1953,16 +1953,100 @@
 i%E6%98%AF%E5%90%A6%E8%BF%9E%E6%8E%A5%E5%8F%AF%E7%94%A8%0A
+     * %3Cp/%3E%0A     * %E6%B3%A8%E6%84%8F%E9%9C%80%E8%A6%81%E6%9D%83%E9%99%90: %7B@link android.Manifest.permission#ACCESS_NETWORK_STATE%7D%0A
      *%0A 
@@ -2184,39 +2184,34 @@
 ectivityManager 
+c
 m
-anager
  = (Connectivity
@@ -2298,31 +2298,26 @@
 Info info = 
+c
 m
-anager
 .getNetworkI

update deviceutil