Search in sources :

Example 1 with NetworkNotAvailableException

use of org.openhab.habdroid.core.connection.exception.NetworkNotAvailableException in project openhab-android by openhab.

the class ConnectionFactory method determineAvailableConnection.

// called in update thread
private static Connection determineAvailableConnection(Context context, Connection local, Connection remote) throws ConnectionException {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = connectivityManager.getActiveNetworkInfo();
    if (info == null || !info.isConnected()) {
        Log.e(TAG, "Network is not available");
        throw new NetworkNotAvailableException();
    }
    // If we are on a mobile network go directly to remote URL from settings
    if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
        if (remote == null) {
            throw new NoUrlInformationException(false);
        }
        return remote;
    }
    // Else if we are on Wifi, Ethernet, WIMAX or VPN network
    if (LOCAL_CONNECTION_TYPES.contains(info.getType())) {
        // If local URL is configured and rechable
        if (local != null && local.checkReachabilityInBackground()) {
            Log.d(TAG, "Connecting to local URL");
            return local;
        }
        // If local URL is not reachable or not configured, try with remote URL
        if (remote != null) {
            Log.d(TAG, "Connecting to remote URL");
            return remote;
        } else {
            throw new NoUrlInformationException(true);
        }
    // Else we treat other networks types as unsupported
    } else {
        Log.e(TAG, "Network type (" + info.getTypeName() + ") is unsupported");
        throw new NetworkNotSupportedException(info);
    }
}
Also used : NetworkInfo(android.net.NetworkInfo) NoUrlInformationException(org.openhab.habdroid.core.connection.exception.NoUrlInformationException) ConnectivityManager(android.net.ConnectivityManager) NetworkNotAvailableException(org.openhab.habdroid.core.connection.exception.NetworkNotAvailableException) NetworkNotSupportedException(org.openhab.habdroid.core.connection.exception.NetworkNotSupportedException)

Aggregations

ConnectivityManager (android.net.ConnectivityManager)1 NetworkInfo (android.net.NetworkInfo)1 NetworkNotAvailableException (org.openhab.habdroid.core.connection.exception.NetworkNotAvailableException)1 NetworkNotSupportedException (org.openhab.habdroid.core.connection.exception.NetworkNotSupportedException)1 NoUrlInformationException (org.openhab.habdroid.core.connection.exception.NoUrlInformationException)1