use of org.openhab.habdroid.model.OpenHABSitemap in project openhab-android by openhab.
the class OpenHABMainActivity method selectConfiguredSitemapFromList.
private OpenHABSitemap selectConfiguredSitemapFromList() {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
String configuredSitemap = settings.getString(Constants.PREFERENCE_SITEMAP_NAME, "");
final OpenHABSitemap result;
if (mSitemapList.size() == 1) {
// We only have one sitemap, use it
result = mSitemapList.get(0);
} else if (!configuredSitemap.isEmpty()) {
// Select configured sitemap if still present, nothing otherwise
result = Util.getSitemapByName(mSitemapList, configuredSitemap);
} else {
// Nothing configured -> can't auto-select anything
result = null;
}
Log.d(TAG, "Configured sitemap is '" + configuredSitemap + "', selected " + result);
boolean hasResult = result != null;
boolean hasConfigured = !configuredSitemap.isEmpty();
if (!hasResult && hasConfigured) {
// clear old configuration
settings.edit().remove(Constants.PREFERENCE_SITEMAP_LABEL).remove(Constants.PREFERENCE_SITEMAP_NAME).apply();
} else if (hasResult && (!hasConfigured || !configuredSitemap.equals(result.name()))) {
// update result
settings.edit().putString(Constants.PREFERENCE_SITEMAP_NAME, result.name()).putString(Constants.PREFERENCE_SITEMAP_LABEL, result.label()).apply();
}
return result;
}
use of org.openhab.habdroid.model.OpenHABSitemap in project openhab-android by openhab.
the class OpenHABMainActivity method setupDrawer.
private void setupDrawer() {
mDrawerLayout = findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close);
mDrawerLayout.addDrawerListener(mDrawerToggle);
mDrawerLayout.addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
@Override
public void onDrawerOpened(View drawerView) {
if (mInitState == InitState.DONE) {
loadSitemapList(false);
}
}
});
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
NavigationView drawerMenu = findViewById(R.id.left_drawer);
drawerMenu.inflateMenu(R.menu.left_drawer);
mDrawerMenu = drawerMenu.getMenu();
// We only want to tint the menu icons, but not our loaded sitemap icons. NavigationView
// unfortunately doesn't support this directly, so we tint the icon drawables manually
// instead of letting NavigationView do it.
mDrawerIconTintList = drawerMenu.getItemIconTintList();
drawerMenu.setItemIconTintList(null);
for (int i = 0; i < mDrawerMenu.size(); i++) {
MenuItem item = mDrawerMenu.getItem(i);
item.setIcon(applyDrawerIconTint(item.getIcon()));
}
drawerMenu.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
mDrawerLayout.closeDrawers();
switch(item.getItemId()) {
case R.id.notifications:
openNotifications();
return true;
case R.id.settings:
Intent settingsIntent = new Intent(OpenHABMainActivity.this, OpenHABPreferencesActivity.class);
startActivityForResult(settingsIntent, SETTINGS_REQUEST_CODE);
return true;
case R.id.about:
openAbout();
return true;
}
if (item.getGroupId() == GROUP_ID_SITEMAPS) {
OpenHABSitemap sitemap = mSitemapList.get(item.getItemId());
openSitemap(sitemap);
return true;
}
return false;
}
});
}
use of org.openhab.habdroid.model.OpenHABSitemap in project openhab-android by openhab.
the class Util method parseSitemapList.
public static List<OpenHABSitemap> parseSitemapList(Document document) {
List<OpenHABSitemap> sitemapList = new ArrayList<OpenHABSitemap>();
NodeList sitemapNodes = document.getElementsByTagName("sitemap");
if (sitemapNodes.getLength() > 0) {
for (int i = 0; i < sitemapNodes.getLength(); i++) {
sitemapList.add(OpenHABSitemap.fromXml(sitemapNodes.item(i)));
}
}
// Sort by sitename label
Collections.sort(sitemapList, new Comparator<OpenHABSitemap>() {
@Override
public int compare(OpenHABSitemap sitemap1, OpenHABSitemap sitemap2) {
if (sitemap1.label() == null) {
return sitemap2.label() == null ? 0 : -1;
}
if (sitemap2.label() == null) {
return 1;
}
return sitemap1.label().compareTo(sitemap2.label());
}
});
return sitemapList;
}
use of org.openhab.habdroid.model.OpenHABSitemap in project openhab-android by openhab.
the class OpenHABMainActivity method loadSitemapList.
/**
* Get sitemaps from openHAB, if user already configured preffered sitemap
* just open it. If no preffered sitemap is configured - let user select one.
*/
private void loadSitemapList(final boolean selectSitemapAfterLoad) {
if (mConnection == null) {
return;
}
Log.d(TAG, "Loading sitemap list from /rest/sitemaps");
mInitState = InitState.LOAD_SITEMAPS;
mPendingCall = mConnection.getAsyncHttpClient().get("/rest/sitemaps", new DefaultHttpResponseHandler() {
@Override
public void onSuccess(Call call, int statusCode, Headers headers, byte[] responseBody) {
Log.d(TAG, new String(responseBody));
mPendingCall = null;
mInitState = InitState.DONE;
// OH1 returns XML, later versions return JSON
List<OpenHABSitemap> result = mOpenHABVersion == 1 ? loadSitemapsFromXml(responseBody) : loadSitemapsFromJson(responseBody);
Log.d(TAG, "Server returned sitemaps: " + result);
mSitemapList.clear();
if (result != null) {
mSitemapList.addAll(result);
}
updateSitemapDrawerItems();
if (!selectSitemapAfterLoad) {
return;
}
if (mSitemapList.isEmpty()) {
Log.e(TAG, "openHAB returned empty sitemap list");
mController.indicateServerCommunicationFailure(getString(R.string.error_empty_sitemap_list));
} else {
OpenHABSitemap sitemap = selectConfiguredSitemapFromList();
if (sitemap != null) {
openSitemap(sitemap);
} else {
showSitemapSelectionDialog();
}
}
}
});
}
use of org.openhab.habdroid.model.OpenHABSitemap in project openhab-android by openhab.
the class OpenHABMainActivity method updateSitemapDrawerItems.
private void updateSitemapDrawerItems() {
MenuItem sitemapItem = mDrawerMenu.findItem(R.id.sitemaps);
MenuItem notificationsItem = mDrawerMenu.findItem(R.id.notifications);
notificationsItem.setVisible(getNotificationSettings() != null);
if (mSitemapList.isEmpty()) {
sitemapItem.setVisible(false);
} else {
sitemapItem.setVisible(true);
SubMenu menu = sitemapItem.getSubMenu();
menu.clear();
for (int i = 0; i < mSitemapList.size(); i++) {
OpenHABSitemap sitemap = mSitemapList.get(i);
MenuItem item = menu.add(GROUP_ID_SITEMAPS, i, i, sitemap.label());
loadSitemapIcon(sitemap, item);
}
}
}
Aggregations