From c20c0aa6e7a6caaea7127229f686baf6adc4689d Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Thu, 17 Dec 2020 15:06:41 +0200 Subject: [PATCH] Add direction to avoid roads --- .../navigation/ABlockedRoadParams.java | 40 +++++++++++-- OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java | 2 +- .../plus/helpers/AvoidSpecificRoads.java | 13 +++- .../backend/ImpassableRoadsStorage.java | 59 ++++++++++++++++--- .../plus/settings/backend/OsmandSettings.java | 1 + .../backup/AvoidRoadsSettingsItem.java | 28 +++++---- 6 files changed, 114 insertions(+), 29 deletions(-) diff --git a/OsmAnd-api/src/net/osmand/aidlapi/navigation/ABlockedRoadParams.java b/OsmAnd-api/src/net/osmand/aidlapi/navigation/ABlockedRoadParams.java index 02c59c0ae5..a6dd2a18b5 100644 --- a/OsmAnd-api/src/net/osmand/aidlapi/navigation/ABlockedRoadParams.java +++ b/OsmAnd-api/src/net/osmand/aidlapi/navigation/ABlockedRoadParams.java @@ -7,16 +7,18 @@ import net.osmand.aidlapi.AidlParams; public class ABlockedRoadParams extends AidlParams { - public long roadId; - public double latitude; - public double longitude; - public String name; - public String appModeKey; + private long roadId; + private double latitude; + private double longitude; + private double direction; + private String name; + private String appModeKey; - public ABlockedRoadParams(long roadId, double latitude, double longitude, String name, String appModeKey) { + public ABlockedRoadParams(long roadId, double latitude, double longitude, double direction, String name, String appModeKey) { this.roadId = roadId; this.latitude = latitude; this.longitude = longitude; + this.direction = direction; this.name = name; this.appModeKey = appModeKey; } @@ -37,11 +39,36 @@ public class ABlockedRoadParams extends AidlParams { } }; + public long getRoadId() { + return roadId; + } + + public double getLatitude() { + return latitude; + } + + public double getLongitude() { + return longitude; + } + + public double getDirection() { + return direction; + } + + public String getName() { + return name; + } + + public String getAppModeKey() { + return appModeKey; + } + @Override protected void readFromBundle(Bundle bundle) { roadId = bundle.getLong("roadId"); latitude = bundle.getDouble("latitude"); longitude = bundle.getDouble("longitude"); + direction = bundle.getDouble("direction"); name = bundle.getString("name"); appModeKey = bundle.getString("appModeKey"); } @@ -51,6 +78,7 @@ public class ABlockedRoadParams extends AidlParams { bundle.putLong("roadId", roadId); bundle.putDouble("latitude", latitude); bundle.putDouble("longitude", longitude); + bundle.putDouble("direction", direction); bundle.putString("name", name); bundle.putString("appModeKey", appModeKey); } diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java index 3937055df4..864a128114 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java @@ -2379,7 +2379,7 @@ public class OsmandAidlApi { public boolean getBlockedRoads(List blockedRoads) { Map impassableRoads = app.getAvoidSpecificRoads().getImpassableRoads(); for (AvoidRoadInfo info : impassableRoads.values()) { - blockedRoads.add(new ABlockedRoadParams(info.id, info.latitude, info.longitude, info.name, info.appModeKey)); + blockedRoads.add(new ABlockedRoadParams(info.id, info.latitude, info.longitude, info.direction, info.name, info.appModeKey)); } return true; } diff --git a/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java b/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java index 22e0607c7f..690c990589 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java +++ b/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java @@ -26,7 +26,6 @@ import net.osmand.data.LatLon; import net.osmand.data.PointDescription; import net.osmand.data.QuadPoint; import net.osmand.data.RotatedTileBox; -import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; @@ -35,6 +34,7 @@ import net.osmand.plus.activities.MapActivity; import net.osmand.plus.mapcontextmenu.MapContextMenu; import net.osmand.plus.routing.RoutingHelper; import net.osmand.plus.routing.RoutingHelper.RouteSegmentSearchResult; +import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.views.layers.ContextMenuLayer; import net.osmand.router.RouteSegmentResult; import net.osmand.router.RoutingConfiguration; @@ -59,7 +59,7 @@ public class AvoidSpecificRoads { loadImpassableRoads(); } - public void loadImpassableRoads(){ + public void loadImpassableRoads() { for (AvoidRoadInfo avoidRoadInfo : app.getSettings().getImpassableRoadPoints()) { impassableRoads.put(new LatLon(avoidRoadInfo.latitude, avoidRoadInfo.longitude), avoidRoadInfo); } @@ -390,7 +390,13 @@ public class AvoidSpecificRoads { if (avoidRoadInfo == null) { avoidRoadInfo = new AvoidRoadInfo(); } - avoidRoadInfo.id = object != null ? object.id : 0; + if (object != null) { + avoidRoadInfo.id = object.id; +// avoidRoadInfo.direction = object.directionRoute(0, true); + } else { + avoidRoadInfo.id = 0; + avoidRoadInfo.direction = Double.NaN; + } avoidRoadInfo.latitude = lat; avoidRoadInfo.longitude = lon; avoidRoadInfo.appModeKey = appModeKey; @@ -400,6 +406,7 @@ public class AvoidSpecificRoads { public static class AvoidRoadInfo { public long id; + public double direction; public double latitude; public double longitude; public String name; diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/ImpassableRoadsStorage.java b/OsmAnd/src/net/osmand/plus/settings/backend/ImpassableRoadsStorage.java index 80242478e5..25586ef35a 100644 --- a/OsmAnd/src/net/osmand/plus/settings/backend/ImpassableRoadsStorage.java +++ b/OsmAnd/src/net/osmand/plus/settings/backend/ImpassableRoadsStorage.java @@ -12,6 +12,7 @@ import java.util.StringTokenizer; class ImpassableRoadsStorage extends SettingsMapPointsStorage { protected String roadsIdsKey; + protected String directionsKey; protected String appModeKey; public ImpassableRoadsStorage(OsmandSettings osmandSettings) { @@ -19,6 +20,7 @@ class ImpassableRoadsStorage extends SettingsMapPointsStorage { pointsKey = OsmandSettings.IMPASSABLE_ROAD_POINTS; descriptionsKey = OsmandSettings.IMPASSABLE_ROADS_DESCRIPTIONS; roadsIdsKey = OsmandSettings.IMPASSABLE_ROADS_IDS; + directionsKey = OsmandSettings.IMPASSABLE_ROADS_DIRECTIONS; appModeKey = OsmandSettings.IMPASSABLE_ROADS_APP_MODE_KEYS; } @@ -37,6 +39,21 @@ class ImpassableRoadsStorage extends SettingsMapPointsStorage { return list; } + public List getDirections(int size) { + List list = new ArrayList<>(); + String directions = getSettingsAPI().getString(getOsmandSettings().getGlobalPreferences(), directionsKey, ""); + if (directions.trim().length() > 0) { + StringTokenizer tok = new StringTokenizer(directions, ","); + while (tok.hasMoreTokens() && list.size() <= size) { + list.add(Double.parseDouble(tok.nextToken())); + } + } + while (list.size() < size) { + list.add(0.0); + } + return list; + } + public List getAppModeKeys(int size) { List list = new ArrayList<>(); String roadIds = getSettingsAPI().getString(getOsmandSettings().getGlobalPreferences(), appModeKey, ""); @@ -55,6 +72,7 @@ class ImpassableRoadsStorage extends SettingsMapPointsStorage { public List getImpassableRoadsInfo() { List points = getPoints(); List roadIds = getRoadIds(points.size()); + List directions = getDirections(points.size()); List appModeKeys = getAppModeKeys(points.size()); List descriptions = getPointDescriptions(points.size()); @@ -68,6 +86,7 @@ class ImpassableRoadsStorage extends SettingsMapPointsStorage { avoidRoadInfo.id = roadIds.get(i); avoidRoadInfo.latitude = latLon.getLatitude(); avoidRoadInfo.longitude = latLon.getLongitude(); + avoidRoadInfo.direction = directions.get(i); avoidRoadInfo.name = description.getName(); avoidRoadInfo.appModeKey = appModeKeys.get(i); avoidRoadsInfo.add(avoidRoadInfo); @@ -79,15 +98,17 @@ class ImpassableRoadsStorage extends SettingsMapPointsStorage { public boolean addImpassableRoadInfo(AvoidRoadInfo avoidRoadInfo) { List points = getPoints(); List roadIds = getRoadIds(points.size()); + List directions = getDirections(points.size()); List appModeKeys = getAppModeKeys(points.size()); List descriptions = getPointDescriptions(points.size()); roadIds.add(0, avoidRoadInfo.id); + directions.add(0, avoidRoadInfo.direction); points.add(0, new LatLon(avoidRoadInfo.latitude, avoidRoadInfo.longitude)); appModeKeys.add(0, avoidRoadInfo.appModeKey); descriptions.add(0, PointDescription.serializeToString(new PointDescription("", avoidRoadInfo.name))); - return saveAvoidRoadData(points, descriptions, roadIds, appModeKeys); + return saveAvoidRoadData(points, descriptions, roadIds, appModeKeys, directions); } public boolean updateImpassableRoadInfo(AvoidRoadInfo avoidRoadInfo) { @@ -96,13 +117,15 @@ class ImpassableRoadsStorage extends SettingsMapPointsStorage { int index = points.indexOf(new LatLon(avoidRoadInfo.latitude, avoidRoadInfo.longitude)); if (index != -1) { List roadIds = getRoadIds(points.size()); + List directions = getDirections(points.size()); List appModeKeys = getAppModeKeys(points.size()); List descriptions = getPointDescriptions(points.size()); roadIds.set(index, avoidRoadInfo.id); + directions.set(index, avoidRoadInfo.direction); appModeKeys.set(index, avoidRoadInfo.appModeKey); descriptions.set(index, PointDescription.serializeToString(new PointDescription("", avoidRoadInfo.name))); - return saveAvoidRoadData(points, descriptions, roadIds, appModeKeys); + return saveAvoidRoadData(points, descriptions, roadIds, appModeKeys, directions); } return false; } @@ -111,15 +134,17 @@ class ImpassableRoadsStorage extends SettingsMapPointsStorage { public boolean deletePoint(int index) { List points = getPoints(); List roadIds = getRoadIds(points.size()); + List directions = getDirections(points.size()); List appModeKeys = getAppModeKeys(points.size()); List descriptions = getPointDescriptions(points.size()); if (index < points.size()) { points.remove(index); roadIds.remove(index); + directions.remove(index); appModeKeys.remove(index); descriptions.remove(index); - return saveAvoidRoadData(points, descriptions, roadIds, appModeKeys); + return saveAvoidRoadData(points, descriptions, roadIds, appModeKeys, directions); } return false; } @@ -128,6 +153,7 @@ class ImpassableRoadsStorage extends SettingsMapPointsStorage { public boolean deletePoint(LatLon latLon) { List points = getPoints(); List roadIds = getRoadIds(points.size()); + List directions = getDirections(points.size()); List appModeKeys = getAppModeKeys(points.size()); List descriptions = getPointDescriptions(points.size()); @@ -135,9 +161,10 @@ class ImpassableRoadsStorage extends SettingsMapPointsStorage { if (index != -1) { points.remove(index); roadIds.remove(index); + directions.remove(index); appModeKeys.remove(index); descriptions.remove(index); - return saveAvoidRoadData(points, descriptions, roadIds, appModeKeys); + return saveAvoidRoadData(points, descriptions, roadIds, appModeKeys, directions); } return false; } @@ -146,21 +173,23 @@ class ImpassableRoadsStorage extends SettingsMapPointsStorage { public boolean movePoint(LatLon latLonEx, LatLon latLonNew) { List points = getPoints(); List roadIds = getRoadIds(points.size()); + List directions = getDirections(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 saveAvoidRoadData(points, descriptions, roadIds, appModeKeys); + return saveAvoidRoadData(points, descriptions, roadIds, appModeKeys, directions); } else { return false; } } - public boolean saveAvoidRoadData(List points, List descriptions, - List roadIds, List appModeKeys) { - return savePoints(points, descriptions) && saveRoadIds(roadIds) && saveAppModeKeys(appModeKeys); + public boolean saveAvoidRoadData(List points, List descriptions, List roadIds, + List appModeKeys, List directions) { + return savePoints(points, descriptions) && saveRoadIds(roadIds) + && saveAppModeKeys(appModeKeys) && saveDirections(directions); } public boolean saveRoadIds(List roadIds) { @@ -177,6 +206,20 @@ class ImpassableRoadsStorage extends SettingsMapPointsStorage { .commit(); } + public boolean saveDirections(List directions) { + StringBuilder stringBuilder = new StringBuilder(); + Iterator iterator = directions.iterator(); + while (iterator.hasNext()) { + stringBuilder.append(iterator.next()); + if (iterator.hasNext()) { + stringBuilder.append(","); + } + } + return getSettingsAPI().edit(getOsmandSettings().getGlobalPreferences()) + .putString(directionsKey, stringBuilder.toString()) + .commit(); + } + public boolean saveAppModeKeys(List appModeKeys) { StringBuilder stringBuilder = new StringBuilder(); Iterator iterator = appModeKeys.iterator(); diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java index 554b96fc63..fbac3348e8 100644 --- a/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java @@ -2106,6 +2106,7 @@ public class OsmandSettings { public static final String IMPASSABLE_ROAD_POINTS = "impassable_road_points"; public static final String IMPASSABLE_ROADS_DESCRIPTIONS = "impassable_roads_descriptions"; public static final String IMPASSABLE_ROADS_IDS = "impassable_roads_ids"; + public static final String IMPASSABLE_ROADS_DIRECTIONS = "impassable_roads_directions"; public static final String IMPASSABLE_ROADS_APP_MODE_KEYS = "impassable_roads_app_mode_keys"; public void backupPointToStart() { diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/backup/AvoidRoadsSettingsItem.java b/OsmAnd/src/net/osmand/plus/settings/backend/backup/AvoidRoadsSettingsItem.java index 6507ab874e..0f88f0e961 100644 --- a/OsmAnd/src/net/osmand/plus/settings/backend/backup/AvoidRoadsSettingsItem.java +++ b/OsmAnd/src/net/osmand/plus/settings/backend/backup/AvoidRoadsSettingsItem.java @@ -9,6 +9,7 @@ import net.osmand.data.LatLon; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.helpers.AvoidSpecificRoads; +import net.osmand.plus.helpers.AvoidSpecificRoads.AvoidRoadInfo; import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.settings.backend.OsmandSettings; @@ -19,16 +20,16 @@ import org.json.JSONObject; import java.util.ArrayList; import java.util.List; -public class AvoidRoadsSettingsItem extends CollectionSettingsItem { +public class AvoidRoadsSettingsItem extends CollectionSettingsItem { private OsmandSettings settings; private AvoidSpecificRoads specificRoads; - public AvoidRoadsSettingsItem(@NonNull OsmandApplication app, @NonNull List items) { + public AvoidRoadsSettingsItem(@NonNull OsmandApplication app, @NonNull List items) { super(app, null, items); } - public AvoidRoadsSettingsItem(@NonNull OsmandApplication app, @Nullable AvoidRoadsSettingsItem baseItem, @NonNull List items) { + public AvoidRoadsSettingsItem(@NonNull OsmandApplication app, @Nullable AvoidRoadsSettingsItem baseItem, @NonNull List items) { super(app, baseItem, items); } @@ -64,16 +65,16 @@ public class AvoidRoadsSettingsItem extends CollectionSettingsItem newItems = getNewItems(); + List newItems = getNewItems(); if (!newItems.isEmpty() || !duplicateItems.isEmpty()) { appliedItems = new ArrayList<>(newItems); - for (AvoidSpecificRoads.AvoidRoadInfo duplicate : duplicateItems) { + for (AvoidRoadInfo duplicate : duplicateItems) { LatLon latLon = new LatLon(duplicate.latitude, duplicate.longitude); if (settings.removeImpassableRoad(latLon)) { settings.addImpassableRoad(duplicate); } } - for (AvoidSpecificRoads.AvoidRoadInfo avoidRoad : appliedItems) { + for (AvoidRoadInfo avoidRoad : appliedItems) { settings.addImpassableRoad(avoidRoad); } specificRoads.loadImpassableRoads(); @@ -82,8 +83,8 @@ public class AvoidRoadsSettingsItem extends CollectionSettingsItem