Add default attributes to custom routing config

This commit is contained in:
Vitaliy 2020-02-17 11:43:08 +02:00
parent 30d063f2af
commit 2f3474766e
11 changed files with 83 additions and 44 deletions

View file

@ -55,7 +55,15 @@ public class RoutingConfiguration {
private String defaultRouter = "";
private Map<String, GeneralRouter> routers = new LinkedHashMap<>();
private Map<String, String> attributes = new LinkedHashMap<>();
private static HashMap<Long, Location> impassableRoadLocations = new HashMap<>();
private HashMap<Long, Location> impassableRoadLocations = new HashMap<>();
public Builder() {
}
public Builder(Map<String, String> defaultAttributes) {
attributes.putAll(defaultAttributes);
}
// Example
// {
@ -103,19 +111,22 @@ public class RoutingConfiguration {
return i;
}
public static Map<Long, Location> getImpassableRoadLocations() {
public Map<Long, Location> getImpassableRoadLocations() {
return impassableRoadLocations;
}
public static boolean addImpassableRoad(RouteDataObject route, Location location) {
public boolean addImpassableRoad(RouteDataObject route, Location location) {
if (!impassableRoadLocations.containsKey(route.id)){
impassableRoadLocations.put(route.id, location);
return true;
}
return false;
}
public Map<String, String> getAttributes() {
return attributes;
}
private String getAttribute(VehicleRouter router, String propertyName) {
if (router.containsAttribute(propertyName)) {
return router.getAttribute(propertyName);
@ -148,7 +159,7 @@ public class RoutingConfiguration {
return routers;
}
public static void removeImpassableRoad(RouteDataObject obj) {
public void removeImpassableRoad(RouteDataObject obj) {
impassableRoadLocations.remove(obj.id);
}
}

View file

@ -599,8 +599,9 @@ public class AppInitializer implements IProgress {
@Override
protected Map<String, RoutingConfiguration.Builder> doInBackground(Void... voids) {
RoutingConfiguration.getDefault(); // load default builder
Map<String, String> defaultAttributes = getDefaultAttributes();
Map<String, RoutingConfiguration.Builder> customConfigs = new HashMap<>();
File routingFolder = app.getAppPath(IndexConstants.ROUTING_PROFILES_DIR);
if (routingFolder.isDirectory()) {
File[] fl = routingFolder.listFiles();
@ -609,7 +610,7 @@ public class AppInitializer implements IProgress {
if (f.isFile() && f.getName().endsWith(IndexConstants.ROUTING_FILE_EXT) && f.canRead()) {
try {
String fileName = f.getName();
RoutingConfiguration.Builder builder = new RoutingConfiguration.Builder();
RoutingConfiguration.Builder builder = new RoutingConfiguration.Builder(defaultAttributes);
RoutingConfiguration.parseFromInputStream(new FileInputStream(f), fileName, builder);
customConfigs.put(fileName, builder);
@ -630,6 +631,19 @@ public class AppInitializer implements IProgress {
}
callback.onRoutingFilesLoaded();
}
private Map<String, String> getDefaultAttributes() {
Map<String, String> defaultAttributes = new HashMap<>();
RoutingConfiguration.Builder builder = RoutingConfiguration.getDefault();
for (Map.Entry<String, String> entry : builder.getAttributes().entrySet()) {
String key = entry.getKey();
if (!"routerName".equals(key)) {
defaultAttributes.put(key, entry.getValue());
}
}
return defaultAttributes;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

View file

@ -67,6 +67,7 @@ import net.osmand.plus.routing.TransportRoutingHelper;
import net.osmand.plus.search.QuickSearchHelper;
import net.osmand.plus.voice.CommandPlayer;
import net.osmand.plus.wikivoyage.data.TravelDbHelper;
import net.osmand.router.GeneralRouter;
import net.osmand.router.RoutingConfiguration;
import net.osmand.router.RoutingConfiguration.Builder;
import net.osmand.search.SearchUICore;
@ -79,6 +80,7 @@ import java.io.FileWriter;
import java.io.PrintStream;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
@ -811,6 +813,12 @@ public class OsmandApplication extends MultiDexApplication {
return localizedResources != null ? localizedResources : super.getResources();
}
public List<RoutingConfiguration.Builder> getAllRoutingConfigs() {
List<RoutingConfiguration.Builder> builders = new ArrayList<>(customRoutingConfigs.values());
builders.add(0, getDefaultRoutingConfig());
return builders;
}
public synchronized RoutingConfiguration.Builder getDefaultRoutingConfig() {
return RoutingConfiguration.getDefault();
}
@ -836,6 +844,19 @@ public class OsmandApplication extends MultiDexApplication {
return builder != null ? builder : getDefaultRoutingConfig();
}
public static GeneralRouter getRouter(OsmandApplication app, ApplicationMode mode) {
Builder builder = app.getRoutingConfigForMode(mode);
return getRouter(builder, mode);
}
public static GeneralRouter getRouter(Builder builder, ApplicationMode am) {
GeneralRouter router = builder.getRouter(am.getRoutingProfile());
if (router == null && am.getParent() != null) {
router = builder.getRouter(am.getParent().getStringKey());
}
return router;
}
public OsmandRegions getRegions() {
return regions;
}

View file

@ -52,7 +52,6 @@ import net.osmand.router.GeneralRouter;
import net.osmand.router.GeneralRouter.GeneralRouterProfile;
import net.osmand.router.GeneralRouter.RoutingParameter;
import net.osmand.router.GeneralRouter.RoutingParameterType;
import net.osmand.router.RoutingConfiguration;
import net.osmand.util.Algorithms;
import java.util.ArrayList;
@ -315,7 +314,7 @@ public class SettingsNavigationActivity extends SettingsBaseActivity {
cat.addPreference(fastRoute);
} else {
ApplicationMode am = settings.getApplicationMode();
GeneralRouter router = getRouter(getMyApplication(), am);
GeneralRouter router = OsmandApplication.getRouter(settings.getContext(), am);
clearParameters();
if (router != null) {
GeneralRouterProfile routerProfile = router.getProfile();
@ -431,20 +430,7 @@ public class SettingsNavigationActivity extends SettingsBaseActivity {
reliefFactorParameters.clear();
}
public static GeneralRouter getRouter(OsmandApplication app, ApplicationMode mode) {
RoutingConfiguration.Builder builder = app.getRoutingConfigForMode(mode);
return getRouter(builder, mode);
}
public static GeneralRouter getRouter(net.osmand.router.RoutingConfiguration.Builder builder, ApplicationMode am) {
GeneralRouter router = builder.getRouter(am.getRoutingProfile());
if (router == null && am.getParent() != null) {
router = builder.getRouter(am.getParent().getStringKey());
}
return router;
}
public void updateAllSettings() {
public void updateAllSettings() {
prepareRoutingPrefs(getPreferenceScreen());
reloadVoiceListPreference(getPreferenceScreen());
super.updateAllSettings();
@ -733,7 +719,7 @@ public class SettingsNavigationActivity extends SettingsBaseActivity {
final OsmandApplication app = (OsmandApplication) activity.getApplication();
final OsmandSettings settings = app.getSettings();
GeneralRouter router = getRouter(app, mode);
GeneralRouter router = OsmandApplication.getRouter(app, mode);
SpeedConstants units = settings.SPEED_SYSTEM.getModeValue(mode);
String speedUnits = units.toShortString(activity);
final float[] ratio = new float[1];

View file

@ -163,7 +163,9 @@ public class AvoidSpecificRoads {
app.getSettings().removeImpassableRoad(latLon);
RouteDataObject obj = impassableRoads.remove(latLon);
if (obj != null) {
RoutingConfiguration.Builder.removeImpassableRoad(obj);
for (RoutingConfiguration.Builder builder : app.getAllRoutingConfigs()) {
builder.removeImpassableRoad(obj);
}
}
}
@ -289,7 +291,9 @@ public class AvoidSpecificRoads {
final LatLon oldLoc = getLocation(currentObject);
app.getSettings().moveImpassableRoad(oldLoc, newLoc);
impassableRoads.remove(oldLoc);
RoutingConfiguration.Builder.removeImpassableRoad(currentObject);
for (RoutingConfiguration.Builder builder : app.getAllRoutingConfigs()) {
builder.removeImpassableRoad(currentObject);
}
addImpassableRoadInternal(object, ll, showDialog, activity, newLoc);
if (callback != null) {
@ -311,7 +315,11 @@ public class AvoidSpecificRoads {
boolean showDialog,
@Nullable MapActivity activity,
@NonNull LatLon loc) {
if (RoutingConfiguration.Builder.addImpassableRoad(object, ll)) {
boolean roadAdded = false;
for (RoutingConfiguration.Builder builder : app.getAllRoutingConfigs()) {
roadAdded |= builder.addImpassableRoad(object, ll);
}
if (roadAdded) {
impassableRoads.put(loc, object);
} else {
LatLon location = getLocation(object);
@ -340,7 +348,13 @@ public class AvoidSpecificRoads {
}
public LatLon getLocation(RouteDataObject object) {
Location location = RoutingConfiguration.Builder.getImpassableRoadLocations().get(object.getId());
Location location = null;
for (RoutingConfiguration.Builder builder : app.getAllRoutingConfigs()) {
location = builder.getImpassableRoadLocations().get(object.getId());
if (location != null) {
break;
}
}
return location == null ? null : new LatLon(location.getLatitude(), location.getLongitude());
}

View file

@ -34,7 +34,6 @@ import net.osmand.data.FavouritePoint;
import net.osmand.plus.AppInitializer;
import net.osmand.plus.AppInitializer.AppInitializeListener;
import net.osmand.plus.AppInitializer.InitEvents;
import net.osmand.plus.ApplicationMode;
import net.osmand.plus.FavouritesDbHelper;
import net.osmand.plus.GPXDatabase;
import net.osmand.plus.OsmandApplication;
@ -51,9 +50,7 @@ import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.DividerHalfItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.ShortDescriptionItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem;
import net.osmand.plus.profiles.AdditionalDataWrapper;
import net.osmand.plus.profiles.ExportImportProfileBottomSheet;
import net.osmand.plus.quickaction.QuickAction;
import net.osmand.plus.rastermaps.OsmandRasterMapsPlugin;
import net.osmand.plus.views.OsmandMapTileView;
import net.osmand.router.RoutingConfiguration;

View file

@ -31,7 +31,6 @@ import net.osmand.plus.TargetPointsHelper;
import net.osmand.plus.UiUtilities;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.SettingsBaseActivity;
import net.osmand.plus.activities.SettingsNavigationActivity;
import net.osmand.plus.dashboard.DashboardOnMap;
import net.osmand.plus.download.DownloadActivity;
import net.osmand.plus.download.DownloadActivityType;
@ -408,7 +407,7 @@ public class RoutingOptionsHelper {
public LocalRoutingParameter getRoutingParameterInnerById(ApplicationMode am, String parameterId) {
RouteProvider.GPXRouteParamsBuilder rparams = app.getRoutingHelper().getCurrentGPXRoute();
GeneralRouter rm = SettingsNavigationActivity.getRouter(app, am);
GeneralRouter rm = OsmandApplication.getRouter(app, am);
if (rm == null || (rparams != null && !rparams.isCalculateOsmAndRoute()) && !rparams.getFile().hasRtePt()) {
return null;
}
@ -492,7 +491,7 @@ public class RoutingOptionsHelper {
RouteProvider.GPXRouteParamsBuilder rparams = app.getRoutingHelper().getCurrentGPXRoute();
List<LocalRoutingParameter> list = new ArrayList<LocalRoutingParameter>(getGpxRouterParameters(am));
GeneralRouter rm = SettingsNavigationActivity.getRouter(app, am);
GeneralRouter rm = OsmandApplication.getRouter(app, am);
if (rm == null || (rparams != null && !rparams.isCalculateOsmAndRoute()) && !rparams.getFile().hasRtePt()) {
return list;
}
@ -582,7 +581,7 @@ public class RoutingOptionsHelper {
public List<GeneralRouter.RoutingParameter> getAvoidRoutingPrefsForAppMode(ApplicationMode applicationMode) {
List<GeneralRouter.RoutingParameter> avoidParameters = new ArrayList<GeneralRouter.RoutingParameter>();
GeneralRouter router = SettingsNavigationActivity.getRouter(app, applicationMode);
GeneralRouter router = OsmandApplication.getRouter(app, applicationMode);
if (router != null) {
for (Map.Entry<String, GeneralRouter.RoutingParameter> e : router.getParameters().entrySet()) {
String param = e.getKey();
@ -596,7 +595,7 @@ public class RoutingOptionsHelper {
}
public GeneralRouter.RoutingParameter getRoutingPrefsForAppModeById(ApplicationMode applicationMode, String parameterId) {
GeneralRouter router = SettingsNavigationActivity.getRouter(app, applicationMode);
GeneralRouter router = OsmandApplication.getRouter(app, applicationMode);
GeneralRouter.RoutingParameter parameter = null;
if (router != null) {

View file

@ -27,7 +27,6 @@ import net.osmand.plus.R;
import net.osmand.plus.TargetPointsHelper;
import net.osmand.plus.TargetPointsHelper.TargetPoint;
import net.osmand.plus.Version;
import net.osmand.plus.activities.SettingsNavigationActivity;
import net.osmand.plus.render.NativeOsmandLibrary;
import net.osmand.router.GeneralRouter;
import net.osmand.router.GeneralRouter.RoutingParameter;
@ -601,7 +600,7 @@ public class RouteProvider {
router.setUseFastRecalculation(settings.USE_FAST_RECALCULATION.get());
RoutingConfiguration.Builder config = params.ctx.getRoutingConfigForMode(params.mode);
GeneralRouter generalRouter = SettingsNavigationActivity.getRouter(config, params.mode);
GeneralRouter generalRouter = OsmandApplication.getRouter(config, params.mode);
if (generalRouter == null) {
return applicationModeNotSupported(params);
}

View file

@ -220,8 +220,7 @@ public class NavigationFragment extends BaseSettingsFragment {
false, null));
}
collectRoutingProfilesFromConfig(context, context.getDefaultRoutingConfig(), profilesObjects);
for (RoutingConfiguration.Builder builder : context.getCustomRoutingConfigs().values()) {
for (RoutingConfiguration.Builder builder : context.getAllRoutingConfigs()) {
collectRoutingProfilesFromConfig(context, builder, profilesObjects);
}
return profilesObjects;

View file

@ -139,7 +139,7 @@ public class RouteParametersFragment extends BaseSettingsFragment implements OnP
if (am.getRouteService() != RouteProvider.RouteService.OSMAND) {
screen.addPreference(fastRoute);
} else {
GeneralRouter router = SettingsNavigationActivity.getRouter(app, am);
GeneralRouter router = OsmandApplication.getRouter(app, am);
clearParameters();
if (router != null) {
Map<String, RoutingParameter> parameters = router.getParameters();

View file

@ -18,7 +18,6 @@ import net.osmand.router.GeneralRouter;
import java.util.Map;
import static net.osmand.plus.activities.SettingsNavigationActivity.getRouter;
import static net.osmand.plus.activities.SettingsNavigationActivity.showSeekbarSettingsDialog;
public class VehicleParametersFragment extends BaseSettingsFragment implements OnPreferenceChanged {
@ -42,7 +41,7 @@ public class VehicleParametersFragment extends BaseSettingsFragment implements O
RouteService routeService = mode.getRouteService();
if (routeService == RouteService.OSMAND) {
GeneralRouter router = getRouter(app, mode);
GeneralRouter router = OsmandApplication.getRouter(app, mode);
if (router != null) {
Map<String, GeneralRouter.RoutingParameter> parameters = router.getParameters();