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());
}
Aggregations