Custom routing config initial commit

This commit is contained in:
Vitaliy 2020-02-13 16:54:41 +02:00
parent 8cee733641
commit 5a62f55a5b
13 changed files with 97 additions and 68 deletions

View file

@ -69,8 +69,10 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import btools.routingapp.BRouterServiceConnection;
@ -593,19 +595,23 @@ public class AppInitializer implements IProgress {
}
public static void loadRoutingFiles(final OsmandApplication app, final LoadRoutingFilesCallback callback) {
new AsyncTask<Void, Void, RoutingConfiguration.Builder>() {
new AsyncTask<Void, Void, Map<String, RoutingConfiguration.Builder>>() {
@Override
protected RoutingConfiguration.Builder doInBackground(Void... voids) {
protected Map<String, RoutingConfiguration.Builder> doInBackground(Void... voids) {
Map<String, RoutingConfiguration.Builder> customConfigs = new HashMap<>();
File routingFolder = app.getAppPath(IndexConstants.ROUTING_PROFILES_DIR);
RoutingConfiguration.Builder builder = RoutingConfiguration.getDefault();
RoutingConfiguration.Builder defaultBuilder = RoutingConfiguration.getDefault();
if (routingFolder.isDirectory()) {
File[] fl = routingFolder.listFiles();
if (fl != null && fl.length > 0) {
for (File f : fl) {
if (f.isFile() && f.getName().endsWith(".xml") && f.canRead()) {
if (f.isFile() && f.getName().endsWith(IndexConstants.ROUTING_FILE_EXT) && f.canRead()) {
try {
RoutingConfiguration.parseFromInputStream(new FileInputStream(f), f.getName(), builder);
String fileName = f.getName();
RoutingConfiguration.Builder builder = new RoutingConfiguration.Builder();
RoutingConfiguration.parseFromInputStream(new FileInputStream(f), fileName, builder);
customConfigs.put(fileName, builder);
} catch (XmlPullParserException | IOException e) {
throw new IllegalStateException(e);
}
@ -613,13 +619,14 @@ public class AppInitializer implements IProgress {
}
}
}
return builder;
return customConfigs;
}
@Override
protected void onPostExecute(RoutingConfiguration.Builder builder) {
super.onPostExecute(builder);
app.updateRoutingConfig(builder);
protected void onPostExecute(Map<String, RoutingConfiguration.Builder> customConfigs) {
if (!customConfigs.isEmpty()) {
app.getCustomRoutingConfigs().putAll(customConfigs);
}
callback.onRoutingFilesLoaded();
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

View file

@ -137,11 +137,11 @@ public class CurrentPositionHelper {
for (BinaryMapReaderResource rep : checkReaders) {
rs[i++] = rep.getReader(BinaryMapReaderResourceType.STREET_LOOKUP);
}
RoutingConfiguration cfg = app.getRoutingConfig().build(p, 10,
RoutingConfiguration cfg = app.getRoutingConfigForMode(am).build(p, 10,
new HashMap<String, String>());
cfg.routeCalculationTime = System.currentTimeMillis();
ctx = new RoutePlannerFrontEnd().buildRoutingContext(cfg, null, rs);
RoutingConfiguration defCfg = app.getRoutingConfig().build("geocoding", 10,
RoutingConfiguration defCfg = app.getDefaultRoutingConfig().build("geocoding", 10,
new HashMap<String, String>());
defCtx = new RoutePlannerFrontEnd().buildRoutingContext(defCfg, null, rs);
} else {

View file

@ -25,12 +25,9 @@ import android.support.v7.app.AlertDialog;
import android.text.format.DateFormat;
import android.view.View;
import android.view.accessibility.AccessibilityManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import net.osmand.AndroidUtils;
import net.osmand.CallbackWithObject;
import net.osmand.IndexConstants;
import net.osmand.PlatformUtil;
import net.osmand.access.AccessibilityPlugin;
@ -59,7 +56,6 @@ import net.osmand.plus.helpers.AvoidSpecificRoads;
import net.osmand.plus.helpers.LockHelper;
import net.osmand.plus.helpers.WaypointHelper;
import net.osmand.plus.inapp.InAppPurchaseHelper;
import net.osmand.plus.mapcontextmenu.other.RoutePreferencesMenu;
import net.osmand.plus.mapmarkers.MapMarkersDbHelper;
import net.osmand.plus.monitoring.LiveMonitoringHelper;
import net.osmand.plus.poi.PoiFiltersHelper;
@ -84,11 +80,15 @@ import java.io.PrintStream;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import btools.routingapp.BRouterServiceConnection;
import btools.routingapp.IBRouterService;
import static net.osmand.IndexConstants.ROUTING_FILE_EXT;
public class OsmandApplication extends MultiDexApplication {
public static final String EXCEPTION_PATH = "exception.log";
public static final String OSMAND_PRIVACY_POLICY_URL = "https://osmand.net/help-online/privacy-policy";
@ -143,7 +143,8 @@ public class OsmandApplication extends MultiDexApplication {
private Resources localizedResources;
private RoutingConfiguration.Builder routingConfig;
private Map<String, Builder> customRoutingConfigs = new ConcurrentHashMap<>();
private Locale preferredLocale = null;
private Locale defaultLocale;
private File externalStorageDirectory;
@ -810,18 +811,29 @@ public class OsmandApplication extends MultiDexApplication {
return localizedResources != null ? localizedResources : super.getResources();
}
public synchronized RoutingConfiguration.Builder getRoutingConfig() {
RoutingConfiguration.Builder rc;
if(routingConfig == null) {
rc = new RoutingConfiguration.Builder();
} else {
rc = routingConfig;
}
return rc;
public RoutingConfiguration.Builder getDefaultRoutingConfig() {
return RoutingConfiguration.getDefault();
}
public void updateRoutingConfig(Builder update) {
routingConfig = update;
public Map<String, RoutingConfiguration.Builder> getCustomRoutingConfigs() {
return customRoutingConfigs;
}
public RoutingConfiguration.Builder getCustomRoutingConfig(String key) {
return customRoutingConfigs.get(key);
}
public synchronized RoutingConfiguration.Builder getRoutingConfigForMode(ApplicationMode mode) {
RoutingConfiguration.Builder builder = null;
String routingProfileKey = mode.getRoutingProfile();
if (!Algorithms.isEmpty(routingProfileKey)) {
int index = routingProfileKey.indexOf(ROUTING_FILE_EXT);
if (index != -1) {
String configKey = routingProfileKey.substring(0, index + ROUTING_FILE_EXT.length());
builder = customRoutingConfigs.get(configKey);
}
}
return builder != null ? builder : getDefaultRoutingConfig();
}
public OsmandRegions getRegions() {

View file

@ -314,7 +314,7 @@ public class SettingsNavigationActivity extends SettingsBaseActivity {
cat.addPreference(fastRoute);
} else {
ApplicationMode am = settings.getApplicationMode();
GeneralRouter router = getRouter(getMyApplication().getRoutingConfig(), am);
GeneralRouter router = getRouter(getMyApplication().getRoutingConfigForMode(am), am);
clearParameters();
if (router != null) {
GeneralRouterProfile routerProfile = router.getProfile();
@ -728,7 +728,7 @@ public class SettingsNavigationActivity extends SettingsBaseActivity {
final OsmandApplication app = (OsmandApplication) activity.getApplication();
final OsmandSettings settings = app.getSettings();
GeneralRouter router = getRouter(app.getRoutingConfig(), mode);
GeneralRouter router = getRouter(app.getRoutingConfigForMode(mode), mode);
SpeedConstants units = settings.SPEED_SYSTEM.getModeValue(mode);
String speedUnits = units.toShortString(activity);
final float[] ratio = new float[1];

View file

@ -162,7 +162,7 @@ public class AvoidSpecificRoads {
app.getSettings().removeImpassableRoad(latLon);
RouteDataObject obj = impassableRoads.remove(latLon);
if (obj != null) {
app.getRoutingConfig().removeImpassableRoad(obj);
app.getDefaultRoutingConfig().removeImpassableRoad(obj);
}
}
@ -288,7 +288,7 @@ public class AvoidSpecificRoads {
final LatLon oldLoc = getLocation(currentObject);
app.getSettings().moveImpassableRoad(oldLoc, newLoc);
impassableRoads.remove(oldLoc);
app.getRoutingConfig().removeImpassableRoad(currentObject);
app.getDefaultRoutingConfig().removeImpassableRoad(currentObject);
addImpassableRoadInternal(object, ll, showDialog, activity, newLoc);
if (callback != null) {
@ -310,7 +310,7 @@ public class AvoidSpecificRoads {
boolean showDialog,
@Nullable MapActivity activity,
@NonNull LatLon loc) {
if (app.getRoutingConfig().addImpassableRoad(object, ll)) {
if (app.getDefaultRoutingConfig().addImpassableRoad(object, ll)) {
impassableRoads.put(loc, object);
} else {
LatLon location = getLocation(object);
@ -339,7 +339,7 @@ public class AvoidSpecificRoads {
}
public LatLon getLocation(RouteDataObject object) {
Location location = app.getRoutingConfig().getImpassableRoadLocations().get(object.getId());
Location location = app.getDefaultRoutingConfig().getImpassableRoadLocations().get(object.getId());
return location == null ? null : new LatLon(location.getLatitude(), location.getLongitude());
}

View file

@ -55,7 +55,6 @@ import net.osmand.router.RoutingConfiguration;
import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log;
import org.xmlpull.v1.XmlPullParserException;
import java.io.ByteArrayInputStream;
import java.io.File;
@ -661,7 +660,7 @@ public class ImportHelper {
}
@SuppressLint("StaticFieldLeak")
private void handleRoutingFileImport(final Uri uri, final String fileName, final CallbackWithObject<String> callback) {
private void handleRoutingFileImport(final Uri uri, final String fileName, final CallbackWithObject<RoutingConfiguration.Builder> callback) {
final AsyncTask<Void, Void, String> routingImportTask = new AsyncTask<Void, Void, String>() {
String mFileName;
@ -698,11 +697,11 @@ public class ImportHelper {
if (isActivityNotDestroyed(activity)) {
progress.dismiss();
}
String profileKey = app.getRoutingConfig().getRoutingProfileKeyByFileName(mFileName);
if (profileKey != null) {
RoutingConfiguration.Builder builder = app.getCustomRoutingConfig(mFileName);
if (builder != null) {
app.showShortToastMessage(app.getString(R.string.file_imported_successfully, mFileName));
if (callback != null) {
callback.processResult(profileKey);
callback.processResult(builder);
}
} else {
app.showToastMessage(app.getString(R.string.file_does_not_contain_routing_rules, mFileName));

View file

@ -30,6 +30,7 @@ import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem;
import net.osmand.plus.settings.MainSettingsFragment;
import net.osmand.plus.settings.NavigationFragment;
import net.osmand.plus.settings.ProfileAppearanceFragment;
import net.osmand.router.RoutingConfiguration;
import org.apache.commons.logging.Log;
@ -160,9 +161,9 @@ public class SelectProfileBottomSheetDialogFragment extends MenuBottomSheetDialo
return;
}
mapActivity.getImportHelper().chooseFileToImport(ROUTING, false,
new CallbackWithObject<String>() {
new CallbackWithObject<RoutingConfiguration.Builder>() {
@Override
public boolean processResult(String profileKey) {
public boolean processResult(RoutingConfiguration.Builder builder) {
refreshView();
return false;
}

View file

@ -409,7 +409,7 @@ public class RoutingOptionsHelper {
public LocalRoutingParameter getRoutingParameterInnerById(ApplicationMode am, String parameterId) {
RouteProvider.GPXRouteParamsBuilder rparams = app.getRoutingHelper().getCurrentGPXRoute();
GeneralRouter rm = getRouter(app.getRoutingConfig(), am);
GeneralRouter rm = getRouter(app.getRoutingConfigForMode(am), am);
if (rm == null || (rparams != null && !rparams.isCalculateOsmAndRoute()) && !rparams.getFile().hasRtePt()) {
return null;
}
@ -493,7 +493,7 @@ public class RoutingOptionsHelper {
RouteProvider.GPXRouteParamsBuilder rparams = app.getRoutingHelper().getCurrentGPXRoute();
List<LocalRoutingParameter> list = new ArrayList<LocalRoutingParameter>(getGpxRouterParameters(am));
GeneralRouter rm = SettingsNavigationActivity.getRouter(app.getRoutingConfig(), am);
GeneralRouter rm = SettingsNavigationActivity.getRouter(app.getRoutingConfigForMode(am), am);
if (rm == null || (rparams != null && !rparams.isCalculateOsmAndRoute()) && !rparams.getFile().hasRtePt()) {
return list;
}
@ -583,7 +583,7 @@ public class RoutingOptionsHelper {
public List<GeneralRouter.RoutingParameter> getAvoidRoutingPrefsForAppMode(ApplicationMode applicationMode) {
List<GeneralRouter.RoutingParameter> avoidParameters = new ArrayList<GeneralRouter.RoutingParameter>();
GeneralRouter router = getRouter(app.getRoutingConfig(), applicationMode);
GeneralRouter router = getRouter(app.getRoutingConfigForMode(applicationMode), applicationMode);
if (router != null) {
for (Map.Entry<String, GeneralRouter.RoutingParameter> e : router.getParameters().entrySet()) {
String param = e.getKey();
@ -597,7 +597,7 @@ public class RoutingOptionsHelper {
}
public GeneralRouter.RoutingParameter getRoutingPrefsForAppModeById(ApplicationMode applicationMode, String parameterId) {
GeneralRouter router = getRouter(app.getRoutingConfig(), applicationMode);
GeneralRouter router = getRouter(app.getRoutingConfigForMode(applicationMode), applicationMode);
GeneralRouter.RoutingParameter parameter = null;
if (router != null) {

View file

@ -597,10 +597,10 @@ public class RouteProvider {
RoutePlannerFrontEnd router = new RoutePlannerFrontEnd();
OsmandSettings settings = params.ctx.getSettings();
router.setUseFastRecalculation(settings.USE_FAST_RECALCULATION.get());
RoutingConfiguration.Builder config = params.ctx.getRoutingConfig();
RoutingConfiguration.Builder config = params.ctx.getRoutingConfigForMode(params.mode);
GeneralRouter generalRouter = SettingsNavigationActivity.getRouter(config, params.mode);
if(generalRouter == null) {
if (generalRouter == null) {
return applicationModeNotSupported(params);
}
RoutingConfiguration cf = initOsmAndRoutingConfig(config, params, settings, generalRouter);

View file

@ -452,7 +452,7 @@ public class TransportRoutingHelper {
}
private List<TransportRouteResult> calculateRouteImpl(TransportRouteCalculationParams params) throws IOException, InterruptedException {
RoutingConfiguration.Builder config = params.ctx.getRoutingConfig();
RoutingConfiguration.Builder config = params.ctx.getRoutingConfigForMode(params.mode);
BinaryMapIndexReader[] files = params.ctx.getResourceManager().getTransportRoutingMapFiles();
params.params.clear();
OsmandSettings settings = params.ctx.getSettings();

View file

@ -19,6 +19,7 @@ import net.osmand.plus.profiles.SelectProfileBottomSheetDialogFragment;
import net.osmand.plus.routing.RouteProvider;
import net.osmand.plus.settings.preferences.SwitchPreferenceEx;
import net.osmand.router.GeneralRouter;
import net.osmand.router.RoutingConfiguration;
import net.osmand.util.Algorithms;
import java.util.ArrayList;
@ -158,7 +159,7 @@ public class NavigationFragment extends BaseSettingsFragment {
RouteProvider.RouteService routeService;
if (profileKey.equals(RoutingProfilesResources.STRAIGHT_LINE_MODE.name())) {
routeService = RouteProvider.RouteService.STRAIGHT;
} else if (profileKey.equals(RoutingProfilesResources.DIRECT_TO_MODE.name())){
} else if (profileKey.equals(RoutingProfilesResources.DIRECT_TO_MODE.name())) {
routeService = RouteProvider.RouteService.DIRECT_TO;
} else if (profileKey.equals(RoutingProfilesResources.BROUTER_MODE.name())) {
routeService = RouteProvider.RouteService.BROUTER;
@ -227,26 +228,33 @@ public class NavigationFragment extends BaseSettingsFragment {
false, null));
}
Map<String, GeneralRouter> inputProfiles = context.getRoutingConfig().getAllRouters();
for (Map.Entry<String, GeneralRouter> e : inputProfiles.entrySet()) {
if (!e.getKey().equals("geocoding")) {
int iconRes = R.drawable.ic_action_gdirections_dark;
String name = e.getValue().getProfileName();
String description = context.getString(R.string.osmand_default_routing);
if (!Algorithms.isEmpty(e.getValue().getFilename())) {
description = e.getValue().getFilename();
} else if (RoutingProfilesResources.isRpValue(name.toUpperCase())) {
iconRes = RoutingProfilesResources.valueOf(name.toUpperCase()).getIconRes();
name = context
.getString(RoutingProfilesResources.valueOf(name.toUpperCase()).getStringRes());
}
profilesObjects.put(e.getKey(), new RoutingProfileDataObject(e.getKey(), name, description,
iconRes, false, e.getValue().getFilename()));
}
collectRoutingProfilesFromConfig(context, context.getDefaultRoutingConfig(), profilesObjects);
for (RoutingConfiguration.Builder builder : context.getCustomRoutingConfigs().values()) {
collectRoutingProfilesFromConfig(context, builder, profilesObjects);
}
return profilesObjects;
}
private static void collectRoutingProfilesFromConfig(OsmandApplication app, RoutingConfiguration.Builder builder, Map<String, RoutingProfileDataObject> profilesObjects) {
for (Map.Entry<String, GeneralRouter> entry : builder.getAllRouters().entrySet()) {
String routerKey = entry.getKey();
GeneralRouter router = entry.getValue();
if (!routerKey.equals("geocoding")) {
int iconRes = R.drawable.ic_action_gdirections_dark;
String name = router.getProfileName();
String description = app.getString(R.string.osmand_default_routing);
if (!Algorithms.isEmpty(router.getFilename())) {
description = router.getFilename();
} else if (RoutingProfilesResources.isRpValue(name.toUpperCase())) {
iconRes = RoutingProfilesResources.valueOf(name.toUpperCase()).getIconRes();
name = app.getString(RoutingProfilesResources.valueOf(name.toUpperCase()).getStringRes());
}
profilesObjects.put(routerKey, new RoutingProfileDataObject(routerKey, name, description,
iconRes, false, router.getFilename()));
}
}
}
public static List<ProfileDataObject> getBaseProfiles(Context ctx) {
List<ProfileDataObject> profiles = new ArrayList<>();
for (ApplicationMode mode : ApplicationMode.getDefaultValues()) {

View file

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

View file

@ -6,6 +6,7 @@ import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceViewHolder;
import android.widget.ImageView;
import net.osmand.plus.ApplicationMode;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
@ -38,9 +39,10 @@ public class VehicleParametersFragment extends BaseSettingsFragment implements O
vehicleParametersInfo.setIcon(getContentIcon(R.drawable.ic_action_info_dark));
vehicleParametersInfo.setTitle(getString(R.string.route_parameters_info, getSelectedAppMode().toHumanString()));
RouteService routeService = getSelectedAppMode().getRouteService();
ApplicationMode mode = getSelectedAppMode();
RouteService routeService = mode.getRouteService();
if (routeService == RouteService.OSMAND) {
GeneralRouter router = getRouter(app.getRoutingConfig(), getSelectedAppMode());
GeneralRouter router = getRouter(app.getRoutingConfigForMode(mode), mode);
if (router != null) {
Map<String, GeneralRouter.RoutingParameter> parameters = router.getParameters();