Search in sources :

Example 26 with ContextMenuAdapter

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

the class RoutePreferencesMenu method getItemClickListener.

public OnItemClickListener getItemClickListener(final ArrayAdapter<?> listAdapter) {
    return new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int item, long l) {
            Object obj = listAdapter.getItem(item);
            if (obj instanceof LocalRoutingParameterGroup) {
                final LocalRoutingParameterGroup group = (LocalRoutingParameterGroup) obj;
                final ContextMenuAdapter adapter = new ContextMenuAdapter();
                int i = 0;
                int selectedIndex = -1;
                for (LocalRoutingParameter p : group.getRoutingParameters()) {
                    adapter.addItem(ContextMenuItem.createBuilder(p.getText(mapActivity)).setSelected(false).createItem());
                    if (p.isSelected(settings)) {
                        selectedIndex = i;
                    }
                    i++;
                }
                if (selectedIndex == -1) {
                    selectedIndex = 0;
                }
                AlertDialog.Builder builder = new AlertDialog.Builder(mapActivity);
                final int layout = R.layout.list_menu_item_native_singlechoice;
                final ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(mapActivity, layout, R.id.text1, adapter.getItemNames()) {

                    @NonNull
                    @Override
                    public View getView(final int position, View convertView, ViewGroup parent) {
                        // User super class to create the View
                        View v = convertView;
                        if (v == null) {
                            v = mapActivity.getLayoutInflater().inflate(layout, null);
                        }
                        final ContextMenuItem item = adapter.getItem(position);
                        TextView tv = (TextView) v.findViewById(R.id.text1);
                        tv.setText(item.getTitle());
                        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f);
                        return v;
                    }
                };
                final int[] selectedPosition = { selectedIndex };
                builder.setSingleChoiceItems(listAdapter, selectedIndex, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int position) {
                        selectedPosition[0] = position;
                    }
                });
                builder.setTitle(group.getText(mapActivity)).setPositiveButton(R.string.shared_string_ok, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        int position = selectedPosition[0];
                        if (position >= 0 && position < group.getRoutingParameters().size()) {
                            for (int i = 0; i < group.getRoutingParameters().size(); i++) {
                                LocalRoutingParameter rp = group.getRoutingParameters().get(i);
                                rp.setSelected(settings, i == position);
                            }
                            mapActivity.getRoutingHelper().recalculateRouteDueToSettingsChange();
                            updateParameters();
                        }
                    }
                }).setNegativeButton(R.string.shared_string_cancel, null);
                builder.create().show();
            } else if (obj instanceof OtherSettingsRoutingParameter) {
                final Intent settings = new Intent(mapActivity, SettingsNavigationActivity.class);
                settings.putExtra(SettingsNavigationActivity.INTENT_SKIP_DIALOG, true);
                settings.putExtra(SettingsBaseActivity.INTENT_APP_MODE, routingHelper.getAppMode().getStringKey());
                mapActivity.startActivity(settings);
            } else if (obj instanceof MuteSoundRoutingParameter) {
                final CompoundButton btn = (CompoundButton) view.findViewById(R.id.toggle_item);
                btn.performClick();
            } else if (obj instanceof VoiceGuidanceRoutingParameter) {
                doSelectVoiceGuidance();
            } else if (obj instanceof InterruptMusicRoutingParameter) {
                final CompoundButton btn = (CompoundButton) view.findViewById(R.id.toggle_item);
                btn.performClick();
            } else if (obj instanceof AvoidRoadsRoutingParameter) {
                selectRestrictedRoads();
            } else if (view.findViewById(R.id.GPXRouteSpinner) != null) {
                showOptionsMenu((TextView) view.findViewById(R.id.GPXRouteSpinner));
            } else {
                CheckBox ch = (CheckBox) view.findViewById(R.id.toggle_item);
                if (ch != null) {
                    ch.setChecked(!ch.isChecked());
                }
            }
        }
    };
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) DialogInterface(android.content.DialogInterface) TextView(android.widget.TextView) OnItemClickListener(android.widget.AdapterView.OnItemClickListener) ContextMenuItem(net.osmand.plus.ContextMenuItem) ViewGroup(android.view.ViewGroup) Intent(android.content.Intent) ImageView(android.widget.ImageView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) SettingsNavigationActivity(net.osmand.plus.activities.SettingsNavigationActivity) ContextMenuAdapter(net.osmand.plus.ContextMenuAdapter) CheckBox(android.widget.CheckBox) AdapterView(android.widget.AdapterView) CallbackWithObject(net.osmand.CallbackWithObject) ArrayAdapter(android.widget.ArrayAdapter) CompoundButton(android.widget.CompoundButton)

Example 27 with ContextMenuAdapter

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

the class LocalIndexesFragment method onCreateOptionsMenu.

@SuppressWarnings("deprecation")
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    if (!this.isAdded()) {
        return;
    }
    // Next line throws NPE in some circumstances when called from dashboard and listAdpater=null is not checked for. (Checking !this.isAdded above is not sufficient!)
    if (listAdapter != null && listAdapter.getGroupCount() == 0 && getDownloadActivity().getLocalIndexInfos().size() > 0) {
        for (LocalIndexInfo info : getDownloadActivity().getLocalIndexInfos()) {
            listAdapter.addLocalIndexInfo(info);
        }
        listAdapter.sortData();
        getExpandableListView().setAdapter(listAdapter);
        expandAllGroups();
    }
    ActionBar actionBar = getDownloadActivity().getSupportActionBar();
    // hide action bar from downloadindexfragment
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    optionsMenuAdapter = new ContextMenuAdapter();
    ItemClickListener listener = new ContextMenuAdapter.ItemClickListener() {

        @Override
        public boolean onContextMenuClick(ArrayAdapter<ContextMenuItem> adapter, int itemId, int pos, boolean isChecked, int[] viewCoordinates) {
            localOptionsMenu(itemId);
            return true;
        }
    };
    optionsMenuAdapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.local_index_mi_reload, getContext()).setIcon(R.drawable.ic_action_refresh_dark).setListener(listener).createItem());
    optionsMenuAdapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.shared_string_delete, getContext()).setIcon(R.drawable.ic_action_delete_dark).setListener(listener).createItem());
    optionsMenuAdapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.local_index_mi_backup, getContext()).setListener(listener).createItem());
    optionsMenuAdapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.local_index_mi_restore, getContext()).setListener(listener).createItem());
    // doesn't work correctly
    // int max =  getResources().getInteger(R.integer.abs__max_action_buttons);
    int max = 3;
    SubMenu split = null;
    for (int j = 0; j < optionsMenuAdapter.length(); j++) {
        MenuItem item;
        ContextMenuItem contextMenuItem = optionsMenuAdapter.getItem(j);
        if (j + 1 >= max && optionsMenuAdapter.length() > max) {
            if (split == null) {
                split = menu.addSubMenu(0, 1, j + 1, R.string.shared_string_more_actions);
                split.setIcon(R.drawable.ic_overflow_menu_white);
                split.getItem();
                MenuItemCompat.setShowAsAction(split.getItem(), MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
            }
            item = split.add(0, contextMenuItem.getTitleId(), j + 1, contextMenuItem.getTitle());
            MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
        } else {
            item = menu.add(0, contextMenuItem.getTitleId(), j + 1, contextMenuItem.getTitle());
            MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
        }
        if (contextMenuItem.getIcon() != -1) {
            item.setIcon(contextMenuItem.getIcon());
        }
    }
    if (operationTask == null || operationTask.getStatus() == AsyncTask.Status.FINISHED) {
        menu.setGroupVisible(0, true);
    } else {
        menu.setGroupVisible(0, false);
    }
}
Also used : ContextMenuAdapter(net.osmand.plus.ContextMenuAdapter) ItemClickListener(net.osmand.plus.ContextMenuAdapter.ItemClickListener) ContextMenuItem(net.osmand.plus.ContextMenuItem) SubMenu(android.view.SubMenu) MenuItem(android.view.MenuItem) ContextMenuItem(net.osmand.plus.ContextMenuItem) LocalIndexInfo(net.osmand.plus.activities.LocalIndexInfo) ActionBar(android.support.v7.app.ActionBar) ArrayAdapter(android.widget.ArrayAdapter)

Example 28 with ContextMenuAdapter

use of net.osmand.plus.ContextMenuAdapter 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)

Example 29 with ContextMenuAdapter

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

the class ConfigureMapMenu method createRenderingAttributeItems.

private void createRenderingAttributeItems(List<RenderingRuleProperty> customRules, final ContextMenuAdapter adapter, final MapActivity activity) {
    adapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.map_widget_map_rendering, activity).setCategory(true).setLayout(R.layout.list_group_title_with_switch).createItem());
    adapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.map_widget_renderer, activity).setDescription(getRenderDescr(activity)).setLayout(R.layout.list_item_single_line_descrition_narrow).setIcon(R.drawable.ic_map).setListener(new ContextMenuAdapter.ItemClickListener() {

        @Override
        public boolean onContextMenuClick(final ArrayAdapter<ContextMenuItem> ad, int itemId, final int pos, boolean isChecked, int[] viewCoordinates) {
            AlertDialog.Builder bld = new AlertDialog.Builder(activity);
            bld.setTitle(R.string.renderers);
            final OsmandApplication app = activity.getMyApplication();
            final ArrayList<String> items = new ArrayList<>(app.getRendererRegistry().getRendererNames());
            boolean nauticalPluginDisabled = OsmandPlugin.getEnabledPlugin(NauticalMapsPlugin.class) == null;
            final List<String> visibleNamesList = new ArrayList<>();
            int selected = -1;
            final String selectedName = app.getRendererRegistry().getCurrentSelectedRenderer().getName();
            int i = 0;
            Iterator<String> iterator = items.iterator();
            while (iterator.hasNext()) {
                String item = iterator.next();
                if (nauticalPluginDisabled && item.equals(RendererRegistry.NAUTICAL_RENDER)) {
                    iterator.remove();
                } else {
                    if (item.equals(selectedName)) {
                        selected = i;
                    }
                    String translation = RendererRegistry.getTranslatedRendererName(activity, item);
                    visibleNamesList.add(translation != null ? translation : item.replace('_', ' ').replace('-', ' '));
                    i++;
                }
            }
            bld.setSingleChoiceItems(visibleNamesList.toArray(new String[visibleNamesList.size()]), selected, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String renderer = items.get(which);
                    RenderingRulesStorage loaded = app.getRendererRegistry().getRenderer(renderer);
                    if (loaded != null) {
                        OsmandMapTileView view = activity.getMapView();
                        view.getSettings().RENDERER.set(renderer);
                        app.getRendererRegistry().setCurrentSelectedRender(loaded);
                        refreshMapComplete(activity);
                    } else {
                        Toast.makeText(app, R.string.renderer_load_exception, Toast.LENGTH_SHORT).show();
                    }
                    adapter.getItem(pos).setDescription(getRenderDescr(activity));
                    activity.getDashboard().refreshContent(true);
                    dialog.dismiss();
                }
            });
            bld.setNegativeButton(R.string.shared_string_dismiss, null);
            bld.show();
            return false;
        }
    }).createItem());
    adapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.map_mode, activity).setDescription(getDayNightDescr(activity)).setLayout(R.layout.list_item_single_line_descrition_narrow).setIcon(getDayNightIcon(activity)).setListener(new ItemClickListener() {

        @Override
        public boolean onContextMenuClick(final ArrayAdapter<ContextMenuItem> ad, int itemId, final int pos, boolean isChecked, int[] viewCoordinates) {
            final OsmandMapTileView view = activity.getMapView();
            AlertDialog.Builder bld = new AlertDialog.Builder(view.getContext());
            bld.setTitle(R.string.daynight);
            final String[] items = new String[OsmandSettings.DayNightMode.values().length];
            for (int i = 0; i < items.length; i++) {
                items[i] = OsmandSettings.DayNightMode.values()[i].toHumanString(activity.getMyApplication());
            }
            int i = view.getSettings().DAYNIGHT_MODE.get().ordinal();
            bld.setSingleChoiceItems(items, i, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    view.getSettings().DAYNIGHT_MODE.set(OsmandSettings.DayNightMode.values()[which]);
                    refreshMapComplete(activity);
                    dialog.dismiss();
                    activity.getDashboard().refreshContent(true);
                // adapter.getItem(pos).setDescription(s, getDayNightDescr(activity));
                // ad.notifyDataSetInvalidated();
                }
            });
            bld.setNegativeButton(R.string.shared_string_dismiss, null);
            bld.show();
            return false;
        }
    }).createItem());
    adapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.map_magnifier, activity).setDescription(String.format(Locale.UK, "%.0f", 100f * activity.getMyApplication().getSettings().MAP_DENSITY.get()) + " %").setLayout(R.layout.list_item_single_line_descrition_narrow).setIcon(R.drawable.ic_action_map_magnifier).setListener(new ContextMenuAdapter.ItemClickListener() {

        @Override
        public boolean onContextMenuClick(final ArrayAdapter<ContextMenuItem> ad, int itemId, final int pos, boolean isChecked, int[] viewCoordinates) {
            final OsmandMapTileView view = activity.getMapView();
            final OsmandSettings.OsmandPreference<Float> mapDensity = view.getSettings().MAP_DENSITY;
            final AlertDialog.Builder bld = new AlertDialog.Builder(view.getContext());
            int p = (int) (mapDensity.get() * 100);
            final TIntArrayList tlist = new TIntArrayList(new int[] { 33, 50, 75, 100, 150, 200, 300, 400 });
            final List<String> values = new ArrayList<>();
            int i = -1;
            for (int k = 0; k <= tlist.size(); k++) {
                final boolean end = k == tlist.size();
                if (i == -1) {
                    if ((end || p < tlist.get(k))) {
                        values.add(p + " %");
                        i = k;
                    } else if (p == tlist.get(k)) {
                        i = k;
                    }
                }
                if (k < tlist.size()) {
                    values.add(tlist.get(k) + " %");
                }
            }
            if (values.size() != tlist.size()) {
                tlist.insert(i, p);
            }
            bld.setTitle(R.string.map_magnifier);
            bld.setSingleChoiceItems(values.toArray(new String[values.size()]), i, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    int p = tlist.get(which);
                    mapDensity.set(p / 100.0f);
                    view.setComplexZoom(view.getZoom(), view.getSettingsMapDensity());
                    MapRendererContext mapContext = NativeCoreContext.getMapRendererContext();
                    if (mapContext != null) {
                        mapContext.updateMapSettings();
                    }
                    adapter.getItem(pos).setDescription(String.format(Locale.UK, "%.0f", 100f * activity.getMyApplication().getSettings().MAP_DENSITY.get()) + " %");
                    ad.notifyDataSetInvalidated();
                    dialog.dismiss();
                }
            });
            bld.setNegativeButton(R.string.shared_string_dismiss, null);
            bld.show();
            return false;
        }
    }).createItem());
    ContextMenuItem props;
    props = createRenderingProperty(customRules, adapter, activity, R.drawable.ic_action_intersection, ROAD_STYLE_ATTR);
    if (props != null) {
        adapter.addItem(props);
    }
    adapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.text_size, activity).setDescription(getScale(activity)).setLayout(R.layout.list_item_single_line_descrition_narrow).setIcon(R.drawable.ic_action_map_text_size).setListener(new ContextMenuAdapter.ItemClickListener() {

        @Override
        public boolean onContextMenuClick(final ArrayAdapter<ContextMenuItem> ad, int itemId, final int pos, boolean isChecked, int[] viewCoordinates) {
            final OsmandMapTileView view = activity.getMapView();
            AlertDialog.Builder b = new AlertDialog.Builder(view.getContext());
            // test old descr as title
            b.setTitle(R.string.text_size);
            final Float[] txtValues = new Float[] { 0.75f, 1f, 1.25f, 1.5f, 2f, 3f };
            int selected = -1;
            final String[] txtNames = new String[txtValues.length];
            for (int i = 0; i < txtNames.length; i++) {
                txtNames[i] = (int) (txtValues[i] * 100) + " %";
                if (Math.abs(view.getSettings().TEXT_SCALE.get() - txtValues[i]) < 0.1f) {
                    selected = i;
                }
            }
            b.setSingleChoiceItems(txtNames, selected, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    view.getSettings().TEXT_SCALE.set(txtValues[which]);
                    refreshMapComplete(activity);
                    adapter.getItem(pos).setDescription(getScale(activity));
                    ad.notifyDataSetInvalidated();
                    dialog.dismiss();
                }
            });
            b.setNegativeButton(R.string.shared_string_dismiss, null);
            b.show();
            return false;
        }
    }).createItem());
    String localeDescr = activity.getMyApplication().getSettings().MAP_PREFERRED_LOCALE.get();
    localeDescr = localeDescr == null || localeDescr.equals("") ? activity.getString(R.string.local_map_names) : localeDescr;
    adapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.map_locale, activity).setDescription(localeDescr).setLayout(R.layout.list_item_single_line_descrition_narrow).setIcon(R.drawable.ic_action_map_language).setListener(new ContextMenuAdapter.ItemClickListener() {

        @Override
        public boolean onContextMenuClick(final ArrayAdapter<ContextMenuItem> ad, int itemId, final int pos, boolean isChecked, int[] viewCoordinates) {
            final OsmandMapTileView view = activity.getMapView();
            final AlertDialog.Builder b = new AlertDialog.Builder(view.getContext());
            b.setTitle(activity.getString(R.string.map_locale));
            final String[] txtIds = getSortedMapNamesIds(activity, mapNamesIds, getMapNamesValues(activity, mapNamesIds));
            final String[] txtValues = getMapNamesValues(activity, txtIds);
            int selected = -1;
            for (int i = 0; i < txtIds.length; i++) {
                if (view.getSettings().MAP_PREFERRED_LOCALE.get().equals(txtIds[i])) {
                    selected = i;
                    break;
                }
            }
            selectedLanguageIndex = selected;
            transliterateNames = view.getSettings().MAP_TRANSLITERATE_NAMES.get();
            final OnCheckedChangeListener translitChangdListener = new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    transliterateNames = isChecked;
                }
            };
            final ArrayAdapter<CharSequence> singleChoiceAdapter = new ArrayAdapter<CharSequence>(activity, R.layout.single_choice_switch_item, R.id.text1, txtValues) {

                @NonNull
                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    View v = super.getView(position, convertView, parent);
                    AppCompatCheckedTextView checkedTextView = (AppCompatCheckedTextView) v.findViewById(R.id.text1);
                    if (position == selectedLanguageIndex && position > 0) {
                        checkedTextView.setChecked(true);
                        v.findViewById(R.id.topDivider).setVisibility(View.VISIBLE);
                        v.findViewById(R.id.bottomDivider).setVisibility(View.VISIBLE);
                        v.findViewById(R.id.switchLayout).setVisibility(View.VISIBLE);
                        TextView switchText = (TextView) v.findViewById(R.id.switchText);
                        switchText.setText(activity.getString(R.string.translit_name_if_miss, txtValues[position]));
                        SwitchCompat check = (SwitchCompat) v.findViewById(R.id.check);
                        check.setChecked(transliterateNames);
                        check.setOnCheckedChangeListener(translitChangdListener);
                    } else {
                        checkedTextView.setChecked(position == selectedLanguageIndex);
                        v.findViewById(R.id.topDivider).setVisibility(View.GONE);
                        v.findViewById(R.id.bottomDivider).setVisibility(View.GONE);
                        v.findViewById(R.id.switchLayout).setVisibility(View.GONE);
                    }
                    return v;
                }
            };
            b.setAdapter(singleChoiceAdapter, null);
            b.setSingleChoiceItems(txtValues, selected, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    selectedLanguageIndex = which;
                    ((AlertDialog) dialog).getListView().setSelection(which);
                    singleChoiceAdapter.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) {
                    view.getSettings().MAP_TRANSLITERATE_NAMES.set(selectedLanguageIndex > 0 && transliterateNames);
                    AlertDialog dlg = (AlertDialog) dialog;
                    int index = dlg.getListView().getCheckedItemPosition();
                    view.getSettings().MAP_PREFERRED_LOCALE.set(txtIds[index]);
                    refreshMapComplete(activity);
                    String localeDescr = txtIds[index];
                    localeDescr = localeDescr == null || localeDescr.equals("") ? activity.getString(R.string.local_map_names) : localeDescr;
                    adapter.getItem(pos).setDescription(localeDescr);
                    ad.notifyDataSetInvalidated();
                }
            });
            b.show();
            return false;
        }
    }).createItem());
    props = createProperties(customRules, null, R.string.rendering_category_transport, R.drawable.ic_action_bus_dark, "transport", null, adapter, activity, true);
    if (props != null) {
        adapter.addItem(props);
    }
    props = createProperties(customRules, null, R.string.rendering_category_details, R.drawable.ic_action_layers_dark, "details", null, adapter, activity, true);
    if (props != null) {
        adapter.addItem(props);
    }
    props = createProperties(customRules, null, R.string.rendering_category_hide, R.drawable.ic_action_hide, "hide", null, adapter, activity, true);
    if (props != null) {
        adapter.addItem(props);
    }
    List<RenderingRuleProperty> customRulesIncluded = new ArrayList<>();
    for (RenderingRuleProperty p : customRules) {
        if (p.getAttrName().equals(HIKING_ROUTES_OSMC_ATTR)) {
            customRulesIncluded.add(p);
            break;
        }
    }
    props = createProperties(customRules, customRulesIncluded, R.string.rendering_category_routes, R.drawable.ic_action_map_routes, "routes", null, adapter, activity, true);
    if (props != null) {
        adapter.addItem(props);
    }
    if (getCustomRenderingPropertiesSize(customRules) > 0) {
        adapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.rendering_category_others, activity).setCategory(true).setLayout(R.layout.list_group_title_with_switch).createItem());
        createCustomRenderingProperties(adapter, activity, customRules);
    }
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) ItemClickListener(net.osmand.plus.ContextMenuAdapter.ItemClickListener) OsmandApplication(net.osmand.plus.OsmandApplication) DialogInterface(android.content.DialogInterface) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) ItemClickListener(net.osmand.plus.ContextMenuAdapter.ItemClickListener) NonNull(android.support.annotation.NonNull) Iterator(java.util.Iterator) TIntArrayList(gnu.trove.list.array.TIntArrayList) List(java.util.List) ArrayList(java.util.ArrayList) AppCompatCheckedTextView(android.support.v7.widget.AppCompatCheckedTextView) TextView(android.widget.TextView) OnCheckedChangeListener(android.widget.CompoundButton.OnCheckedChangeListener) ContextMenuItem(net.osmand.plus.ContextMenuItem) ViewGroup(android.view.ViewGroup) RenderingRuleProperty(net.osmand.render.RenderingRuleProperty) 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) RenderingRulesStorage(net.osmand.render.RenderingRulesStorage) TIntArrayList(gnu.trove.list.array.TIntArrayList) ContextMenuAdapter(net.osmand.plus.ContextMenuAdapter) AppCompatCheckedTextView(android.support.v7.widget.AppCompatCheckedTextView) OsmandMapTileView(net.osmand.plus.views.OsmandMapTileView) MapRendererContext(net.osmand.core.android.MapRendererContext) CompoundButton(android.widget.CompoundButton) ArrayAdapter(android.widget.ArrayAdapter) SwitchCompat(android.support.v7.widget.SwitchCompat)

Example 30 with ContextMenuAdapter

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

the class RasterMapMenu method createListAdapter.

public static ContextMenuAdapter createListAdapter(final MapActivity mapActivity, final RasterMapType type) {
    ContextMenuAdapter adapter = new ContextMenuAdapter();
    adapter.setDefaultLayoutId(R.layout.list_item_icon_and_menu);
    createLayersItems(adapter, mapActivity, type);
    return adapter;
}
Also used : ContextMenuAdapter(net.osmand.plus.ContextMenuAdapter)

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