From 2871edbb5576fbca60acb6ca032ee958f2774a33 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 23 Aug 2017 18:11:45 +0300 Subject: [PATCH] Move snap to road logic --- .../MeasurementEditingContext.java | 115 ++++++++++++++++-- .../MeasurementToolFragment.java | 106 ++-------------- 2 files changed, 114 insertions(+), 107 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementEditingContext.java b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementEditingContext.java index 7647df1d0b..a2138bec0a 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementEditingContext.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementEditingContext.java @@ -1,12 +1,23 @@ package net.osmand.plus.measurementtool; import android.util.Pair; +import android.view.View; +import android.widget.ProgressBar; +import net.osmand.Location; +import net.osmand.data.LatLon; import net.osmand.plus.ApplicationMode; import net.osmand.plus.GPXUtilities.TrkSegment; import net.osmand.plus.GPXUtilities.WptPt; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.OsmandSettings; +import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.routing.RouteCalculationParams; +import net.osmand.plus.routing.RoutingHelper; import net.osmand.plus.views.Renderable; +import net.osmand.router.RouteCalculationProgress; +import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; import java.util.List; @@ -15,6 +26,8 @@ import java.util.Queue; public class MeasurementEditingContext { + private MapActivity mapActivity; + private TrkSegment before = new TrkSegment(); // cache should be deleted if after changed or snappedToRoadPoints private TrkSegment beforeCacheForSnap; @@ -27,14 +40,19 @@ public class MeasurementEditingContext { private boolean inAddPointBeforeMode; private boolean inAddPointAfterMode; - private boolean isInSnapToRoadMode; + private ProgressBar progressBar; + private boolean inSnapToRoadMode; private ApplicationMode snapToRoadAppMode; - private Pair currentPair; + private RouteCalculationProgress calculationProgress; private Queue> snapToRoadPairsToCalculate = new LinkedList<>(); private Map, List> snappedToRoadPoints = new HashMap<>(); private List measurementPoints = new LinkedList<>(); + public void setMapActivity(MapActivity mapActivity) { + this.mapActivity = mapActivity; + } + public void setBefore(TrkSegment before) { this.before = before; addBeforeRenders(); @@ -62,7 +80,7 @@ public class MeasurementEditingContext { } public boolean isInSnapToRoadMode() { - return isInSnapToRoadMode; + return inSnapToRoadMode; } public void setInAddPointBeforeMode(boolean inAddPointBeforeMode) { @@ -82,7 +100,7 @@ public class MeasurementEditingContext { } public void setInSnapToRoadMode(boolean inSnapToRoadMode) { - isInSnapToRoadMode = inSnapToRoadMode; + inSnapToRoadMode = inSnapToRoadMode; } public ApplicationMode getSnapToRoadAppMode() { @@ -126,10 +144,6 @@ public class MeasurementEditingContext { return after; } - public void scheduleRouteCalculateIfNotEmpty() { - - } - public void recreateSegments() { before = new TrkSegment(); before.points.addAll(measurementPoints); @@ -152,4 +166,89 @@ public class MeasurementEditingContext { before = new TrkSegment(); after = new TrkSegment(); } + + public void scheduleRouteCalculateIfNotEmpty(ProgressBar progressBar) { + if (mapActivity == null || measurementPoints.size() < 1) { + return; + } + for (int i = 0; i < measurementPoints.size() - 1; i++) { + snapToRoadPairsToCalculate.add(new Pair<>(measurementPoints.get(i), measurementPoints.get(i + 1))); + } + this.progressBar = progressBar; + if (!snapToRoadPairsToCalculate.isEmpty()) { + mapActivity.getMyApplication().getRoutingHelper().startRouteCalculationThread(getParams(), true, true); + progressBar.setVisibility(View.VISIBLE); + } + } + + public void cancelSnapToRoad() { + if (calculationProgress != null) { + calculationProgress.isCancelled = true; + } + } + + private RouteCalculationParams getParams() { + OsmandApplication app = mapActivity.getMyApplication(); + OsmandSettings settings = app.getSettings(); + + final Pair currentPair = snapToRoadPairsToCalculate.poll(); + + Location start = new Location(""); + start.setLatitude(currentPair.first.getLatitude()); + start.setLongitude(currentPair.first.getLongitude()); + + LatLon end = new LatLon(currentPair.second.getLatitude(), currentPair.second.getLongitude()); + + final RouteCalculationParams params = new RouteCalculationParams(); + params.start = start; + params.end = end; + params.leftSide = settings.DRIVING_REGION.get().leftHandDriving; + params.fast = settings.FAST_ROUTE_MODE.getModeValue(snapToRoadAppMode); + params.type = settings.ROUTER_SERVICE.getModeValue(snapToRoadAppMode); + params.mode = snapToRoadAppMode; + params.ctx = app; + params.calculationProgress = calculationProgress = new RouteCalculationProgress(); + params.calculationProgressCallback = new RoutingHelper.RouteCalculationProgressCallback() { + @Override + public void updateProgress(int progress) { + progressBar.setProgress(progress); + } + + @Override + public void requestPrivateAccessRouting() { + + } + + @Override + public void finish() { + mapActivity.refreshMap(); + } + }; + params.resultListener = new RouteCalculationParams.RouteCalculationResultListener() { + @Override + public void onRouteCalculated(List locations) { + ArrayList pts = new ArrayList<>(locations.size()); + for (Location loc : locations) { + WptPt pt = new WptPt(); + pt.lat = loc.getLatitude(); + pt.lon = loc.getLongitude(); + pts.add(pt); + } + snappedToRoadPoints.put(currentPair, pts); + + if (!snapToRoadPairsToCalculate.isEmpty()) { + mapActivity.getMyApplication().getRoutingHelper().startRouteCalculationThread(getParams(), true, true); + } else { + mapActivity.getMyApplication().runInUIThread(new Runnable() { + @Override + public void run() { + progressBar.setVisibility(View.GONE); + } + }); + } + } + }; + + return params; + } } diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java index d1203d86db..bbb992504d 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java @@ -33,8 +33,6 @@ import android.widget.Toast; import net.osmand.AndroidUtils; import net.osmand.CallbackWithObject; import net.osmand.IndexConstants; -import net.osmand.Location; -import net.osmand.data.LatLon; import net.osmand.plus.ApplicationMode; import net.osmand.plus.GPXUtilities; import net.osmand.plus.GPXUtilities.GPXFile; @@ -45,8 +43,6 @@ import net.osmand.plus.GPXUtilities.WptPt; import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; import net.osmand.plus.IconsCache; import net.osmand.plus.OsmAndFormatter; -import net.osmand.plus.OsmandApplication; -import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.TrackActivity; @@ -65,18 +61,13 @@ import net.osmand.plus.measurementtool.command.MeasurementCommandManager; import net.osmand.plus.measurementtool.command.MovePointCommand; import net.osmand.plus.measurementtool.command.RemovePointCommand; import net.osmand.plus.measurementtool.command.ReorderPointCommand; -import net.osmand.plus.measurementtool.command.SnapToRoadCommand; -import net.osmand.plus.routing.RouteCalculationParams; -import net.osmand.plus.routing.RoutingHelper; import net.osmand.plus.views.OsmandMapTileView; import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory; import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarController; -import net.osmand.router.RouteCalculationProgress; import java.io.File; import java.text.MessageFormat; import java.text.SimpleDateFormat; -import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Locale; @@ -108,8 +99,6 @@ public class MeasurementToolFragment extends Fragment { private ImageView undoBtn; private ImageView redoBtn; private ImageView mainIcon; - private ProgressBar snapToRoadProgressBar; - private RouteCalculationProgress calculationProgress; private boolean wasCollapseButtonVisible; private boolean pointsListOpened; @@ -145,6 +134,7 @@ public class MeasurementToolFragment extends Fragment { final MapActivity mapActivity = (MapActivity) getActivity(); final MeasurementToolLayer measurementLayer = mapActivity.getMapLayers().getMeasurementToolLayer(); + editingCtx.setMapActivity(mapActivity); measurementLayer.setEditingCtx(editingCtx); if (selectedPointPos != -1 && selectedCachedPoint != null) { @@ -473,11 +463,9 @@ public class MeasurementToolFragment extends Fragment { } private void recalculateSnapToRoadIfNedeed() { - if (calculationProgress != null) { - calculationProgress.isCancelled = true; - } + editingCtx.cancelSnapToRoad(); if (isInSnapToRoadMode) { - doSnapToRoad(); +// doSnapToRoad(); } } @@ -594,9 +582,7 @@ public class MeasurementToolFragment extends Fragment { public void clearAllOnClick() { commandManager.execute(new ClearPointsCommand(measurementLayer)); editingCtx.clearSegments(); - if (calculationProgress != null) { - calculationProgress.isCancelled = true; - } + editingCtx.cancelSnapToRoad(); if (pointsListOpened) { hidePointsList(); } @@ -781,12 +767,12 @@ public class MeasurementToolFragment extends Fragment { }); snapToRoadBtn.setVisibility(View.VISIBLE); - snapToRoadProgressBar = (ProgressBar) mainView.findViewById(R.id.snap_to_road_progress_bar); + ProgressBar snapToRoadProgressBar = (ProgressBar) mainView.findViewById(R.id.snap_to_road_progress_bar); snapToRoadProgressBar.setMinimumHeight(0); if (!enableAfterRotating) { snapToRoadProgressBar.setProgress(0); - doSnapToRoad(); +// doSnapToRoad(); } mapActivity.refreshMap(); @@ -794,90 +780,12 @@ public class MeasurementToolFragment extends Fragment { } } - private void doSnapToRoad() { - MapActivity mapActivity = getMapActivity(); - if (mapActivity != null && editingCtx.getPointsCount() > 1) { - Location start = new Location(""); - WptPt first = editingCtx.getPoints().get(0); - start.setLatitude(first.getLatitude()); - start.setLongitude(first.getLongitude()); - - WptPt last = editingCtx.getPoints().get(editingCtx.getPointsCount() - 1); - LatLon end = new LatLon(last.getLatitude(), last.getLongitude()); - - List intermediates = new ArrayList<>(); - if (editingCtx.getPointsCount() > 2) { - for (int i = 1; i < editingCtx.getPointsCount() - 1; i++) { - WptPt pt = editingCtx.getPoints().get(i); - intermediates.add(new LatLon(pt.getLatitude(), pt.getLongitude())); - } - } - - OsmandApplication app = mapActivity.getMyApplication(); - OsmandSettings settings = app.getSettings(); - final RouteCalculationParams params = new RouteCalculationParams(); - params.start = start; - params.end = end; - params.intermediates = intermediates; // todo delete - params.leftSide = settings.DRIVING_REGION.get().leftHandDriving; - params.fast = settings.FAST_ROUTE_MODE.getModeValue(snapToRoadAppMode); - params.type = settings.ROUTER_SERVICE.getModeValue(snapToRoadAppMode); - params.mode = snapToRoadAppMode; - params.ctx = app; - params.calculationProgress = calculationProgress = new RouteCalculationProgress(); - params.calculationProgressCallback = new RoutingHelper.RouteCalculationProgressCallback() { - @Override - public void updateProgress(int progress) { - snapToRoadProgressBar.setProgress(progress); - } - - @Override - public void requestPrivateAccessRouting() { - - } - - @Override - public void finish() { - snapToRoadProgressBar.setVisibility(View.GONE); - MapActivity mapActivity = getMapActivity(); - if (mapActivity != null) { - mapActivity.refreshMap(); - } - } - }; - params.resultListener = new RouteCalculationParams.RouteCalculationResultListener() { - @Override - public void onRouteCalculated(List locations) { - ArrayList pts = new ArrayList<>(locations.size()); - for (Location loc : locations) { - WptPt pt = new WptPt(); - pt.lat = loc.getLatitude(); - pt.lon = loc.getLongitude(); - pts.add(pt); - } - MeasurementToolLayer layer = getMeasurementLayer(); - if (layer != null) { - commandManager.execute(new SnapToRoadCommand(layer, pts)); - editingCtx.recreateSegments(); - } - } - }; - snapToRoadProgressBar.setVisibility(View.VISIBLE); - - mapActivity.getMyApplication().getRoutingHelper().startRouteCalculationThread(params, true, true); - } else if (calculationProgress != null) { - calculationProgress.isCancelled = true; - } - } - private void disableSnapToRoadMode() { toolBarController.setTopBarSwitchVisible(false); toolBarController.setTitle(previousToolBarTitle); isInSnapToRoadMode = false; mainIcon.setImageDrawable(getActiveIcon(R.drawable.ic_action_ruler)); - if (calculationProgress != null) { - calculationProgress.isCancelled = true; - } + editingCtx.cancelSnapToRoad(); MapActivity mapActivity = getMapActivity(); if (mapActivity != null) { mapActivity.findViewById(R.id.snap_to_road_image_button).setVisibility(View.GONE);