Search in sources :

Example 6 with Connection

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

the class OpenHABVoiceService method onStartCommand.

@Override
public int onStartCommand(Intent intent, int flags, final int startId) {
    final String voiceCommand = extractVoiceCommand(intent);
    boolean hasSentCommand = false;
    if (!voiceCommand.isEmpty()) {
        try {
            Connection conn = ConnectionFactory.getUsableConnection();
            if (conn != null) {
                sendItemCommand("VoiceCommand", voiceCommand, conn, startId);
            } else {
                mPendingCommands.add(Pair.create(voiceCommand, startId));
                ConnectionFactory.addListener(this);
            }
            hasSentCommand = true;
        } catch (ConnectionException e) {
            Log.w(TAG, "Couldn't determine OpenHAB URL", e);
            Toast.makeText(this, R.string.error_couldnt_determine_openhab_url, Toast.LENGTH_SHORT).show();
        }
    }
    if (!hasSentCommand) {
        stopSelf(startId);
    }
    return START_NOT_STICKY;
}
Also used : Connection(org.openhab.habdroid.core.connection.Connection) ConnectionException(org.openhab.habdroid.core.connection.exception.ConnectionException)

Example 7 with Connection

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

Connection (org.openhab.habdroid.core.connection.Connection)7 ConnectionException (org.openhab.habdroid.core.connection.exception.ConnectionException)4 DemoConnection (org.openhab.habdroid.core.connection.DemoConnection)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 MalformedURLException (java.net.MalformedURLException)2 JSONException (org.json.JSONException)2 NetworkNotSupportedException (org.openhab.habdroid.core.connection.exception.NetworkNotSupportedException)2 NoUrlInformationException (org.openhab.habdroid.core.connection.exception.NoUrlInformationException)2 PendingIntent (android.app.PendingIntent)1 ActivityNotFoundException (android.content.ActivityNotFoundException)1 Intent (android.content.Intent)1 IntentFilter (android.content.IntentFilter)1 SharedPreferences (android.content.SharedPreferences)1 NetworkInfo (android.net.NetworkInfo)1 RecognizerIntent (android.speech.RecognizerIntent)1 RecyclerView (android.support.v7.widget.RecyclerView)1 ViewStub (android.view.ViewStub)1 IOException (java.io.IOException)1 ConnectException (java.net.ConnectException)1 SocketTimeoutException (java.net.SocketTimeoutException)1