号簿助手 v4.6.31版本的 MD5 值为:9d7ff98a6bb5cc178eac2df4d1b2f0e0
以下内容为反编译后的 Connection.java 源代码,内容仅作参考
package com.chinatelecom.pim.foundation.lang.net;
import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import com.chinatelecom.pim.foundation.lang.log.Log;
import com.chinatelecom.pim.foundation.lang.utils.StringUtils;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import org.apache.http.conn.util.InetAddressUtils;
public class Connection {
private static final String CTNET_USER = "ctnet@mycdma.cn";
private static final String CTWAP_USER = "ctwap@mycdma.cn";
private static final String KEY_USER = "user";
private static Connection instance;
private Context context;
private static final Log logger = Log.build(Connection.class);
private static final Uri PREFER_APN = Uri.parse("content://telephony/carriers/preferapn");
private static final String LOG_TAG = Connection.class.getName();
public static boolean isHTCApnDevice = false;
private String CTWAP = "ctwap";
private String CTNET = "ctnet";
public enum Type {
NONE,
WIFI,
CTWAP,
CTNET,
CMWAP,
CMNET,
OTHER,
GNET,
GWAP,
CTLTE
}
public boolean isWIFI() {
return getType() == Type.WIFI;
}
public boolean isCtwap() {
return getType() == Type.CTWAP;
}
public boolean isCmwap() {
return getType() == Type.CMWAP;
}
public String getTypeString() {
Type type = getType();
return type == null ? "" : type.toString();
}
public boolean isConnected() {
return checkConnection(this.context);
}
public static void initialize(Context context) {
if (instance == null) {
instance = new Connection(context);
}
}
private Connection(Context context) {
this.context = context;
}
public static Connection getInstance(Context context) {
if (instance == null) {
initialize(context);
}
return instance;
}
public static boolean checkConnection(Context context) {
ConnectivityManager mConnectivityManager;
NetworkInfo mNetworkInfo;
return (context == null || (mConnectivityManager = (ConnectivityManager) context.getSystemService("connectivity")) == null || (mNetworkInfo = mConnectivityManager.getActiveNetworkInfo()) == null || !mNetworkInfo.isConnected() || !mNetworkInfo.isAvailable()) ? false : true;
}
public static boolean isMobileNow(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService("connectivity");
NetworkInfo networkINfo = cm.getActiveNetworkInfo();
return networkINfo != null && networkINfo.getType() == 0;
}
public static boolean isWifiNow(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService("connectivity");
NetworkInfo networkINfo = cm.getActiveNetworkInfo();
return networkINfo != null && networkINfo.getType() == 1;
}
private Type getType() {
if (this.context != null) {
ConnectivityManager connectivityManager = (ConnectivityManager) this.context.getSystemService("connectivity");
if (connectivityManager == null) {
return Type.NONE;
}
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
boolean connected = networkInfo != null && networkInfo.getState() == NetworkInfo.State.CONNECTED && networkInfo.isAvailable();
if (connected) {
if (networkInfo.getType() == 1) {
return Type.WIFI;
}
if (networkInfo.getType() == 0) {
if (isHTCApnDevice) {
InetAddress inetAddress = getLocalInetAddress();
if (inetAddress != null && inetAddress.getHostAddress() != null) {
logger.debug("current HTC ip Address:%s", inetAddress.getHostAddress().toString());
if (StringUtils.startsWith(inetAddress.getHostAddress().toString(), "10.")) {
return Type.CTWAP;
}
return Type.CTNET;
}
} else {
if (StringUtils.equalsIgnoreCase(networkInfo.getExtraInfo(), "ctwap") && (StringUtils.equals(networkInfo.getReason(), "dataEnabled") || StringUtils.equals(networkInfo.getReason(), "connected"))) {
return Type.CTWAP;
}
if (StringUtils.equalsIgnoreCase(networkInfo.getExtraInfo(), "ctnet") && (StringUtils.equals(networkInfo.getReason(), "dataEnabled") || StringUtils.equals(networkInfo.getReason(), "connected"))) {
return Type.CTNET;
}
if (StringUtils.equals(networkInfo.getExtraInfo(), "ctlte") || StringUtils.equals(networkInfo.getExtraInfo(), "connected")) {
return Type.CTLTE;
}
if (StringUtils.equals(networkInfo.getExtraInfo(), "cmwap") || StringUtils.equals(networkInfo.getExtraInfo(), "cmwap:GSM")) {
return Type.CMWAP;
}
if (StringUtils.equals(networkInfo.getExtraInfo(), "cmnet") || StringUtils.equals(networkInfo.getExtraInfo(), "cmnet:GSM")) {
return Type.CMNET;
}
if (StringUtils.equals(networkInfo.getExtraInfo(), "3gnet")) {
return Type.GNET;
}
if (StringUtils.equals(networkInfo.getExtraInfo(), "3gwap")) {
return Type.GWAP;
}
if ((networkInfo.getExtraInfo() != null && networkInfo.getExtraInfo().indexOf("GSM") == -1) || networkInfo.getExtraInfo() == null) {
return getApnTypeCT(this.context);
}
}
}
} else {
return Type.NONE;
}
}
return Type.NONE;
}
public Type getApnTypeCM(Context context) {
Type netType = Type.NONE;
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService("connectivity");
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo == null) {
return netType;
}
int nType = networkInfo.getType();
if (nType == 0) {
logger.debug("networkInfo.getExtraInfo() is %s", networkInfo.getExtraInfo());
if (networkInfo.getExtraInfo().toLowerCase().equals("cmnet")) {
netType = Type.CMNET;
} else {
netType = Type.CMWAP;
}
} else if (nType == 1) {
netType = Type.WIFI;
}
return netType;
}
private Type getApnTypeCT(Context context) {
Type apntype = Type.NONE;
try {
Cursor cursor = context.getContentResolver().query(PREFER_APN, null, null, null, null);
cursor.moveToFirst();
String user = cursor.getString(cursor.getColumnIndex("apn"));
logger.debug("chinatelecom network APN Type : %s", user);
if (user.startsWith(this.CTNET)) {
apntype = Type.CTNET;
} else if (user.startsWith(this.CTWAP)) {
apntype = Type.CTWAP;
}
if (cursor != null) {
cursor.close();
}
return apntype;
} catch (Exception e) {
e.printStackTrace();
return Type.NONE;
}
}
private InetAddress getLocalInetAddress() {
try {
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
NetworkInterface intf = en.nextElement();
Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
while (enumIpAddr.hasMoreElements()) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && (inetAddress instanceof Inet4Address)) {
return inetAddress;
}
}
}
} catch (SocketException ex) {
logger.debug("%s", ex.toString());
}
return null;
}
public static String getPhoneIp() {
try {
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
NetworkInterface intf = en.nextElement();
Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
while (enumIpAddr.hasMoreElements()) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && (inetAddress instanceof Inet4Address)) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (Exception e) {
}
return "";
}
public static String getLocalIpAddress() {
try {
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
NetworkInterface intf = en.nextElement();
Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
while (enumIpAddr.hasMoreElements()) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return null;
}
public static boolean isWifiConnected(Context context) {
ConnectivityManager mConnectivityManager;
NetworkInfo mWiFiNetworkInfo;
if (context == null || (mConnectivityManager = (ConnectivityManager) context.getSystemService("connectivity")) == null || (mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(1)) == null) {
return false;
}
return mWiFiNetworkInfo.isAvailable();
}
public static boolean isMobileConnected(Context context) {
ConnectivityManager mConnectivityManager;
NetworkInfo mMobileNetworkInfo;
if (context == null || (mConnectivityManager = (ConnectivityManager) context.getSystemService("connectivity")) == null || (mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(0)) == null) {
return false;
}
return mMobileNetworkInfo.isAvailable();
}
public static int getConnectedType(Context context) {
ConnectivityManager mConnectivityManager;
NetworkInfo mNetworkInfo;
if (context == null || (mConnectivityManager = (ConnectivityManager) context.getSystemService("connectivity")) == null || (mNetworkInfo = mConnectivityManager.getActiveNetworkInfo()) == null || !mNetworkInfo.isAvailable()) {
return -1;
}
return mNetworkInfo.getType();
}
}