Add direction to avoid roads
This commit is contained in:
parent
425b98c6dc
commit
c20c0aa6e7
6 changed files with 114 additions and 29 deletions
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -2379,7 +2379,7 @@ public class OsmandAidlApi {
|
|||
public boolean getBlockedRoads(List<ABlockedRoadParams> blockedRoads) {
|
||||
Map<LatLon, AvoidRoadInfo> 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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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<Double> getDirections(int size) {
|
||||
List<Double> 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<String> getAppModeKeys(int size) {
|
||||
List<String> list = new ArrayList<>();
|
||||
String roadIds = getSettingsAPI().getString(getOsmandSettings().getGlobalPreferences(), appModeKey, "");
|
||||
|
@ -55,6 +72,7 @@ class ImpassableRoadsStorage extends SettingsMapPointsStorage {
|
|||
public List<AvoidRoadInfo> getImpassableRoadsInfo() {
|
||||
List<LatLon> points = getPoints();
|
||||
List<Long> roadIds = getRoadIds(points.size());
|
||||
List<Double> directions = getDirections(points.size());
|
||||
List<String> appModeKeys = getAppModeKeys(points.size());
|
||||
List<String> 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<LatLon> points = getPoints();
|
||||
List<Long> roadIds = getRoadIds(points.size());
|
||||
List<Double> directions = getDirections(points.size());
|
||||
List<String> appModeKeys = getAppModeKeys(points.size());
|
||||
List<String> 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<Long> roadIds = getRoadIds(points.size());
|
||||
List<Double> directions = getDirections(points.size());
|
||||
List<String> appModeKeys = getAppModeKeys(points.size());
|
||||
List<String> 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<LatLon> points = getPoints();
|
||||
List<Long> roadIds = getRoadIds(points.size());
|
||||
List<Double> directions = getDirections(points.size());
|
||||
List<String> appModeKeys = getAppModeKeys(points.size());
|
||||
List<String> 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<LatLon> points = getPoints();
|
||||
List<Long> roadIds = getRoadIds(points.size());
|
||||
List<Double> directions = getDirections(points.size());
|
||||
List<String> appModeKeys = getAppModeKeys(points.size());
|
||||
List<String> 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<LatLon> points = getPoints();
|
||||
List<Long> roadIds = getRoadIds(points.size());
|
||||
List<Double> directions = getDirections(points.size());
|
||||
List<String> appModeKeys = getAppModeKeys(points.size());
|
||||
List<String> 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<LatLon> points, List<String> descriptions,
|
||||
List<Long> roadIds, List<String> appModeKeys) {
|
||||
return savePoints(points, descriptions) && saveRoadIds(roadIds) && saveAppModeKeys(appModeKeys);
|
||||
public boolean saveAvoidRoadData(List<LatLon> points, List<String> descriptions, List<Long> roadIds,
|
||||
List<String> appModeKeys, List<Double> directions) {
|
||||
return savePoints(points, descriptions) && saveRoadIds(roadIds)
|
||||
&& saveAppModeKeys(appModeKeys) && saveDirections(directions);
|
||||
}
|
||||
|
||||
public boolean saveRoadIds(List<Long> roadIds) {
|
||||
|
@ -177,6 +206,20 @@ class ImpassableRoadsStorage extends SettingsMapPointsStorage {
|
|||
.commit();
|
||||
}
|
||||
|
||||
public boolean saveDirections(List<Double> directions) {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
Iterator<Double> 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<String> appModeKeys) {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
Iterator<String> iterator = appModeKeys.iterator();
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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<AvoidSpecificRoads.AvoidRoadInfo> {
|
||||
public class AvoidRoadsSettingsItem extends CollectionSettingsItem<AvoidRoadInfo> {
|
||||
|
||||
private OsmandSettings settings;
|
||||
private AvoidSpecificRoads specificRoads;
|
||||
|
||||
public AvoidRoadsSettingsItem(@NonNull OsmandApplication app, @NonNull List<AvoidSpecificRoads.AvoidRoadInfo> items) {
|
||||
public AvoidRoadsSettingsItem(@NonNull OsmandApplication app, @NonNull List<AvoidRoadInfo> items) {
|
||||
super(app, null, items);
|
||||
}
|
||||
|
||||
public AvoidRoadsSettingsItem(@NonNull OsmandApplication app, @Nullable AvoidRoadsSettingsItem baseItem, @NonNull List<AvoidSpecificRoads.AvoidRoadInfo> items) {
|
||||
public AvoidRoadsSettingsItem(@NonNull OsmandApplication app, @Nullable AvoidRoadsSettingsItem baseItem, @NonNull List<AvoidRoadInfo> items) {
|
||||
super(app, baseItem, items);
|
||||
}
|
||||
|
||||
|
@ -64,16 +65,16 @@ public class AvoidRoadsSettingsItem extends CollectionSettingsItem<AvoidSpecific
|
|||
|
||||
@Override
|
||||
public void apply() {
|
||||
List<AvoidSpecificRoads.AvoidRoadInfo> newItems = getNewItems();
|
||||
List<AvoidRoadInfo> 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<AvoidSpecific
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean isDuplicate(@NonNull AvoidSpecificRoads.AvoidRoadInfo item) {
|
||||
for (AvoidSpecificRoads.AvoidRoadInfo roadInfo : existingItems) {
|
||||
public boolean isDuplicate(@NonNull AvoidRoadInfo item) {
|
||||
for (AvoidRoadInfo roadInfo : existingItems) {
|
||||
if (roadInfo.id == item.id) {
|
||||
return true;
|
||||
}
|
||||
|
@ -103,7 +104,7 @@ public class AvoidRoadsSettingsItem extends CollectionSettingsItem<AvoidSpecific
|
|||
|
||||
@NonNull
|
||||
@Override
|
||||
public AvoidSpecificRoads.AvoidRoadInfo renameItem(@NonNull AvoidSpecificRoads.AvoidRoadInfo item) {
|
||||
public AvoidRoadInfo renameItem(@NonNull AvoidRoadInfo item) {
|
||||
return item;
|
||||
}
|
||||
|
||||
|
@ -118,13 +119,15 @@ public class AvoidRoadsSettingsItem extends CollectionSettingsItem<AvoidSpecific
|
|||
JSONObject object = jsonArray.getJSONObject(i);
|
||||
double latitude = object.optDouble("latitude");
|
||||
double longitude = object.optDouble("longitude");
|
||||
double direction = object.optDouble("direction");
|
||||
String name = object.optString("name");
|
||||
String appModeKey = object.optString("appModeKey");
|
||||
long id = object.optLong("roadId");
|
||||
AvoidSpecificRoads.AvoidRoadInfo roadInfo = new AvoidSpecificRoads.AvoidRoadInfo();
|
||||
AvoidRoadInfo roadInfo = new AvoidRoadInfo();
|
||||
roadInfo.id = id;
|
||||
roadInfo.latitude = latitude;
|
||||
roadInfo.longitude = longitude;
|
||||
roadInfo.direction = direction;
|
||||
roadInfo.name = name;
|
||||
if (ApplicationMode.valueOfStringKey(appModeKey, null) != null) {
|
||||
roadInfo.appModeKey = appModeKey;
|
||||
|
@ -144,13 +147,16 @@ public class AvoidRoadsSettingsItem extends CollectionSettingsItem<AvoidSpecific
|
|||
JSONArray jsonArray = new JSONArray();
|
||||
if (!items.isEmpty()) {
|
||||
try {
|
||||
for (AvoidSpecificRoads.AvoidRoadInfo avoidRoad : items) {
|
||||
for (AvoidRoadInfo avoidRoad : items) {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("latitude", avoidRoad.latitude);
|
||||
jsonObject.put("longitude", avoidRoad.longitude);
|
||||
jsonObject.put("name", avoidRoad.name);
|
||||
jsonObject.put("appModeKey", avoidRoad.appModeKey);
|
||||
jsonObject.put("roadId", avoidRoad.id);
|
||||
if (Double.isNaN(avoidRoad.direction)) {
|
||||
jsonObject.put("direction", avoidRoad.direction);
|
||||
}
|
||||
jsonArray.put(jsonObject);
|
||||
}
|
||||
json.put("items", jsonArray);
|
||||
|
|
Loading…
Reference in a new issue