From 81d6a6611319601b60e23dee3e700880956cabb4 Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Sat, 5 Mar 2016 12:40:11 +0300 Subject: [PATCH] Added 'Avoid road' menu item to the three dots menu --- OsmAnd/res/values/strings.xml | 1 + .../DistanceCalculatorPlugin.java | 4 +- .../plus/helpers/AvoidSpecificRoads.java | 16 +++++-- .../plus/mapcontextmenu/MapContextMenu.java | 6 +-- .../RoutePointsLayer.java | 4 +- .../osmand/plus/views/ContextMenuLayer.java | 2 +- .../plus/views/ImpassableRoadsLayer.java | 45 +++++++++++++++---- .../net/osmand/plus/views/OsmandMapLayer.java | 6 ++- 8 files changed, 60 insertions(+), 24 deletions(-) diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 72f0a0192f..e979d46d93 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -9,6 +9,7 @@ 3. All your modified/created strings are in the top of the file (to make easier find what\'s translated). PLEASE: Have a look at http://code.google.com/p/osmand/wiki/UIConsistency, it may really improve your and our work :-) Thx - Hardy --> + Avoid road Currently selected Data storage folder is readonly. The storage folder was temporarily switched to Internal memory. Please choose valid storage directory. Shared memory Topbar diff --git a/OsmAnd/src/net/osmand/plus/distancecalculator/DistanceCalculatorPlugin.java b/OsmAnd/src/net/osmand/plus/distancecalculator/DistanceCalculatorPlugin.java index 2a451260a6..7b2b4fd7d5 100644 --- a/OsmAnd/src/net/osmand/plus/distancecalculator/DistanceCalculatorPlugin.java +++ b/OsmAnd/src/net/osmand/plus/distancecalculator/DistanceCalculatorPlugin.java @@ -635,8 +635,8 @@ public class DistanceCalculatorPlugin extends OsmandPlugin { } @Override - public void populateObjectContextMenu(Object o, ContextMenuAdapter adapter) { - if(o instanceof WptPt) { + public void populateObjectContextMenu(LatLon latLon, Object o, ContextMenuAdapter adapter) { + if (o != null && o instanceof WptPt) { final WptPt p = (WptPt) o; boolean containsPoint = false; for (int i = 0; i < measurementPoints.size(); i++) { diff --git a/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java b/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java index d350fa0076..b802fbd260 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java +++ b/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java @@ -20,6 +20,7 @@ import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.mapcontextmenu.MapContextMenu; import net.osmand.plus.routing.RoutingHelper; import net.osmand.plus.views.AnimateDraggingMapThread; import net.osmand.plus.views.ContextMenuLayer; @@ -134,16 +135,17 @@ public class AvoidSpecificRoads { protected void selectFromMap(final MapActivity mapActivity) { ContextMenuLayer cm = mapActivity.getMapLayers().getContextMenuLayer(); cm.setSelectOnMap(new CallbackWithObject() { - + @Override public boolean processResult(LatLon result) { - findRoad(mapActivity, result); + addImpassableRoad(mapActivity, result, true); return true; } }); } - private void findRoad(final MapActivity activity, final LatLon loc) { + + public void addImpassableRoad(final MapActivity activity, final LatLon loc, final boolean showDialog) { final Location ll = new Location(""); ll.setLatitude(loc.getLatitude()); ll.setLongitude(loc.getLongitude()); @@ -159,7 +161,13 @@ public class AvoidSpecificRoads { if(rh.isRouteCalculated() || rh.isRouteBeingCalculated()) { rh.recalculateRouteDueToSettingsChange(); } - showDialog(activity); + if (showDialog) { + showDialog(activity); + } + MapContextMenu menu = activity.getContextMenu(); + if (menu.isActive() && menu.getLatLon().equals(loc)) { + menu.close(); + } } return true; } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java index 7b2e5dbcd1..8175fcb692 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java @@ -554,10 +554,8 @@ public class MapContextMenu extends MenuTitleController implements StateChangedL public void buttonMorePressed() { final ContextMenuAdapter menuAdapter = new ContextMenuAdapter(mapActivity); - if (object != null) { - for (OsmandMapLayer layer : mapActivity.getMapView().getLayers()) { - layer.populateObjectContextMenu(object, menuAdapter); - } + for (OsmandMapLayer layer : mapActivity.getMapView().getLayers()) { + layer.populateObjectContextMenu(latLon, object, menuAdapter); } mapActivity.getMapActions().contextMenuPoint(latLon.getLatitude(), latLon.getLongitude(), menuAdapter, object); diff --git a/OsmAnd/src/net/osmand/plus/routepointsnavigation/RoutePointsLayer.java b/OsmAnd/src/net/osmand/plus/routepointsnavigation/RoutePointsLayer.java index 06b510ff31..cd36416676 100644 --- a/OsmAnd/src/net/osmand/plus/routepointsnavigation/RoutePointsLayer.java +++ b/OsmAnd/src/net/osmand/plus/routepointsnavigation/RoutePointsLayer.java @@ -81,8 +81,8 @@ public class RoutePointsLayer extends OsmandMapLayer implements ContextMenuLaye } @Override - public void populateObjectContextMenu(Object o, ContextMenuAdapter adapter) { - if (o instanceof GPXUtilities.WptPt && plugin.getCurrentRoute() != null){ + public void populateObjectContextMenu(LatLon latLon, Object o, ContextMenuAdapter adapter) { + if (o != null && o instanceof GPXUtilities.WptPt && plugin.getCurrentRoute() != null){ final GPXUtilities.WptPt point = (GPXUtilities.WptPt) o; ContextMenuAdapter.OnContextMenuClick listener = new ContextMenuAdapter.OnContextMenuClick() { @Override diff --git a/OsmAnd/src/net/osmand/plus/views/ContextMenuLayer.java b/OsmAnd/src/net/osmand/plus/views/ContextMenuLayer.java index bb08f0dde6..1a0cc8b273 100644 --- a/OsmAnd/src/net/osmand/plus/views/ContextMenuLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/ContextMenuLayer.java @@ -102,7 +102,7 @@ public class ContextMenuLayer extends OsmandMapLayer { } @Override - public void populateObjectContextMenu(Object o, ContextMenuAdapter adapter) { + public void populateObjectContextMenu(LatLon latLon, Object o, ContextMenuAdapter adapter) { if (menu.hasHiddenBottomInfo()) { ContextMenuAdapter.OnContextMenuClick listener = new ContextMenuAdapter.OnContextMenuClick() { @Override diff --git a/OsmAnd/src/net/osmand/plus/views/ImpassableRoadsLayer.java b/OsmAnd/src/net/osmand/plus/views/ImpassableRoadsLayer.java index 64facd89de..2f95b0dbfc 100644 --- a/OsmAnd/src/net/osmand/plus/views/ImpassableRoadsLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/ImpassableRoadsLayer.java @@ -5,14 +5,18 @@ import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PointF; +import android.widget.ArrayAdapter; import net.osmand.Location; import net.osmand.binary.RouteDataObject; import net.osmand.data.LatLon; +import net.osmand.data.LocationPoint; import net.osmand.data.PointDescription; import net.osmand.data.RotatedTileBox; +import net.osmand.plus.ContextMenuAdapter; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.routing.RoutingHelper; import java.util.List; import java.util.Map; @@ -26,6 +30,7 @@ public class ImpassableRoadsLayer extends OsmandMapLayer implements ContextMenuL private Paint paint; private Map missingRoadLocations; private List missingRoads; + private RoutingHelper routingHelper; public ImpassableRoadsLayer(MapActivity activity) { this.activity = activity; @@ -36,6 +41,7 @@ public class ImpassableRoadsLayer extends OsmandMapLayer implements ContextMenuL this.view = view; roadWorkIcon = BitmapFactory.decodeResource(view.getResources(), R.drawable.map_pin_avoid_road); paint = new Paint(); + routingHelper = activity.getRoutingHelper(); } @Override @@ -58,14 +64,14 @@ public class ImpassableRoadsLayer extends OsmandMapLayer implements ContextMenuL } public Map getMissingRoadLocations() { - if(missingRoadLocations == null) { + if (missingRoadLocations == null) { missingRoadLocations = activity.getMyApplication().getDefaultRoutingConfig().getImpassableRoadLocations(); } return missingRoadLocations; } public List getMissingRoads() { - if(missingRoads == null) { + if (missingRoads == null) { missingRoads = activity.getMyApplication().getDefaultRoutingConfig().getImpassableRoads(); } return missingRoads; @@ -81,9 +87,9 @@ public class ImpassableRoadsLayer extends OsmandMapLayer implements ContextMenuL return true; } - public int getRadiusPoi(RotatedTileBox tb){ + public int getRadiusPoi(RotatedTileBox tb) { int r = 0; - if(tb.getZoom() < startZoom){ + if (tb.getZoom() < startZoom) { r = 0; } else { r = 15; @@ -92,7 +98,7 @@ public class ImpassableRoadsLayer extends OsmandMapLayer implements ContextMenuL } private boolean calculateBelongs(int ex, int ey, int objx, int objy, int radius) { - return Math.abs(objx - ex) <= radius && (ey - objy) <= radius / 2 && (objy - ey) <= 3 * radius ; + return Math.abs(objx - ex) <= radius && (ey - objy) <= radius / 2 && (objy - ey) <= 3 * radius; } @Override @@ -125,8 +131,8 @@ public class ImpassableRoadsLayer extends OsmandMapLayer implements ContextMenuL @Override public LatLon getObjectLocation(Object o) { - if(o instanceof RouteDataObject) { - RouteDataObject route = (RouteDataObject) o; + if (o instanceof RouteDataObject) { + RouteDataObject route = (RouteDataObject) o; Location location = missingRoadLocations.get(route.getId()); return new LatLon(location.getLatitude(), location.getLongitude()); } @@ -140,11 +146,32 @@ public class ImpassableRoadsLayer extends OsmandMapLayer implements ContextMenuL @Override public PointDescription getObjectName(Object o) { - if(o instanceof RouteDataObject) { - RouteDataObject route = (RouteDataObject) o; + if (o instanceof RouteDataObject) { + RouteDataObject route = (RouteDataObject) o; return new PointDescription(PointDescription.POINT_TYPE_BLOCKED_ROAD, route.getName()); } return null; } + @Override + public void populateObjectContextMenu(final LatLon latLon, final Object o, ContextMenuAdapter adapter) { + if (latLon != null && o == null + && (routingHelper.isRoutePlanningMode() || routingHelper.isFollowingMode())) { + + ContextMenuAdapter.OnContextMenuClick listener = new ContextMenuAdapter.OnContextMenuClick() { + @Override + public boolean onContextMenuClick(ArrayAdapter adapter, int itemId, int pos, boolean isChecked) { + if (itemId == R.string.avoid_road) { + activity.getMyApplication().getAvoidSpecificRoads().addImpassableRoad( + activity, latLon, false); + } + activity.refreshMap(); + return true; + } + }; + + adapter.item(R.string.avoid_road).iconColor( + R.drawable.ic_action_road_works_dark).listen(listener).reg(); + } + } } diff --git a/OsmAnd/src/net/osmand/plus/views/OsmandMapLayer.java b/OsmAnd/src/net/osmand/plus/views/OsmandMapLayer.java index 04de4a573f..ef89f75c05 100644 --- a/OsmAnd/src/net/osmand/plus/views/OsmandMapLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/OsmandMapLayer.java @@ -6,6 +6,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import net.osmand.data.LatLon; import net.osmand.data.QuadRect; import net.osmand.data.QuadTree; import net.osmand.data.RotatedTileBox; @@ -16,6 +17,7 @@ import android.graphics.Path; import android.graphics.PointF; import android.os.AsyncTask; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.view.MotionEvent; public abstract class OsmandMapLayer { @@ -31,8 +33,8 @@ public abstract class OsmandMapLayer { public abstract void destroyLayer(); public void onRetainNonConfigurationInstance(Map map) {} - - public void populateObjectContextMenu(Object o, ContextMenuAdapter adapter) {} + + public void populateObjectContextMenu(LatLon latLon, Object o, ContextMenuAdapter adapter) {} public boolean onSingleTap(PointF point, RotatedTileBox tileBox) { return false;