content: /*
   *   Copyright (C)  2016 android@19code.com
   *
   *   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 com.rsen.github.common;
  
  import android.app.Activity;
  import android.content.ComponentName;
  import android.content.Context;
  import android.content.Intent;
  import android.net.ConnectivityManager;
  import android.net.NetworkInfo;
  import android.net.wifi.ScanResult;
  import android.net.wifi.WifiInfo;
  import android.net.wifi.WifiManager;
  import android.telephony.TelephonyManager;
  import android.text.TextUtils;
  
  import java.util.List;
  
  /**
   * Blog : http://blog.csdn.net/u011240877
   * checked
   */
  public class NetUtils {
  
      public static final String NETWORK_TYPE_WIFI = "wifi";
      public static final String NETWORK_TYPE_3G = "3g";
      public static final String NETWORK_TYPE_2G = "2g";
      public static final String NETWORK_TYPE_WAP = "wap";
      public static final String NETWORK_TYPE_UNKNOWN = "unknown";
      public static final String NETWORK_TYPE_DISCONNECT = "disconnect";
  
      public static int getNetworkType(Context context) {
          ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo networkInfo = connectivityManager == null ? null : connectivityManager.getActiveNetworkInfo();
          return networkInfo == null ? -1 : networkInfo.getType();
      }
  
      public static String getNetworkTypeName(Context context) {
          ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo networkInfo;
          String type = NETWORK_TYPE_DISCONNECT;
          if (manager == null || (networkInfo = manager.getActiveNetworkInfo()) == null) {
              return type;
          }
  
          if (networkInfo.isConnected()) {
              String typeName = networkInfo.getTypeName();
              if ("WIFI".equalsIgnoreCase(typeName)) {
                  type = NETWORK_TYPE_WIFI;
              } else if ("MOBILE".equalsIgnoreCase(typeName)) {
                  //String proxyHost = android.net.Proxy.getDefaultHost();//deprecated
                  String proxyHost = System.getProperty("http.proxyHost");
                  type = TextUtils.isEmpty(proxyHost) ? (isFastMobileNetwork(context) ? NETWORK_TYPE_3G : NETWORK_TYPE_2G) : NETWORK_TYPE_WAP;
              } else {
                  type = NETWORK_TYPE_UNKNOWN;
              }
          }
          return type;
      }
  
      public static boolean isConnected(Context context) {
          ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo info = cm.getActiveNetworkInfo();
          if (info != null && info.isConnected()) {
              if (info.getState() == NetworkInfo.State.CONNECTED) {
                  return true;
              }
          }
          return false;
      }
  
      public static boolean isNetworkAvailable(Context context) {
          if (context == null) {
              return false;
          }
          try {
              ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
              if (connectivity != null) {
                  NetworkInfo info = connectivity.getActiveNetworkInfo();
                  return info.isAvailable();
              }
          } catch (Exception e) {
              return false;
          }
          return false;
      }
  
      public static boolean isWiFi(Context cxt) {
          ConnectivityManager cm = (ConnectivityManager) cxt.getSystemService(Context.CONNECTIVITY_SERVICE);
          // wifi的状态：ConnectivityManager.TYPE_WIFI
          // 3G的状态：ConnectivityManager.TYPE_MOBILE
          return cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI;
      }
  
      //unchecked
      public static void openNetSetting(Activity act) {
          Intent intent = new Intent();
          ComponentName cm = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings");
          intent.setComponent(cm);
          intent.setAction("android.intent.action.VIEW");
          act.startActivityForResult(intent, 0);
      }
  
  
      /**
       * Whether is fast mobile network
       */
  
      private static boolean isFastMobileNetwork(Context context) {
          TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
          if (telephonyManager == null) {
              return false;
          }
  
          switch (telephonyManager.getNetworkType()) {
              case TelephonyManager.NETWORK_TYPE_1xRTT:
                  return false;
              case TelephonyManager.NETWORK_TYPE_CDMA:
                  return false;
              case TelephonyManager.NETWORK_TYPE_EDGE:
                  return false;
              case TelephonyManager.NETWORK_TYPE_EVDO_0:
                  return true;
              case TelephonyManager.NETWORK_TYPE_EVDO_A:
                  return true;
              case TelephonyManager.NETWORK_TYPE_GPRS:
                  return false;
              case TelephonyManager.NETWORK_TYPE_HSDPA:
                  return true;
              case TelephonyManager.NETWORK_TYPE_HSPA:
                  return true;
              case TelephonyManager.NETWORK_TYPE_HSUPA:
                  return true;
              case TelephonyManager.NETWORK_TYPE_UMTS:
                  return true;
              case TelephonyManager.NETWORK_TYPE_EHRPD:
                  return true;
              case TelephonyManager.NETWORK_TYPE_EVDO_B:
                  return true;
              case TelephonyManager.NETWORK_TYPE_HSPAP:
                  return true;
              case TelephonyManager.NETWORK_TYPE_IDEN:
                  return false;
              case TelephonyManager.NETWORK_TYPE_LTE:
                  return true;
              case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                  return false;
              default:
                  return false;
          }
      }
  
      public static void setWifiEnabled(Context context, boolean enabled) {
          WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
          wifiManager.setWifiEnabled(enabled);
      }
  
      public static List<ScanResult> getWifiScanResults(Context context) {
          WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
          return wifiManager.startScan() ? wifiManager.getScanResults() : null;
      }
  
      public static ScanResult getScanResultsByBSSID(Context context, String bssid) {
          WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
          ScanResult scanResult = null;
          boolean f = wifiManager.startScan();
          if (!f) {
              getScanResultsByBSSID(context, bssid);
          }
          List<ScanResult> list = wifiManager.getScanResults();
          if (list != null) {
              for (int i = 0; i < list.size(); i++) {
                  scanResult = list.get(i);
                  if (scanResult.BSSID.equals(bssid)) {
                      break;
                  }
              }
          }
          return scanResult;
      }
  
      public static WifiInfo getWifiConnectionInfo(Context context) {
          WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
          return wifiManager.getConnectionInfo();
      }
  }
  