From 370fded329afcf164f81eeb06b3b1556fa87f3a6 Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Fri, 14 Sep 2018 13:33:39 +0300 Subject: [PATCH 1/4] Remove redundant callback --- .../osmand/plus/helpers/AvoidSpecificRoads.java | 14 +++----------- .../PointDescriptionMenuController.java | 2 +- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java b/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java index 7b10e23a8f..f60b031a74 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java +++ b/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java @@ -44,7 +44,7 @@ public class AvoidSpecificRoads { public void initPreservedData() { List impassibleRoads = app.getSettings().getImpassableRoadPoints(); for (LatLon impassibleRoad : impassibleRoads) { - addImpassableRoad(null, impassibleRoad, false, null, true); + addImpassableRoad(null, impassibleRoad, false, true); } } @@ -152,7 +152,7 @@ public class AvoidSpecificRoads { @Override public boolean processResult(LatLon result) { - addImpassableRoad(mapActivity, result, true, null, false); + addImpassableRoad(mapActivity, result, true, false); return true; } @@ -162,7 +162,6 @@ public class AvoidSpecificRoads { public void addImpassableRoad(@Nullable final MapActivity activity, @NonNull final LatLon loc, final boolean showDialog, - @Nullable final AvoidSpecificRoadsCallback callback, final boolean skipWritingSettings) { final Location ll = new Location(""); ll.setLatitude(loc.getLatitude()); @@ -177,22 +176,15 @@ public class AvoidSpecificRoads { if (activity != null) { Toast.makeText(activity, R.string.error_avoid_specific_road, Toast.LENGTH_LONG).show(); } - if (callback != null) { - callback.onAddImpassableRoad(false, null); - } } else { addImpassableRoadInternal(object, ll, showDialog, activity, loc); - - if (callback != null) { - callback.onAddImpassableRoad(true, object); - } } return true; } @Override public boolean isCancelled() { - return callback != null && callback.isCancelled(); + return false; } }); diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/PointDescriptionMenuController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/PointDescriptionMenuController.java index c055ea7ad6..86026d1a31 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/PointDescriptionMenuController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/PointDescriptionMenuController.java @@ -33,7 +33,7 @@ public class PointDescriptionMenuController extends MenuController { MapActivity activity = getMapActivity(); if (activity != null) { AvoidSpecificRoads roads = activity.getMyApplication().getAvoidSpecificRoads(); - roads.addImpassableRoad(activity, getLatLon(), false, null, false); + roads.addImpassableRoad(activity, getLatLon(), false, false); } } }; From e9a0ff6e09cd56435bf68a7f8d69dbc318d02c87 Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Fri, 14 Sep 2018 15:30:00 +0300 Subject: [PATCH 2/4] Refactor avoided roads --- .../osmand/router/RoutingConfiguration.java | 12 +- .../plus/helpers/AvoidSpecificRoads.java | 206 ++++++++++-------- .../plus/views/ImpassableRoadsLayer.java | 109 +++------ 3 files changed, 157 insertions(+), 170 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/router/RoutingConfiguration.java b/OsmAnd-java/src/main/java/net/osmand/router/RoutingConfiguration.java index 3a79929142..e3271c2378 100644 --- a/OsmAnd-java/src/main/java/net/osmand/router/RoutingConfiguration.java +++ b/OsmAnd-java/src/main/java/net/osmand/router/RoutingConfiguration.java @@ -13,10 +13,8 @@ import org.xmlpull.v1.XmlPullParserException; import java.io.IOException; import java.io.InputStream; -import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; -import java.util.List; import java.util.Map; import java.util.Stack; @@ -56,8 +54,7 @@ public class RoutingConfiguration { private Map routers = new LinkedHashMap(); private Map attributes = new LinkedHashMap(); private HashMap impassableRoadLocations = new HashMap(); - private List impassableRoads = new ArrayList(); - + // Example // { // impassableRoadLocations.add(23000069L); @@ -104,11 +101,6 @@ public class RoutingConfiguration { return i; } - - public List getImpassableRoads() { - return impassableRoads; - } - public Map getImpassableRoadLocations() { return impassableRoadLocations; } @@ -116,7 +108,6 @@ public class RoutingConfiguration { public boolean addImpassableRoad(RouteDataObject route, Location location) { if (!impassableRoadLocations.containsKey(route.id)){ impassableRoadLocations.put(route.id, location); - impassableRoads.add(route); return true; } return false; @@ -142,7 +133,6 @@ public class RoutingConfiguration { public void removeImpassableRoad(RouteDataObject obj) { impassableRoadLocations.remove(obj.id); - impassableRoads.remove(obj); } } diff --git a/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java b/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java index f60b031a74..ed15c266b9 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java +++ b/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java @@ -1,9 +1,12 @@ package net.osmand.plus.helpers; import android.content.DialogInterface; +import android.graphics.drawable.Drawable; +import android.support.annotation.DrawableRes; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v7.app.AlertDialog; +import android.text.TextUtils; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; @@ -18,6 +21,7 @@ import net.osmand.ResultMatcher; import net.osmand.binary.RouteDataObject; import net.osmand.data.LatLon; import net.osmand.data.PointDescription; +import net.osmand.plus.AppInitializer; import net.osmand.plus.ApplicationMode; import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmandApplication; @@ -25,43 +29,61 @@ 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; -import net.osmand.util.Algorithms; import net.osmand.util.MapUtils; import java.util.ArrayList; -import java.util.List; +import java.util.LinkedHashMap; +import java.util.Map; public class AvoidSpecificRoads { - private List impassableRoads; + private OsmandApplication app; - public AvoidSpecificRoads(OsmandApplication app) { + private Map impassableRoads = new LinkedHashMap<>(); + + public AvoidSpecificRoads(final OsmandApplication app) { this.app = app; - } + for (LatLon latLon : app.getSettings().getImpassableRoadPoints()) { + impassableRoads.put(latLon, null); + } + if (app.isApplicationInitializing()) { + app.runInUIThread(new Runnable() { + @Override + public void run() { + app.getAppInitializer().addListener(new AppInitializer.AppInitializeListener() { + @Override + public void onProgress(AppInitializer init, AppInitializer.InitEvents event) { - public void initPreservedData() { - List impassibleRoads = app.getSettings().getImpassableRoadPoints(); - for (LatLon impassibleRoad : impassibleRoads) { - addImpassableRoad(null, impassibleRoad, false, true); + } + + @Override + public void onFinish(AppInitializer init) { + initRouteObjects(); + init.removeListener(this); + } + }); + } + }); + } else { + initRouteObjects(); } } - private List getImpassableRoads() { - if (impassableRoads == null) { - impassableRoads = app.getDefaultRoutingConfig().getImpassableRoads(); - } + public Map getImpassableRoads() { return impassableRoads; } - private ArrayAdapter createAdapter(final MapActivity ctx) { - final ArrayList points = new ArrayList<>(); - points.addAll(getImpassableRoads()); - final LatLon mapLocation = ctx.getMapLocation(); - return new ArrayAdapter(ctx, - R.layout.waypoint_reached, R.id.title, points) { + private void initRouteObjects() { + for (LatLon latLon : impassableRoads.keySet()) { + addImpassableRoad(null, latLon, false, true); + } + } + private ArrayAdapter createAdapter(final MapActivity ctx) { + final ArrayList points = new ArrayList<>(impassableRoads.keySet()); + final LatLon mapLocation = ctx.getMapLocation(); + return new ArrayAdapter(ctx, R.layout.waypoint_reached, R.id.title, points) { @NonNull @Override public View getView(final int position, View convertView, @NonNull ViewGroup parent) { @@ -69,30 +91,22 @@ public class AvoidSpecificRoads { if (v == null || v.findViewById(R.id.info_close) == null) { v = ctx.getLayoutInflater().inflate(R.layout.waypoint_reached, parent, false); } - final RouteDataObject obj = getItem(position); + final LatLon item = getItem(position); v.findViewById(R.id.all_points).setVisibility(View.GONE); - ((ImageView) v.findViewById(R.id.waypoint_icon)).setImageDrawable( - app.getUIUtilities().getThemedIcon(R.drawable.ic_action_road_works_dark)); - double dist = MapUtils.getDistance(mapLocation, MapUtils.get31LatitudeY(obj.getPoint31YTile(0)), - MapUtils.get31LongitudeX(obj.getPoint31XTile(0))); - ((TextView) v.findViewById(R.id.waypoint_dist)).setText(OsmAndFormatter.getFormattedDistance((float) dist, app)); - - ((TextView) v.findViewById(R.id.waypoint_text)).setText(getText(obj)); + ((ImageView) v.findViewById(R.id.waypoint_icon)) + .setImageDrawable(getIcon(R.drawable.ic_action_road_works_dark)); + ((TextView) v.findViewById(R.id.waypoint_dist)).setText(getDist(mapLocation, item)); + ((TextView) v.findViewById(R.id.waypoint_text)).setText(getText(item)); ImageButton remove = (ImageButton) v.findViewById(R.id.info_close); remove.setVisibility(View.VISIBLE); - remove.setImageDrawable(app.getUIUtilities().getThemedIcon( - R.drawable.ic_action_remove_dark)); + remove.setImageDrawable(getIcon(R.drawable.ic_action_remove_dark)); remove.setOnClickListener(new View.OnClickListener() { - @Override public void onClick(View v) { - remove(obj); - removeImpassableRoad(obj); + remove(item); + removeImpassableRoad(item); notifyDataSetChanged(); - RoutingHelper rh = app.getRoutingHelper(); - if (rh.isRouteCalculated() || rh.isRouteBeingCalculated()) { - rh.recalculateRouteDueToSettingsChange(); - } + recalculateRoute(); } }); return v; @@ -100,41 +114,73 @@ public class AvoidSpecificRoads { }; } - public void removeImpassableRoad(RouteDataObject obj) { - app.getSettings().removeImpassableRoad(getLocation(obj)); - app.getDefaultRoutingConfig().removeImpassableRoad(obj); + private Drawable getIcon(@DrawableRes int iconId) { + return app.getUIUtilities().getThemedIcon(iconId); } + private String getDist(@NonNull LatLon loc, @Nullable LatLon point) { + double dist = point == null ? 0 : MapUtils.getDistance(loc, point); + return OsmAndFormatter.getFormattedDistance((float) dist, app); + } - protected String getText(RouteDataObject obj) { - String name = RoutingHelper.formatStreetName(obj.getName(app.getSettings().MAP_PREFERRED_LOCALE.get(), - app.getSettings().MAP_TRANSLITERATE_NAMES.get()), - obj.getRef(app.getSettings().MAP_PREFERRED_LOCALE.get(), app.getSettings().MAP_TRANSLITERATE_NAMES.get(), true), - obj.getDestinationName(app.getSettings().MAP_PREFERRED_LOCALE.get(), app.getSettings().MAP_TRANSLITERATE_NAMES.get(), true), - app.getString(R.string.towards)); + private String getText(@Nullable LatLon point) { + if (point != null) { + RouteDataObject obj = impassableRoads.get(point); + if (obj != null) { + String locale = app.getSettings().MAP_PREFERRED_LOCALE.get(); + boolean transliterate = app.getSettings().MAP_TRANSLITERATE_NAMES.get(); + String name = RoutingHelper.formatStreetName( + obj.getName(locale, transliterate), + obj.getRef(locale, transliterate, true), + obj.getDestinationName(locale, transliterate, true), + app.getString(R.string.towards) + ); + if (!TextUtils.isEmpty(name)) { + return name; + } + } + } + return app.getString(R.string.shared_string_road); + } - return Algorithms.isEmpty(name) ? app.getString(R.string.shared_string_road) : name; + private void recalculateRoute() { + RoutingHelper rh = app.getRoutingHelper(); + if (rh.isRouteCalculated() || rh.isRouteBeingCalculated()) { + rh.recalculateRouteDueToSettingsChange(); + } + } + + private void removeImpassableRoad(LatLon latLon) { + app.getSettings().removeImpassableRoad(latLon); + RouteDataObject obj = impassableRoads.remove(latLon); + if (obj != null) { + app.getDefaultRoutingConfig().removeImpassableRoad(obj); + } + } + + public void removeImpassableRoad(RouteDataObject obj) { + removeImpassableRoad(getLocation(obj)); } public void showDialog(@NonNull final MapActivity mapActivity) { AlertDialog.Builder bld = new AlertDialog.Builder(mapActivity); bld.setTitle(R.string.impassable_road); - if (getImpassableRoads().size() == 0) { + if (impassableRoads.isEmpty()) { bld.setMessage(R.string.avoid_roads_msg); } else { - final ArrayAdapter listAdapter = createAdapter(mapActivity); + final ArrayAdapter listAdapter = createAdapter(mapActivity); bld.setAdapter(listAdapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - RouteDataObject obj = getImpassableRoads().get(which); - double lat = MapUtils.get31LatitudeY(obj.getPoint31YTile(0)); - double lon = MapUtils.get31LongitudeX(obj.getPoint31XTile(0)); - showOnMap(mapActivity, lat, lon, getText(obj), dialog); + LatLon point = listAdapter.getItem(which); + if (point != null) { + showOnMap(mapActivity, point.getLatitude(), point.getLongitude(), getText(point)); + } + dialog.dismiss(); } }); } - bld.setPositiveButton(R.string.shared_string_select_on_map, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { @@ -145,17 +191,14 @@ public class AvoidSpecificRoads { bld.show(); } - private void selectFromMap(final MapActivity mapActivity) { ContextMenuLayer cm = mapActivity.getMapLayers().getContextMenuLayer(); cm.setSelectOnMap(new CallbackWithObject() { - @Override public boolean processResult(LatLon result) { addImpassableRoad(mapActivity, result, true, false); return true; } - }); } @@ -193,16 +236,14 @@ public class AvoidSpecificRoads { } } - public void replaceImpassableRoad(final MapActivity activity, final RouteDataObject currentObject, - final LatLon loc, final boolean showDialog, + public void replaceImpassableRoad(final MapActivity activity, + final RouteDataObject currentObject, + final LatLon newLoc, + final boolean showDialog, final AvoidSpecificRoadsCallback callback) { - - LatLon latLon = getLocation(currentObject); - app.getSettings().moveImpassableRoad(latLon, loc); - final Location ll = new Location(""); - ll.setLatitude(loc.getLatitude()); - ll.setLongitude(loc.getLongitude()); + ll.setLatitude(newLoc.getLatitude()); + ll.setLongitude(newLoc.getLongitude()); ApplicationMode appMode = app.getRoutingHelper().getAppMode(); app.getLocationProvider().getRouteSegment(ll, appMode, new ResultMatcher() { @@ -215,8 +256,11 @@ public class AvoidSpecificRoads { callback.onAddImpassableRoad(false, null); } } else { + final LatLon oldLoc = getLocation(currentObject); + app.getSettings().moveImpassableRoad(oldLoc, newLoc); + impassableRoads.remove(oldLoc); app.getDefaultRoutingConfig().removeImpassableRoad(currentObject); - addImpassableRoadInternal(object, ll, showDialog, activity, loc); + addImpassableRoadInternal(object, ll, showDialog, activity, newLoc); if (callback != null) { callback.onAddImpassableRoad(true, object); @@ -237,16 +281,15 @@ public class AvoidSpecificRoads { boolean showDialog, @Nullable MapActivity activity, @NonNull LatLon loc) { - if (!app.getDefaultRoutingConfig().addImpassableRoad(object, ll)) { + if (app.getDefaultRoutingConfig().addImpassableRoad(object, ll)) { + impassableRoads.put(loc, object); + } else { LatLon location = getLocation(object); if (location != null) { - app.getSettings().removeImpassableRoad(getLocation(object)); + app.getSettings().removeImpassableRoad(location); } } - RoutingHelper rh = app.getRoutingHelper(); - if (rh.isRouteCalculated() || rh.isRouteBeingCalculated()) { - rh.recalculateRouteDueToSettingsChange(); - } + recalculateRoute(); if (activity != null) { if (showDialog) { showDialog(activity); @@ -259,21 +302,14 @@ public class AvoidSpecificRoads { } } - private void showOnMap(MapActivity ctx, double lat, double lon, String name, - DialogInterface dialog) { - AnimateDraggingMapThread thread = ctx.getMapView().getAnimatedDraggingThread(); - int fZoom = ctx.getMapView().getZoom() < 15 ? 15 : ctx.getMapView().getZoom(); - if (thread.isAnimating()) { - ctx.getMapView().setIntZoom(fZoom); - ctx.getMapView().setLatLon(lat, lon); - } else { - thread.startMoving(lat, lon, fZoom, true); - } - ctx.getContextMenu().show(new LatLon(lat, lon), new PointDescription("", name), null); - dialog.dismiss(); + private void showOnMap(MapActivity ctx, double lat, double lon, String name) { + int zoom = ctx.getMapView().getZoom() < 15 ? 15 : ctx.getMapView().getZoom(); + PointDescription pd = new PointDescription("", name); + ctx.getMyApplication().getSettings().setMapLocationToShow(lat, lon, zoom, pd, false, null); + MapActivity.launchMapActivityMoveToTop(ctx); } - private LatLon getLocation(RouteDataObject object) { + public LatLon getLocation(RouteDataObject object) { Location location = app.getDefaultRoutingConfig().getImpassableRoadLocations().get(object.getId()); return location == null ? null : new LatLon(location.getLatitude(), location.getLongitude()); } diff --git a/OsmAnd/src/net/osmand/plus/views/ImpassableRoadsLayer.java b/OsmAnd/src/net/osmand/plus/views/ImpassableRoadsLayer.java index f3d382bd98..7bef99b217 100644 --- a/OsmAnd/src/net/osmand/plus/views/ImpassableRoadsLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/ImpassableRoadsLayer.java @@ -3,12 +3,13 @@ package net.osmand.plus.views; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; +import android.graphics.ColorMatrix; +import android.graphics.ColorMatrixColorFilter; import android.graphics.Paint; import android.graphics.PointF; import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import net.osmand.Location; import net.osmand.binary.RouteDataObject; import net.osmand.data.LatLon; import net.osmand.data.PointDescription; @@ -16,27 +17,25 @@ import net.osmand.data.RotatedTileBox; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.helpers.AvoidSpecificRoads; import net.osmand.plus.helpers.AvoidSpecificRoads.AvoidSpecificRoadsCallback; import net.osmand.plus.views.ContextMenuLayer.ApplyMovedObjectCallback; -import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Set; public class ImpassableRoadsLayer extends OsmandMapLayer implements ContextMenuLayer.IContextMenuProvider, ContextMenuLayer.IMoveObjectProvider { - private static final int startZoom = 10; - private final MapActivity activity; - private Bitmap roadWorkIcon; - private Paint paint; - private Map impassableRoadLocations; - private List impassableRoads; + private static final int START_ZOOM = 10; + private final MapActivity activity; + private AvoidSpecificRoads avoidSpecificRoads; private ContextMenuLayer contextMenuLayer; - private Set storedRoadDataObjects; + private Bitmap roadWorkIcon; + private Paint activePaint; + private Paint paint; public ImpassableRoadsLayer(MapActivity activity) { this.activity = activity; @@ -44,76 +43,55 @@ public class ImpassableRoadsLayer extends OsmandMapLayer implements @Override public void initLayer(OsmandMapTileView view) { - roadWorkIcon = BitmapFactory.decodeResource(view.getResources(), R.drawable.map_pin_avoid_road); - paint = new Paint(); - + avoidSpecificRoads = activity.getMyApplication().getAvoidSpecificRoads(); contextMenuLayer = view.getLayerByClass(ContextMenuLayer.class); - - List impassibleRoads = activity.getMyApplication().getSettings().getImpassableRoadPoints(); - storedRoadDataObjects = new HashSet<>(impassibleRoads.size()); - for (LatLon impassibleRoad : impassibleRoads) { - storedRoadDataObjects.add(new StoredRoadDataObject(impassibleRoad)); - } + roadWorkIcon = BitmapFactory.decodeResource(view.getResources(), R.drawable.map_pin_avoid_road); + activePaint = new Paint(); + ColorMatrix matrix = new ColorMatrix(); + matrix.setSaturation(0); + paint = new Paint(); + paint.setColorFilter(new ColorMatrixColorFilter(matrix)); } @Override public void onDraw(Canvas canvas, RotatedTileBox tileBox, DrawSettings settings) { if (contextMenuLayer.getMoveableObject() instanceof RouteDataObject) { PointF pf = contextMenuLayer.getMovableCenterPoint(tileBox); - drawPoint(canvas, pf.x, pf.y); + drawPoint(canvas, pf.x, pf.y, true); } } @Override public void onPrepareBufferImage(Canvas canvas, RotatedTileBox tileBox, DrawSettings settings) { - if (tileBox.getZoom() >= startZoom) { - for (long id : getImpassableRoadLocations().keySet()) { - if (contextMenuLayer.getMoveableObject() instanceof RouteDataObject) { + if (tileBox.getZoom() >= START_ZOOM) { + for (Map.Entry entry : avoidSpecificRoads.getImpassableRoads().entrySet()) { + LatLon location = entry.getKey(); + RouteDataObject road = entry.getValue(); + if (road != null && contextMenuLayer.getMoveableObject() instanceof RouteDataObject) { RouteDataObject object = (RouteDataObject) contextMenuLayer.getMoveableObject(); - if (object.id == id) { + if (object.id == road.id) { continue; } } - Location location = getImpassableRoadLocations().get(id); final double latitude = location.getLatitude(); final double longitude = location.getLongitude(); if (tileBox.containsLatLon(latitude, longitude)) { - drawPoint(canvas, tileBox, latitude, longitude); - } - } - for (StoredRoadDataObject storedRoadDataObject : storedRoadDataObjects) { - final LatLon latLon = storedRoadDataObject.getLatLon(); - if (tileBox.containsLatLon(latLon)) { - drawPoint(canvas, tileBox, latLon.getLatitude(), latLon.getLongitude()); + drawPoint(canvas, tileBox, latitude, longitude, road != null); } } } } - private void drawPoint(Canvas canvas, RotatedTileBox tileBox, double latitude, double longitude) { + private void drawPoint(Canvas canvas, RotatedTileBox tileBox, double latitude, double longitude, boolean active) { float x = tileBox.getPixXFromLatLon(latitude, longitude); float y = tileBox.getPixYFromLatLon(latitude, longitude); - drawPoint(canvas, x, y); + drawPoint(canvas, x, y, active); } - private void drawPoint(Canvas canvas, float x, float y) { + private void drawPoint(Canvas canvas, float x, float y, boolean active) { float left = x - roadWorkIcon.getWidth() / 2; float top = y - roadWorkIcon.getHeight(); - canvas.drawBitmap(roadWorkIcon, left, top, paint); - } - - private Map getImpassableRoadLocations() { - if (impassableRoadLocations == null) { - impassableRoadLocations = activity.getMyApplication().getDefaultRoutingConfig().getImpassableRoadLocations(); - } - return impassableRoadLocations; - } - - private List getImpassableRoads() { - if (impassableRoads == null) { - impassableRoads = activity.getMyApplication().getDefaultRoutingConfig().getImpassableRoads(); - } - return impassableRoads; + canvas.drawBitmap(roadWorkIcon, left, top, active ? activePaint : paint); } @Override @@ -128,7 +106,7 @@ public class ImpassableRoadsLayer extends OsmandMapLayer implements private int getRadiusPoi(RotatedTileBox tb) { int r; - if (tb.getZoom() < startZoom) { + if (tb.getZoom() < START_ZOOM) { r = 0; } else { r = 15; @@ -162,15 +140,16 @@ public class ImpassableRoadsLayer extends OsmandMapLayer implements @Override public void collectObjectsFromPoint(PointF point, RotatedTileBox tileBox, List o, boolean unknownLocation) { - if (tileBox.getZoom() >= startZoom) { + if (tileBox.getZoom() >= START_ZOOM) { int ex = (int) point.x; int ey = (int) point.y; int compare = getRadiusPoi(tileBox); int radius = compare * 3 / 2; - for (RouteDataObject road : getImpassableRoads()) { - Location location = getImpassableRoadLocations().get(road.getId()); - if (location != null) { + for (Map.Entry entry : avoidSpecificRoads.getImpassableRoads().entrySet()) { + LatLon location = entry.getKey(); + RouteDataObject road = entry.getValue(); + if (location != null && road != null) { int x = (int) tileBox.getPixXFromLatLon(location.getLatitude(), location.getLongitude()); int y = (int) tileBox.getPixYFromLatLon(location.getLatitude(), location.getLongitude()); if (calculateBelongs(ex, ey, x, y, compare)) { @@ -180,18 +159,12 @@ public class ImpassableRoadsLayer extends OsmandMapLayer implements } } } - if (!storedRoadDataObjects.isEmpty()) { - activity.getMyApplication().getAvoidSpecificRoads().initPreservedData(); - storedRoadDataObjects.clear(); - } } @Override public LatLon getObjectLocation(Object o) { if (o instanceof RouteDataObject) { - RouteDataObject route = (RouteDataObject) o; - Location location = impassableRoadLocations.get(route.getId()); - return new LatLon(location.getLatitude(), location.getLongitude()); + return avoidSpecificRoads.getLocation((RouteDataObject) o); } return null; } @@ -232,16 +205,4 @@ public class ImpassableRoadsLayer extends OsmandMapLayer implements }); } } - - private static class StoredRoadDataObject { - private final LatLon latLon; - - private StoredRoadDataObject(LatLon latLon) { - this.latLon = latLon; - } - - public LatLon getLatLon() { - return latLon; - } - } } From e8d803a2ebf28e02b7fa854e119684a5f04cb18f Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Fri, 14 Sep 2018 17:30:02 +0300 Subject: [PATCH 3/4] Fix #3644 --- .../osmand/plus/CurrentPositionHelper.java | 22 +++++++++++-------- .../osmand/plus/OsmAndLocationProvider.java | 8 ++++--- .../plus/helpers/AvoidSpecificRoads.java | 4 ++-- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/CurrentPositionHelper.java b/OsmAnd/src/net/osmand/plus/CurrentPositionHelper.java index a2c3e3df36..5e452b4b4f 100644 --- a/OsmAnd/src/net/osmand/plus/CurrentPositionHelper.java +++ b/OsmAnd/src/net/osmand/plus/CurrentPositionHelper.java @@ -49,13 +49,15 @@ public class CurrentPositionHelper { this.app = app; } - public boolean getRouteSegment(Location loc, @Nullable ApplicationMode appMode, + public boolean getRouteSegment(Location loc, + @Nullable ApplicationMode appMode, + boolean filterFrequentRequests, ResultMatcher result) { - return scheduleRouteSegmentFind(loc, false, true, null, result, appMode); + return scheduleRouteSegmentFind(loc, false, true, filterFrequentRequests, null, result, appMode); } - + public boolean getGeocodingResult(Location loc, ResultMatcher result) { - return scheduleRouteSegmentFind(loc, false, false, result, null, null); + return scheduleRouteSegmentFind(loc, false, false, true, result, null, null); } public RouteDataObject getLastKnownRouteSegment(Location loc) { @@ -68,12 +70,12 @@ public class CurrentPositionHelper { return r; } if (r == null) { - scheduleRouteSegmentFind(loc, true, false, null, null, null); + scheduleRouteSegmentFind(loc, true, false, true, null, null, null); return null; } double d = getOrthogonalDistance(r, loc); if (d > 15) { - scheduleRouteSegmentFind(loc, true, false, null, null, null); + scheduleRouteSegmentFind(loc, true, false, true, null, null, null); } if (d < 70) { return r; @@ -90,6 +92,7 @@ public class CurrentPositionHelper { private boolean scheduleRouteSegmentFind(final Location loc, final boolean storeFound, final boolean allowEmptyNames, + final boolean filterFrequentRequests, @Nullable final ResultMatcher geoCoding, @Nullable final ResultMatcher result, @Nullable final ApplicationMode appMode) { @@ -106,7 +109,7 @@ public class CurrentPositionHelper { singleThreadExecutor.submit(new Runnable() { @Override public void run() { - processGeocoding(loc, geoCoding, storeFound, allowEmptyNames, result, appMode, request, finalRequestNumber); + processGeocoding(loc, geoCoding, storeFound, allowEmptyNames, result, appMode, request, finalRequestNumber, filterFrequentRequests); } }); res = true; @@ -155,9 +158,10 @@ public class CurrentPositionHelper { @Nullable final ResultMatcher result, @Nullable ApplicationMode appMode, int request, - @NonNull AtomicInteger requestNumber) { + @NonNull AtomicInteger requestNumber, + boolean filterFrequentRequests) { - if (request != requestNumber.get()) { + if (filterFrequentRequests && request != requestNumber.get()) { return; } diff --git a/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java b/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java index eb4cbe6617..f116787834 100644 --- a/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java +++ b/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java @@ -854,10 +854,12 @@ public class OsmAndLocationProvider implements SensorEventListener { public RouteDataObject getLastKnownRouteSegment() { return currentPositionHelper.getLastKnownRouteSegment(getLastKnownLocation()); } - - public boolean getRouteSegment(net.osmand.Location loc, @Nullable ApplicationMode appMode, + + public boolean getRouteSegment(net.osmand.Location loc, + @Nullable ApplicationMode appMode, + boolean filterFrequentRequests, ResultMatcher result) { - return currentPositionHelper.getRouteSegment(loc, appMode, result); + return currentPositionHelper.getRouteSegment(loc, appMode, filterFrequentRequests, result); } public boolean getGeocodingResult(net.osmand.Location loc, ResultMatcher result) { diff --git a/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java b/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java index ed15c266b9..adc4b49333 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java +++ b/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java @@ -211,7 +211,7 @@ public class AvoidSpecificRoads { ll.setLongitude(loc.getLongitude()); ApplicationMode appMode = app.getRoutingHelper().getAppMode(); - app.getLocationProvider().getRouteSegment(ll, appMode, new ResultMatcher() { + app.getLocationProvider().getRouteSegment(ll, appMode, false, new ResultMatcher() { @Override public boolean publish(RouteDataObject object) { @@ -246,7 +246,7 @@ public class AvoidSpecificRoads { ll.setLongitude(newLoc.getLongitude()); ApplicationMode appMode = app.getRoutingHelper().getAppMode(); - app.getLocationProvider().getRouteSegment(ll, appMode, new ResultMatcher() { + app.getLocationProvider().getRouteSegment(ll, appMode, false, new ResultMatcher() { @Override public boolean publish(RouteDataObject object) { From 6c27d652fe35a9955ed00f4ed51211ec72f1c46e Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Mon, 17 Sep 2018 12:08:41 +0300 Subject: [PATCH 4/4] Rename parameter --- .../src/net/osmand/plus/CurrentPositionHelper.java | 12 ++++++------ .../src/net/osmand/plus/OsmAndLocationProvider.java | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/CurrentPositionHelper.java b/OsmAnd/src/net/osmand/plus/CurrentPositionHelper.java index 5e452b4b4f..3be6a90d69 100644 --- a/OsmAnd/src/net/osmand/plus/CurrentPositionHelper.java +++ b/OsmAnd/src/net/osmand/plus/CurrentPositionHelper.java @@ -51,9 +51,9 @@ public class CurrentPositionHelper { public boolean getRouteSegment(Location loc, @Nullable ApplicationMode appMode, - boolean filterFrequentRequests, + boolean cancelPreviousSearch, ResultMatcher result) { - return scheduleRouteSegmentFind(loc, false, true, filterFrequentRequests, null, result, appMode); + return scheduleRouteSegmentFind(loc, false, true, cancelPreviousSearch, null, result, appMode); } public boolean getGeocodingResult(Location loc, ResultMatcher result) { @@ -92,7 +92,7 @@ public class CurrentPositionHelper { private boolean scheduleRouteSegmentFind(final Location loc, final boolean storeFound, final boolean allowEmptyNames, - final boolean filterFrequentRequests, + final boolean cancelPreviousSearch, @Nullable final ResultMatcher geoCoding, @Nullable final ResultMatcher result, @Nullable final ApplicationMode appMode) { @@ -109,7 +109,7 @@ public class CurrentPositionHelper { singleThreadExecutor.submit(new Runnable() { @Override public void run() { - processGeocoding(loc, geoCoding, storeFound, allowEmptyNames, result, appMode, request, finalRequestNumber, filterFrequentRequests); + processGeocoding(loc, geoCoding, storeFound, allowEmptyNames, result, appMode, request, finalRequestNumber, cancelPreviousSearch); } }); res = true; @@ -159,9 +159,9 @@ public class CurrentPositionHelper { @Nullable ApplicationMode appMode, int request, @NonNull AtomicInteger requestNumber, - boolean filterFrequentRequests) { + boolean cancelPreviousSearch) { - if (filterFrequentRequests && request != requestNumber.get()) { + if (cancelPreviousSearch && request != requestNumber.get()) { return; } diff --git a/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java b/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java index f116787834..53ef5455ac 100644 --- a/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java +++ b/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java @@ -857,9 +857,9 @@ public class OsmAndLocationProvider implements SensorEventListener { public boolean getRouteSegment(net.osmand.Location loc, @Nullable ApplicationMode appMode, - boolean filterFrequentRequests, + boolean cancelPreviousSearch, ResultMatcher result) { - return currentPositionHelper.getRouteSegment(loc, appMode, filterFrequentRequests, result); + return currentPositionHelper.getRouteSegment(loc, appMode, cancelPreviousSearch, result); } public boolean getGeocodingResult(net.osmand.Location loc, ResultMatcher result) {