Save app mode keys for avoid roads

This commit is contained in:
Vitaliy 2020-02-18 18:13:27 +02:00
parent 2e15315a16
commit f429cd69ab
4 changed files with 103 additions and 46 deletions

View file

@ -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<Long> getRoadIds(int size) {
List<Long> 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<String> getAppModeKeys(int size) {
List<String> 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<AvoidRoadInfo> getImpassableRoadsInfo() {
List<LatLon> points = mImpassableRoadsStorage.getPoints();
List<String> descriptions = mImpassableRoadsStorage.getPointDescriptions(points.size());
List<LatLon> points = getPoints();
List<Long> roadIds = getRoadIds(points.size());
List<String> appModeKeys = getAppModeKeys(points.size());
List<String> descriptions = getPointDescriptions(points.size());
List<AvoidRoadInfo> 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<LatLon> points = getPoints();
List<String> descriptions = getPointDescriptions(points.size());
List<Long> roadIds = getRoadIds(points.size());
List<String> appModeKeys = getAppModeKeys(points.size());
List<String> 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<LatLon> points = getPoints();
List<Long> roadIds = getRoadIds(points.size());
List<String> descriptions = getPointDescriptions(points.size());
int index = points.indexOf(new LatLon(avoidRoadInfo.latitude, avoidRoadInfo.longitude));
if (index != -1) {
List<Long> roadIds = getRoadIds(points.size());
List<String> appModeKeys = getAppModeKeys(points.size());
List<String> 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<Long> getRoadIds(int size) {
List<Long> 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<LatLon> points = getPoints();
List<Long> roadIds = getRoadIds(points.size());
List<String> appModeKeys = getAppModeKeys(points.size());
List<String> 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<LatLon> points = getPoints();
List<String> descriptions = getPointDescriptions(points.size());
List<Long> roadIds = getRoadIds(points.size());
List<String> appModeKeys = getAppModeKeys(points.size());
List<String> 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<LatLon> points = getPoints();
List<String> descriptions = getPointDescriptions(points.size());
List<Long> roadIds = getRoadIds(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 savePoints(points, descriptions) && saveRoadIds(roadIds);
return saveAvoidRoadData(points, descriptions, roadIds, appModeKeys);
} 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 saveRoadIds(List<Long> roadIds) {
StringBuilder stringBuilder = new StringBuilder();
Iterator<Long> iterator = roadIds.iterator();
@ -2929,6 +2964,20 @@ public class OsmandSettings {
.putString(roadsIdsKey, stringBuilder.toString())
.commit();
}
public boolean saveAppModeKeys(List<String> appModeKeys) {
StringBuilder stringBuilder = new StringBuilder();
Iterator<String> 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 {

View file

@ -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);
}
}
});

View file

@ -65,11 +65,12 @@ public class AvoidSpecificRoads {
public void initRouteObjects(boolean force) {
for (Map.Entry<LatLon, AvoidRoadInfo> 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<LatLon>() {
@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<RouteSegmentResult> 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<RouteDataObject>() {
@ -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;
}
}

View file

@ -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);
}
}
};