From bd0d4a9c9e4f1a660db52ff32a338c6bbda262f6 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Tue, 18 Feb 2020 14:12:40 +0200 Subject: [PATCH 1/7] Avoid saved roads initial commit --- .../osmand/router/RoutingConfiguration.java | 21 +++--- .../src/net/osmand/plus/AppInitializer.java | 2 +- .../src/net/osmand/plus/OsmandSettings.java | 10 +-- .../plus/helpers/AvoidSpecificRoads.java | 64 ++++++++++--------- .../plus/mapcontextmenu/MenuController.java | 6 +- .../ImpassibleRoadsMenuController.java | 14 ++-- .../MapRouteInfoMenu.java | 23 +++---- .../plus/views/ImpassableRoadsLayer.java | 35 +++++----- 8 files changed, 91 insertions(+), 84 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 0a1b87e6e5..4a84a9c6df 100644 --- a/OsmAnd-java/src/main/java/net/osmand/router/RoutingConfiguration.java +++ b/OsmAnd-java/src/main/java/net/osmand/router/RoutingConfiguration.java @@ -1,6 +1,5 @@ package net.osmand.router; -import net.osmand.Location; import net.osmand.PlatformUtil; import net.osmand.binary.RouteDataObject; import net.osmand.router.GeneralRouter.GeneralRouterProfile; @@ -13,8 +12,10 @@ import org.xmlpull.v1.XmlPullParserException; import java.io.IOException; import java.io.InputStream; -import java.util.HashMap; +import java.util.ArrayList; +import java.util.HashSet; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import java.util.Stack; @@ -55,7 +56,7 @@ public class RoutingConfiguration { private String defaultRouter = ""; private Map routers = new LinkedHashMap<>(); private Map attributes = new LinkedHashMap<>(); - private HashMap impassableRoadLocations = new HashMap<>(); + private List impassableRoadLocations = new ArrayList<>(); public Builder() { @@ -95,7 +96,7 @@ public class RoutingConfiguration { i.initialDirection = direction; i.recalculateDistance = parseSilentFloat(getAttribute(i.router, "recalculateDistanceHelp"), i.recalculateDistance) ; i.heuristicCoefficient = parseSilentFloat(getAttribute(i.router, "heuristicCoefficient"), i.heuristicCoefficient); - i.router.addImpassableRoads(impassableRoadLocations.keySet()); + i.router.addImpassableRoads(new HashSet<>(impassableRoadLocations)); i.ZOOM_TO_LOAD_TILES = parseSilentInt(getAttribute(i.router, "zoomToLoadTiles"), i.ZOOM_TO_LOAD_TILES); int desirable = parseSilentInt(getAttribute(i.router, "memoryLimitInMB"), 0); if(desirable != 0) { @@ -111,13 +112,13 @@ public class RoutingConfiguration { return i; } - public Map getImpassableRoadLocations() { + public List getImpassableRoadLocations() { return impassableRoadLocations; } - public boolean addImpassableRoad(RouteDataObject route, Location location) { - if (!impassableRoadLocations.containsKey(route.id)){ - impassableRoadLocations.put(route.id, location); + public boolean addImpassableRoad(RouteDataObject route) { + if (!impassableRoadLocations.contains(route.id)) { + impassableRoadLocations.add(route.id); return true; } return false; @@ -159,8 +160,8 @@ public class RoutingConfiguration { return routers; } - public void removeImpassableRoad(RouteDataObject obj) { - impassableRoadLocations.remove(obj.id); + public void removeImpassableRoad(long routeId) { + impassableRoadLocations.remove(routeId); } } diff --git a/OsmAnd/src/net/osmand/plus/AppInitializer.java b/OsmAnd/src/net/osmand/plus/AppInitializer.java index e786c80f12..bf9720c037 100644 --- a/OsmAnd/src/net/osmand/plus/AppInitializer.java +++ b/OsmAnd/src/net/osmand/plus/AppInitializer.java @@ -739,7 +739,7 @@ public class AppInitializer implements IProgress { notifyEvent(InitEvents.RESTORE_BACKUPS); app.mapMarkersHelper.syncAllGroupsAsync(); app.searchUICore.initSearchUICore(); - app.avoidSpecificRoads.initRouteObjects(); + app.avoidSpecificRoads.initRouteObjects(false); checkLiveUpdatesAlerts(); diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index 6a7e45a409..89bfd57513 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -42,6 +42,7 @@ import net.osmand.plus.api.SettingsAPI; import net.osmand.plus.api.SettingsAPI.SettingsEditor; import net.osmand.plus.api.SettingsAPIImpl; import net.osmand.plus.dialogs.RateUsBottomSheetDialogFragment; +import net.osmand.plus.helpers.AvoidSpecificRoads.AvoidRoadInfo; import net.osmand.plus.helpers.SearchHistoryHelper; import net.osmand.plus.mapillary.MapillaryPlugin; import net.osmand.plus.mapmarkers.CoordinateInputFormats.Format; @@ -2627,6 +2628,7 @@ public class OsmandSettings { private static final String IMPASSABLE_ROAD_POINTS = "impassable_road_points"; private static final String IMPASSABLE_ROADS_DESCRIPTIONS = "impassable_roads_descriptions"; + private static final String IMPASSABLE_ROADS_IDS = "impassable_roads_ids"; private ImpassableRoadsStorage mImpassableRoadsStorage = new ImpassableRoadsStorage(); public void backupPointToStart() { @@ -2989,11 +2991,11 @@ public class OsmandSettings { return settingsAPI.edit(globalPreferences).putInt(POINT_NAVIGATE_ROUTE, NAVIGATE).commit(); } - public List getImpassableRoadPoints() { - return mImpassableRoadsStorage.getPoints(); + public List getImpassableRoadPoints() { + return mImpassableRoadsStorage.getImpassableRoadsInfo(); } - public boolean addImpassableRoad(double latitude, double longitude) { - return mImpassableRoadsStorage.insertPoint(latitude, longitude, null, 0); + public boolean addImpassableRoad(AvoidRoadInfo avoidRoadInfo) { + return mImpassableRoadsStorage.addImpassableRoadInfo(avoidRoadInfo); } public boolean removeImpassableRoad(int index) { diff --git a/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java b/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java index a815efedb4..00b811ad25 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java +++ b/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java @@ -50,22 +50,24 @@ public class AvoidSpecificRoads { private OsmandApplication app; - private Map impassableRoads = new LinkedHashMap<>(); + private Map impassableRoads = new LinkedHashMap<>(); public AvoidSpecificRoads(final OsmandApplication app) { this.app = app; - for (LatLon latLon : app.getSettings().getImpassableRoadPoints()) { - impassableRoads.put(latLon, null); + for (AvoidRoadInfo avoidRoadInfo : app.getSettings().getImpassableRoadPoints()) { + impassableRoads.put(new LatLon(avoidRoadInfo.lat, avoidRoadInfo.lon), avoidRoadInfo); } } - public Map getImpassableRoads() { + public Map getImpassableRoads() { return impassableRoads; } - public void initRouteObjects() { - for (LatLon latLon : impassableRoads.keySet()) { - addImpassableRoad(null, latLon, false, true); + public void initRouteObjects(boolean force) { + for (Map.Entry entry : impassableRoads.entrySet()) { + if (force || entry.getValue().id == 0) { + addImpassableRoad(null, entry.getKey(), false, true); + } } } @@ -117,25 +119,15 @@ public class AvoidSpecificRoads { public 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; - } + AvoidRoadInfo obj = impassableRoads.get(point); + if (obj != null && !TextUtils.isEmpty(obj.name)) { + return obj.name; } } return app.getString(R.string.shared_string_road); } - public String getText(@Nullable RouteDataObject obj) { + public String getRoadName(@Nullable RouteDataObject obj) { if (obj != null) { String locale = app.getSettings().MAP_PREFERRED_LOCALE.get(); boolean transliterate = app.getSettings().MAP_TRANSLITERATE_NAMES.get(); @@ -347,21 +339,35 @@ public class AvoidSpecificRoads { MapActivity.launchMapActivityMoveToTop(ctx); } - public LatLon getLocation(RouteDataObject object) { - Location location = null; + public LatLon getLocation(AvoidRoadInfo avoidRoadInfo) { for (RoutingConfiguration.Builder builder : app.getAllRoutingConfigs()) { - location = builder.getImpassableRoadLocations().get(object.getId()); - if (location != null) { - break; + if (builder.getImpassableRoadLocations().contains(avoidRoadInfo.id)) { + return new LatLon(avoidRoadInfo.lat, avoidRoadInfo.lon); } } - return location == null ? null : new LatLon(location.getLatitude(), location.getLongitude()); + return null; } public interface AvoidSpecificRoadsCallback { - void onAddImpassableRoad(boolean success, RouteDataObject newObject); + void onAddImpassableRoad(boolean success, AvoidRoadInfo avoidRoadInfo); boolean isCancelled(); } -} + + private AvoidRoadInfo createAvoidRoadInfo(@Nullable RouteDataObject object, double lat, double lon) { + AvoidRoadInfo avoidRoadInfo = new AvoidRoadInfo(); + avoidRoadInfo.id = object != null ? object.id : 0; + avoidRoadInfo.lat = lat; + avoidRoadInfo.lon = lon; + avoidRoadInfo.name = getRoadName(object); + return avoidRoadInfo; + } + + public static class AvoidRoadInfo { + public long id; + public double lat; + public double lon; + public String name; + } +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuController.java index 00012480a1..8d25fae1bb 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuController.java @@ -22,7 +22,6 @@ import net.osmand.PlatformUtil; import net.osmand.aidl.AidlMapPointWrapper; import net.osmand.binary.BinaryMapDataObject; import net.osmand.binary.BinaryMapIndexReader.TagValuePair; -import net.osmand.binary.RouteDataObject; import net.osmand.data.Amenity; import net.osmand.data.FavouritePoint; import net.osmand.data.LatLon; @@ -45,6 +44,7 @@ import net.osmand.plus.download.DownloadActivityType; import net.osmand.plus.download.DownloadIndexesThread; import net.osmand.plus.download.DownloadValidationManager; import net.osmand.plus.download.IndexItem; +import net.osmand.plus.helpers.AvoidSpecificRoads; import net.osmand.plus.helpers.SearchHistoryHelper; import net.osmand.plus.mapcontextmenu.MenuBuilder.CollapsableView; import net.osmand.plus.mapcontextmenu.MenuBuilder.CollapseExpandListener; @@ -220,8 +220,8 @@ public abstract class MenuController extends BaseMenuController implements Colla } else if (pointDescription.isMyLocation()) { menuController = new MyLocationMenuController(mapActivity, pointDescription); } - } else if (object instanceof RouteDataObject) { - menuController = new ImpassibleRoadsMenuController(mapActivity, pointDescription, (RouteDataObject) object); + } else if (object instanceof AvoidSpecificRoads.AvoidRoadInfo) { + menuController = new ImpassibleRoadsMenuController(mapActivity, pointDescription, (AvoidSpecificRoads.AvoidRoadInfo) object); } else if (object instanceof RenderedObject) { menuController = new RenderedObjectMenuController(mapActivity, pointDescription, (RenderedObject) object); } else if (object instanceof MapillaryImage) { diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/ImpassibleRoadsMenuController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/ImpassibleRoadsMenuController.java index 5a48de2998..7c78eeface 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/ImpassibleRoadsMenuController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/ImpassibleRoadsMenuController.java @@ -4,24 +4,24 @@ import android.graphics.drawable.Drawable; import android.support.annotation.NonNull; import android.support.v4.content.ContextCompat; -import net.osmand.binary.RouteDataObject; import net.osmand.data.PointDescription; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.helpers.AvoidSpecificRoads.AvoidRoadInfo; import net.osmand.plus.mapcontextmenu.MenuBuilder; import net.osmand.plus.mapcontextmenu.MenuController; import net.osmand.plus.routing.RoutingHelper; public class ImpassibleRoadsMenuController extends MenuController { - private RouteDataObject route; + private AvoidRoadInfo avoidRoadInfo; public ImpassibleRoadsMenuController(@NonNull MapActivity mapActivity, @NonNull PointDescription pointDescription, - @NonNull RouteDataObject route) { + @NonNull AvoidRoadInfo avoidRoadInfo) { super(new MenuBuilder(mapActivity), pointDescription, mapActivity); - this.route = route; + this.avoidRoadInfo = avoidRoadInfo; final OsmandApplication app = mapActivity.getMyApplication(); leftTitleButtonController = new TitleButtonController() { @Override @@ -29,7 +29,7 @@ public class ImpassibleRoadsMenuController extends MenuController { MapActivity activity = getMapActivity(); if (activity != null) { app.getAvoidSpecificRoads().removeImpassableRoad( - ImpassibleRoadsMenuController.this.route); + ImpassibleRoadsMenuController.this.avoidRoadInfo); RoutingHelper rh = app.getRoutingHelper(); if (rh.isRouteCalculated() || rh.isRouteBeingCalculated()) { rh.recalculateRouteDueToSettingsChange(); @@ -44,12 +44,12 @@ public class ImpassibleRoadsMenuController extends MenuController { @Override protected void setObject(Object object) { - route = (RouteDataObject) object; + avoidRoadInfo = (AvoidRoadInfo) object; } @Override protected Object getObject() { - return route; + return avoidRoadInfo; } @NonNull diff --git a/OsmAnd/src/net/osmand/plus/routepreparationmenu/MapRouteInfoMenu.java b/OsmAnd/src/net/osmand/plus/routepreparationmenu/MapRouteInfoMenu.java index a59fbe8a48..acf76e74dd 100644 --- a/OsmAnd/src/net/osmand/plus/routepreparationmenu/MapRouteInfoMenu.java +++ b/OsmAnd/src/net/osmand/plus/routepreparationmenu/MapRouteInfoMenu.java @@ -38,7 +38,6 @@ import net.osmand.Location; import net.osmand.PlatformUtil; import net.osmand.StateChangedListener; import net.osmand.ValueHolder; -import net.osmand.binary.RouteDataObject; import net.osmand.data.FavouritePoint; import net.osmand.data.LatLon; import net.osmand.data.PointDescription; @@ -67,6 +66,7 @@ import net.osmand.plus.base.ContextMenuFragment.MenuState; import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.helpers.GpxUiHelper; import net.osmand.plus.helpers.WaypointHelper; +import net.osmand.plus.helpers.AvoidSpecificRoads.AvoidRoadInfo; import net.osmand.plus.mapcontextmenu.other.TrackDetailsMenuFragment; import net.osmand.plus.mapmarkers.MapMarkerSelectionFragment; import net.osmand.plus.poi.PoiUIFilter; @@ -110,13 +110,13 @@ import org.apache.commons.logging.Log; import java.io.IOException; import java.lang.ref.WeakReference; import java.util.ArrayList; +import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.Stack; -import java.util.TreeMap; public class MapRouteInfoMenu implements IRouteInformationListener, CardListener, FavoritesListener { @@ -1204,7 +1204,7 @@ public class MapRouteInfoMenu implements IRouteInformationListener, CardListener final LinearLayout item = createToolbarOptionView(false, null, -1, -1, null); if (item != null) { item.findViewById(R.id.route_option_container).setVisibility(View.GONE); - Map impassableRoads = new TreeMap<>(); + Map impassableRoads = new HashMap<>(); if (parameter instanceof AvoidRoadsRoutingParameter) { impassableRoads = app.getAvoidSpecificRoads().getImpassableRoads(); } @@ -1232,22 +1232,19 @@ public class MapRouteInfoMenu implements IRouteInformationListener, CardListener return avoidedParameters; } - private void createImpassableRoadsItems(MapActivity mapActivity, Map impassableRoads, final LocalRoutingParameter parameter, final RouteMenuAppModes mode, final LinearLayout item) { - OsmandApplication app = mapActivity.getMyApplication(); - Iterator it = impassableRoads.values().iterator(); + private void createImpassableRoadsItems(MapActivity mapActivity, Map impassableRoads, + final LocalRoutingParameter parameter, final RouteMenuAppModes mode, final LinearLayout item) { + Iterator it = impassableRoads.values().iterator(); while (it.hasNext()) { - final RouteDataObject routeDataObject = it.next(); - final View container = createToolbarSubOptionView(false, app.getAvoidSpecificRoads().getText(routeDataObject), R.drawable.ic_action_remove_dark, !it.hasNext(), new OnClickListener() { + final AvoidRoadInfo avoidRoadInfo = it.next(); + final View container = createToolbarSubOptionView(false, avoidRoadInfo.name, R.drawable.ic_action_remove_dark, !it.hasNext(), new OnClickListener() { @Override public void onClick(View v) { MapActivity mapActivity = getMapActivity(); if (mapActivity != null) { OsmandApplication app = mapActivity.getMyApplication(); - RoutingHelper routingHelper = app.getRoutingHelper(); - if (routeDataObject != null) { - app.getAvoidSpecificRoads().removeImpassableRoad(routeDataObject); - } - routingHelper.recalculateRouteDueToSettingsChange(); + app.getAvoidSpecificRoads().removeImpassableRoad(avoidRoadInfo); + app.getRoutingHelper().recalculateRouteDueToSettingsChange(); if (app.getAvoidSpecificRoads().getImpassableRoads().isEmpty() && getAvoidedParameters(app).isEmpty()) { mode.parameters.remove(parameter); } diff --git a/OsmAnd/src/net/osmand/plus/views/ImpassableRoadsLayer.java b/OsmAnd/src/net/osmand/plus/views/ImpassableRoadsLayer.java index 7bef99b217..b5b1d80ae1 100644 --- a/OsmAnd/src/net/osmand/plus/views/ImpassableRoadsLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/ImpassableRoadsLayer.java @@ -10,7 +10,6 @@ import android.graphics.PointF; import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import net.osmand.binary.RouteDataObject; import net.osmand.data.LatLon; import net.osmand.data.PointDescription; import net.osmand.data.RotatedTileBox; @@ -18,6 +17,7 @@ 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.AvoidRoadInfo; import net.osmand.plus.helpers.AvoidSpecificRoads.AvoidSpecificRoadsCallback; import net.osmand.plus.views.ContextMenuLayer.ApplyMovedObjectCallback; @@ -55,7 +55,7 @@ public class ImpassableRoadsLayer extends OsmandMapLayer implements @Override public void onDraw(Canvas canvas, RotatedTileBox tileBox, DrawSettings settings) { - if (contextMenuLayer.getMoveableObject() instanceof RouteDataObject) { + if (contextMenuLayer.getMoveableObject() instanceof AvoidRoadInfo) { PointF pf = contextMenuLayer.getMovableCenterPoint(tileBox); drawPoint(canvas, pf.x, pf.y, true); } @@ -64,11 +64,11 @@ public class ImpassableRoadsLayer extends OsmandMapLayer implements @Override public void onPrepareBufferImage(Canvas canvas, RotatedTileBox tileBox, DrawSettings settings) { if (tileBox.getZoom() >= START_ZOOM) { - for (Map.Entry entry : avoidSpecificRoads.getImpassableRoads().entrySet()) { + 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(); + AvoidRoadInfo road = entry.getValue(); + if (road != null && contextMenuLayer.getMoveableObject() instanceof AvoidRoadInfo) { + AvoidRoadInfo object = (AvoidRoadInfo) contextMenuLayer.getMoveableObject(); if (object.id == road.id) { continue; } @@ -146,9 +146,9 @@ public class ImpassableRoadsLayer extends OsmandMapLayer implements int compare = getRadiusPoi(tileBox); int radius = compare * 3 / 2; - for (Map.Entry entry : avoidSpecificRoads.getImpassableRoads().entrySet()) { + for (Map.Entry entry : avoidSpecificRoads.getImpassableRoads().entrySet()) { LatLon location = entry.getKey(); - RouteDataObject road = entry.getValue(); + AvoidRoadInfo 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()); @@ -163,36 +163,37 @@ public class ImpassableRoadsLayer extends OsmandMapLayer implements @Override public LatLon getObjectLocation(Object o) { - if (o instanceof RouteDataObject) { - return avoidSpecificRoads.getLocation((RouteDataObject) o); + if (o instanceof AvoidRoadInfo) { + AvoidRoadInfo avoidRoadInfo = (AvoidRoadInfo) o; + return new LatLon(avoidRoadInfo.lat, avoidRoadInfo.lon); } return null; } @Override public PointDescription getObjectName(Object o) { - if (o instanceof RouteDataObject) { - RouteDataObject route = (RouteDataObject) o; - return new PointDescription(PointDescription.POINT_TYPE_BLOCKED_ROAD, route.getName()); + if (o instanceof AvoidRoadInfo) { + AvoidRoadInfo route = (AvoidRoadInfo) o; + return new PointDescription(PointDescription.POINT_TYPE_BLOCKED_ROAD, route.name); } return null; } @Override public boolean isObjectMovable(Object o) { - return o instanceof RouteDataObject; + return o instanceof AvoidRoadInfo; } @Override public void applyNewObjectPosition(@NonNull Object o, @NonNull LatLon position, @Nullable final ApplyMovedObjectCallback callback) { - if (o instanceof RouteDataObject) { - final RouteDataObject object = (RouteDataObject) o; + if (o instanceof AvoidRoadInfo) { + final AvoidRoadInfo object = (AvoidRoadInfo) o; final OsmandApplication application = activity.getMyApplication(); application.getAvoidSpecificRoads().replaceImpassableRoad(activity, object, position, false, new AvoidSpecificRoadsCallback() { @Override - public void onAddImpassableRoad(boolean success, RouteDataObject newObject) { + public void onAddImpassableRoad(boolean success, AvoidRoadInfo newObject) { if (callback != null) { callback.onApplyMovedObject(success, newObject); } From 1cb49fe4026c2c6c774206ab397179feb6b98fa2 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Tue, 18 Feb 2020 16:40:39 +0200 Subject: [PATCH 2/7] Avoid roads second part --- .../osmand/router/RoutingConfiguration.java | 6 +- .../src/net/osmand/plus/AppInitializer.java | 4 +- .../src/net/osmand/plus/OsmandSettings.java | 132 ++++++++++++++++++ .../plus/helpers/AvoidSpecificRoads.java | 70 ++++++---- 4 files changed, 180 insertions(+), 32 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 4a84a9c6df..005f3ba88c 100644 --- a/OsmAnd-java/src/main/java/net/osmand/router/RoutingConfiguration.java +++ b/OsmAnd-java/src/main/java/net/osmand/router/RoutingConfiguration.java @@ -116,9 +116,9 @@ public class RoutingConfiguration { return impassableRoadLocations; } - public boolean addImpassableRoad(RouteDataObject route) { - if (!impassableRoadLocations.contains(route.id)) { - impassableRoadLocations.add(route.id); + public boolean addImpassableRoad(long routeId) { + if (!impassableRoadLocations.contains(routeId)) { + impassableRoadLocations.add(routeId); return true; } return false; diff --git a/OsmAnd/src/net/osmand/plus/AppInitializer.java b/OsmAnd/src/net/osmand/plus/AppInitializer.java index bf9720c037..1f89aca20b 100644 --- a/OsmAnd/src/net/osmand/plus/AppInitializer.java +++ b/OsmAnd/src/net/osmand/plus/AppInitializer.java @@ -629,6 +629,7 @@ public class AppInitializer implements IProgress { if (!customConfigs.isEmpty()) { app.getCustomRoutingConfigs().putAll(customConfigs); } + app.avoidSpecificRoads.initRouteObjects(false); callback.onRoutingFilesLoaded(); } @@ -739,8 +740,7 @@ public class AppInitializer implements IProgress { notifyEvent(InitEvents.RESTORE_BACKUPS); app.mapMarkersHelper.syncAllGroupsAsync(); app.searchUICore.initSearchUICore(); - app.avoidSpecificRoads.initRouteObjects(false); - + checkLiveUpdatesAlerts(); } catch (RuntimeException e) { diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index 89bfd57513..b5ca671ace 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -2798,9 +2798,136 @@ public class OsmandSettings { } private class ImpassableRoadsStorage extends MapPointsStorage { + + protected String roadsIdsKey; + public ImpassableRoadsStorage() { pointsKey = IMPASSABLE_ROAD_POINTS; descriptionsKey = IMPASSABLE_ROADS_DESCRIPTIONS; + roadsIdsKey = IMPASSABLE_ROADS_IDS; + } + + public List getImpassableRoadsInfo() { + List points = mImpassableRoadsStorage.getPoints(); + List descriptions = mImpassableRoadsStorage.getPointDescriptions(points.size()); + List roadIds = getRoadIds(points.size()); + + List avoidRoadsInfo = new ArrayList<>(); + + for (int i = 0; i < points.size(); i++) { + LatLon latLon = points.get(i); + PointDescription description = PointDescription.deserializeFromString(descriptions.get(i), null); + + AvoidRoadInfo avoidRoadInfo = new AvoidRoadInfo(); + avoidRoadInfo.id = roadIds.get(i); + avoidRoadInfo.lat = latLon.getLatitude(); + avoidRoadInfo.lon = latLon.getLongitude(); + avoidRoadInfo.name = description.getName(); + avoidRoadsInfo.add(avoidRoadInfo); + } + + return avoidRoadsInfo; + } + + public boolean addImpassableRoadInfo(AvoidRoadInfo avoidRoadInfo) { + List points = getPoints(); + List descriptions = getPointDescriptions(points.size()); + List roadIds = getRoadIds(points.size()); + + points.add(0, new LatLon(avoidRoadInfo.lat, avoidRoadInfo.lon)); + descriptions.add(0, PointDescription.serializeToString(new PointDescription("", avoidRoadInfo.name))); + roadIds.add(0, avoidRoadInfo.id); + + return savePoints(points, descriptions) && saveRoadIds(roadIds); + } + + public boolean updateImpassableRoadInfo(AvoidRoadInfo avoidRoadInfo) { + List points = getPoints(); + List roadIds = getRoadIds(points.size()); + List descriptions = getPointDescriptions(points.size()); + + int index = points.indexOf(new LatLon(avoidRoadInfo.lat, avoidRoadInfo.lon)); + if (index != -1) { + roadIds.set(index, avoidRoadInfo.id); + descriptions.set(index, PointDescription.serializeToString(new PointDescription("", avoidRoadInfo.name))); + return savePoints(points, descriptions) && saveRoadIds(roadIds); + } + return false; + } + + public List getRoadIds(int size) { + List list = new ArrayList<>(); + String roadIds = settingsAPI.getString(globalPreferences, roadsIdsKey, ""); + if (roadIds.trim().length() > 0) { + StringTokenizer tok = new StringTokenizer(roadIds, ","); + while (tok.hasMoreTokens() && list.size() <= size) { + String token = tok.nextToken(); + list.add(Long.parseLong(token)); + } + } + while (list.size() < size) { + list.add(0L); + } + return list; + } + + @Override + public boolean deletePoint(int index) { + List points = getPoints(); + List roadIds = getRoadIds(points.size()); + List descriptions = getPointDescriptions(points.size()); + + if (index < points.size()) { + points.remove(index); + roadIds.remove(index); + descriptions.remove(index); + return savePoints(points, descriptions) && saveRoadIds(roadIds); + } + return false; + } + + @Override + public boolean deletePoint(LatLon latLon) { + List points = getPoints(); + List descriptions = getPointDescriptions(points.size()); + List roadIds = getRoadIds(points.size()); + int index = points.indexOf(latLon); + if (index != -1) { + points.remove(index); + roadIds.remove(index); + descriptions.remove(index); + return savePoints(points, descriptions) && saveRoadIds(roadIds); + } + return false; + } + + @Override + public boolean movePoint(LatLon latLonEx, LatLon latLonNew) { + List points = getPoints(); + List descriptions = getPointDescriptions(points.size()); + List roadIds = getRoadIds(points.size()); + + int i = points.indexOf(latLonEx); + if (i != -1) { + points.set(i, latLonNew); + return savePoints(points, descriptions) && saveRoadIds(roadIds); + } else { + return false; + } + } + + public boolean saveRoadIds(List roadIds) { + StringBuilder stringBuilder = new StringBuilder(); + Iterator iterator = roadIds.iterator(); + while (iterator.hasNext()) { + stringBuilder.append(iterator.next()); + if (iterator.hasNext()) { + stringBuilder.append(","); + } + } + return settingsAPI.edit(globalPreferences) + .putString(roadsIdsKey, stringBuilder.toString()) + .commit(); } } @@ -2994,10 +3121,15 @@ public class OsmandSettings { public List getImpassableRoadPoints() { return mImpassableRoadsStorage.getImpassableRoadsInfo(); } + public boolean addImpassableRoad(AvoidRoadInfo avoidRoadInfo) { return mImpassableRoadsStorage.addImpassableRoadInfo(avoidRoadInfo); } + public boolean updateImpassableRoadInfo(AvoidRoadInfo avoidRoadInfo) { + return mImpassableRoadsStorage.updateImpassableRoadInfo(avoidRoadInfo); + } + public boolean removeImpassableRoad(int index) { return mImpassableRoadsStorage.deletePoint(index); } diff --git a/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java b/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java index 00b811ad25..628e354e7c 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java +++ b/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java @@ -67,17 +67,21 @@ public class AvoidSpecificRoads { for (Map.Entry entry : impassableRoads.entrySet()) { if (force || entry.getValue().id == 0) { addImpassableRoad(null, entry.getKey(), false, true); + } else { + for (RoutingConfiguration.Builder builder : app.getAllRoutingConfigs()) { + builder.addImpassableRoad(entry.getValue().id); + } } } } - private ArrayAdapter createAdapter(MapActivity mapActivity, boolean nightMode) { - final ArrayList points = new ArrayList<>(impassableRoads.keySet()); + private ArrayAdapter createAdapter(MapActivity mapActivity, boolean nightMode) { + final ArrayList points = new ArrayList<>(impassableRoads.values()); final LatLon mapLocation = mapActivity.getMapLocation(); final LayoutInflater inflater = UiUtilities.getInflater(mapActivity, nightMode); Context themedContext = UiUtilities.getThemedContext(mapActivity, nightMode); - return new ArrayAdapter(themedContext, R.layout.waypoint_reached, R.id.title, points) { + return new ArrayAdapter(themedContext, R.layout.waypoint_reached, R.id.title, points) { @NonNull @Override public View getView(final int position, View convertView, @NonNull ViewGroup parent) { @@ -85,12 +89,15 @@ public class AvoidSpecificRoads { if (v == null || v.findViewById(R.id.info_close) == null) { v = inflater.inflate(R.layout.waypoint_reached, parent, false); } - final LatLon item = getItem(position); + final AvoidRoadInfo item = getItem(position); v.findViewById(R.id.all_points).setVisibility(View.GONE); ((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)); + + LatLon latLon = item != null ? new LatLon(item.lat, item.lon) : null; + String name = item != null ? item.name : app.getString(R.string.shared_string_road); + ((TextView) v.findViewById(R.id.waypoint_dist)).setText(getDist(mapLocation, latLon)); + ((TextView) v.findViewById(R.id.waypoint_text)).setText(name); ImageButton remove = (ImageButton) v.findViewById(R.id.info_close); remove.setVisibility(View.VISIBLE); remove.setImageDrawable(getIcon(R.drawable.ic_action_remove_dark)); @@ -153,15 +160,15 @@ public class AvoidSpecificRoads { public void removeImpassableRoad(LatLon latLon) { app.getSettings().removeImpassableRoad(latLon); - RouteDataObject obj = impassableRoads.remove(latLon); + AvoidRoadInfo obj = impassableRoads.remove(latLon); if (obj != null) { for (RoutingConfiguration.Builder builder : app.getAllRoutingConfigs()) { - builder.removeImpassableRoad(obj); + builder.removeImpassableRoad(obj.id); } } } - public void removeImpassableRoad(RouteDataObject obj) { + public void removeImpassableRoad(AvoidRoadInfo obj) { removeImpassableRoad(getLocation(obj)); } @@ -174,13 +181,13 @@ public class AvoidSpecificRoads { if (impassableRoads.isEmpty()) { bld.setMessage(R.string.avoid_roads_msg); } else { - final ArrayAdapter listAdapter = createAdapter(mapActivity, nightMode); + final ArrayAdapter listAdapter = createAdapter(mapActivity, nightMode); bld.setAdapter(listAdapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - LatLon point = listAdapter.getItem(which); + AvoidRoadInfo point = listAdapter.getItem(which); if (point != null) { - showOnMap(mapActivity, point.getLatitude(), point.getLongitude(), getText(point)); + showOnMap(mapActivity, point.lat, point.lon, point.name); } dialog.dismiss(); } @@ -228,9 +235,12 @@ public class AvoidSpecificRoads { LatLon newLoc = new LatLon(MapUtils.get31LatitudeY((int) point.y), MapUtils.get31LongitudeX((int) point.x)); ll.setLatitude(newLoc.getLatitude()); ll.setLongitude(newLoc.getLongitude()); - addImpassableRoadInternal(roads.get(searchResult.getRoadIndex()).getObject(), ll, showDialog, mapActivity, newLoc); + + RouteDataObject object = roads.get(searchResult.getRoadIndex()).getObject(); + AvoidRoadInfo avoidRoadInfo = getAvoidRoadInfoForDataObject(object, newLoc.getLatitude(), newLoc.getLongitude()); + addImpassableRoadInternal(avoidRoadInfo, showDialog, mapActivity, newLoc); if (!skipWritingSettings) { - app.getSettings().addImpassableRoad(newLoc.getLatitude(), newLoc.getLongitude()); + app.getSettings().addImpassableRoad(avoidRoadInfo); } return; } @@ -244,7 +254,8 @@ public class AvoidSpecificRoads { Toast.makeText(mapActivity, R.string.error_avoid_specific_road, Toast.LENGTH_LONG).show(); } } else { - addImpassableRoadInternal(object, ll, showDialog, mapActivity, loc); + AvoidRoadInfo avoidRoadInfo = getAvoidRoadInfoForDataObject(object, ll.getLatitude(), ll.getLongitude()); + addImpassableRoadInternal(avoidRoadInfo, showDialog, mapActivity, loc); } return true; } @@ -256,12 +267,13 @@ public class AvoidSpecificRoads { }); if (!skipWritingSettings) { - app.getSettings().addImpassableRoad(loc.getLatitude(), loc.getLongitude()); + AvoidRoadInfo avoidRoadInfo = getAvoidRoadInfoForDataObject(null, loc.getLatitude(), loc.getLongitude()); + app.getSettings().addImpassableRoad(avoidRoadInfo); } } public void replaceImpassableRoad(final MapActivity activity, - final RouteDataObject currentObject, + final AvoidRoadInfo currentObject, final LatLon newLoc, final boolean showDialog, final AvoidSpecificRoadsCallback callback) { @@ -284,12 +296,13 @@ public class AvoidSpecificRoads { app.getSettings().moveImpassableRoad(oldLoc, newLoc); impassableRoads.remove(oldLoc); for (RoutingConfiguration.Builder builder : app.getAllRoutingConfigs()) { - builder.removeImpassableRoad(currentObject); + builder.removeImpassableRoad(currentObject.id); } - addImpassableRoadInternal(object, ll, showDialog, activity, newLoc); + AvoidRoadInfo avoidRoadInfo = getAvoidRoadInfoForDataObject(object, newLoc.getLatitude(), newLoc.getLongitude()); + addImpassableRoadInternal(avoidRoadInfo, showDialog, activity, newLoc); if (callback != null) { - callback.onAddImpassableRoad(true, object); + callback.onAddImpassableRoad(true, avoidRoadInfo); } } return true; @@ -302,19 +315,19 @@ public class AvoidSpecificRoads { }); } - private void addImpassableRoadInternal(@NonNull RouteDataObject object, - @NonNull Location ll, + private void addImpassableRoadInternal(@NonNull AvoidRoadInfo avoidRoadInfo, boolean showDialog, @Nullable MapActivity activity, @NonNull LatLon loc) { boolean roadAdded = false; for (RoutingConfiguration.Builder builder : app.getAllRoutingConfigs()) { - roadAdded |= builder.addImpassableRoad(object, ll); + roadAdded |= builder.addImpassableRoad(avoidRoadInfo.id); } if (roadAdded) { - impassableRoads.put(loc, object); + app.getSettings().updateImpassableRoadInfo(avoidRoadInfo); + impassableRoads.put(loc, avoidRoadInfo); } else { - LatLon location = getLocation(object); + LatLon location = getLocation(avoidRoadInfo); if (location != null) { app.getSettings().removeImpassableRoad(location); } @@ -355,8 +368,11 @@ public class AvoidSpecificRoads { boolean isCancelled(); } - private AvoidRoadInfo createAvoidRoadInfo(@Nullable RouteDataObject object, double lat, double lon) { - AvoidRoadInfo avoidRoadInfo = new AvoidRoadInfo(); + private AvoidRoadInfo getAvoidRoadInfoForDataObject(@Nullable RouteDataObject object, double lat, double lon) { + AvoidRoadInfo avoidRoadInfo = impassableRoads.get(new LatLon(lat, lon)); + if (avoidRoadInfo == null) { + avoidRoadInfo = new AvoidRoadInfo(); + } avoidRoadInfo.id = object != null ? object.id : 0; avoidRoadInfo.lat = lat; avoidRoadInfo.lon = lon; From 2e15315a16b4d95ee529e2703e19e719a81c1e55 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Tue, 18 Feb 2020 17:07:36 +0200 Subject: [PATCH 3/7] Add full names to avoid road info --- .../net/osmand/router/RoutingConfiguration.java | 16 +++++----------- OsmAnd/src/net/osmand/plus/OsmandSettings.java | 8 ++++---- .../osmand/plus/helpers/AvoidSpecificRoads.java | 16 ++++++++-------- .../osmand/plus/views/ImpassableRoadsLayer.java | 2 +- 4 files changed, 18 insertions(+), 24 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 005f3ba88c..fb3432d4aa 100644 --- a/OsmAnd-java/src/main/java/net/osmand/router/RoutingConfiguration.java +++ b/OsmAnd-java/src/main/java/net/osmand/router/RoutingConfiguration.java @@ -1,7 +1,6 @@ package net.osmand.router; import net.osmand.PlatformUtil; -import net.osmand.binary.RouteDataObject; import net.osmand.router.GeneralRouter.GeneralRouterProfile; import net.osmand.router.GeneralRouter.RouteAttributeContext; import net.osmand.router.GeneralRouter.RouteDataObjectAttribute; @@ -12,11 +11,10 @@ import org.xmlpull.v1.XmlPullParserException; import java.io.IOException; import java.io.InputStream; -import java.util.ArrayList; import java.util.HashSet; import java.util.LinkedHashMap; -import java.util.List; import java.util.Map; +import java.util.Set; import java.util.Stack; public class RoutingConfiguration { @@ -56,7 +54,7 @@ public class RoutingConfiguration { private String defaultRouter = ""; private Map routers = new LinkedHashMap<>(); private Map attributes = new LinkedHashMap<>(); - private List impassableRoadLocations = new ArrayList<>(); + private Set impassableRoadLocations = new HashSet<>(); public Builder() { @@ -111,17 +109,13 @@ public class RoutingConfiguration { // i.planRoadDirection = 1; return i; } - - public List getImpassableRoadLocations() { + + public Set getImpassableRoadLocations() { return impassableRoadLocations; } public boolean addImpassableRoad(long routeId) { - if (!impassableRoadLocations.contains(routeId)) { - impassableRoadLocations.add(routeId); - return true; - } - return false; + return impassableRoadLocations.add(routeId); } public Map getAttributes() { diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index b5ca671ace..bfeb7f1865 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -2820,8 +2820,8 @@ public class OsmandSettings { AvoidRoadInfo avoidRoadInfo = new AvoidRoadInfo(); avoidRoadInfo.id = roadIds.get(i); - avoidRoadInfo.lat = latLon.getLatitude(); - avoidRoadInfo.lon = latLon.getLongitude(); + avoidRoadInfo.latitude = latLon.getLatitude(); + avoidRoadInfo.longitude = latLon.getLongitude(); avoidRoadInfo.name = description.getName(); avoidRoadsInfo.add(avoidRoadInfo); } @@ -2834,7 +2834,7 @@ public class OsmandSettings { List descriptions = getPointDescriptions(points.size()); List roadIds = getRoadIds(points.size()); - points.add(0, new LatLon(avoidRoadInfo.lat, avoidRoadInfo.lon)); + points.add(0, new LatLon(avoidRoadInfo.latitude, avoidRoadInfo.longitude)); descriptions.add(0, PointDescription.serializeToString(new PointDescription("", avoidRoadInfo.name))); roadIds.add(0, avoidRoadInfo.id); @@ -2846,7 +2846,7 @@ public class OsmandSettings { List roadIds = getRoadIds(points.size()); List descriptions = getPointDescriptions(points.size()); - int index = points.indexOf(new LatLon(avoidRoadInfo.lat, avoidRoadInfo.lon)); + int index = points.indexOf(new LatLon(avoidRoadInfo.latitude, avoidRoadInfo.longitude)); if (index != -1) { roadIds.set(index, avoidRoadInfo.id); descriptions.set(index, PointDescription.serializeToString(new PointDescription("", avoidRoadInfo.name))); diff --git a/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java b/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java index 628e354e7c..965de9fe08 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java +++ b/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java @@ -55,7 +55,7 @@ public class AvoidSpecificRoads { public AvoidSpecificRoads(final OsmandApplication app) { this.app = app; for (AvoidRoadInfo avoidRoadInfo : app.getSettings().getImpassableRoadPoints()) { - impassableRoads.put(new LatLon(avoidRoadInfo.lat, avoidRoadInfo.lon), avoidRoadInfo); + impassableRoads.put(new LatLon(avoidRoadInfo.latitude, avoidRoadInfo.longitude), avoidRoadInfo); } } @@ -94,7 +94,7 @@ public class AvoidSpecificRoads { ((ImageView) v.findViewById(R.id.waypoint_icon)) .setImageDrawable(getIcon(R.drawable.ic_action_road_works_dark)); - LatLon latLon = item != null ? new LatLon(item.lat, item.lon) : null; + LatLon latLon = item != null ? new LatLon(item.latitude, item.longitude) : null; String name = item != null ? item.name : app.getString(R.string.shared_string_road); ((TextView) v.findViewById(R.id.waypoint_dist)).setText(getDist(mapLocation, latLon)); ((TextView) v.findViewById(R.id.waypoint_text)).setText(name); @@ -187,7 +187,7 @@ public class AvoidSpecificRoads { public void onClick(DialogInterface dialog, int which) { AvoidRoadInfo point = listAdapter.getItem(which); if (point != null) { - showOnMap(mapActivity, point.lat, point.lon, point.name); + showOnMap(mapActivity, point.latitude, point.longitude, point.name); } dialog.dismiss(); } @@ -355,7 +355,7 @@ public class AvoidSpecificRoads { public LatLon getLocation(AvoidRoadInfo avoidRoadInfo) { for (RoutingConfiguration.Builder builder : app.getAllRoutingConfigs()) { if (builder.getImpassableRoadLocations().contains(avoidRoadInfo.id)) { - return new LatLon(avoidRoadInfo.lat, avoidRoadInfo.lon); + return new LatLon(avoidRoadInfo.latitude, avoidRoadInfo.longitude); } } return null; @@ -374,16 +374,16 @@ public class AvoidSpecificRoads { avoidRoadInfo = new AvoidRoadInfo(); } avoidRoadInfo.id = object != null ? object.id : 0; - avoidRoadInfo.lat = lat; - avoidRoadInfo.lon = lon; + avoidRoadInfo.latitude = lat; + avoidRoadInfo.longitude = lon; avoidRoadInfo.name = getRoadName(object); return avoidRoadInfo; } public static class AvoidRoadInfo { public long id; - public double lat; - public double lon; + public double latitude; + public double longitude; public String name; } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/views/ImpassableRoadsLayer.java b/OsmAnd/src/net/osmand/plus/views/ImpassableRoadsLayer.java index b5b1d80ae1..1ab8daa1e6 100644 --- a/OsmAnd/src/net/osmand/plus/views/ImpassableRoadsLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/ImpassableRoadsLayer.java @@ -165,7 +165,7 @@ public class ImpassableRoadsLayer extends OsmandMapLayer implements public LatLon getObjectLocation(Object o) { if (o instanceof AvoidRoadInfo) { AvoidRoadInfo avoidRoadInfo = (AvoidRoadInfo) o; - return new LatLon(avoidRoadInfo.lat, avoidRoadInfo.lon); + return new LatLon(avoidRoadInfo.latitude, avoidRoadInfo.longitude); } return null; } From f429cd69abc51f4c0ea4c9428699375bdcd09a87 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Tue, 18 Feb 2020 18:13:27 +0200 Subject: [PATCH 4/7] Save app mode keys for avoid roads --- .../src/net/osmand/plus/OsmandSettings.java | 109 +++++++++++++----- .../plus/activities/MapActivityActions.java | 2 +- .../plus/helpers/AvoidSpecificRoads.java | 36 +++--- .../PointDescriptionMenuController.java | 2 +- 4 files changed, 103 insertions(+), 46 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index bfeb7f1865..8460556e1d 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -2629,6 +2629,7 @@ public class OsmandSettings { private static final String IMPASSABLE_ROAD_POINTS = "impassable_road_points"; private static final String IMPASSABLE_ROADS_DESCRIPTIONS = "impassable_roads_descriptions"; private static final String IMPASSABLE_ROADS_IDS = "impassable_roads_ids"; + private static final String IMPASSABLE_ROADS_APP_MODE_KEYS = "impassable_roads_app_mode_keys"; private ImpassableRoadsStorage mImpassableRoadsStorage = new ImpassableRoadsStorage(); public void backupPointToStart() { @@ -2800,17 +2801,50 @@ public class OsmandSettings { private class ImpassableRoadsStorage extends MapPointsStorage { protected String roadsIdsKey; + protected String appModeKey; public ImpassableRoadsStorage() { pointsKey = IMPASSABLE_ROAD_POINTS; descriptionsKey = IMPASSABLE_ROADS_DESCRIPTIONS; roadsIdsKey = IMPASSABLE_ROADS_IDS; + appModeKey = IMPASSABLE_ROADS_APP_MODE_KEYS; + } + + public List getRoadIds(int size) { + List list = new ArrayList<>(); + String roadIds = settingsAPI.getString(globalPreferences, roadsIdsKey, ""); + if (roadIds.trim().length() > 0) { + StringTokenizer tok = new StringTokenizer(roadIds, ","); + while (tok.hasMoreTokens() && list.size() <= size) { + list.add(Long.parseLong(tok.nextToken())); + } + } + while (list.size() < size) { + list.add(0L); + } + return list; + } + + public List getAppModeKeys(int size) { + List list = new ArrayList<>(); + String roadIds = settingsAPI.getString(globalPreferences, appModeKey, ""); + if (roadIds.trim().length() > 0) { + StringTokenizer tok = new StringTokenizer(roadIds, ","); + while (tok.hasMoreTokens() && list.size() <= size) { + list.add(tok.nextToken()); + } + } + while (list.size() < size) { + list.add(""); + } + return list; } public List getImpassableRoadsInfo() { - List points = mImpassableRoadsStorage.getPoints(); - List descriptions = mImpassableRoadsStorage.getPointDescriptions(points.size()); + List points = getPoints(); List roadIds = getRoadIds(points.size()); + List appModeKeys = getAppModeKeys(points.size()); + List descriptions = getPointDescriptions(points.size()); List avoidRoadsInfo = new ArrayList<>(); @@ -2823,6 +2857,7 @@ public class OsmandSettings { avoidRoadInfo.latitude = latLon.getLatitude(); avoidRoadInfo.longitude = latLon.getLongitude(); avoidRoadInfo.name = description.getName(); + avoidRoadInfo.appModeKey = appModeKeys.get(i); avoidRoadsInfo.add(avoidRoadInfo); } @@ -2831,57 +2866,48 @@ public class OsmandSettings { public boolean addImpassableRoadInfo(AvoidRoadInfo avoidRoadInfo) { List points = getPoints(); - List descriptions = getPointDescriptions(points.size()); List roadIds = getRoadIds(points.size()); + List appModeKeys = getAppModeKeys(points.size()); + List descriptions = getPointDescriptions(points.size()); - points.add(0, new LatLon(avoidRoadInfo.latitude, avoidRoadInfo.longitude)); - descriptions.add(0, PointDescription.serializeToString(new PointDescription("", avoidRoadInfo.name))); roadIds.add(0, avoidRoadInfo.id); + points.add(0, new LatLon(avoidRoadInfo.latitude, avoidRoadInfo.longitude)); + appModeKeys.add(0, avoidRoadInfo.appModeKey); + descriptions.add(0, PointDescription.serializeToString(new PointDescription("", avoidRoadInfo.name))); - return savePoints(points, descriptions) && saveRoadIds(roadIds); + return saveAvoidRoadData(points, descriptions, roadIds, appModeKeys); } public boolean updateImpassableRoadInfo(AvoidRoadInfo avoidRoadInfo) { List points = getPoints(); - List roadIds = getRoadIds(points.size()); - List descriptions = getPointDescriptions(points.size()); int index = points.indexOf(new LatLon(avoidRoadInfo.latitude, avoidRoadInfo.longitude)); if (index != -1) { + List roadIds = getRoadIds(points.size()); + List appModeKeys = getAppModeKeys(points.size()); + List descriptions = getPointDescriptions(points.size()); + roadIds.set(index, avoidRoadInfo.id); + appModeKeys.set(index, avoidRoadInfo.appModeKey); descriptions.set(index, PointDescription.serializeToString(new PointDescription("", avoidRoadInfo.name))); - return savePoints(points, descriptions) && saveRoadIds(roadIds); + return saveAvoidRoadData(points, descriptions, roadIds, appModeKeys); } return false; } - public List getRoadIds(int size) { - List list = new ArrayList<>(); - String roadIds = settingsAPI.getString(globalPreferences, roadsIdsKey, ""); - if (roadIds.trim().length() > 0) { - StringTokenizer tok = new StringTokenizer(roadIds, ","); - while (tok.hasMoreTokens() && list.size() <= size) { - String token = tok.nextToken(); - list.add(Long.parseLong(token)); - } - } - while (list.size() < size) { - list.add(0L); - } - return list; - } - @Override public boolean deletePoint(int index) { List points = getPoints(); List roadIds = getRoadIds(points.size()); + List appModeKeys = getAppModeKeys(points.size()); List descriptions = getPointDescriptions(points.size()); if (index < points.size()) { points.remove(index); roadIds.remove(index); + appModeKeys.remove(index); descriptions.remove(index); - return savePoints(points, descriptions) && saveRoadIds(roadIds); + return saveAvoidRoadData(points, descriptions, roadIds, appModeKeys); } return false; } @@ -2889,14 +2915,17 @@ public class OsmandSettings { @Override public boolean deletePoint(LatLon latLon) { List points = getPoints(); - List descriptions = getPointDescriptions(points.size()); List roadIds = getRoadIds(points.size()); + List appModeKeys = getAppModeKeys(points.size()); + List descriptions = getPointDescriptions(points.size()); + int index = points.indexOf(latLon); if (index != -1) { points.remove(index); roadIds.remove(index); + appModeKeys.remove(index); descriptions.remove(index); - return savePoints(points, descriptions) && saveRoadIds(roadIds); + return saveAvoidRoadData(points, descriptions, roadIds, appModeKeys); } return false; } @@ -2904,18 +2933,24 @@ public class OsmandSettings { @Override public boolean movePoint(LatLon latLonEx, LatLon latLonNew) { List points = getPoints(); - List descriptions = getPointDescriptions(points.size()); List roadIds = getRoadIds(points.size()); + List appModeKeys = getAppModeKeys(points.size()); + List descriptions = getPointDescriptions(points.size()); int i = points.indexOf(latLonEx); if (i != -1) { points.set(i, latLonNew); - return savePoints(points, descriptions) && saveRoadIds(roadIds); + return saveAvoidRoadData(points, descriptions, roadIds, appModeKeys); } else { return false; } } + public boolean saveAvoidRoadData(List points, List descriptions, + List roadIds, List appModeKeys) { + return savePoints(points, descriptions) && saveRoadIds(roadIds) && saveAppModeKeys(appModeKeys); + } + public boolean saveRoadIds(List roadIds) { StringBuilder stringBuilder = new StringBuilder(); Iterator iterator = roadIds.iterator(); @@ -2929,6 +2964,20 @@ public class OsmandSettings { .putString(roadsIdsKey, stringBuilder.toString()) .commit(); } + + public boolean saveAppModeKeys(List appModeKeys) { + StringBuilder stringBuilder = new StringBuilder(); + Iterator iterator = appModeKeys.iterator(); + while (iterator.hasNext()) { + stringBuilder.append(iterator.next()); + if (iterator.hasNext()) { + stringBuilder.append(","); + } + } + return settingsAPI.edit(globalPreferences) + .putString(appModeKey, stringBuilder.toString()) + .commit(); + } } private abstract class MapPointsStorage { diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java index 057f214a87..7338700698 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java @@ -426,7 +426,7 @@ public class MapActivityActions implements DialogProvider { mapActivity.getContextMenu().close(); MeasurementToolFragment.showInstance(mapActivity.getSupportFragmentManager(), new LatLon(latitude, longitude)); } else if (standardId == R.string.avoid_road) { - getMyApplication().getAvoidSpecificRoads().addImpassableRoad(mapActivity, new LatLon(latitude, longitude), true, false); + getMyApplication().getAvoidSpecificRoads().addImpassableRoad(mapActivity, new LatLon(latitude, longitude), true, false, null); } } }); diff --git a/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java b/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java index 965de9fe08..382de8112d 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java +++ b/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java @@ -65,11 +65,12 @@ public class AvoidSpecificRoads { public void initRouteObjects(boolean force) { for (Map.Entry entry : impassableRoads.entrySet()) { - if (force || entry.getValue().id == 0) { - addImpassableRoad(null, entry.getKey(), false, true); + AvoidRoadInfo roadInfo = entry.getValue(); + if (force || roadInfo.id == 0) { + addImpassableRoad(null, entry.getKey(), false, true, roadInfo.appModeKey); } else { for (RoutingConfiguration.Builder builder : app.getAllRoutingConfigs()) { - builder.addImpassableRoad(entry.getValue().id); + builder.addImpassableRoad(roadInfo.id); } } } @@ -209,20 +210,23 @@ public class AvoidSpecificRoads { cm.setSelectOnMap(new CallbackWithObject() { @Override public boolean processResult(LatLon result) { - addImpassableRoad(mapActivity, result, true, false); + addImpassableRoad(mapActivity, result, true, false, null); return true; } }); } public void addImpassableRoad(@Nullable final MapActivity mapActivity, - @NonNull final LatLon loc, - final boolean showDialog, - final boolean skipWritingSettings) { + @NonNull final LatLon loc, + final boolean showDialog, + final boolean skipWritingSettings, + @Nullable final String appModeKey) { final Location ll = new Location(""); ll.setLatitude(loc.getLatitude()); ll.setLongitude(loc.getLongitude()); - ApplicationMode appMode = app.getRoutingHelper().getAppMode(); + + ApplicationMode defaultAppMode = app.getRoutingHelper().getAppMode(); + final ApplicationMode appMode = ApplicationMode.valueOfStringKey(appModeKey, defaultAppMode); List roads = app.getRoutingHelper().getRoute().getOriginalRoute(); if (mapActivity != null && roads != null) { @@ -237,7 +241,7 @@ public class AvoidSpecificRoads { ll.setLongitude(newLoc.getLongitude()); RouteDataObject object = roads.get(searchResult.getRoadIndex()).getObject(); - AvoidRoadInfo avoidRoadInfo = getAvoidRoadInfoForDataObject(object, newLoc.getLatitude(), newLoc.getLongitude()); + AvoidRoadInfo avoidRoadInfo = getAvoidRoadInfoForDataObject(object, newLoc.getLatitude(), newLoc.getLongitude(), appMode.getStringKey()); addImpassableRoadInternal(avoidRoadInfo, showDialog, mapActivity, newLoc); if (!skipWritingSettings) { app.getSettings().addImpassableRoad(avoidRoadInfo); @@ -254,7 +258,7 @@ public class AvoidSpecificRoads { Toast.makeText(mapActivity, R.string.error_avoid_specific_road, Toast.LENGTH_LONG).show(); } } else { - AvoidRoadInfo avoidRoadInfo = getAvoidRoadInfoForDataObject(object, ll.getLatitude(), ll.getLongitude()); + AvoidRoadInfo avoidRoadInfo = getAvoidRoadInfoForDataObject(object, ll.getLatitude(), ll.getLongitude(), appMode.getStringKey()); addImpassableRoadInternal(avoidRoadInfo, showDialog, mapActivity, loc); } return true; @@ -267,7 +271,7 @@ public class AvoidSpecificRoads { }); if (!skipWritingSettings) { - AvoidRoadInfo avoidRoadInfo = getAvoidRoadInfoForDataObject(null, loc.getLatitude(), loc.getLongitude()); + AvoidRoadInfo avoidRoadInfo = getAvoidRoadInfoForDataObject(null, loc.getLatitude(), loc.getLongitude(), appMode.getStringKey()); app.getSettings().addImpassableRoad(avoidRoadInfo); } } @@ -280,7 +284,9 @@ public class AvoidSpecificRoads { final Location ll = new Location(""); ll.setLatitude(newLoc.getLatitude()); ll.setLongitude(newLoc.getLongitude()); - ApplicationMode appMode = app.getRoutingHelper().getAppMode(); + + ApplicationMode defaultAppMode = app.getRoutingHelper().getAppMode(); + final ApplicationMode appMode = ApplicationMode.valueOfStringKey(currentObject.appModeKey, defaultAppMode); app.getLocationProvider().getRouteSegment(ll, appMode, false, new ResultMatcher() { @@ -298,7 +304,7 @@ public class AvoidSpecificRoads { for (RoutingConfiguration.Builder builder : app.getAllRoutingConfigs()) { builder.removeImpassableRoad(currentObject.id); } - AvoidRoadInfo avoidRoadInfo = getAvoidRoadInfoForDataObject(object, newLoc.getLatitude(), newLoc.getLongitude()); + AvoidRoadInfo avoidRoadInfo = getAvoidRoadInfoForDataObject(object, newLoc.getLatitude(), newLoc.getLongitude(), appMode.getStringKey()); addImpassableRoadInternal(avoidRoadInfo, showDialog, activity, newLoc); if (callback != null) { @@ -368,7 +374,7 @@ public class AvoidSpecificRoads { boolean isCancelled(); } - private AvoidRoadInfo getAvoidRoadInfoForDataObject(@Nullable RouteDataObject object, double lat, double lon) { + private AvoidRoadInfo getAvoidRoadInfoForDataObject(@Nullable RouteDataObject object, double lat, double lon, String appModeKey) { AvoidRoadInfo avoidRoadInfo = impassableRoads.get(new LatLon(lat, lon)); if (avoidRoadInfo == null) { avoidRoadInfo = new AvoidRoadInfo(); @@ -376,6 +382,7 @@ public class AvoidSpecificRoads { avoidRoadInfo.id = object != null ? object.id : 0; avoidRoadInfo.latitude = lat; avoidRoadInfo.longitude = lon; + avoidRoadInfo.appModeKey = appModeKey; avoidRoadInfo.name = getRoadName(object); return avoidRoadInfo; } @@ -385,5 +392,6 @@ public class AvoidSpecificRoads { public double latitude; public double longitude; public String name; + public String appModeKey; } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/PointDescriptionMenuController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/PointDescriptionMenuController.java index 1db5444bc4..c810aa2ce8 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, false); + roads.addImpassableRoad(activity, getLatLon(), false, false, null); } } }; From 7de0a92ca6479b53a1ad45a61e353bfb42ff3fe1 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Tue, 18 Feb 2020 18:56:24 +0200 Subject: [PATCH 5/7] Recalculate avoid roads after maps downloaded --- .../osmand/plus/download/DownloadIndexesThread.java | 1 + .../net/osmand/plus/helpers/AvoidSpecificRoads.java | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java index 18f06cc21e..98cc159720 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java @@ -118,6 +118,7 @@ public class DownloadIndexesThread { if (app.getDownloadService() != null) { app.getDownloadService().stopService(app); } + app.getAvoidSpecificRoads().initRouteObjects(true); } public void initSettingsFirstMap(WorldRegion reg) { diff --git a/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java b/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java index 382de8112d..c8891d3347 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java +++ b/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java @@ -66,12 +66,17 @@ public class AvoidSpecificRoads { public void initRouteObjects(boolean force) { for (Map.Entry entry : impassableRoads.entrySet()) { AvoidRoadInfo roadInfo = entry.getValue(); + if (roadInfo.id != 0) { + for (RoutingConfiguration.Builder builder : app.getAllRoutingConfigs()) { + if (force) { + builder.removeImpassableRoad(roadInfo.id); + } else { + builder.addImpassableRoad(roadInfo.id); + } + } + } if (force || roadInfo.id == 0) { addImpassableRoad(null, entry.getKey(), false, true, roadInfo.appModeKey); - } else { - for (RoutingConfiguration.Builder builder : app.getAllRoutingConfigs()) { - builder.addImpassableRoad(roadInfo.id); - } } } } From 1b9fbc5792266535efb5b92f86c1ac430bf8252d Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Wed, 19 Feb 2020 12:47:59 +0200 Subject: [PATCH 6/7] Add check for possible npe --- OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java b/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java index c8891d3347..e6c7ab9853 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java +++ b/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java @@ -232,7 +232,9 @@ public class AvoidSpecificRoads { ApplicationMode defaultAppMode = app.getRoutingHelper().getAppMode(); final ApplicationMode appMode = ApplicationMode.valueOfStringKey(appModeKey, defaultAppMode); - + if (appMode == null) { + return; + } List roads = app.getRoutingHelper().getRoute().getOriginalRoute(); if (mapActivity != null && roads != null) { RotatedTileBox tb = mapActivity.getMapView().getCurrentRotatedTileBox().copy(); From a0342f95482f7902229e03ddfc0fa7349b58c029 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Wed, 19 Feb 2020 12:53:00 +0200 Subject: [PATCH 7/7] Add check for appModeKey --- OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java b/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java index e6c7ab9853..d724e0f9ce 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java +++ b/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java @@ -231,10 +231,8 @@ public class AvoidSpecificRoads { ll.setLongitude(loc.getLongitude()); ApplicationMode defaultAppMode = app.getRoutingHelper().getAppMode(); - final ApplicationMode appMode = ApplicationMode.valueOfStringKey(appModeKey, defaultAppMode); - if (appMode == null) { - return; - } + final ApplicationMode appMode = appModeKey != null ? ApplicationMode.valueOfStringKey(appModeKey, defaultAppMode) : defaultAppMode; + List roads = app.getRoutingHelper().getRoute().getOriginalRoute(); if (mapActivity != null && roads != null) { RotatedTileBox tb = mapActivity.getMapView().getCurrentRotatedTileBox().copy();