Search in sources :

Example 41 with ContextMenuAdapter

use of net.osmand.plus.ContextMenuAdapter in project Osmand by osmandapp.

the class SRTMPlugin method registerLayerContextMenuActions.

@Override
public void registerLayerContextMenuActions(final OsmandMapTileView mapView, ContextMenuAdapter adapter, final MapActivity mapActivity) {
    ContextMenuAdapter.ItemClickListener listener = new ContextMenuAdapter.OnRowItemClick() {

        @Override
        public boolean onRowItemClick(ArrayAdapter<ContextMenuItem> adapter, View view, int itemId, int position) {
            int[] viewCoordinates = AndroidUtils.getCenterViewCoordinates(view);
            if (itemId == R.string.srtm_plugin_name) {
                mapActivity.getDashboard().setDashboardVisibility(true, DashboardOnMap.DashboardType.CONTOUR_LINES, viewCoordinates);
                return false;
            } else if (itemId == R.string.layer_hillshade) {
                mapActivity.getDashboard().setDashboardVisibility(true, DashboardOnMap.DashboardType.HILLSHADE, viewCoordinates);
                return false;
            }
            return true;
        }

        @Override
        public boolean onContextMenuClick(final ArrayAdapter<ContextMenuItem> adapter, final int itemId, final int position, final boolean isChecked, final int[] viewCoordinates) {
            if (itemId == R.string.srtm_plugin_name) {
                toggleContourLines(mapActivity, isChecked, new Runnable() {

                    @Override
                    public void run() {
                        RenderingRuleProperty contourLinesProp = app.getRendererRegistry().getCustomRenderingRuleProperty(CONTOUR_LINES_ATTR);
                        if (contourLinesProp != null) {
                            OsmandSettings settings = app.getSettings();
                            final OsmandSettings.CommonPreference<String> pref = settings.getCustomRenderProperty(contourLinesProp.getAttrName());
                            boolean selected = !pref.get().equals(CONTOUR_LINES_DISABLED_VALUE);
                            SRTMPlugin plugin = OsmandPlugin.getPlugin(SRTMPlugin.class);
                            if (selected && plugin != null && !plugin.isActive() && !plugin.needsInstallation()) {
                                OsmandPlugin.enablePlugin(mapActivity, mapActivity.getMyApplication(), plugin, true);
                            }
                            ContextMenuItem item = adapter.getItem(position);
                            if (item != null) {
                                item.setDescription(app.getString(R.string.display_zoom_level, getPrefDescription(app, contourLinesProp, pref)));
                                item.setColorRes(selected ? R.color.osmand_orange : ContextMenuItem.INVALID_ID);
                                item.setSelected(selected);
                                adapter.notifyDataSetChanged();
                            }
                            refreshMapComplete(mapActivity);
                        }
                    }
                });
            } else if (itemId == R.string.layer_hillshade) {
                toggleHillshade(mapActivity, isChecked, new Runnable() {

                    @Override
                    public void run() {
                        boolean selected = HILLSHADE.get();
                        SRTMPlugin plugin = OsmandPlugin.getPlugin(SRTMPlugin.class);
                        if (selected && plugin != null && !plugin.isActive() && !plugin.needsInstallation()) {
                            OsmandPlugin.enablePlugin(mapActivity, mapActivity.getMyApplication(), plugin, true);
                        }
                        ContextMenuItem item = adapter.getItem(position);
                        if (item != null) {
                            item.setColorRes(selected ? R.color.osmand_orange : ContextMenuItem.INVALID_ID);
                            item.setSelected(selected);
                            adapter.notifyDataSetChanged();
                        }
                        updateLayers(mapView, mapActivity);
                        refreshMapComplete(mapActivity);
                    }
                });
            }
            return true;
        }
    };
    RenderingRuleProperty contourLinesProp = app.getRendererRegistry().getCustomRenderingRuleProperty(CONTOUR_LINES_ATTR);
    if (contourLinesProp != null) {
        final OsmandSettings.CommonPreference<String> pref = app.getSettings().getCustomRenderProperty(contourLinesProp.getAttrName());
        boolean contourLinesSelected;
        if (!Algorithms.isEmpty(pref.get())) {
            contourLinesSelected = !pref.get().equals(CONTOUR_LINES_DISABLED_VALUE);
        } else {
            contourLinesSelected = !contourLinesProp.getDefaultValueDescription().equals(CONTOUR_LINES_DISABLED_VALUE);
        }
        String descr = getPrefDescription(app, contourLinesProp, pref);
        adapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.srtm_plugin_name, mapActivity).setSelected(contourLinesSelected).setIcon(R.drawable.ic_plugin_srtm).setDescription(app.getString(R.string.display_zoom_level, descr)).setColor(contourLinesSelected ? R.color.osmand_orange : ContextMenuItem.INVALID_ID).setSecondaryIcon(R.drawable.ic_action_additional_option).setPosition(12).setListener(listener).createItem());
    }
    adapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.layer_hillshade, mapActivity).setSelected(HILLSHADE.get()).setColor(HILLSHADE.get() ? R.color.osmand_orange : ContextMenuItem.INVALID_ID).setIcon(R.drawable.ic_action_hillshade_dark).setSecondaryIcon(R.drawable.ic_action_additional_option).setListener(listener).setPosition(13).createItem());
}
Also used : CommonPreference(net.osmand.plus.OsmandSettings.CommonPreference) ContextMenuItem(net.osmand.plus.ContextMenuItem) RenderingRuleProperty(net.osmand.render.RenderingRuleProperty) OsmandMapTileView(net.osmand.plus.views.OsmandMapTileView) View(android.view.View) OsmandSettings(net.osmand.plus.OsmandSettings) ContextMenuAdapter(net.osmand.plus.ContextMenuAdapter) ArrayAdapter(android.widget.ArrayAdapter)

Aggregations

ContextMenuAdapter (net.osmand.plus.ContextMenuAdapter)41 ContextMenuItem (net.osmand.plus.ContextMenuItem)28 ArrayAdapter (android.widget.ArrayAdapter)17 View (android.view.View)15 DialogInterface (android.content.DialogInterface)13 OsmandApplication (net.osmand.plus.OsmandApplication)12 AlertDialog (android.support.v7.app.AlertDialog)11 ItemClickListener (net.osmand.plus.ContextMenuAdapter.ItemClickListener)10 OsmandMapTileView (net.osmand.plus.views.OsmandMapTileView)10 OsmandSettings (net.osmand.plus.OsmandSettings)9 ArrayList (java.util.ArrayList)8 AdapterView (android.widget.AdapterView)7 TextView (android.widget.TextView)7 ListView (android.widget.ListView)6 ImageView (android.widget.ImageView)5 Intent (android.content.Intent)4 Drawable (android.graphics.drawable.Drawable)4 ViewGroup (android.view.ViewGroup)4 CompoundButton (android.widget.CompoundButton)4 SelectedGpxFile (net.osmand.plus.GpxSelectionHelper.SelectedGpxFile)4