com.eg.agent.android.util.ConnectivityInformation Maven / Gradle / Ivy
package com.eg.agent.android.util;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.Log;
import java.text.MessageFormat;
public class ConnectivityInformation {
private static ConnectivityInformation instance=new ConnectivityInformation();
public static ConnectivityInformation getInstance(){
return instance;
}
private static final String ANDROID = "Android";
public static String carrierNameFromContext(Context context)
{
NetworkInfo networkInfo;
try
{
networkInfo = getNetworkInfo(context);
}
catch (SecurityException e)
{
return "unknown";
}
if (!isConnected(networkInfo)) {
return "none";
}
if (isWan(networkInfo)) {
return carrierNameFromTelephonyManager(context);
}
if (isWifi(networkInfo)) {
return "wifi";
}
Log.w("eG Android Agent", MessageFormat.format("Unknown network type: {0} [{1}]", new Object[]{networkInfo.getTypeName(), Integer.valueOf(networkInfo.getType())}));
return "unknown";
}
public static String wanType(Context context)
{
NetworkInfo networkInfo;
try
{
networkInfo = getNetworkInfo(context);
}
catch (SecurityException e)
{
return "unknown";
}
if (!isConnected(networkInfo)) {
return "none";
}
if (isWifi(networkInfo)) {
return "wifi";
}
if (isWan(networkInfo)) {
return connectionNameFromNetworkSubtype(networkInfo.getSubtype());
}
return "unknown";
}
private static boolean isConnected(NetworkInfo networkInfo)
{
return (networkInfo != null) && (networkInfo.isConnected());
}
private static boolean isWan(NetworkInfo networkInfo)
{
switch (networkInfo.getType())
{
case 0:
case 2:
case 3:
case 4:
case 5:
return true;
}
return false;
}
private static boolean isWifi(NetworkInfo networkInfo)
{
switch (networkInfo.getType())
{
case 1:
case 6:
case 7:
case 9:
return true;
}
return false;
}
private static NetworkInfo getNetworkInfo(Context context)
throws SecurityException
{
ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
try
{
return connectivityManager.getActiveNetworkInfo();
}
catch (SecurityException e)
{
Log.w("eG Android Agent","Cannot determine network state. Enable android.permission.ACCESS_NETWORK_STATE in your manifest.");
throw e;
}
}
public static String carrierNameFromTelephonyManager(Context context)
{
TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String networkOperator = telephonyManager.getNetworkOperatorName();
boolean smellsLikeAnEmulator = (Build.PRODUCT.equals("google_sdk")) || (Build.PRODUCT.equals("sdk")) || (Build.PRODUCT.equals("sdk_x86")) || (Build.FINGERPRINT.startsWith("generic"));
if ((networkOperator.equals("Android")) && (smellsLikeAnEmulator)) {
return "wifi";
}
return networkOperator;
}
private static String connectionNameFromNetworkSubtype(int subType)
{
switch (subType)
{
case 7:
return "1xRTT";
case 4:
return "CDMA";
case 2:
return "EDGE";
case 5:
return "EVDO rev 0";
case 6:
return "EVDO rev A";
case 1:
return "GPRS";
case 8:
return "HSDPA";
case 10:
return "HSPA";
case 9:
return "HSUPA";
case 3:
return "UMTS";
case 11:
return "IDEN";
case 12:
return "EVDO rev B";
case 15:
return "HSPAP";
case 14:
return "HRPD";
case 13:
return "LTE";
}
return "unknown";
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy