diff --git a/OsmAnd-java/src/net/osmand/router/GeneralRouter.java b/OsmAnd-java/src/net/osmand/router/GeneralRouter.java index 1194c52ea5..ad2364ee0f 100644 --- a/OsmAnd-java/src/net/osmand/router/GeneralRouter.java +++ b/OsmAnd-java/src/net/osmand/router/GeneralRouter.java @@ -101,7 +101,7 @@ public class GeneralRouter implements VehicleRouter { ruleToValue = new ArrayList(); parameters = new LinkedHashMap(); } - + public GeneralRouter(GeneralRouter parent, Map params) { this.attributes = new LinkedHashMap(); Iterator> e = parent.attributes.entrySet().iterator(); @@ -127,6 +127,9 @@ public class GeneralRouter implements VehicleRouter { } + public Map getParameters() { + return parameters; + } public void addAttribute(String k, String v) { attributes.put(k, v); diff --git a/OsmAnd-java/src/net/osmand/router/RoutingConfiguration.java b/OsmAnd-java/src/net/osmand/router/RoutingConfiguration.java index 6498017233..e9cae01794 100644 --- a/OsmAnd-java/src/net/osmand/router/RoutingConfiguration.java +++ b/OsmAnd-java/src/net/osmand/router/RoutingConfiguration.java @@ -98,6 +98,15 @@ public class RoutingConfiguration { return attributes.get(propertyName); } + + public String getDefaultRouter() { + return defaultRouter; + } + + public GeneralRouter getRouter(String applicationMode) { + return routers.get(applicationMode); + + } } private static int parseSilentInt(String t, int v) { @@ -128,7 +137,7 @@ public class RoutingConfiguration { } return DEFAULT; } - + public static RoutingConfiguration.Builder parseFromInputStream(InputStream is) throws IOException, XmlPullParserException { XmlPullParser parser = PlatformUtil.newXMLPullParser(); final RoutingConfiguration.Builder config = new RoutingConfiguration.Builder(); diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index 513f3f6e19..13e3a77263 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -1394,12 +1394,32 @@ public class OsmandSettings { Map> customBooleanRendersProps = new LinkedHashMap>(); public CommonPreference getCustomRenderBooleanProperty(String attrName){ - if(!customRendersProps.containsKey(attrName)){ + if(!customBooleanRendersProps.containsKey(attrName)){ customBooleanRendersProps.put(attrName, new BooleanPreference("nrenderer_"+attrName, false).makeProfile()); } return customBooleanRendersProps.get(attrName); } + Map> customRoutingProps = new LinkedHashMap>(); + public CommonPreference getCustomRoutingProperty(String attrName){ + if(!customRoutingProps.containsKey(attrName)){ + customRoutingProps.put(attrName, new StringPreference("prouting_"+attrName, "").makeProfile()); + } + return customRoutingProps.get(attrName); + } + { +// CommonPreference pref = getCustomRoutingProperty("appMode"); +// pref.setModeDefaultValue(ApplicationMode.CAR, "car"); + } + + Map> customBooleanRoutingProps = new LinkedHashMap>(); + public CommonPreference getCustomRoutingBooleanProperty(String attrName){ + if(!customBooleanRoutingProps.containsKey(attrName)){ + customBooleanRoutingProps.put(attrName, new BooleanPreference("prouting_"+attrName, false).makeProfile()); + } + return customBooleanRoutingProps.get(attrName); + } + public final OsmandPreference VOICE_MUTE = new BooleanPreference("voice_mute", false).makeGlobal(); // for background service diff --git a/OsmAnd/src/net/osmand/plus/activities/SettingsBaseActivity.java b/OsmAnd/src/net/osmand/plus/activities/SettingsBaseActivity.java index e2ba19169f..300663bf9d 100644 --- a/OsmAnd/src/net/osmand/plus/activities/SettingsBaseActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/SettingsBaseActivity.java @@ -82,6 +82,15 @@ public abstract class SettingsBaseActivity extends SherlockPreferenceActivity im booleanPreferences.put(b.getId(), b); return p; } + + public CheckBoxPreference createCheckBoxPreference(OsmandPreference b) { + CheckBoxPreference p = new CheckBoxPreference(this); + p.setKey(b.getId()); + p.setOnPreferenceChangeListener(this); + screenPreferences.put(b.getId(), p); + booleanPreferences.put(b.getId(), b); + return p; + } public void registerSeekBarPreference(OsmandPreference b, PreferenceScreen screen) { SeekBarPreference p = (SeekBarPreference) screen.findPreference(b.getId()); @@ -89,6 +98,32 @@ public abstract class SettingsBaseActivity extends SherlockPreferenceActivity im screenPreferences.put(b.getId(), p); seekBarPreferences.put(b.getId(), b); } + + public static String getRoutingStringPropertyName(Context ctx, String propertyName, String defValue) { + try { + Field f = R.string.class.getField("routing_attr_" + propertyName + "_name"); + if (f != null) { + Integer in = (Integer) f.get(null); + return ctx.getString(in); + } + } catch (Exception e) { + System.err.println(e.getMessage()); + } + return defValue; + } + + public static String getRoutingStringPropertyDescription(Context ctx, String propertyName, String defValue) { + try { + Field f = R.string.class.getField("routing_attr_" + propertyName + "_description"); + if (f != null) { + Integer in = (Integer) f.get(null); + return ctx.getString(in); + } + } catch (Exception e) { + System.err.println(e.getMessage()); + } + return defValue; + } public static String getStringPropertyName(Context ctx, String propertyName, String defValue) { try { @@ -143,6 +178,13 @@ public abstract class SettingsBaseActivity extends SherlockPreferenceActivity im prepareListPreference(b, names, values, p); return p; } + + public ListPreference createListPreference(OsmandPreference b, String[] names, T[] values) { + ListPreference p = new ListPreference(this); + p.setKey(b.getId()); + prepareListPreference(b, names, values, p); + return p; + } private void prepareListPreference(OsmandPreference b, String[] names, T[] values, ListPreference p) { p.setOnPreferenceChangeListener(this); diff --git a/OsmAnd/src/net/osmand/plus/activities/SettingsNavigationActivity.java b/OsmAnd/src/net/osmand/plus/activities/SettingsNavigationActivity.java index e45bb8c9ae..7bf386dc86 100644 --- a/OsmAnd/src/net/osmand/plus/activities/SettingsNavigationActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/SettingsNavigationActivity.java @@ -2,21 +2,30 @@ package net.osmand.plus.activities; import java.io.File; +import java.util.ArrayList; import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; import java.util.Set; import net.osmand.IndexConstants; +import net.osmand.plus.ApplicationMode; import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings.AutoZoomMap; import net.osmand.plus.OsmandSettings.OsmandPreference; import net.osmand.plus.R; import net.osmand.plus.routing.RouteProvider.RouteService; +import net.osmand.router.GeneralRouter; +import net.osmand.router.GeneralRouter.RoutingParameter; +import net.osmand.router.GeneralRouter.RoutingParameterType; +import net.osmand.router.RoutingConfiguration; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.content.DialogInterface.OnMultiChoiceClickListener; import android.content.Intent; import android.os.Bundle; +import android.preference.CheckBoxPreference; import android.preference.ListPreference; import android.preference.Preference; import android.preference.PreferenceCategory; @@ -32,6 +41,9 @@ public class SettingsNavigationActivity extends SettingsBaseActivity { private ListPreference autoZoomMapPreference; public static final String MORE_VALUE = "MORE_VALUE"; + private List avoidParameters = new ArrayList(); + private List preferParameters = new ArrayList(); + public SettingsNavigationActivity() { super(true); } @@ -63,34 +75,28 @@ public class SettingsNavigationActivity extends SettingsBaseActivity { addPreferencesFromResource(R.xml.navigation_settings); PreferenceScreen screen = getPreferenceScreen(); settings = getMyApplication().getSettings(); + routerServicePreference = (ListPreference) screen.findPreference(settings.ROUTER_SERVICE.getId()); + + RouteService[] vls = RouteService.getAvailableRouters(getMyApplication()); + String[] entries = new String[vls.length]; + for(int i=0; i parameters = router.getParameters(); + if(parameters.containsKey("short_way")) { + cat.addPreference(fastRoute); + } + List others = new ArrayList(); + for(Map.Entry e : parameters.entrySet()) { + String param = e.getKey(); + if(param.startsWith("avoid_")) { + avoidParameters.add(e.getValue()); + } else if(param.startsWith("prefer_")) { + preferParameters.add(e.getValue()); + } else if(!param.equals("short_way")) { + others.add(e.getValue()); + } + } + if (avoidParameters.size() > 0) { + avoidRouting = new Preference(this); + avoidRouting.setTitle(R.string.avoid_in_routing_title); + avoidRouting.setSummary(R.string.avoid_in_routing_descr); + avoidRouting.setOnPreferenceClickListener(this); + cat.addPreference(avoidRouting); + } + if (preferParameters.size() > 0) { + preferRouting = new Preference(this); + preferRouting.setTitle(R.string.prefer_in_routing_title); + preferRouting.setSummary(R.string.prefer_in_routing_descr); + preferRouting.setOnPreferenceClickListener(this); + cat.addPreference(preferRouting); + } + for(RoutingParameter p : others) { + Preference basePref; + if(p.getType() == RoutingParameterType.BOOLEAN) { + basePref = createCheckBoxPreference(settings.getCustomRoutingBooleanProperty(p.getId())); + } else { + Object[] vls = p.getPossibleValues(); + String[] svlss = new String[vls.length]; + int i = 0; + for(Object o : vls) { + svlss[i++] = o.toString(); + } + basePref = createListPreference(settings.getCustomRoutingProperty(p.getId()), + p.getPossibleValueDescriptions(), svlss); + } + basePref.setTitle(SettingsBaseActivity.getRoutingStringPropertyName(this, p.getId(), p.getName())); + basePref.setSummary(SettingsBaseActivity.getRoutingStringPropertyName(this, p.getId(), p.getDescription())); + cat.addPreference(basePref); + } + } + } + } + + + private void clearParameters() { + preferParameters.clear(); + avoidParameters.clear(); + } + + + public static GeneralRouter getRouter(ApplicationMode am) { + GeneralRouter router = RoutingConfiguration.getDefault().getRouter(am.getStringKey()); + if(router == null && am.getParent() != null) { + router = RoutingConfiguration.getDefault().getRouter(am.getParent().getStringKey()); + } + return router; + } private void reloadVoiceListPreference(PreferenceScreen screen) { @@ -137,6 +221,7 @@ public class SettingsNavigationActivity extends SettingsBaseActivity { public void updateAllSettings() { reloadVoiceListPreference(getPreferenceScreen()); + prepareRoutingPrefs(getPreferenceScreen()); super.updateAllSettings(); routerServicePreference.setSummary(getString(R.string.router_service_descr) + " [" + settings.ROUTER_SERVICE.get() + "]"); } @@ -156,11 +241,11 @@ public class SettingsNavigationActivity extends SettingsBaseActivity { } return true; } - boolean changed = super.onPreferenceChange(preference, newValue); - + super.onPreferenceChange(preference, newValue); if (id.equals(settings.ROUTER_SERVICE.getId())) { routerServicePreference.setSummary(getString(R.string.router_service_descr) + " [" + settings.ROUTER_SERVICE.get() + "]"); + prepareRoutingPrefs(getPreferenceScreen()); } return true; } @@ -169,15 +254,16 @@ public class SettingsNavigationActivity extends SettingsBaseActivity { @SuppressWarnings("unchecked") @Override public boolean onPreferenceClick(Preference preference) { - if (preference == avoidRouting) { - showBooleanSettings(new String[] { getString(R.string.avoid_toll_roads), getString(R.string.avoid_ferries), - getString(R.string.avoid_unpaved), getString(R.string.avoid_motorway) - }, new OsmandPreference[] { settings.AVOID_TOLL_ROADS, - settings.AVOID_FERRIES, settings.AVOID_UNPAVED_ROADS, settings.AVOID_MOTORWAY }); - return true; - } else if (preference == preferRouting) { - showBooleanSettings(new String[] { getString(R.string.prefer_motorways)}, - new OsmandPreference[] { settings.PREFER_MOTORWAYS}); + if (preference == avoidRouting || preference == preferRouting) { + List prms = preference == avoidRouting ? avoidParameters : preferParameters; + String[] vals = new String[prms.size()]; + OsmandPreference[] bls = new OsmandPreference[prms.size()]; + for(int i = 0; i < prms.size(); i++) { + RoutingParameter p = prms.get(i); + vals[i] = SettingsBaseActivity.getRoutingStringPropertyName(this, p.getId(), p.getName()); + bls[i] = settings.getCustomRoutingBooleanProperty(p.getId()); + } + showBooleanSettings(vals, bls); return true; } else if (preference == showAlarms) { showBooleanSettings(new String[] { getString(R.string.show_traffic_warnings), getString(R.string.show_cameras), diff --git a/OsmAnd/src/net/osmand/plus/routing/RouteProvider.java b/OsmAnd/src/net/osmand/plus/routing/RouteProvider.java index 9246dfaadd..05c6d9feeb 100644 --- a/OsmAnd/src/net/osmand/plus/routing/RouteProvider.java +++ b/OsmAnd/src/net/osmand/plus/routing/RouteProvider.java @@ -39,11 +39,15 @@ import net.osmand.plus.GPXUtilities.TrkSegment; import net.osmand.plus.GPXUtilities.WptPt; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; +import net.osmand.plus.OsmandSettings.CommonPreference; import net.osmand.plus.R; import net.osmand.plus.TargetPointsHelper; import net.osmand.plus.Version; +import net.osmand.plus.activities.SettingsNavigationActivity; 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.RoutePlannerFrontEnd; import net.osmand.router.RoutePlannerFrontEnd.RouteCalculationMode; import net.osmand.router.RouteSegmentResult; @@ -391,23 +395,28 @@ public class RouteProvider { } else { return applicationModeNotSupported(params); } + GeneralRouter generalRouter = SettingsNavigationActivity.getRouter(params.mode); + if(generalRouter == null) { + return applicationModeNotSupported(params); + } Map paramsR = new LinkedHashMap(); - if (!settings.FAST_ROUTE_MODE.getModeValue(params.mode)) { - paramsR.put(GeneralRouter.USE_SHORTEST_WAY, "true"); - } - if(settings.AVOID_FERRIES.getModeValue(params.mode)){ - paramsR.put(GeneralRouter.AVOID_FERRIES, "true"); - } - if(settings.AVOID_TOLL_ROADS.getModeValue(params.mode)){ - paramsR.put(GeneralRouter.AVOID_TOLL, "true"); - } - if(settings.AVOID_MOTORWAY.getModeValue(params.mode)){ - paramsR.put(GeneralRouter.AVOID_MOTORWAY, "true"); - } else if(settings.PREFER_MOTORWAYS.getModeValue(params.mode)){ - paramsR.put(GeneralRouter.PREFER_MOTORWAYS, "true"); - } - if(settings.AVOID_UNPAVED_ROADS.getModeValue(params.mode)){ - paramsR.put(GeneralRouter.AVOID_UNPAVED, "true"); + for(Map.Entry e : generalRouter.getParameters().entrySet()){ + String key = e.getKey(); + RoutingParameter pr = e.getValue(); + String vl; + if(key.equals(GeneralRouter.USE_SHORTEST_WAY)) { + Boolean bool = settings.FAST_ROUTE_MODE.getModeValue(params.mode); + vl = bool ? "true" : null; + } else if(pr.getType() == RoutingParameterType.BOOLEAN) { + CommonPreference pref = settings.getCustomRoutingBooleanProperty(key); + Boolean bool = pref.getModeValue(params.mode); + vl = bool ? "true" : null; + } else { + vl = settings.getCustomRoutingProperty(key).getModeValue(params.mode); + } + if(vl != null && vl.length() > 0) { + paramsR.put(key, vl); + } } float mb = (1 << 20); Runtime rt = Runtime.getRuntime(); @@ -957,4 +966,6 @@ public class RouteProvider { return new RouteCalculationResult(res, null, params, null); } + + }