Search in sources :

Example 1 with IconPopupMenu

use of net.osmand.plus.widgets.IconPopupMenu in project Osmand by osmandapp.

the class MapWidgetRegistry method addControls.

private void addControls(final MapActivity mapActivity, final ContextMenuAdapter contextMenuAdapter, Set<MapWidgetRegInfo> groupTitle, final ApplicationMode mode) {
    for (final MapWidgetRegInfo r : groupTitle) {
        if (!mode.isWidgetAvailable(r.key)) {
            continue;
        }
        final boolean selected = r.visibleCollapsed(mode) || r.visible(mode);
        final String desc = mapActivity.getString(R.string.shared_string_collapse);
        ContextMenuItem.ItemBuilder itemBuilder = new ContextMenuItem.ItemBuilder().setIcon(r.getDrawableMenu()).setSelected(selected).setColor(selected ? R.color.osmand_orange : ContextMenuItem.INVALID_ID).setSecondaryIcon(r.widget != null ? R.drawable.ic_action_additional_option : ContextMenuItem.INVALID_ID).setDescription(r.visibleCollapsed(mode) ? desc : null).setListener(new ContextMenuAdapter.OnRowItemClick() {

            @Override
            public boolean onRowItemClick(final ArrayAdapter<ContextMenuItem> adapter, final View view, final int itemId, final int pos) {
                if (r.widget == null) {
                    setVisibility(adapter, pos, !r.visible(mode), false);
                    return false;
                }
                View textWrapper = view.findViewById(R.id.text_wrapper);
                IconPopupMenu popup = new IconPopupMenu(view.getContext(), textWrapper);
                MenuInflater inflater = popup.getMenuInflater();
                final Menu menu = popup.getMenu();
                inflater.inflate(R.menu.widget_visibility_menu, menu);
                IconsCache ic = mapActivity.getMyApplication().getIconsCache();
                menu.findItem(R.id.action_show).setIcon(ic.getThemedIcon(R.drawable.ic_action_view));
                menu.findItem(R.id.action_hide).setIcon(ic.getThemedIcon(R.drawable.ic_action_hide));
                menu.findItem(R.id.action_collapse).setIcon(ic.getThemedIcon(R.drawable.ic_action_widget_collapse));
                final int[] menuIconIds = r.getDrawableMenuIds();
                final int[] menuTitleIds = r.getMessageIds();
                final int[] menuItemIds = r.getItemIds();
                int checkedId = r.getItemId();
                boolean selected = r.visibleCollapsed(mode) || r.visible(mode);
                if (menuIconIds != null && menuTitleIds != null && menuItemIds != null && menuIconIds.length == menuTitleIds.length && menuIconIds.length == menuItemIds.length) {
                    for (int i = 0; i < menuIconIds.length; i++) {
                        int iconId = menuIconIds[i];
                        int titleId = menuTitleIds[i];
                        int id = menuItemIds[i];
                        MenuItem menuItem = menu.add(R.id.single_selection_group, id, i, titleId).setChecked(id == checkedId);
                        menuItem.setIcon(menuItem.isChecked() && selected ? ic.getIcon(iconId, R.color.osmand_orange) : ic.getThemedIcon(iconId));
                    }
                    menu.setGroupCheckable(R.id.single_selection_group, true, true);
                    menu.setGroupVisible(R.id.single_selection_group, true);
                }
                popup.setOnMenuItemClickListener(new IconPopupMenu.OnMenuItemClickListener() {

                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) {
                        switch(menuItem.getItemId()) {
                            case R.id.action_show:
                                setVisibility(adapter, pos, true, false);
                                return true;
                            case R.id.action_hide:
                                setVisibility(adapter, pos, false, false);
                                return true;
                            case R.id.action_collapse:
                                setVisibility(adapter, pos, true, true);
                                return true;
                            default:
                                if (menuItemIds != null) {
                                    for (int menuItemId : menuItemIds) {
                                        if (menuItem.getItemId() == menuItemId) {
                                            r.changeState(menuItemId);
                                            MapInfoLayer mil = mapActivity.getMapLayers().getMapInfoLayer();
                                            if (mil != null) {
                                                mil.recreateControls();
                                            }
                                            ContextMenuItem item = adapter.getItem(pos);
                                            item.setIcon(r.getDrawableMenu());
                                            if (r.getMessage() != null) {
                                                item.setTitle(r.getMessage());
                                            } else {
                                                item.setTitle(mapActivity.getResources().getString(r.getMessageId()));
                                            }
                                            adapter.notifyDataSetChanged();
                                            return true;
                                        }
                                    }
                                }
                        }
                        return false;
                    }
                });
                popup.show();
                return false;
            }

            @Override
            public boolean onContextMenuClick(ArrayAdapter<ContextMenuItem> a, int itemId, int pos, boolean isChecked, int[] viewCoordinates) {
                setVisibility(a, pos, isChecked, false);
                return false;
            }

            private void setVisibility(ArrayAdapter<ContextMenuItem> adapter, int position, boolean visible, boolean collapsed) {
                MapWidgetRegistry.this.setVisibility(r, visible, collapsed);
                MapInfoLayer mil = mapActivity.getMapLayers().getMapInfoLayer();
                if (mil != null) {
                    mil.recreateControls();
                }
                ContextMenuItem item = adapter.getItem(position);
                item.setSelected(visible);
                item.setColorRes(visible ? R.color.osmand_orange : ContextMenuItem.INVALID_ID);
                item.setDescription(visible && collapsed ? desc : null);
                adapter.notifyDataSetChanged();
            }
        });
        if (r.getMessage() != null) {
            itemBuilder.setTitle(r.getMessage());
        } else {
            itemBuilder.setTitleId(r.getMessageId(), mapActivity);
        }
        contextMenuAdapter.addItem(itemBuilder.createItem());
    }
}
Also used : ContextMenuItem(net.osmand.plus.ContextMenuItem) MenuInflater(android.view.MenuInflater) MenuItem(android.view.MenuItem) ContextMenuItem(net.osmand.plus.ContextMenuItem) View(android.view.View) IconPopupMenu(net.osmand.plus.widgets.IconPopupMenu) ContextMenuAdapter(net.osmand.plus.ContextMenuAdapter) MapInfoLayer(net.osmand.plus.views.MapInfoLayer) Menu(android.view.Menu) IconPopupMenu(net.osmand.plus.widgets.IconPopupMenu) ConfigureMapMenu(net.osmand.plus.dialogs.ConfigureMapMenu) IconsCache(net.osmand.plus.IconsCache)

Aggregations

Menu (android.view.Menu)1 MenuInflater (android.view.MenuInflater)1 MenuItem (android.view.MenuItem)1 View (android.view.View)1 ContextMenuAdapter (net.osmand.plus.ContextMenuAdapter)1 ContextMenuItem (net.osmand.plus.ContextMenuItem)1 IconsCache (net.osmand.plus.IconsCache)1 ConfigureMapMenu (net.osmand.plus.dialogs.ConfigureMapMenu)1 MapInfoLayer (net.osmand.plus.views.MapInfoLayer)1 IconPopupMenu (net.osmand.plus.widgets.IconPopupMenu)1