Search in sources :

Example 1 with ConnectionException

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

the class ConnectionFactory method handleMessage.

@Override
public boolean handleMessage(Message msg) {
    switch(msg.what) {
        case // update thread
        MSG_TRIGGER_UPDATE:
            Message result = mMainHandler.obtainMessage(MSG_UPDATE_DONE);
            Connection local, remote;
            synchronized (this) {
                local = mLocalConnection;
                remote = mRemoteConnection;
            }
            try {
                result.obj = determineAvailableConnection(ctx, local, remote);
            } catch (ConnectionException e) {
                result.obj = e;
            }
            mMainHandler.sendMessage(result);
            return true;
        case // main thread
        MSG_UPDATE_DONE:
            if (msg.obj instanceof ConnectionException) {
                updateAvailableConnection(null, (ConnectionException) msg.obj);
            } else {
                // a new callback.
                if (msg.obj == mLocalConnection || msg.obj == mRemoteConnection) {
                    updateAvailableConnection((Connection) msg.obj, null);
                }
            }
            return true;
    }
    return false;
}
Also used : Message(android.os.Message) ConnectionException(org.openhab.habdroid.core.connection.exception.ConnectionException)

Example 2 with ConnectionException

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

the class OpenHABMainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.d(TAG, "onCreate()");
    // Set default values, false means do it one time during the very first launch
    PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    // Set the theme to one from preferences
    mSettings = PreferenceManager.getDefaultSharedPreferences(this);
    // Disable screen timeout if set in preferences
    if (mSettings.getBoolean(Constants.PREFERENCE_SCREENTIMEROFF, false)) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }
    Util.setActivityTheme(this);
    super.onCreate(savedInstanceState);
    String controllerClassName = getResources().getString(R.string.controller_class);
    try {
        Class<?> controllerClass = Class.forName(controllerClassName);
        Constructor<?> constructor = controllerClass.getConstructor(OpenHABMainActivity.class);
        mController = (ContentController) constructor.newInstance(this);
    } catch (Exception e) {
        Log.wtf(TAG, "Could not instantiate activity controller class '" + controllerClassName + "'");
        throw new RuntimeException(e);
    }
    setContentView(R.layout.activity_main);
    // inflate the controller dependent content view
    ViewStub contentStub = findViewById(R.id.content_stub);
    mController.inflateViews(contentStub);
    setupToolbar();
    setupDrawer();
    gcmRegisterBackground();
    mViewPool = new RecyclerView.RecycledViewPool();
    MemorizingTrustManager.setResponder(this);
    // Check if we have openHAB page url in saved instance state?
    if (savedInstanceState != null) {
        mOpenHABVersion = savedInstanceState.getInt("openHABVersion");
        mSitemapList = savedInstanceState.getParcelableArrayList("sitemapList");
        mSelectedSitemap = savedInstanceState.getParcelable("sitemap");
        mInitState = InitState.values()[savedInstanceState.getInt("initState")];
        int lastConnectionHash = savedInstanceState.getInt("connectionHash");
        if (lastConnectionHash != -1) {
            try {
                Connection c = ConnectionFactory.getUsableConnection();
                if (c != null && c.hashCode() == lastConnectionHash) {
                    mConnection = c;
                }
            } catch (ConnectionException e) {
            // ignored
            }
        }
        mController.onRestoreInstanceState(savedInstanceState);
        String lastControllerClass = savedInstanceState.getString("controller");
        if (mSelectedSitemap != null && !mController.getClass().getCanonicalName().equals(lastControllerClass)) {
            // Our controller type changed, so we need to make the new controller aware of the
            // page hierarchy. If the controller didn't change, the hierarchy will be restored
            // via the fragment state restoration.
            mController.recreateFragmentState();
        }
        updateSitemapDrawerItems();
    } else {
        mSitemapList = new ArrayList<>();
    }
    if (getIntent() != null) {
        processIntent(getIntent());
    }
    if (isFullscreenEnabled()) {
        registerReceiver(dreamReceiver, new IntentFilter("android.intent.action.DREAMING_STARTED"));
        registerReceiver(dreamReceiver, new IntentFilter("android.intent.action.DREAMING_STOPPED"));
        checkFullscreen();
    }
    // Create a new boolean and preference and set it to true
    boolean isFirstStart = mSettings.getBoolean("firstStart", true);
    SharedPreferences.Editor prefsEdit = sharedPrefs.edit();
    // If the activity has never started before...
    if (isFirstStart) {
        // Launch app intro
        final Intent i = new Intent(OpenHABMainActivity.this, IntroActivity.class);
        startActivityForResult(i, INTRO_REQUEST_CODE);
        prefsEdit.putBoolean("firstStart", false).apply();
    }
    OnUpdateBroadcastReceiver.updateComparableVersion(prefsEdit);
    prefsEdit.apply();
}
Also used : IntentFilter(android.content.IntentFilter) SharedPreferences(android.content.SharedPreferences) Connection(org.openhab.habdroid.core.connection.Connection) DemoConnection(org.openhab.habdroid.core.connection.DemoConnection) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) RecognizerIntent(android.speech.RecognizerIntent) CertificateExpiredException(java.security.cert.CertificateExpiredException) JSONException(org.json.JSONException) SSLException(javax.net.ssl.SSLException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) CertificateRevokedException(java.security.cert.CertificateRevokedException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ConnectionException(org.openhab.habdroid.core.connection.exception.ConnectionException) CertPathValidatorException(java.security.cert.CertPathValidatorException) NoUrlInformationException(org.openhab.habdroid.core.connection.exception.NoUrlInformationException) ActivityNotFoundException(android.content.ActivityNotFoundException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CertificateNotYetValidException(java.security.cert.CertificateNotYetValidException) MalformedURLException(java.net.MalformedURLException) NetworkNotSupportedException(org.openhab.habdroid.core.connection.exception.NetworkNotSupportedException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) ViewStub(android.view.ViewStub) RecyclerView(android.support.v7.widget.RecyclerView) ConnectionException(org.openhab.habdroid.core.connection.exception.ConnectionException)

Example 3 with ConnectionException

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

the class OpenHABVoiceService method onConnectionChanged.

@Override
public void onConnectionChanged() {
    try {
        Connection conn = ConnectionFactory.getUsableConnection();
        for (Pair<String, Integer> entry : mPendingCommands) {
            sendItemCommand("VoiceCommand", entry.first, conn, entry.second);
        }
    } catch (ConnectionException e) {
        Log.w(TAG, "Couldn't determine OpenHAB URL", e);
        Toast.makeText(OpenHABVoiceService.this, R.string.error_couldnt_determine_openhab_url, Toast.LENGTH_SHORT).show();
    } finally {
        ConnectionFactory.removeListener(this);
        mPendingCommands.clear();
    }
}
Also used : Connection(org.openhab.habdroid.core.connection.Connection) ConnectionException(org.openhab.habdroid.core.connection.exception.ConnectionException)

Example 4 with ConnectionException

use of org.openhab.habdroid.core.connection.exception.ConnectionException 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 5 with ConnectionException

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

ConnectionException (org.openhab.habdroid.core.connection.exception.ConnectionException)5 Connection (org.openhab.habdroid.core.connection.Connection)4 DemoConnection (org.openhab.habdroid.core.connection.DemoConnection)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 Message (android.os.Message)1 RecognizerIntent (android.speech.RecognizerIntent)1 RecyclerView (android.support.v7.widget.RecyclerView)1 ViewStub (android.view.ViewStub)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ConnectException (java.net.ConnectException)1 MalformedURLException (java.net.MalformedURLException)1 SocketTimeoutException (java.net.SocketTimeoutException)1