Search in sources :

Example 1 with OpenHABSitemap

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;
}
Also used : SharedPreferences(android.content.SharedPreferences) OpenHABSitemap(org.openhab.habdroid.model.OpenHABSitemap)

Example 2 with OpenHABSitemap

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;
        }
    });
}
Also used : NavigationView(android.support.design.widget.NavigationView) ActionBarDrawerToggle(android.support.v7.app.ActionBarDrawerToggle) MenuItem(android.view.MenuItem) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) RecognizerIntent(android.speech.RecognizerIntent) DrawerLayout(android.support.v4.widget.DrawerLayout) RecyclerView(android.support.v7.widget.RecyclerView) NavigationView(android.support.design.widget.NavigationView) View(android.view.View) OpenHABSitemap(org.openhab.habdroid.model.OpenHABSitemap)

Example 3 with OpenHABSitemap

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;
}
Also used : NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) OpenHABSitemap(org.openhab.habdroid.model.OpenHABSitemap)

Example 4 with OpenHABSitemap

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();
                }
            }
        }
    });
}
Also used : Call(okhttp3.Call) Headers(okhttp3.Headers) OpenHABSitemap(org.openhab.habdroid.model.OpenHABSitemap)

Example 5 with OpenHABSitemap

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);
        }
    }
}
Also used : MenuItem(android.view.MenuItem) SubMenu(android.view.SubMenu) OpenHABSitemap(org.openhab.habdroid.model.OpenHABSitemap)

Aggregations

OpenHABSitemap (org.openhab.habdroid.model.OpenHABSitemap)6 MenuItem (android.view.MenuItem)2 ArrayList (java.util.ArrayList)2 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 RecognizerIntent (android.speech.RecognizerIntent)1 NavigationView (android.support.design.widget.NavigationView)1 DrawerLayout (android.support.v4.widget.DrawerLayout)1 ActionBarDrawerToggle (android.support.v7.app.ActionBarDrawerToggle)1 RecyclerView (android.support.v7.widget.RecyclerView)1 SubMenu (android.view.SubMenu)1 View (android.view.View)1 Call (okhttp3.Call)1 Headers (okhttp3.Headers)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 NodeList (org.w3c.dom.NodeList)1