From 716eff040be30099a21b7ce4cdb3005322603560 Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Wed, 6 Jul 2016 22:00:44 +0300 Subject: [PATCH] Fix #2558 --- .../src/net/osmand/plus/MapMarkersHelper.java | 10 ---- .../net/osmand/plus/TargetPointsHelper.java | 49 ++++++++++++++++++- .../plus/mapcontextmenu/MapContextMenu.java | 35 +++++++++---- .../plus/views/PointNavigationLayer.java | 9 ++-- 4 files changed, 78 insertions(+), 25 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java index 6226eafe56..9873a067e3 100644 --- a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java +++ b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java @@ -90,25 +90,15 @@ public class MapMarkersHelper { if (colorIndex != mapMarker.colorIndex) return false; if (pos != mapMarker.pos) return false; - //if (index != mapMarker.index) return false; - //if (history != mapMarker.history) return false; - //if (selected != mapMarker.selected) return false; - //if (dist != mapMarker.dist) return false; return point.equals(mapMarker.point); - //return pointDescription != null ? pointDescription.equals(mapMarker.pointDescription) : mapMarker.pointDescription == null; } @Override public int hashCode() { int result = point.hashCode(); - result = 31 * result + (pointDescription != null ? pointDescription.hashCode() : 0); result = 31 * result + colorIndex; result = 31 * result + pos; - result = 31 * result + index; - result = 31 * result + (history ? 1 : 0); - result = 31 * result + (selected ? 1 : 0); - result = 31 * result + dist; return result; } } diff --git a/OsmAnd/src/net/osmand/plus/TargetPointsHelper.java b/OsmAnd/src/net/osmand/plus/TargetPointsHelper.java index 7af0878838..b3bc3525d3 100644 --- a/OsmAnd/src/net/osmand/plus/TargetPointsHelper.java +++ b/OsmAnd/src/net/osmand/plus/TargetPointsHelper.java @@ -26,11 +26,16 @@ public class TargetPointsHelper { private OsmandSettings settings; private RoutingHelper routingHelper; private List> listeners = new ArrayList<>(); + private List pointListeners = new ArrayList<>(); private OsmandApplication ctx; private AddressLookupRequest startPointRequest; private AddressLookupRequest targetPointRequest; + public interface TargetPointChangedListener { + void onTargetPointChanged(TargetPoint targetPoint); + } + public static class TargetPoint implements LocationPoint { public LatLon point; private PointDescription pointDescription; @@ -49,7 +54,30 @@ public class TargetPointsHelper { this.index = index; this.intermediate = true; } - + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + TargetPoint targetPoint = (TargetPoint) o; + + if (start != targetPoint.start) return false; + if (intermediate != targetPoint.intermediate) return false; + if (index != targetPoint.index) return false; + return point.equals(targetPoint.point); + + } + + @Override + public int hashCode() { + int result = point.hashCode(); + result = 31 * result + index; + result = 31 * result + (start ? 10 : 20); + result = 31 * result + (intermediate ? 100 : 200); + return result; + } + @SuppressLint("StringFormatInvalid") public PointDescription getPointDescription(Context ctx) { if (!intermediate) { @@ -154,6 +182,7 @@ public class TargetPointsHelper { settings.updateIntermediatePoint(p.point.getLatitude(), p.point.getLongitude(), p.pointDescription); updateRouteAndRefresh(false); + updateTargetPoint(p); break; } } @@ -176,6 +205,7 @@ public class TargetPointsHelper { settings.setPointToStart(pointToStart.point.getLatitude(), pointToStart.point.getLongitude(), pointToStart.pointDescription); updateRouteAndRefresh(false); + updateTargetPoint(pointToStart); } } }, null); @@ -196,6 +226,7 @@ public class TargetPointsHelper { settings.setPointToNavigate(pointToNavigate.point.getLatitude(), pointToNavigate.point.getLongitude(), pointToNavigate.pointDescription); updateRouteAndRefresh(false); + updateTargetPoint(pointToNavigate); } } }, null); @@ -558,4 +589,20 @@ public class TargetPointsHelper { ctx.getGeocodingLookupService().cancel(latLon); } } + + public void addPointListener(TargetPointChangedListener l) { + if (!pointListeners.contains(l)) { + pointListeners.add(l); + } + } + + public void removePointListener(TargetPointChangedListener l) { + pointListeners.remove(l); + } + + private void updateTargetPoint(TargetPoint targetPoint) { + for (TargetPointChangedListener l : pointListeners) { + l.onTargetPointChanged(targetPoint); + } + } } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java index 2cc20e02e2..c3fbae0e6c 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java @@ -27,6 +27,7 @@ import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; import net.osmand.plus.TargetPointsHelper; import net.osmand.plus.TargetPointsHelper.TargetPoint; +import net.osmand.plus.TargetPointsHelper.TargetPointChangedListener; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.dashboard.DashboardOnMap; import net.osmand.plus.dialogs.DirectionsDialogs; @@ -49,7 +50,7 @@ import java.lang.ref.WeakReference; import java.util.List; public class MapContextMenu extends MenuTitleController implements StateChangedListener, - MapMarkerChangedListener { + MapMarkerChangedListener, TargetPointChangedListener { private MapActivity mapActivity; private OsmandSettings settings; @@ -80,6 +81,9 @@ public class MapContextMenu extends MenuTitleController implements StateChangedL private MenuAction searchDoneAction; + public MapContextMenu() { + } + @Override public MapActivity getMapActivity() { return mapActivity; @@ -214,9 +218,6 @@ public class MapContextMenu extends MenuTitleController implements StateChangedL return menuController; } - public MapContextMenu() { - } - public boolean init(@NonNull LatLon latLon, @Nullable PointDescription pointDescription, @Nullable Object object) { @@ -281,6 +282,8 @@ public class MapContextMenu extends MenuTitleController implements StateChangedL if (object instanceof MapMarker) { mapActivity.getMyApplication().getMapMarkersHelper().addListener(this); + } else if (object instanceof TargetPoint) { + mapActivity.getMyApplication().getTargetPointsHelper().addPointListener(this); } return true; @@ -382,13 +385,9 @@ public class MapContextMenu extends MenuTitleController implements StateChangedL @Override public void onMapMarkerChanged(MapMarker mapMarker) { - if (object.equals(mapMarker)) { + if (object != null && object.equals(mapMarker)) { String address = mapMarker.getOnlyName(); - nameStr = address; - pointDescription.setName(address); - WeakReference fragmentRef = findMenuFragment(); - if (fragmentRef != null) - fragmentRef.get().refreshTitle(); + updateTitle(address); } } @@ -396,6 +395,22 @@ public class MapContextMenu extends MenuTitleController implements StateChangedL public void onMapMarkersChanged() { } + @Override + public void onTargetPointChanged(TargetPoint targetPoint) { + if (object != null && object.equals(targetPoint)) { + String address = targetPoint.getOnlyName(); + updateTitle(address); + } + } + + private void updateTitle(String address) { + nameStr = address; + pointDescription.setName(address); + WeakReference fragmentRef = findMenuFragment(); + if (fragmentRef != null) + fragmentRef.get().refreshTitle(); + } + @Override public void stateChanged(ApplicationMode change) { appModeChanged = active; diff --git a/OsmAnd/src/net/osmand/plus/views/PointNavigationLayer.java b/OsmAnd/src/net/osmand/plus/views/PointNavigationLayer.java index 14f54c1729..f87a7c247d 100644 --- a/OsmAnd/src/net/osmand/plus/views/PointNavigationLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/PointNavigationLayer.java @@ -268,14 +268,15 @@ public class PointNavigationLayer extends OsmandMapLayer implements public void applyNewObjectPosition(@NonNull Object o, @NonNull LatLon position, @Nullable ContextMenuLayer.ApplyMovedObjectCallback callback) { boolean result = false; + TargetPoint newTargetPoint = null; if (o instanceof TargetPoint) { - TargetPoint point = (TargetPoint) o; - TargetPointsHelper tph = map.getMyApplication().getTargetPointsHelper(); - tph.navigateToPoint(position, true, -1, point.getPointDescription(map)); + TargetPointsHelper targetPointsHelper = map.getMyApplication().getTargetPointsHelper(); + targetPointsHelper.navigateToPoint(position, true, -1, new PointDescription(PointDescription.POINT_TYPE_LOCATION, "")); + newTargetPoint = targetPointsHelper.getPointToNavigate(); result = true; } if (callback != null) { - callback.onApplyMovedObject(result, o); + callback.onApplyMovedObject(result, newTargetPoint == null ? o : newTargetPoint); } } }