Example usage for android.net ConnectivityManager requestRouteToHost

List of usage examples for android.net ConnectivityManager requestRouteToHost

Introduction

In this page you can find the example usage for android.net ConnectivityManager requestRouteToHost.

Prototype

@Deprecated
public boolean requestRouteToHost(int networkType, int hostAddress) 

Source Link

Document

Ensure that a network route exists to deliver traffic to the specified host via the specified network interface.

Usage

From source file:org.thoughtcrime.securesms.mms.MmsCommunication.java

private static void checkRouteToHost(Context context, String host) throws IOException {
    InetAddress inetAddress = InetAddress.getByName(host);
    byte[] ipAddressBytes = inetAddress.getAddress();
    int ipAddress = Conversions.byteArrayToIntLittleEndian(ipAddressBytes, 0);
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    if (!manager.requestRouteToHost(MmsDownloader.TYPE_MOBILE_MMS, ipAddress))
        throw new IOException("Connection manager could not obtain route to host.");
    //        if (!manager.requestRouteToHost(ConnectivityManager.TYPE_MOBILE, ipAddress))
    //           throw new IOException("Connection manager could not obtain route to host.");      

}

From source file:org.fdroid.enigtext.mms.MmsCommunication.java

private static void checkRouteToHost(Context context, String host, boolean usingMmsRadio) throws IOException {
    InetAddress inetAddress = InetAddress.getByName(host);

    if (!usingMmsRadio) {
        if (inetAddress.isSiteLocalAddress()) {
            throw new IOException("RFC1918 address in non-MMS radio situation!");
        }//from   w  w w. j  a  v  a 2  s .  c  o  m

        return;
    }

    Log.w("MmsCommunication", "Checking route to address: " + host + " , " + inetAddress.getHostAddress());

    byte[] ipAddressBytes = inetAddress.getAddress();

    if (ipAddressBytes != null && ipAddressBytes.length == 4) {
        int ipAddress = Conversions.byteArrayToIntLittleEndian(ipAddressBytes, 0);
        ConnectivityManager manager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        if (!manager.requestRouteToHost(MmsDownloader.TYPE_MOBILE_MMS, ipAddress))
            throw new IOException("Connection manager could not obtain route to host.");
    }
}

From source file:org.thoughtcrime.securesms.mms.MmsConnection.java

protected static boolean checkRouteToHost(Context context, String host, boolean usingMmsRadio)
        throws IOException {
    InetAddress inetAddress = InetAddress.getByName(host);
    if (!usingMmsRadio) {
        if (inetAddress.isSiteLocalAddress()) {
            throw new IOException("RFC1918 address in non-MMS radio situation!");
        }//from w  w w. java  2s.c  o  m
        Log.w(TAG, "returning vacuous success since MMS radio is not in use");
        return true;
    }
    byte[] ipAddressBytes = inetAddress.getAddress();
    if (ipAddressBytes == null || ipAddressBytes.length != 4) {
        Log.w(TAG, "returning vacuous success since android.net package doesn't support IPv6");
        return true;
    }

    Log.w(TAG, "Checking route to address: " + host + ", " + inetAddress.getHostAddress());
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    int ipAddress = Conversions.byteArrayToIntLittleEndian(ipAddressBytes, 0);
    boolean routeToHostObtained = manager.requestRouteToHost(MmsRadio.TYPE_MOBILE_MMS, ipAddress);
    Log.w(TAG, "requestRouteToHost result: " + routeToHostObtained);
    return routeToHostObtained;
}

From source file:com.mytwitter.Network.NetworkHelper.java

/**
 * Checks that a route to the remote host exists.
 *//*  w ww  .  ja v a2 s .c  om*/
public static boolean isHostRoutable(Context context, String hostname) {

    boolean result = false;
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivity.getActiveNetworkInfo();

    if (connectivity != null && networkInfo != null && networkInfo.isConnected()) {
        int networkType = networkInfo.getType();
        int ipAddress = lookupHost(hostname);
        result = connectivity.requestRouteToHost(networkType, ipAddress);
    }

    return result;
}

From source file:com.tingtingapps.securesms.mms.LegacyMmsConnection.java

@SuppressWarnings("TryWithIdenticalCatches")
protected static boolean checkRouteToHost(Context context, String host, boolean usingMmsRadio)
        throws IOException {
    InetAddress inetAddress = InetAddress.getByName(host);
    if (!usingMmsRadio) {
        if (inetAddress.isSiteLocalAddress()) {
            throw new IOException("RFC1918 address in non-MMS radio situation!");
        }/*w  ww .  java 2  s. co  m*/
        Log.w(TAG, "returning vacuous success since MMS radio is not in use");
        return true;
    }

    if (inetAddress == null) {
        throw new IOException("Unable to lookup host: InetAddress.getByName() returned null.");
    }

    byte[] ipAddressBytes = inetAddress.getAddress();
    if (ipAddressBytes == null) {
        Log.w(TAG, "resolved IP address bytes are null, returning true to attempt a connection anyway.");
        return true;
    }

    Log.w(TAG, "Checking route to address: " + host + ", " + inetAddress.getHostAddress());
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    try {
        final Method requestRouteMethod = manager.getClass().getMethod("requestRouteToHostAddress",
                Integer.TYPE, InetAddress.class);
        final boolean routeToHostObtained = (Boolean) requestRouteMethod.invoke(manager,
                MmsRadio.TYPE_MOBILE_MMS, inetAddress);
        Log.w(TAG, "requestRouteToHostAddress(" + inetAddress + ") -> " + routeToHostObtained);
        return routeToHostObtained;
    } catch (NoSuchMethodException nsme) {
        Log.w(TAG, nsme);
    } catch (IllegalAccessException iae) {
        Log.w(TAG, iae);
    } catch (InvocationTargetException ite) {
        Log.w(TAG, ite);
    }

    final int ipAddress = Conversions.byteArrayToIntLittleEndian(ipAddressBytes, 0);
    final boolean routeToHostObtained = manager.requestRouteToHost(MmsRadio.TYPE_MOBILE_MMS, ipAddress);
    Log.w(TAG, "requestRouteToHost(" + ipAddress + ") -> " + routeToHostObtained);
    return routeToHostObtained;
}