Merge pull request #8497 from osmandapp/avoid_cached_roads

Avoid saved roads
This commit is contained in:
max-klaus 2020-02-19 13:53:47 +03:00 committed by GitHub
commit c2b9cb1035
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 335 additions and 123 deletions

View file

@ -1,8 +1,6 @@
package net.osmand.router;
import net.osmand.Location;
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;
@ -13,9 +11,10 @@ import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
public class RoutingConfiguration {
@ -55,7 +54,7 @@ public class RoutingConfiguration {
private String defaultRouter = "";
private Map<String, GeneralRouter> routers = new LinkedHashMap<>();
private Map<String, String> attributes = new LinkedHashMap<>();
private HashMap<Long, Location> impassableRoadLocations = new HashMap<>();
private Set<Long> impassableRoadLocations = new HashSet<>();
public Builder() {
@ -95,7 +94,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) {
@ -110,17 +109,13 @@ public class RoutingConfiguration {
// i.planRoadDirection = 1;
return i;
}
public Map<Long, Location> getImpassableRoadLocations() {
public Set<Long> getImpassableRoadLocations() {
return impassableRoadLocations;
}
public boolean addImpassableRoad(RouteDataObject route, Location location) {
if (!impassableRoadLocations.containsKey(route.id)){
impassableRoadLocations.put(route.id, location);
return true;
}
return false;
public boolean addImpassableRoad(long routeId) {
return impassableRoadLocations.add(routeId);
}
public Map<String, String> getAttributes() {
@ -159,8 +154,8 @@ public class RoutingConfiguration {
return routers;
}
public void removeImpassableRoad(RouteDataObject obj) {
impassableRoadLocations.remove(obj.id);
public void removeImpassableRoad(long routeId) {
impassableRoadLocations.remove(routeId);
}
}

View file

@ -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();
checkLiveUpdatesAlerts();
} catch (RuntimeException e) {

View file

@ -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,8 @@ 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() {
@ -2796,9 +2799,184 @@ 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 = getPoints();
List<Long> roadIds = getRoadIds(points.size());
List<String> appModeKeys = getAppModeKeys(points.size());
List<String> descriptions = getPointDescriptions(points.size());
List<AvoidRoadInfo> 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.latitude = latLon.getLatitude();
avoidRoadInfo.longitude = latLon.getLongitude();
avoidRoadInfo.name = description.getName();
avoidRoadInfo.appModeKey = appModeKeys.get(i);
avoidRoadsInfo.add(avoidRoadInfo);
}
return avoidRoadsInfo;
}
public boolean addImpassableRoadInfo(AvoidRoadInfo avoidRoadInfo) {
List<LatLon> points = getPoints();
List<Long> roadIds = getRoadIds(points.size());
List<String> appModeKeys = getAppModeKeys(points.size());
List<String> descriptions = getPointDescriptions(points.size());
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 saveAvoidRoadData(points, descriptions, roadIds, appModeKeys);
}
public boolean updateImpassableRoadInfo(AvoidRoadInfo avoidRoadInfo) {
List<LatLon> points = getPoints();
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 saveAvoidRoadData(points, descriptions, roadIds, appModeKeys);
}
return false;
}
@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 saveAvoidRoadData(points, descriptions, roadIds, appModeKeys);
}
return false;
}
@Override
public boolean deletePoint(LatLon latLon) {
List<LatLon> points = getPoints();
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 saveAvoidRoadData(points, descriptions, roadIds, appModeKeys);
}
return false;
}
@Override
public boolean movePoint(LatLon latLonEx, LatLon latLonNew) {
List<LatLon> points = getPoints();
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 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();
while (iterator.hasNext()) {
stringBuilder.append(iterator.next());
if (iterator.hasNext()) {
stringBuilder.append(",");
}
}
return settingsAPI.edit(globalPreferences)
.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();
}
}
@ -2989,11 +3167,16 @@ public class OsmandSettings {
return settingsAPI.edit(globalPreferences).putInt(POINT_NAVIGATE_ROUTE, NAVIGATE).commit();
}
public List<LatLon> getImpassableRoadPoints() {
return mImpassableRoadsStorage.getPoints();
public List<AvoidRoadInfo> 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 updateImpassableRoadInfo(AvoidRoadInfo avoidRoadInfo) {
return mImpassableRoadsStorage.updateImpassableRoadInfo(avoidRoadInfo);
}
public boolean removeImpassableRoad(int index) {

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

@ -118,6 +118,7 @@ public class DownloadIndexesThread {
if (app.getDownloadService() != null) {
app.getDownloadService().stopService(app);
}
app.getAvoidSpecificRoads().initRouteObjects(true);
}
public void initSettingsFirstMap(WorldRegion reg) {

View file

@ -50,32 +50,44 @@ public class AvoidSpecificRoads {
private OsmandApplication app;
private Map<LatLon, RouteDataObject> impassableRoads = new LinkedHashMap<>();
private Map<LatLon, AvoidRoadInfo> 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.latitude, avoidRoadInfo.longitude), avoidRoadInfo);
}
}
public Map<LatLon, RouteDataObject> getImpassableRoads() {
public Map<LatLon, AvoidRoadInfo> getImpassableRoads() {
return impassableRoads;
}
public void initRouteObjects() {
for (LatLon latLon : impassableRoads.keySet()) {
addImpassableRoad(null, latLon, false, true);
public void initRouteObjects(boolean force) {
for (Map.Entry<LatLon, AvoidRoadInfo> 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);
}
}
}
private ArrayAdapter<LatLon> createAdapter(MapActivity mapActivity, boolean nightMode) {
final ArrayList<LatLon> points = new ArrayList<>(impassableRoads.keySet());
private ArrayAdapter<AvoidRoadInfo> createAdapter(MapActivity mapActivity, boolean nightMode) {
final ArrayList<AvoidRoadInfo> 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<LatLon>(themedContext, R.layout.waypoint_reached, R.id.title, points) {
return new ArrayAdapter<AvoidRoadInfo>(themedContext, R.layout.waypoint_reached, R.id.title, points) {
@NonNull
@Override
public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
@ -83,12 +95,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.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);
ImageButton remove = (ImageButton) v.findViewById(R.id.info_close);
remove.setVisibility(View.VISIBLE);
remove.setImageDrawable(getIcon(R.drawable.ic_action_remove_dark));
@ -117,25 +132,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();
@ -161,15 +166,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));
}
@ -182,13 +187,13 @@ public class AvoidSpecificRoads {
if (impassableRoads.isEmpty()) {
bld.setMessage(R.string.avoid_roads_msg);
} else {
final ArrayAdapter<LatLon> listAdapter = createAdapter(mapActivity, nightMode);
final ArrayAdapter<AvoidRoadInfo> 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.latitude, point.longitude, point.name);
}
dialog.dismiss();
}
@ -210,20 +215,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 = appModeKey != null ? ApplicationMode.valueOfStringKey(appModeKey, defaultAppMode) : defaultAppMode;
List<RouteSegmentResult> roads = app.getRoutingHelper().getRoute().getOriginalRoute();
if (mapActivity != null && roads != null) {
@ -236,9 +244,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(), appMode.getStringKey());
addImpassableRoadInternal(avoidRoadInfo, showDialog, mapActivity, newLoc);
if (!skipWritingSettings) {
app.getSettings().addImpassableRoad(newLoc.getLatitude(), newLoc.getLongitude());
app.getSettings().addImpassableRoad(avoidRoadInfo);
}
return;
}
@ -252,7 +263,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(), appMode.getStringKey());
addImpassableRoadInternal(avoidRoadInfo, showDialog, mapActivity, loc);
}
return true;
}
@ -264,19 +276,22 @@ public class AvoidSpecificRoads {
});
if (!skipWritingSettings) {
app.getSettings().addImpassableRoad(loc.getLatitude(), loc.getLongitude());
AvoidRoadInfo avoidRoadInfo = getAvoidRoadInfoForDataObject(null, loc.getLatitude(), loc.getLongitude(), appMode.getStringKey());
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) {
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>() {
@ -292,12 +307,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(), appMode.getStringKey());
addImpassableRoadInternal(avoidRoadInfo, showDialog, activity, newLoc);
if (callback != null) {
callback.onAddImpassableRoad(true, object);
callback.onAddImpassableRoad(true, avoidRoadInfo);
}
}
return true;
@ -310,19 +326,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);
}
@ -347,21 +363,40 @@ 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.latitude, avoidRoadInfo.longitude);
}
}
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 getAvoidRoadInfoForDataObject(@Nullable RouteDataObject object, double lat, double lon, String appModeKey) {
AvoidRoadInfo avoidRoadInfo = impassableRoads.get(new LatLon(lat, lon));
if (avoidRoadInfo == null) {
avoidRoadInfo = new AvoidRoadInfo();
}
avoidRoadInfo.id = object != null ? object.id : 0;
avoidRoadInfo.latitude = lat;
avoidRoadInfo.longitude = lon;
avoidRoadInfo.appModeKey = appModeKey;
avoidRoadInfo.name = getRoadName(object);
return avoidRoadInfo;
}
public static class AvoidRoadInfo {
public long id;
public double latitude;
public double longitude;
public String name;
public String appModeKey;
}
}

View file

@ -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) {

View file

@ -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

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

View file

@ -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<LatLon, RouteDataObject> impassableRoads = new TreeMap<>();
Map<LatLon, AvoidRoadInfo> 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<LatLon, RouteDataObject> impassableRoads, final LocalRoutingParameter parameter, final RouteMenuAppModes mode, final LinearLayout item) {
OsmandApplication app = mapActivity.getMyApplication();
Iterator<RouteDataObject> it = impassableRoads.values().iterator();
private void createImpassableRoadsItems(MapActivity mapActivity, Map<LatLon, AvoidRoadInfo> impassableRoads,
final LocalRoutingParameter parameter, final RouteMenuAppModes mode, final LinearLayout item) {
Iterator<AvoidRoadInfo> 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);
}

View file

@ -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<LatLon, RouteDataObject> entry : avoidSpecificRoads.getImpassableRoads().entrySet()) {
for (Map.Entry<LatLon, AvoidRoadInfo> 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<LatLon, RouteDataObject> entry : avoidSpecificRoads.getImpassableRoads().entrySet()) {
for (Map.Entry<LatLon, AvoidRoadInfo> 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.latitude, avoidRoadInfo.longitude);
}
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);
}