use of org.openhab.habdroid.core.connection.Connection 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.Connection in project openhab-android by openhab.
the class OpenHABMainActivity method getNotificationSettings.
/**
* Returns the notification settings object
* @return Returns the NotificationSettings or null, if openHAB-cloud isn't used
*/
public NotificationSettings getNotificationSettings() {
if (mNotifySettings != null) {
return mNotifySettings;
}
Connection cloudConnection = ConnectionFactory.getConnection(TYPE_CLOUD);
if (cloudConnection == null) {
return null;
}
// check whether URL is valid
try {
new URL(cloudConnection.getOpenHABUrl());
mNotifySettings = new NotificationSettings(cloudConnection);
return mNotifySettings;
} catch (MalformedURLException e) {
return null;
}
}
use of org.openhab.habdroid.core.connection.Connection in project openhab-android by openhab.
the class OpenHABNotificationAdapter method onBindViewHolder.
@Override
public void onBindViewHolder(NotificationViewHolder holder, int position) {
OpenHABNotification notification = mItems.get(position);
holder.mCreatedView.setText(DateUtils.getRelativeDateTimeString(mContext, notification.createdTimestamp(), DateUtils.MINUTE_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, 0));
holder.mMessageView.setText(notification.message());
if (notification.icon() != null) {
Connection conn = ConnectionFactory.getConnection(Connection.TYPE_CLOUD);
String iconUrl = String.format(Locale.US, "%simages/%s.png", conn.getOpenHABUrl(), Uri.encode(notification.icon()));
holder.mIconView.setImageUrl(iconUrl, conn.getUsername(), conn.getPassword(), R.drawable.ic_openhab_appicon_24dp);
} else {
holder.mIconView.setImageResource(R.drawable.ic_openhab_appicon_24dp);
}
}
use of org.openhab.habdroid.core.connection.Connection in project openhab-android by openhab.
the class OpenHABNotificationFragment method loadNotifications.
private void loadNotifications() {
Connection conn = ConnectionFactory.getConnection(Connection.TYPE_CLOUD);
if (conn == null) {
return;
}
startProgressIndicator();
mRequestHandle = conn.getAsyncHttpClient().get("/api/v1/notifications?limit=20", new MyHttpClient.ResponseHandler() {
@Override
public void onSuccess(Call call, int statusCode, Headers headers, byte[] responseBody) {
stopProgressIndicator();
Log.d(TAG, "Notifications request success");
try {
String jsonString = new String(responseBody, "UTF-8");
JSONArray jsonArray = new JSONArray(jsonString);
Log.d(TAG, jsonArray.toString());
mNotifications.clear();
for (int i = 0; i < jsonArray.length(); i++) {
try {
JSONObject sitemapJson = jsonArray.getJSONObject(i);
mNotifications.add(OpenHABNotification.fromJson(sitemapJson));
} catch (JSONException e) {
e.printStackTrace();
}
}
mNotificationAdapter.notifyDataSetChanged();
} catch (UnsupportedEncodingException | JSONException e) {
Log.d(TAG, e.getMessage(), e);
}
}
@Override
public void onFailure(Call call, int statusCode, Headers headers, byte[] responseBody, Throwable error) {
stopProgressIndicator();
Log.e(TAG, "Notifications request failure");
}
});
}
use of org.openhab.habdroid.core.connection.Connection 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();
}
}
Aggregations