Search in sources :

Example 1 with OnRowItemClick

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

the class ConfigureMapMenu method createProperties.

private ContextMenuItem createProperties(List<RenderingRuleProperty> customRules, final List<RenderingRuleProperty> customRulesIncluded, @StringRes final int strId, @DrawableRes final int icon, String category, final ListStringPreference defaultSettings, final ContextMenuAdapter adapter, final MapActivity activity, final boolean useDescription) {
    final List<RenderingRuleProperty> ps = new ArrayList<>();
    final List<OsmandSettings.CommonPreference<Boolean>> prefs = new ArrayList<>();
    Iterator<RenderingRuleProperty> it = customRules.iterator();
    while (it.hasNext()) {
        RenderingRuleProperty p = it.next();
        if (category.equals(p.getCategory()) && p.isBoolean()) {
            ps.add(p);
            final OsmandSettings.CommonPreference<Boolean> pref = activity.getMyApplication().getSettings().getCustomRenderBooleanProperty(p.getAttrName());
            prefs.add(pref);
            it.remove();
        }
    }
    if (prefs.size() > 0) {
        final List<OsmandSettings.CommonPreference<String>> includedPrefs = new ArrayList<>();
        if (customRulesIncluded != null) {
            for (RenderingRuleProperty p : customRulesIncluded) {
                if (!p.isBoolean()) {
                    final OsmandSettings.CommonPreference<String> pref = activity.getMyApplication().getSettings().getCustomRenderProperty(p.getAttrName());
                    includedPrefs.add(pref);
                }
            }
        }
        final ItemClickListener clickListener = new ContextMenuAdapter.ItemClickListener() {

            @Override
            public boolean onContextMenuClick(ArrayAdapter<ContextMenuItem> a, int itemId, int pos, boolean isChecked, int[] viewCoordinates) {
                if (!isChecked && !useDescription) {
                    if (defaultSettings != null) {
                        defaultSettings.set("");
                        for (int i = 0; i < prefs.size(); i++) {
                            if (prefs.get(i).get()) {
                                defaultSettings.addValue(prefs.get(i).getId());
                            }
                        }
                    }
                    for (int i = 0; i < prefs.size(); i++) {
                        prefs.get(i).set(false);
                    }
                    adapter.getItem(pos).setColorRes(ContextMenuItem.INVALID_ID);
                    a.notifyDataSetInvalidated();
                    refreshMapComplete(activity);
                    activity.getMapLayers().updateLayers(activity.getMapView());
                } else {
                    showPreferencesDialog(adapter, a, pos, activity, activity.getString(strId), ps, prefs, useDescription, defaultSettings, true, customRulesIncluded);
                }
                return false;
            }
        };
        ContextMenuItem.ItemBuilder builder = new ContextMenuItem.ItemBuilder().setTitleId(strId, activity).setIcon(icon).setListener(clickListener);
        boolean selected = false;
        for (OsmandSettings.CommonPreference<Boolean> p : prefs) {
            if (p.get()) {
                selected = true;
                break;
            }
        }
        if (!selected && includedPrefs.size() > 0) {
            for (OsmandSettings.CommonPreference<String> p : includedPrefs) {
                if (!Algorithms.isEmpty(p.get())) {
                    selected = true;
                    break;
                }
            }
        }
        builder.setColor(selected ? R.color.osmand_orange : ContextMenuItem.INVALID_ID);
        if (useDescription) {
            final String descr = getDescription(prefs, includedPrefs);
            builder.setDescription(descr);
            builder.setLayout(R.layout.list_item_single_line_descrition_narrow);
        } else {
            builder.setListener(new OnRowItemClick() {

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

                @Override
                public boolean onRowItemClick(ArrayAdapter<ContextMenuItem> a, View view, int itemId, int pos) {
                    showPreferencesDialog(adapter, a, pos, activity, activity.getString(strId), ps, prefs, useDescription, defaultSettings, false, customRulesIncluded);
                    return false;
                }
            });
            builder.setSecondaryIcon(R.drawable.ic_action_additional_option);
            builder.setSelected(selected);
        }
        return builder.createItem();
    // createCustomRenderingProperties(adapter, activity, ps);
    }
    return null;
}
Also used : ItemClickListener(net.osmand.plus.ContextMenuAdapter.ItemClickListener) CommonPreference(net.osmand.plus.OsmandSettings.CommonPreference) ContextMenuItem(net.osmand.plus.ContextMenuItem) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) RenderingRuleProperty(net.osmand.render.RenderingRuleProperty) OnRowItemClick(net.osmand.plus.ContextMenuAdapter.OnRowItemClick) AppCompatCheckedTextView(android.support.v7.widget.AppCompatCheckedTextView) ImageView(android.widget.ImageView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) OsmandMapTileView(net.osmand.plus.views.OsmandMapTileView) OsmandSettings(net.osmand.plus.OsmandSettings) ArrayAdapter(android.widget.ArrayAdapter)

Example 2 with OnRowItemClick

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

the class DashboardOnMap method getOptionsMenuOnClickListener.

private OnItemClickListener getOptionsMenuOnClickListener(final ContextMenuAdapter cm, final ArrayAdapter<ContextMenuItem> listAdapter) {
    return new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int which, long id) {
            ContextMenuItem item = cm.getItem(which);
            ContextMenuAdapter.ItemClickListener click = item.getItemClickListener();
            if (click instanceof OnRowItemClick) {
                boolean cl = ((OnRowItemClick) click).onRowItemClick(listAdapter, view, item.getTitleId(), which);
                if (cl) {
                    hideDashboard();
                }
            } else if (click != null) {
                CompoundButton btn = (CompoundButton) view.findViewById(R.id.toggle_item);
                if (btn != null && btn.getVisibility() == View.VISIBLE) {
                    btn.setChecked(!btn.isChecked());
                } else {
                    if (click.onContextMenuClick(listAdapter, item.getTitleId(), which, false, null)) {
                        hideDashboard();
                    }
                }
            } else {
                if (!item.isCategory()) {
                    hideDashboard();
                }
            }
        }
    };
}
Also used : ContextMenuAdapter(net.osmand.plus.ContextMenuAdapter) OnItemClickListener(android.widget.AdapterView.OnItemClickListener) ContextMenuItem(net.osmand.plus.ContextMenuItem) AdapterView(android.widget.AdapterView) OnRowItemClick(net.osmand.plus.ContextMenuAdapter.OnRowItemClick) ImageView(android.widget.ImageView) DynamicListView(net.osmand.plus.views.controls.DynamicListView) OsmandMapTileView(net.osmand.plus.views.OsmandMapTileView) ScrollView(android.widget.ScrollView) ObservableScrollView(com.github.ksoichiro.android.observablescrollview.ObservableScrollView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) AbsListView(android.widget.AbsListView) CompoundButton(android.widget.CompoundButton)

Example 3 with OnRowItemClick

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

the class ConfigureMapMenu method createLayersItems.

private void createLayersItems(List<RenderingRuleProperty> customRules, ContextMenuAdapter adapter, final MapActivity activity) {
    final OsmandApplication app = activity.getMyApplication();
    final OsmandSettings settings = app.getSettings();
    LayerMenuListener l = new LayerMenuListener(activity, adapter);
    adapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.shared_string_show, activity).setCategory(true).setLayout(R.layout.list_group_title_with_switch).createItem());
    // String appMode = " [" + settings.getApplicationMode().toHumanString(view.getApplication()) +"] ";
    boolean selected = settings.SHOW_FAVORITES.get();
    adapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.shared_string_favorites, activity).setSelected(settings.SHOW_FAVORITES.get()).setColor(selected ? R.color.osmand_orange : ContextMenuItem.INVALID_ID).setIcon(R.drawable.ic_action_fav_dark).setListener(l).createItem());
    selected = app.getPoiFilters().isShowingAnyPoi();
    adapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.layer_poi, activity).setSelected(selected).setDescription(app.getPoiFilters().getSelectedPoiFiltersName()).setColor(selected ? R.color.osmand_orange : ContextMenuItem.INVALID_ID).setIcon(R.drawable.ic_action_info_dark).setSecondaryIcon(R.drawable.ic_action_additional_option).setListener(l).createItem());
    selected = settings.SHOW_POI_LABEL.get();
    adapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.layer_amenity_label, activity).setSelected(settings.SHOW_POI_LABEL.get()).setColor(selected ? R.color.osmand_orange : ContextMenuItem.INVALID_ID).setIcon(R.drawable.ic_action_text_dark).setListener(l).createItem());
    /*
		ContextMenuItem item = createProperties(customRules, null, R.string.rendering_category_transport, R.drawable.ic_action_bus_dark,
				"transport", settings.TRANSPORT_DEFAULT_SETTINGS, adapter, activity, false);
		if (item != null) {
			adapter.addItem(item);
		}
		*/
    final List<RenderingRuleProperty> transportRules = new ArrayList<>();
    final List<OsmandSettings.CommonPreference<Boolean>> transportPrefs = new ArrayList<>();
    Iterator<RenderingRuleProperty> it = customRules.iterator();
    while (it.hasNext()) {
        RenderingRuleProperty p = it.next();
        if ("transport".equals(p.getCategory()) && p.isBoolean()) {
            transportRules.add(p);
            final OsmandSettings.CommonPreference<Boolean> pref = activity.getMyApplication().getSettings().getCustomRenderBooleanProperty(p.getAttrName());
            transportPrefs.add(pref);
            it.remove();
        }
    }
    selected = false;
    for (OsmandSettings.CommonPreference<Boolean> p : transportPrefs) {
        if (p.get()) {
            selected = true;
            break;
        }
    }
    final boolean transportSelected = selected;
    adapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.rendering_category_transport, activity).setIcon(R.drawable.ic_action_bus_dark).setSecondaryIcon(R.drawable.ic_action_additional_option).setSelected(transportSelected).setColor(transportSelected ? R.color.osmand_orange : ContextMenuItem.INVALID_ID).setListener(new ContextMenuAdapter.OnRowItemClick() {

        ArrayAdapter<CharSequence> adapter;

        boolean transportSelectedInner = transportSelected;

        @Override
        public boolean onRowItemClick(ArrayAdapter<ContextMenuItem> adapter, View view, int itemId, int position) {
            if (transportSelectedInner) {
                showTransportDialog(adapter, position);
                return false;
            } else {
                CompoundButton btn = (CompoundButton) view.findViewById(R.id.toggle_item);
                if (btn != null && btn.getVisibility() == View.VISIBLE) {
                    btn.setChecked(!btn.isChecked());
                    adapter.getItem(position).setColorRes(btn.isChecked() ? R.color.osmand_orange : ContextMenuItem.INVALID_ID);
                    adapter.notifyDataSetChanged();
                    return false;
                } else {
                    return onContextMenuClick(adapter, itemId, position, false, null);
                }
            }
        }

        @Override
        public boolean onContextMenuClick(final ArrayAdapter<ContextMenuItem> ad, int itemId, final int pos, boolean isChecked, int[] viewCoordinates) {
            if (transportSelectedInner) {
                for (int i = 0; i < transportPrefs.size(); i++) {
                    transportPrefs.get(i).set(false);
                }
                transportSelectedInner = false;
                ad.getItem(pos).setColorRes(ContextMenuItem.INVALID_ID);
                refreshMapComplete(activity);
                activity.getMapLayers().updateLayers(activity.getMapView());
            } else {
                ad.getItem(pos).setColorRes(R.color.osmand_orange);
                showTransportDialog(ad, pos);
            }
            ad.notifyDataSetChanged();
            return false;
        }

        private void showTransportDialog(final ArrayAdapter<ContextMenuItem> ad, final int pos) {
            final AlertDialog.Builder b = new AlertDialog.Builder(activity);
            b.setTitle(activity.getString(R.string.rendering_category_transport));
            final int[] iconIds = new int[transportPrefs.size()];
            final boolean[] checkedItems = new boolean[transportPrefs.size()];
            for (int i = 0; i < transportPrefs.size(); i++) {
                checkedItems[i] = transportPrefs.get(i).get();
            }
            final String[] vals = new String[transportRules.size()];
            for (int i = 0; i < transportRules.size(); i++) {
                RenderingRuleProperty p = transportRules.get(i);
                String propertyName = SettingsActivity.getStringPropertyName(activity, p.getAttrName(), p.getName());
                vals[i] = propertyName;
                if ("transportStops".equals(p.getAttrName())) {
                    iconIds[i] = R.drawable.ic_action_transport_stop;
                } else if ("publicTransportMode".equals(p.getAttrName())) {
                    iconIds[i] = R.drawable.ic_action_bus_dark;
                } else if ("tramTrainRoutes".equals(p.getAttrName())) {
                    iconIds[i] = R.drawable.ic_action_transport_tram;
                } else if ("subwayMode".equals(p.getAttrName())) {
                    iconIds[i] = R.drawable.ic_action_transport_subway;
                } else {
                    iconIds[i] = R.drawable.ic_action_bus_dark;
                }
            }
            adapter = new ArrayAdapter<CharSequence>(activity, R.layout.popup_list_item_icon24_and_menu, R.id.title, vals) {

                @NonNull
                @Override
                public View getView(final int position, View convertView, ViewGroup parent) {
                    View v = super.getView(position, convertView, parent);
                    final ImageView icon = (ImageView) v.findViewById(R.id.icon);
                    if (checkedItems[position]) {
                        icon.setImageDrawable(app.getIconsCache().getIcon(iconIds[position], R.color.osmand_orange));
                    } else {
                        icon.setImageDrawable(app.getIconsCache().getThemedIcon(iconIds[position]));
                    }
                    v.findViewById(R.id.divider).setVisibility(View.GONE);
                    v.findViewById(R.id.description).setVisibility(View.GONE);
                    v.findViewById(R.id.secondary_icon).setVisibility(View.GONE);
                    final SwitchCompat check = (SwitchCompat) v.findViewById(R.id.toggle_item);
                    check.setOnCheckedChangeListener(null);
                    check.setChecked(checkedItems[position]);
                    check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            checkedItems[position] = isChecked;
                            if (checkedItems[position]) {
                                icon.setImageDrawable(app.getIconsCache().getIcon(iconIds[position], R.color.osmand_orange));
                            } else {
                                icon.setImageDrawable(app.getIconsCache().getThemedIcon(iconIds[position]));
                            }
                        }
                    });
                    return v;
                }
            };
            final ListView listView = new ListView(activity);
            listView.setDivider(null);
            listView.setClickable(true);
            listView.setAdapter(adapter);
            listView.setOnItemClickListener(new ListView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    checkedItems[position] = !checkedItems[position];
                    adapter.notifyDataSetChanged();
                }
            });
            b.setView(listView);
            b.setOnDismissListener(new DialogInterface.OnDismissListener() {

                @Override
                public void onDismiss(DialogInterface dialog) {
                    ContextMenuItem item = ad.getItem(pos);
                    if (item != null) {
                        item.setSelected(transportSelectedInner);
                        item.setColorRes(transportSelectedInner ? R.color.osmand_orange : ContextMenuItem.INVALID_ID);
                        ad.notifyDataSetChanged();
                    }
                }
            });
            b.setNegativeButton(R.string.shared_string_cancel, null);
            b.setPositiveButton(R.string.shared_string_apply, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    transportSelectedInner = false;
                    for (int i = 0; i < transportPrefs.size(); i++) {
                        transportPrefs.get(i).set(checkedItems[i]);
                        if (!transportSelectedInner && checkedItems[i]) {
                            transportSelectedInner = true;
                        }
                    }
                    refreshMapComplete(activity);
                    activity.getMapLayers().updateLayers(activity.getMapView());
                }
            });
            b.show();
        }
    }).createItem());
    selected = app.getSelectedGpxHelper().isShowingAnyGpxFiles();
    adapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.layer_gpx_layer, activity).setSelected(app.getSelectedGpxHelper().isShowingAnyGpxFiles()).setDescription(app.getSelectedGpxHelper().getGpxDescription()).setColor(selected ? R.color.osmand_orange : ContextMenuItem.INVALID_ID).setIcon(R.drawable.ic_action_polygom_dark).setSecondaryIcon(R.drawable.ic_action_additional_option).setListener(l).createItem());
    adapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.layer_map, activity).setIcon(R.drawable.ic_world_globe_dark).setDescription(settings.MAP_ONLINE_DATA.get() ? settings.MAP_TILE_SOURCES.get() : null).setListener(l).createItem());
    OsmandPlugin.registerLayerContextMenu(activity.getMapView(), adapter, activity);
    app.getAppCustomization().prepareLayerContextMenu(activity, adapter);
    boolean srtmDisabled = OsmandPlugin.getEnabledPlugin(SRTMPlugin.class) == null;
    if (srtmDisabled) {
        SRTMPlugin srtmPlugin = OsmandPlugin.getPlugin(SRTMPlugin.class);
        if (srtmPlugin != null) {
            srtmPlugin.registerLayerContextMenuActions(activity.getMapView(), adapter, activity);
        }
    }
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) OsmandApplication(net.osmand.plus.OsmandApplication) DialogInterface(android.content.DialogInterface) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) ListView(android.widget.ListView) ImageView(android.widget.ImageView) SRTMPlugin(net.osmand.plus.srtmplugin.SRTMPlugin) OnCheckedChangeListener(android.widget.CompoundButton.OnCheckedChangeListener) ContextMenuItem(net.osmand.plus.ContextMenuItem) CommonPreference(net.osmand.plus.OsmandSettings.CommonPreference) ViewGroup(android.view.ViewGroup) RenderingRuleProperty(net.osmand.render.RenderingRuleProperty) OnRowItemClick(net.osmand.plus.ContextMenuAdapter.OnRowItemClick) AppCompatCheckedTextView(android.support.v7.widget.AppCompatCheckedTextView) ImageView(android.widget.ImageView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) OsmandMapTileView(net.osmand.plus.views.OsmandMapTileView) OsmandSettings(net.osmand.plus.OsmandSettings) ContextMenuAdapter(net.osmand.plus.ContextMenuAdapter) AdapterView(android.widget.AdapterView) CompoundButton(android.widget.CompoundButton) ArrayAdapter(android.widget.ArrayAdapter) SwitchCompat(android.support.v7.widget.SwitchCompat)

Aggregations

View (android.view.View)3 AdapterView (android.widget.AdapterView)3 ImageView (android.widget.ImageView)3 ListView (android.widget.ListView)3 TextView (android.widget.TextView)3 OnRowItemClick (net.osmand.plus.ContextMenuAdapter.OnRowItemClick)3 ContextMenuItem (net.osmand.plus.ContextMenuItem)3 OsmandMapTileView (net.osmand.plus.views.OsmandMapTileView)3 AppCompatCheckedTextView (android.support.v7.widget.AppCompatCheckedTextView)2 ArrayAdapter (android.widget.ArrayAdapter)2 CompoundButton (android.widget.CompoundButton)2 TIntArrayList (gnu.trove.list.array.TIntArrayList)2 ArrayList (java.util.ArrayList)2 ContextMenuAdapter (net.osmand.plus.ContextMenuAdapter)2 OsmandSettings (net.osmand.plus.OsmandSettings)2 CommonPreference (net.osmand.plus.OsmandSettings.CommonPreference)2 RenderingRuleProperty (net.osmand.render.RenderingRuleProperty)2 DialogInterface (android.content.DialogInterface)1 AlertDialog (android.support.v7.app.AlertDialog)1 SwitchCompat (android.support.v7.widget.SwitchCompat)1