Search in sources :

Example 1 with NoUrlInformationException

use of org.openhab.habdroid.core.connection.exception.NoUrlInformationException 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)

Example 2 with NoUrlInformationException

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

the class OpenHABMainActivity method onConnectionChanged.

@Override
public void onConnectionChanged() {
    Connection newConnection;
    ConnectionException failureReason;
    try {
        newConnection = ConnectionFactory.getUsableConnection();
        failureReason = null;
    } catch (ConnectionException e) {
        newConnection = null;
        failureReason = e;
    }
    if (newConnection != null && newConnection == mConnection) {
        return;
    }
    mConnection = newConnection;
    mNotifySettings = null;
    hideSnackbar();
    mSitemapList.clear();
    mSelectedSitemap = null;
    // Execute pending NFC action if initial connection determination finished
    if (mPendingNfcData != null && (mConnection != null || failureReason != null)) {
        onNfcTag(mPendingNfcData);
        mPendingNfcData = null;
    }
    if (newConnection != null) {
        handleConnectionChange();
        mController.updateConnection(newConnection, null);
    } else {
        if (failureReason instanceof NoUrlInformationException) {
            NoUrlInformationException nuie = (NoUrlInformationException) failureReason;
            // no local connection is configured yes
            if (nuie.wouldHaveUsedLocalConnection() && ConnectionFactory.getConnection(Connection.TYPE_LOCAL) == null) {
                if (mServiceResolver == null) {
                    mServiceResolver = new AsyncServiceResolver(this, this, getString(R.string.openhab_service_type));
                    mServiceResolver.start();
                    mController.updateConnection(null, getString(R.string.resolving_openhab));
                }
            } else {
                mController.indicateMissingConfiguration();
            }
        } else if (failureReason != null) {
            final String message;
            if (failureReason instanceof NetworkNotSupportedException) {
                NetworkInfo info = ((NetworkNotSupportedException) failureReason).getNetworkInfo();
                message = getString(R.string.error_network_type_unsupported, info.getTypeName());
            } else {
                message = getString(R.string.error_network_not_available);
            }
            mController.indicateNoNetwork(message);
        } else {
            mController.updateConnection(null, null);
        }
    }
    mViewPool.clear();
    updateSitemapDrawerItems();
    invalidateOptionsMenu();
    updateTitle();
}
Also used : NoUrlInformationException(org.openhab.habdroid.core.connection.exception.NoUrlInformationException) NetworkInfo(android.net.NetworkInfo) Connection(org.openhab.habdroid.core.connection.Connection) DemoConnection(org.openhab.habdroid.core.connection.DemoConnection) ConnectionException(org.openhab.habdroid.core.connection.exception.ConnectionException) AsyncServiceResolver(org.openhab.habdroid.util.AsyncServiceResolver) NetworkNotSupportedException(org.openhab.habdroid.core.connection.exception.NetworkNotSupportedException)

Aggregations

NetworkInfo (android.net.NetworkInfo)2 NetworkNotSupportedException (org.openhab.habdroid.core.connection.exception.NetworkNotSupportedException)2 NoUrlInformationException (org.openhab.habdroid.core.connection.exception.NoUrlInformationException)2 ConnectivityManager (android.net.ConnectivityManager)1 Connection (org.openhab.habdroid.core.connection.Connection)1 DemoConnection (org.openhab.habdroid.core.connection.DemoConnection)1 ConnectionException (org.openhab.habdroid.core.connection.exception.ConnectionException)1 NetworkNotAvailableException (org.openhab.habdroid.core.connection.exception.NetworkNotAvailableException)1 AsyncServiceResolver (org.openhab.habdroid.util.AsyncServiceResolver)1