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;
}
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();
}
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();
}
}
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;
}
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();
}
Aggregations