Merge pull request #10551 from osmandapp/OnlineRoutingBackend
Implement Online routing backend
This commit is contained in:
commit
3bf77e4ff1
14 changed files with 394 additions and 270 deletions
|
@ -14,10 +14,11 @@
|
|||
android:orientation="horizontal">
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/main_item_part"
|
||||
android:id="@+id/basic_item_body"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:background="?attr/selectableItemBackground"
|
||||
android:paddingLeft="@dimen/content_padding"
|
||||
android:paddingStart="@dimen/content_padding"
|
||||
android:paddingRight="0dp"
|
||||
|
@ -79,22 +80,24 @@
|
|||
|
||||
<View
|
||||
android:layout_width="1dp"
|
||||
android:layout_height="36dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="@dimen/content_padding"
|
||||
android:layout_marginBottom="@dimen/content_padding"
|
||||
android:background="?attr/divider_color_basic"/>
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/eng_button"
|
||||
android:id="@+id/end_button"
|
||||
android:paddingLeft="@dimen/content_padding"
|
||||
android:paddingRight="@dimen/content_padding"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<androidx.appcompat.widget.AppCompatImageView
|
||||
android:id="@+id/end_button_icon"
|
||||
android:layout_width="@dimen/standard_icon_size"
|
||||
android:layout_height="@dimen/standard_icon_size"
|
||||
android:layout_gravity="center_vertical"
|
||||
app:srcCompat="@drawable/ic_action_settings"
|
||||
android:tint="?attr/default_icon_color" />
|
||||
app:srcCompat="@drawable/ic_action_settings" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
|
|
@ -63,8 +63,8 @@ import net.osmand.plus.measurementtool.MeasurementToolFragment;
|
|||
import net.osmand.plus.measurementtool.StartPlanRouteBottomSheet;
|
||||
import net.osmand.plus.monitoring.OsmandMonitoringPlugin;
|
||||
import net.osmand.plus.osmedit.dialogs.DismissRouteBottomSheetFragment;
|
||||
import net.osmand.plus.profiles.ProfileDataObject;
|
||||
import net.osmand.plus.profiles.ProfileDataUtils;
|
||||
import net.osmand.plus.profiles.RoutingProfileDataObject;
|
||||
import net.osmand.plus.routepreparationmenu.MapRouteInfoMenu;
|
||||
import net.osmand.plus.routepreparationmenu.WaypointsFragment;
|
||||
import net.osmand.plus.routing.RouteProvider.GPXRouteParamsBuilder;
|
||||
|
@ -737,7 +737,7 @@ public class MapActivityActions implements DialogProvider {
|
|||
|
||||
String modeDescription;
|
||||
|
||||
Map<String, RoutingProfileDataObject> profilesObjects = ProfileDataUtils.getRoutingProfiles(app);
|
||||
Map<String, ProfileDataObject> profilesObjects = ProfileDataUtils.getRoutingProfiles(app);
|
||||
for (final ApplicationMode appMode : activeModes) {
|
||||
if (appMode.isCustomProfile()) {
|
||||
modeDescription = getProfileDescription(app, appMode, profilesObjects, getString(R.string.profile_type_user_string));
|
||||
|
@ -1046,7 +1046,7 @@ public class MapActivityActions implements DialogProvider {
|
|||
//switch profile button
|
||||
ApplicationMode currentMode = app.getSettings().APPLICATION_MODE.get();
|
||||
String modeDescription;
|
||||
Map<String, RoutingProfileDataObject> profilesObjects = ProfileDataUtils.getRoutingProfiles(app);
|
||||
Map<String, ProfileDataObject> profilesObjects = ProfileDataUtils.getRoutingProfiles(app);
|
||||
if (currentMode.isCustomProfile()) {
|
||||
modeDescription = getProfileDescription(app, currentMode, profilesObjects, getString(R.string.profile_type_user_string));
|
||||
} else {
|
||||
|
@ -1087,12 +1087,12 @@ public class MapActivityActions implements DialogProvider {
|
|||
}
|
||||
|
||||
private String getProfileDescription(OsmandApplication app, ApplicationMode mode,
|
||||
Map<String, RoutingProfileDataObject> profilesObjects, String defaultDescription) {
|
||||
Map<String, ProfileDataObject> profilesObjects, String defaultDescription) {
|
||||
String description = defaultDescription;
|
||||
|
||||
String routingProfileKey = mode.getRoutingProfile();
|
||||
if (!Algorithms.isEmpty(routingProfileKey)) {
|
||||
RoutingProfileDataObject profileDataObject = profilesObjects.get(routingProfileKey);
|
||||
ProfileDataObject profileDataObject = profilesObjects.get(routingProfileKey);
|
||||
if (profileDataObject != null) {
|
||||
description = String.format(app.getString(R.string.profile_type_descr_string),
|
||||
Algorithms.capitalizeFirstLetterAndLowercase(profileDataObject.getName()));
|
||||
|
|
24
OsmAnd/src/net/osmand/plus/onlinerouting/EngineType.java
Normal file
24
OsmAnd/src/net/osmand/plus/onlinerouting/EngineType.java
Normal file
|
@ -0,0 +1,24 @@
|
|||
package net.osmand.plus.onlinerouting;
|
||||
|
||||
public enum EngineType {
|
||||
|
||||
GRAPHHOPER("Graphhoper", "https://graphhopper.com/api/1/route"),
|
||||
OSRM("OSRM", "https://router.project-osrm.org/route/v1/"),
|
||||
ORS("Openroute Service", "https://api.openrouteservice.org/v2/directions/");
|
||||
|
||||
private String title;
|
||||
private String standardUrl;
|
||||
|
||||
EngineType(String title, String standardUrl) {
|
||||
this.title = title;
|
||||
this.standardUrl = standardUrl;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public String getStandardUrl() {
|
||||
return standardUrl;
|
||||
}
|
||||
}
|
|
@ -15,32 +15,32 @@ public class OnlineRoutingEngine {
|
|||
|
||||
public final static String ONLINE_ROUTING_ENGINE_PREFIX = "online_routing_engine_";
|
||||
|
||||
public enum EngineParameterType {
|
||||
CUSTOM_SERVER_URL,
|
||||
public enum EngineParameter {
|
||||
CUSTOM_NAME,
|
||||
CUSTOM_URL,
|
||||
API_KEY
|
||||
}
|
||||
|
||||
private String stringKey;
|
||||
private ServerType serverType;
|
||||
private EngineType type;
|
||||
private String vehicleKey;
|
||||
private Map<String, String> params = new HashMap<>();
|
||||
|
||||
public OnlineRoutingEngine(@NonNull String stringKey,
|
||||
@NonNull ServerType serverType,
|
||||
@NonNull String vehicleKey,
|
||||
@Nullable Map<String, String> params) {
|
||||
this(stringKey, serverType, vehicleKey);
|
||||
@NonNull EngineType type,
|
||||
@NonNull String vehicleKey,
|
||||
@Nullable Map<String, String> params) {
|
||||
this(stringKey, type, vehicleKey);
|
||||
if (!Algorithms.isEmpty(params)) {
|
||||
this.params.putAll(params);
|
||||
}
|
||||
}
|
||||
|
||||
public OnlineRoutingEngine(@NonNull String stringKey,
|
||||
@NonNull ServerType serverType,
|
||||
@NonNull EngineType type,
|
||||
@NonNull String vehicleKey) {
|
||||
this.stringKey = stringKey;
|
||||
this.serverType = serverType;
|
||||
this.type = type;
|
||||
this.vehicleKey = vehicleKey;
|
||||
}
|
||||
|
||||
|
@ -48,8 +48,16 @@ public class OnlineRoutingEngine {
|
|||
return stringKey;
|
||||
}
|
||||
|
||||
public ServerType getServerType() {
|
||||
return serverType;
|
||||
public EngineType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public String getBaseUrl() {
|
||||
String customUrl = getParameter(EngineParameter.CUSTOM_URL);
|
||||
if (Algorithms.isEmpty(customUrl)) {
|
||||
return type.getStandardUrl();
|
||||
}
|
||||
return customUrl;
|
||||
}
|
||||
|
||||
public String getVehicleKey() {
|
||||
|
@ -60,25 +68,16 @@ public class OnlineRoutingEngine {
|
|||
return params;
|
||||
}
|
||||
|
||||
public String getBaseUrl() {
|
||||
String customServerUrl = getParameter(EngineParameterType.CUSTOM_SERVER_URL);
|
||||
if (!Algorithms.isEmpty(customServerUrl)) {
|
||||
return customServerUrl;
|
||||
} else {
|
||||
return serverType.getBaseUrl();
|
||||
}
|
||||
public String getParameter(EngineParameter paramKey) {
|
||||
return params.get(paramKey.name());
|
||||
}
|
||||
|
||||
public String getParameter(EngineParameterType paramType) {
|
||||
return params.get(paramType.name());
|
||||
}
|
||||
|
||||
public void putParameter(EngineParameterType paramType, String paramValue) {
|
||||
params.put(paramType.name(), paramValue);
|
||||
public void putParameter(EngineParameter paramKey, String paramValue) {
|
||||
params.put(paramKey.name(), paramValue);
|
||||
}
|
||||
|
||||
public String getName(@NonNull Context ctx) {
|
||||
String customName = getParameter(EngineParameterType.CUSTOM_NAME);
|
||||
String customName = getParameter(EngineParameter.CUSTOM_NAME);
|
||||
if (customName != null) {
|
||||
return customName;
|
||||
} else {
|
||||
|
@ -87,21 +86,21 @@ public class OnlineRoutingEngine {
|
|||
}
|
||||
|
||||
private String getStandardName(@NonNull Context ctx) {
|
||||
return getStandardName(ctx, serverType, vehicleKey);
|
||||
return getStandardName(ctx, type, vehicleKey);
|
||||
}
|
||||
|
||||
public static String getStandardName(@NonNull Context ctx,
|
||||
@NonNull ServerType serverType,
|
||||
@NonNull EngineType type,
|
||||
@NonNull String vehicleKey) {
|
||||
String vehicleTitle = VehicleType.toHumanString(ctx, vehicleKey);
|
||||
String pattern = ctx.getString(R.string.ltr_or_rtl_combine_via_dash);
|
||||
return String.format(pattern, serverType.getTitle(), vehicleTitle);
|
||||
return String.format(pattern, type.getTitle(), vehicleTitle);
|
||||
}
|
||||
|
||||
public static OnlineRoutingEngine createNewEngine(@NonNull ServerType serverType,
|
||||
@NonNull String vehicleKey,
|
||||
@Nullable Map<String, String> params) {
|
||||
return new OnlineRoutingEngine(generateKey(), serverType, vehicleKey, params);
|
||||
public static OnlineRoutingEngine createNewEngine(@NonNull EngineType type,
|
||||
@NonNull String vehicleKey,
|
||||
@Nullable Map<String, String> params) {
|
||||
return new OnlineRoutingEngine(generateKey(), type, vehicleKey, params);
|
||||
}
|
||||
|
||||
private static String generateKey() {
|
||||
|
|
|
@ -30,7 +30,7 @@ import net.osmand.plus.activities.MapActivity;
|
|||
import net.osmand.plus.base.BaseOsmAndFragment;
|
||||
import net.osmand.plus.mapcontextmenu.other.HorizontalSelectionAdapter.HorizontalSelectionItem;
|
||||
import net.osmand.plus.onlinerouting.OnlineRoutingCard.OnTextChangedListener;
|
||||
import net.osmand.plus.onlinerouting.OnlineRoutingEngine.EngineParameterType;
|
||||
import net.osmand.plus.onlinerouting.OnlineRoutingEngine.EngineParameter;
|
||||
import net.osmand.plus.routepreparationmenu.cards.BaseCard;
|
||||
import net.osmand.plus.settings.backend.ApplicationMode;
|
||||
import net.osmand.util.Algorithms;
|
||||
|
@ -62,7 +62,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
|
|||
private View view;
|
||||
private ViewGroup segmentsContainer;
|
||||
private OnlineRoutingCard nameCard;
|
||||
private OnlineRoutingCard serverCard;
|
||||
private OnlineRoutingCard typeCard;
|
||||
private OnlineRoutingCard vehicleCard;
|
||||
private OnlineRoutingCard apiKeyCard;
|
||||
private OnlineRoutingCard exampleCard;
|
||||
|
@ -132,8 +132,8 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
|
|||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater,
|
||||
@Nullable ViewGroup container,
|
||||
@Nullable Bundle savedInstanceState) {
|
||||
@Nullable ViewGroup container,
|
||||
@Nullable Bundle savedInstanceState) {
|
||||
view = getInflater().inflate(
|
||||
R.layout.online_routing_engine_fragment, container, false);
|
||||
segmentsContainer = (ViewGroup) view.findViewById(R.id.segments_container);
|
||||
|
@ -143,7 +143,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
|
|||
setupToolbar((Toolbar) view.findViewById(R.id.toolbar));
|
||||
|
||||
setupNameCard();
|
||||
setupServerCard();
|
||||
setupTypeCard();
|
||||
setupVehicleCard();
|
||||
setupApiKeyCard();
|
||||
setupExampleCard();
|
||||
|
@ -152,7 +152,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
|
|||
|
||||
setupButtons();
|
||||
|
||||
updateCardViews(nameCard, serverCard, vehicleCard, exampleCard);
|
||||
updateCardViews(nameCard, typeCard, vehicleCard, exampleCard);
|
||||
return view;
|
||||
}
|
||||
|
||||
|
@ -174,38 +174,39 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
|
|||
segmentsContainer.addView(nameCard.getView());
|
||||
}
|
||||
|
||||
private void setupServerCard() {
|
||||
serverCard = new OnlineRoutingCard(mapActivity, isNightMode());
|
||||
serverCard.build(mapActivity);
|
||||
serverCard.setHeaderTitle(getString(R.string.shared_string_type));
|
||||
private void setupTypeCard() {
|
||||
typeCard = new OnlineRoutingCard(mapActivity, isNightMode());
|
||||
typeCard.build(mapActivity);
|
||||
typeCard.setHeaderTitle(getString(R.string.shared_string_type));
|
||||
List<HorizontalSelectionItem> serverItems = new ArrayList<>();
|
||||
for (ServerType server : ServerType.values()) {
|
||||
for (EngineType server : EngineType.values()) {
|
||||
serverItems.add(new HorizontalSelectionItem(server.getTitle(), server));
|
||||
}
|
||||
serverCard.setSelectionMenu(serverItems, engine.serverType.getTitle(),
|
||||
typeCard.setSelectionMenu(serverItems, engine.type.getTitle(),
|
||||
new CallbackWithObject<HorizontalSelectionItem>() {
|
||||
@Override
|
||||
public boolean processResult(HorizontalSelectionItem result) {
|
||||
ServerType server = (ServerType) result.getObject();
|
||||
if (engine.serverType != server) {
|
||||
engine.serverType = server;
|
||||
updateCardViews(nameCard, serverCard, exampleCard);
|
||||
EngineType type = (EngineType) result.getObject();
|
||||
if (engine.type != type) {
|
||||
engine.type = type;
|
||||
updateCardViews(nameCard, typeCard, exampleCard);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
serverCard.setOnTextChangedListener(new OnTextChangedListener() {
|
||||
typeCard.setOnTextChangedListener(new OnTextChangedListener() {
|
||||
@Override
|
||||
public void onTextChanged(boolean editedByUser, String text) {
|
||||
if (editedByUser) {
|
||||
engine.customServerUrl = text;
|
||||
updateCardViews(exampleCard);
|
||||
}
|
||||
}
|
||||
});
|
||||
serverCard.setFieldBoxLabelText(getString(R.string.shared_string_server_url));
|
||||
serverCard.showDivider();
|
||||
segmentsContainer.addView(serverCard.getView());
|
||||
typeCard.setFieldBoxLabelText(getString(R.string.shared_string_server_url));
|
||||
typeCard.showDivider();
|
||||
segmentsContainer.addView(typeCard.getView());
|
||||
}
|
||||
|
||||
private void setupVehicleCard() {
|
||||
|
@ -342,22 +343,28 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
|
|||
for (BaseCard card : cardsToUpdate) {
|
||||
if (nameCard.equals(card)) {
|
||||
if (Algorithms.isEmpty(engine.customName)) {
|
||||
String name = OnlineRoutingEngine.getStandardName(app, engine.serverType, engine.getVehicleKey());
|
||||
String name;
|
||||
if (Algorithms.isEmpty(engine.getVehicleKey())) {
|
||||
name = engine.type.getTitle();
|
||||
} else {
|
||||
name = OnlineRoutingEngine.getStandardName(app, engine.type, engine.getVehicleKey());
|
||||
}
|
||||
nameCard.setEditedText(name);
|
||||
}
|
||||
|
||||
} else if (serverCard.equals(card)) {
|
||||
serverCard.setHeaderSubtitle(engine.serverType.getTitle());
|
||||
serverCard.setEditedText(engine.getBaseUrl());
|
||||
if (engine.serverType == ServerType.GRAPHHOPER) {
|
||||
} else if (typeCard.equals(card)) {
|
||||
typeCard.setHeaderSubtitle(engine.type.getTitle());
|
||||
typeCard.setEditedText(engine.getBaseUrl());
|
||||
if (engine.type == EngineType.GRAPHHOPER || engine.type == EngineType.ORS) {
|
||||
apiKeyCard.show();
|
||||
} else {
|
||||
apiKeyCard.hide();
|
||||
}
|
||||
|
||||
} else if (vehicleCard.equals(card)) {
|
||||
vehicleCard.setHeaderSubtitle(engine.vehicleType.getTitle(app));
|
||||
if (engine.vehicleType == VehicleType.CUSTOM) {
|
||||
VehicleType vt = VehicleType.getVehicleByKey(engine.getVehicleKey());
|
||||
vehicleCard.setHeaderSubtitle(vt.getTitle(app));
|
||||
if (vt == VehicleType.CUSTOM) {
|
||||
vehicleCard.showFieldBox();
|
||||
vehicleCard.setEditedText(engine.getVehicleKey());
|
||||
} else {
|
||||
|
@ -400,15 +407,15 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
|
|||
private void saveChanges() {
|
||||
OnlineRoutingEngine engineToSave;
|
||||
if (isEditingMode()) {
|
||||
engineToSave = new OnlineRoutingEngine(editedEngineKey, engine.serverType, engine.getVehicleKey());
|
||||
engineToSave = new OnlineRoutingEngine(editedEngineKey, engine.type, engine.getVehicleKey());
|
||||
} else {
|
||||
engineToSave = OnlineRoutingEngine.createNewEngine(engine.serverType, engine.getVehicleKey(), null);
|
||||
engineToSave = OnlineRoutingEngine.createNewEngine(engine.type, engine.getVehicleKey(), null);
|
||||
}
|
||||
|
||||
engineToSave.putParameter(EngineParameterType.CUSTOM_SERVER_URL, engine.customServerUrl);
|
||||
engineToSave.putParameter(EngineParameterType.CUSTOM_NAME, engine.customName);
|
||||
if (engine.serverType == ServerType.GRAPHHOPER) {
|
||||
engineToSave.putParameter(EngineParameterType.API_KEY, engine.apiKey);
|
||||
engineToSave.putParameter(EngineParameter.CUSTOM_NAME, engine.customName);
|
||||
engineToSave.putParameter(EngineParameter.CUSTOM_URL, engine.customServerUrl);
|
||||
if (engine.type == EngineType.GRAPHHOPER || engine.type == EngineType.ORS) {
|
||||
engineToSave.putParameter(EngineParameter.API_KEY, engine.apiKey);
|
||||
}
|
||||
|
||||
helper.saveEngine(engineToSave);
|
||||
|
@ -419,30 +426,18 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
|
|||
}
|
||||
|
||||
private String getTestUrl() {
|
||||
String baseUrl = engine.serverType.getBaseUrl();
|
||||
String vehicle = engine.getVehicleKey();
|
||||
|
||||
LatLon startPoint = selectedLocation.getCityCenterLatLon();
|
||||
LatLon endPoint = selectedLocation.getCityAirportLatLon();
|
||||
|
||||
if (engine.serverType == ServerType.GRAPHHOPER) {
|
||||
return baseUrl + "?" + "point=" + startPoint.getLatitude()
|
||||
+ "," + startPoint.getLongitude()
|
||||
+ "&" + "point=" + endPoint.getLatitude()
|
||||
+ "," + endPoint.getLongitude()
|
||||
+ "&" + "vehicle=" + vehicle
|
||||
+ (!Algorithms.isEmpty(engine.apiKey) ? ("&" + "key=" + engine.apiKey) : "");
|
||||
} else {
|
||||
return baseUrl + vehicle + "/" + startPoint.getLatitude()
|
||||
+ "," + startPoint.getLongitude()
|
||||
+ ";" + endPoint.getLatitude()
|
||||
+ "," + endPoint.getLongitude()
|
||||
+ "?" + "geometries=geojson";
|
||||
}
|
||||
List<LatLon> path = new ArrayList<>();
|
||||
path.add(selectedLocation.getCityCenterLatLon());
|
||||
path.add(selectedLocation.getCityAirportLatLon());
|
||||
OnlineRoutingEngine tmpEngine =
|
||||
OnlineRoutingEngine.createNewEngine(engine.type, engine.getVehicleKey(), null);
|
||||
tmpEngine.putParameter(EngineParameter.CUSTOM_URL, engine.customServerUrl);
|
||||
tmpEngine.putParameter(EngineParameter.API_KEY, engine.apiKey);
|
||||
return helper.createFullUrl(tmpEngine, path);
|
||||
}
|
||||
|
||||
private void testEngineWork() {
|
||||
final ServerType server = engine.serverType;
|
||||
final EngineType type = engine.type;
|
||||
final ExampleLocation location = selectedLocation;
|
||||
AndroidNetworkUtils.sendRequestAsync(app, exampleCard.getEditedText(), null,
|
||||
null, false, false, new OnRequestResultListener() {
|
||||
|
@ -453,10 +448,12 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
|
|||
try {
|
||||
JSONObject obj = new JSONObject(response);
|
||||
|
||||
if (server == ServerType.GRAPHHOPER) {
|
||||
if (type == EngineType.GRAPHHOPER) {
|
||||
resultOk = obj.has("paths");
|
||||
} else if (server == ServerType.OSRM) {
|
||||
} else if (type == EngineType.OSRM) {
|
||||
resultOk = obj.has("routes");
|
||||
} else if (type == EngineType.ORS) {
|
||||
resultOk = obj.has("features");
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
|
||||
|
@ -494,7 +491,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
|
|||
|
||||
private void saveState(Bundle outState) {
|
||||
outState.putString(ENGINE_NAME_KEY, engine.customName);
|
||||
outState.putString(ENGINE_SERVER_KEY, engine.serverType.name());
|
||||
outState.putString(ENGINE_SERVER_KEY, engine.type.name());
|
||||
outState.putString(ENGINE_SERVER_URL_KEY, engine.customServerUrl);
|
||||
outState.putString(ENGINE_VEHICLE_TYPE_KEY, engine.vehicleType.name());
|
||||
outState.putString(ENGINE_CUSTOM_VEHICLE_KEY, engine.customVehicleKey);
|
||||
|
@ -508,7 +505,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
|
|||
|
||||
private void restoreState(Bundle savedState) {
|
||||
engine.customName = savedState.getString(ENGINE_NAME_KEY);
|
||||
engine.serverType = ServerType.valueOf(savedState.getString(ENGINE_SERVER_KEY));
|
||||
engine.type = EngineType.valueOf(savedState.getString(ENGINE_SERVER_KEY));
|
||||
engine.customServerUrl = savedState.getString(ENGINE_SERVER_URL_KEY);
|
||||
engine.vehicleType = VehicleType.valueOf(savedState.getString(ENGINE_VEHICLE_TYPE_KEY));
|
||||
engine.customVehicleKey = savedState.getString(ENGINE_CUSTOM_VEHICLE_KEY);
|
||||
|
@ -519,16 +516,15 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
|
|||
}
|
||||
|
||||
private void initState() {
|
||||
engine.serverType = ServerType.values()[0];
|
||||
engine.type = EngineType.values()[0];
|
||||
engine.vehicleType = VehicleType.values()[0];
|
||||
selectedLocation = ExampleLocation.values()[0];
|
||||
|
||||
if (isEditingMode()) {
|
||||
OnlineRoutingEngine editedEngine = helper.getEngineByKey(editedEngineKey);
|
||||
if (editedEngine != null) {
|
||||
engine.customName = editedEngine.getParameter(EngineParameterType.CUSTOM_NAME);
|
||||
engine.serverType = editedEngine.getServerType();
|
||||
engine.customServerUrl = editedEngine.getParameter(EngineParameterType.CUSTOM_SERVER_URL);
|
||||
engine.customName = editedEngine.getParameter(EngineParameter.CUSTOM_NAME);
|
||||
engine.type = editedEngine.getType();
|
||||
String vehicleKey = editedEngine.getVehicleKey();
|
||||
if (vehicleKey != null) {
|
||||
VehicleType vehicleType = VehicleType.getVehicleByKey(vehicleKey);
|
||||
|
@ -537,7 +533,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
|
|||
}
|
||||
engine.vehicleType = vehicleType;
|
||||
}
|
||||
engine.apiKey = editedEngine.getParameter(EngineParameterType.API_KEY);
|
||||
engine.apiKey = editedEngine.getParameter(EngineParameter.API_KEY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -568,8 +564,8 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
|
|||
}
|
||||
|
||||
public static void showInstance(@NonNull FragmentActivity activity,
|
||||
@NonNull ApplicationMode appMode,
|
||||
String editedEngineKey) {
|
||||
@NonNull ApplicationMode appMode,
|
||||
String editedEngineKey) {
|
||||
FragmentManager fm = activity.getSupportFragmentManager();
|
||||
if (!fm.isStateSaved() && fm.findFragmentByTag(OnlineRoutingEngineFragment.TAG) == null) {
|
||||
OnlineRoutingEngineFragment fragment = new OnlineRoutingEngineFragment();
|
||||
|
@ -583,7 +579,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
|
|||
|
||||
private static class OnlineRoutingEngineObject {
|
||||
private String customName;
|
||||
private ServerType serverType;
|
||||
private EngineType type;
|
||||
private String customServerUrl;
|
||||
private VehicleType vehicleType;
|
||||
private String customVehicleKey;
|
||||
|
@ -596,15 +592,18 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
|
|||
return vehicleType.getKey();
|
||||
}
|
||||
|
||||
public String getBaseUrl() {
|
||||
return customServerUrl != null ? customServerUrl : serverType.getBaseUrl();
|
||||
}
|
||||
|
||||
public String getName(Context ctx) {
|
||||
if (customName != null) {
|
||||
return customName;
|
||||
}
|
||||
return OnlineRoutingEngine.getStandardName(ctx, serverType, getVehicleKey());
|
||||
return OnlineRoutingEngine.getStandardName(ctx, type, getVehicleKey());
|
||||
}
|
||||
|
||||
public String getBaseUrl() {
|
||||
if (Algorithms.isEmpty(customServerUrl)) {
|
||||
return type.getStandardUrl();
|
||||
}
|
||||
return customServerUrl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,18 +6,28 @@ import com.google.gson.Gson;
|
|||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import net.osmand.PlatformUtil;
|
||||
import net.osmand.data.LatLon;
|
||||
import net.osmand.osm.io.NetworkUtils;
|
||||
import net.osmand.plus.OsmandApplication;
|
||||
import net.osmand.plus.Version;
|
||||
import net.osmand.plus.onlinerouting.OnlineRoutingEngine.EngineParameter;
|
||||
import net.osmand.plus.settings.backend.OsmandSettings;
|
||||
import net.osmand.util.Algorithms;
|
||||
import net.osmand.util.GeoPolylineParserUtil;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.lang.reflect.Type;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -27,8 +37,7 @@ public class OnlineRoutingHelper {
|
|||
|
||||
private OsmandApplication app;
|
||||
private OsmandSettings settings;
|
||||
private List<OnlineRoutingEngine> cachedEngines;
|
||||
private Map<String, OnlineRoutingEngine> cachedEnginesMap;
|
||||
private Map<String, OnlineRoutingEngine> cachedEngines;
|
||||
|
||||
public OnlineRoutingHelper(OsmandApplication app) {
|
||||
this.app = app;
|
||||
|
@ -38,23 +47,121 @@ public class OnlineRoutingHelper {
|
|||
|
||||
@NonNull
|
||||
public List<OnlineRoutingEngine> getEngines() {
|
||||
return cachedEngines;
|
||||
return new ArrayList<>(cachedEngines.values());
|
||||
}
|
||||
|
||||
public OnlineRoutingEngine getEngineByKey(String stringKey) {
|
||||
return cachedEnginesMap.get(stringKey);
|
||||
return cachedEngines.get(stringKey);
|
||||
}
|
||||
|
||||
public List<LatLon> calculateRouteOnline(@NonNull OnlineRoutingEngine engine,
|
||||
@NonNull List<LatLon> path) throws IOException, JSONException {
|
||||
String fullUrl = createFullUrl(engine, path);
|
||||
String content = makeRequest(fullUrl);
|
||||
return parseResponse(engine, content);
|
||||
}
|
||||
|
||||
public String createFullUrl(OnlineRoutingEngine engine, List<LatLon> path) {
|
||||
StringBuilder sb = new StringBuilder(engine.getBaseUrl());
|
||||
String vehicle = engine.getVehicleKey();
|
||||
String apiKey = engine.getParameter(EngineParameter.API_KEY);
|
||||
switch (engine.getType()) {
|
||||
|
||||
case GRAPHHOPER:
|
||||
sb.append("?");
|
||||
for (LatLon point : path) {
|
||||
sb.append("point=")
|
||||
.append(point.getLatitude())
|
||||
.append(',')
|
||||
.append(point.getLongitude())
|
||||
.append('&');
|
||||
}
|
||||
sb.append("vehicle=").append(vehicle);
|
||||
|
||||
if (!Algorithms.isEmpty(apiKey)) {
|
||||
sb.append('&').append("key=").append(apiKey);
|
||||
}
|
||||
break;
|
||||
|
||||
case OSRM:
|
||||
sb.append(vehicle).append('/');
|
||||
for (int i = 0; i < path.size(); i++) {
|
||||
LatLon point = path.get(i);
|
||||
sb.append(point.getLongitude()).append(',').append(point.getLatitude());
|
||||
if (i < path.size() - 1) {
|
||||
sb.append(';');
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case ORS:
|
||||
if (path.size() > 1) {
|
||||
sb.append("driving-car").append('?'); // todo only for testing
|
||||
if (!Algorithms.isEmpty(apiKey)) {
|
||||
sb.append("api_key=").append(apiKey);
|
||||
}
|
||||
LatLon start = path.get(0);
|
||||
LatLon end = path.get(path.size() - 1);
|
||||
sb.append('&').append("start=")
|
||||
.append(start.getLatitude()).append(',').append(start.getLongitude());
|
||||
sb.append('&').append("end=")
|
||||
.append(end.getLatitude()).append(',').append(end.getLongitude());
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private List<LatLon> parseResponse(OnlineRoutingEngine engine, String content) throws JSONException {
|
||||
JSONObject obj = new JSONObject(content);
|
||||
|
||||
switch (engine.getType()) {
|
||||
|
||||
case GRAPHHOPER:
|
||||
return GeoPolylineParserUtil.parse(
|
||||
obj.getJSONArray("paths").getJSONObject(0).getString("points"),
|
||||
GeoPolylineParserUtil.PRECISION_5);
|
||||
|
||||
case OSRM:
|
||||
return GeoPolylineParserUtil.parse(
|
||||
obj.getJSONArray("routes").getJSONObject(0).getString("geometry"),
|
||||
GeoPolylineParserUtil.PRECISION_5);
|
||||
|
||||
case ORS:
|
||||
JSONArray array = obj.getJSONArray("features").getJSONObject(0)
|
||||
.getJSONObject("geometry").getJSONArray("coordinates");
|
||||
List<LatLon> track = new ArrayList<>();
|
||||
for (int i = 0; i < array.length(); i++) {
|
||||
JSONArray point = array.getJSONArray(i);
|
||||
double lat = Double.parseDouble(point.getString(0));
|
||||
double lon = Double.parseDouble(point.getString(1));
|
||||
track.add(new LatLon(lat, lon));
|
||||
}
|
||||
return track;
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
private String makeRequest(String url) throws IOException {
|
||||
URLConnection connection = NetworkUtils.getHttpURLConnection(url);
|
||||
connection.setRequestProperty("User-Agent", Version.getFullVersion(app));
|
||||
StringBuilder content = new StringBuilder();
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
String s;
|
||||
while ((s = reader.readLine()) != null) {
|
||||
content.append(s);
|
||||
}
|
||||
try {
|
||||
reader.close();
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
return content.toString();
|
||||
}
|
||||
|
||||
public void saveEngine(@NonNull OnlineRoutingEngine engine) {
|
||||
String stringKey = engine.getStringKey();
|
||||
OnlineRoutingEngine existedEngine = cachedEnginesMap.get(stringKey);
|
||||
if (existedEngine != null) {
|
||||
int index = cachedEngines.indexOf(existedEngine);
|
||||
cachedEngines.set(index, engine);
|
||||
} else {
|
||||
cachedEngines.add(engine);
|
||||
}
|
||||
cachedEnginesMap.put(stringKey, engine);
|
||||
cachedEngines.put(stringKey, engine);
|
||||
saveToSettings();
|
||||
}
|
||||
|
||||
|
@ -67,19 +174,18 @@ public class OnlineRoutingHelper {
|
|||
|
||||
public void deleteEngine(@NonNull OnlineRoutingEngine engine) {
|
||||
String stringKey = engine.getStringKey();
|
||||
if (cachedEnginesMap.containsKey(stringKey)) {
|
||||
OnlineRoutingEngine existedEngine = cachedEnginesMap.remove(stringKey);
|
||||
cachedEngines.remove(existedEngine);
|
||||
if (cachedEngines.containsKey(stringKey)) {
|
||||
cachedEngines.remove(stringKey);
|
||||
saveToSettings();
|
||||
}
|
||||
}
|
||||
|
||||
private void loadFromSettings() {
|
||||
cachedEngines = readFromSettings();
|
||||
cachedEnginesMap = new HashMap<>();
|
||||
for (OnlineRoutingEngine engine : cachedEngines) {
|
||||
cachedEnginesMap.put(engine.getStringKey(), engine);
|
||||
Map<String, OnlineRoutingEngine> cachedEngines = new LinkedHashMap<>();
|
||||
for (OnlineRoutingEngine engine : readFromSettings()) {
|
||||
cachedEngines.put(engine.getStringKey(), engine);
|
||||
}
|
||||
this.cachedEngines = cachedEngines;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
|
@ -101,7 +207,7 @@ public class OnlineRoutingHelper {
|
|||
if (!Algorithms.isEmpty(cachedEngines)) {
|
||||
try {
|
||||
JSONObject json = new JSONObject();
|
||||
writeToJson(json, cachedEngines);
|
||||
writeToJson(json, getEngines());
|
||||
settings.ONLINE_ROUTING_ENGINES.set(json.toString());
|
||||
} catch (JSONException e) {
|
||||
LOG.debug("Error when writing engines to JSON ", e);
|
||||
|
@ -123,10 +229,10 @@ public class OnlineRoutingHelper {
|
|||
JSONObject object = itemsJson.getJSONObject(i);
|
||||
String key = object.getString("key");
|
||||
String vehicleKey = object.getString("vehicle");
|
||||
ServerType serverType = ServerType.valueOf(object.getString("serverType"));
|
||||
EngineType engineType = EngineType.valueOf(object.getString("type"));
|
||||
String paramsString = object.getString("params");
|
||||
HashMap<String, String> params = gson.fromJson(paramsString, type);
|
||||
engines.add(new OnlineRoutingEngine(key, serverType, vehicleKey, params));
|
||||
engines.add(new OnlineRoutingEngine(key, engineType, vehicleKey, params));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -138,7 +244,7 @@ public class OnlineRoutingHelper {
|
|||
for (OnlineRoutingEngine engine : engines) {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("key", engine.getStringKey());
|
||||
jsonObject.put("serverType", engine.getServerType().name());
|
||||
jsonObject.put("type", engine.getType().name());
|
||||
jsonObject.put("vehicle", engine.getVehicleKey());
|
||||
jsonObject.put("params", gson.toJson(engine.getParams(), type));
|
||||
jsonArray.put(jsonObject);
|
||||
|
|
|
@ -1,22 +0,0 @@
|
|||
package net.osmand.plus.onlinerouting;
|
||||
|
||||
public enum ServerType {
|
||||
GRAPHHOPER("Graphhoper", "https://graphhopper.com/api/1/route"),
|
||||
OSRM("OSRM", "https://zlzk.biz/route/v1/");
|
||||
|
||||
ServerType(String title, String baseUrl) {
|
||||
this.title = title;
|
||||
this.baseUrl = baseUrl;
|
||||
}
|
||||
|
||||
private String title;
|
||||
private String baseUrl;
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public String getBaseUrl() {
|
||||
return baseUrl;
|
||||
}
|
||||
}
|
|
@ -22,6 +22,7 @@ import java.util.Map;
|
|||
public class ProfileDataUtils {
|
||||
|
||||
public static final String OSMAND_NAVIGATION = "osmand_navigation";
|
||||
public static final String ONLINE_NAVIGATION = "online_navigation";
|
||||
|
||||
public static List<ProfileDataObject> getDataObjects(OsmandApplication app,
|
||||
List<ApplicationMode> appModes) {
|
||||
|
@ -48,9 +49,9 @@ public class ProfileDataUtils {
|
|||
return description;
|
||||
}
|
||||
|
||||
public static List<RoutingProfileDataObject> getSortedRoutingProfiles(OsmandApplication app) {
|
||||
List<RoutingProfileDataObject> result = new ArrayList<>();
|
||||
Map<String, List<RoutingProfileDataObject>> routingProfilesByFileNames = getRoutingProfilesByFileNames(app);
|
||||
public static List<ProfileDataObject> getSortedRoutingProfiles(OsmandApplication app) {
|
||||
List<ProfileDataObject> result = new ArrayList<>();
|
||||
Map<String, List<ProfileDataObject>> routingProfilesByFileNames = getRoutingProfilesByFileNames(app);
|
||||
List<String> fileNames = new ArrayList<>(routingProfilesByFileNames.keySet());
|
||||
Collections.sort(fileNames, new Comparator<String>() {
|
||||
@Override
|
||||
|
@ -59,7 +60,7 @@ public class ProfileDataUtils {
|
|||
}
|
||||
});
|
||||
for (String fileName : fileNames) {
|
||||
List<RoutingProfileDataObject> routingProfilesFromFile = routingProfilesByFileNames.get(fileName);
|
||||
List<ProfileDataObject> routingProfilesFromFile = routingProfilesByFileNames.get(fileName);
|
||||
if (routingProfilesFromFile != null) {
|
||||
Collections.sort(routingProfilesFromFile);
|
||||
result.addAll(routingProfilesFromFile);
|
||||
|
@ -77,14 +78,20 @@ public class ProfileDataUtils {
|
|||
return objects;
|
||||
}
|
||||
|
||||
public static Map<String, List<RoutingProfileDataObject>> getRoutingProfilesByFileNames(OsmandApplication app) {
|
||||
Map<String, List<RoutingProfileDataObject>> result = new HashMap<>();
|
||||
for (final RoutingProfileDataObject profile : getRoutingProfiles(app).values()) {
|
||||
String fileName = profile.getFileName() != null ? profile.getFileName() : OSMAND_NAVIGATION;
|
||||
public static Map<String, List<ProfileDataObject>> getRoutingProfilesByFileNames(OsmandApplication app) {
|
||||
Map<String, List<ProfileDataObject>> result = new HashMap<>();
|
||||
for (final ProfileDataObject profile : getRoutingProfiles(app).values()) {
|
||||
String fileName = null;
|
||||
if (profile instanceof RoutingProfileDataObject) {
|
||||
fileName = ((RoutingProfileDataObject) profile).getFileName();
|
||||
} else if (profile instanceof OnlineRoutingEngineDataObject) {
|
||||
fileName = ONLINE_NAVIGATION;
|
||||
}
|
||||
fileName = fileName != null ? fileName : OSMAND_NAVIGATION;
|
||||
if (result.containsKey(fileName)) {
|
||||
result.get(fileName).add(profile);
|
||||
} else {
|
||||
result.put(fileName, new ArrayList<RoutingProfileDataObject>() {
|
||||
result.put(fileName, new ArrayList<ProfileDataObject>() {
|
||||
{ add(profile); }
|
||||
});
|
||||
}
|
||||
|
@ -92,8 +99,8 @@ public class ProfileDataUtils {
|
|||
return result;
|
||||
}
|
||||
|
||||
public static Map<String, RoutingProfileDataObject> getRoutingProfiles(OsmandApplication context) {
|
||||
Map<String, RoutingProfileDataObject> profilesObjects = new HashMap<>();
|
||||
public static Map<String, ProfileDataObject> getRoutingProfiles(OsmandApplication context) {
|
||||
Map<String, ProfileDataObject> profilesObjects = new HashMap<>();
|
||||
profilesObjects.put(RoutingProfilesResources.STRAIGHT_LINE_MODE.name(), new RoutingProfileDataObject(
|
||||
RoutingProfilesResources.STRAIGHT_LINE_MODE.name(),
|
||||
context.getString(RoutingProfilesResources.STRAIGHT_LINE_MODE.getStringRes()),
|
||||
|
@ -119,11 +126,14 @@ public class ProfileDataUtils {
|
|||
for (RoutingConfiguration.Builder builder : context.getAllRoutingConfigs()) {
|
||||
collectRoutingProfilesFromConfig(context, builder, profilesObjects, disabledRouterNames);
|
||||
}
|
||||
for (OnlineRoutingEngineDataObject onlineEngine : getOnlineRoutingProfiles(context)) {
|
||||
profilesObjects.put(onlineEngine.getStringKey(), onlineEngine);
|
||||
}
|
||||
return profilesObjects;
|
||||
}
|
||||
|
||||
private static void collectRoutingProfilesFromConfig(OsmandApplication app, RoutingConfiguration.Builder builder,
|
||||
Map<String, RoutingProfileDataObject> profilesObjects, List<String> disabledRouterNames) {
|
||||
Map<String, ProfileDataObject> profilesObjects, List<String> disabledRouterNames) {
|
||||
for (Map.Entry<String, GeneralRouter> entry : builder.getAllRouters().entrySet()) {
|
||||
String routerKey = entry.getKey();
|
||||
GeneralRouter router = entry.getValue();
|
||||
|
|
|
@ -4,6 +4,7 @@ import android.app.Activity;
|
|||
import android.content.Context;
|
||||
import android.graphics.Typeface;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.text.SpannableString;
|
||||
import android.text.Spanned;
|
||||
|
@ -58,7 +59,7 @@ public class SelectProfileBottomSheet extends BasePreferenceBottomSheet {
|
|||
|
||||
public final static String PROFILE_KEY_ARG = "profile_key_arg";
|
||||
public final static String USE_LAST_PROFILE_ARG = "use_last_profile_arg";
|
||||
public final static String IS_PROFILE_IMPORTED_ARG = "is_profile_imported_arg";
|
||||
public final static String PROFILES_LIST_UPDATED_ARG = "is_profiles_list_updated";
|
||||
|
||||
private DialogMode dialogMode;
|
||||
private final List<ProfileDataObject> profiles = new ArrayList<>();
|
||||
|
@ -130,7 +131,7 @@ public class SelectProfileBottomSheet extends BasePreferenceBottomSheet {
|
|||
}
|
||||
Bundle args = new Bundle();
|
||||
args.putString(PROFILE_KEY_ARG, item.getName());
|
||||
args.putBoolean(IS_PROFILE_IMPORTED_ARG, true);
|
||||
args.putBoolean(PROFILES_LIST_UPDATED_ARG, true);
|
||||
listener.onSelectedType(args);
|
||||
dismiss();
|
||||
break;
|
||||
|
@ -234,9 +235,10 @@ public class SelectProfileBottomSheet extends BasePreferenceBottomSheet {
|
|||
|
||||
int activeColorResId = nightMode ? R.color.active_color_primary_dark : R.color.active_color_primary_light;
|
||||
int iconDefaultColorResId = nightMode ? R.color.icon_color_default_dark : R.color.icon_color_default_light;
|
||||
final boolean onlineRoutingProfile = profile instanceof OnlineRoutingEngineDataObject;
|
||||
|
||||
View itemView = UiUtilities.getInflater(getContext(), nightMode).inflate(
|
||||
profile instanceof OnlineRoutingEngineDataObject ?
|
||||
onlineRoutingProfile ?
|
||||
R.layout.bottom_sheet_item_with_descr_radio_and_icon_btn :
|
||||
R.layout.bottom_sheet_item_with_descr_and_radio_btn, null);
|
||||
TextView tvTitle = itemView.findViewById(R.id.title);
|
||||
|
@ -262,28 +264,52 @@ public class SelectProfileBottomSheet extends BasePreferenceBottomSheet {
|
|||
UiUtilities.setupCompoundButton(compoundButton, nightMode, UiUtilities.CompoundButtonType.GLOBAL);
|
||||
bottomDivider.setVisibility(showBottomDivider ? View.VISIBLE : View.INVISIBLE);
|
||||
|
||||
items.add(new BaseBottomSheetItem.Builder()
|
||||
.setCustomView(itemView)
|
||||
.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Bundle args = new Bundle();
|
||||
args.putString(PROFILE_KEY_ARG, profile.getStringKey());
|
||||
Fragment target = getTargetFragment();
|
||||
if (target instanceof OnSelectProfileCallback) {
|
||||
if (profile instanceof OnlineRoutingEngineDataObject) {
|
||||
if (getActivity() != null) {
|
||||
OnlineRoutingEngineFragment.showInstance(getActivity(), getAppMode(), profile.getStringKey());
|
||||
}
|
||||
dismiss();
|
||||
} else {
|
||||
((OnSelectProfileCallback) target).onProfileSelected(args);
|
||||
}
|
||||
}
|
||||
dismiss();
|
||||
BaseBottomSheetItem.Builder builder =
|
||||
new BaseBottomSheetItem.Builder().setCustomView(itemView);
|
||||
|
||||
OnClickListener listener = new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Bundle args = new Bundle();
|
||||
args.putString(PROFILE_KEY_ARG, profile.getStringKey());
|
||||
args.putBoolean(PROFILES_LIST_UPDATED_ARG, onlineRoutingProfile);
|
||||
Fragment target = getTargetFragment();
|
||||
if (target instanceof OnSelectProfileCallback) {
|
||||
((OnSelectProfileCallback) target).onProfileSelected(args);
|
||||
}
|
||||
dismiss();
|
||||
}
|
||||
};
|
||||
|
||||
if (onlineRoutingProfile) {
|
||||
View basePart = itemView.findViewById(R.id.basic_item_body);
|
||||
View endBtn = itemView.findViewById(R.id.end_button);
|
||||
ImageView ivEndBtnIcon = itemView.findViewById(R.id.end_button_icon);
|
||||
|
||||
Drawable drawable = getIcon(R.drawable.ic_action_settings,
|
||||
nightMode ?
|
||||
R.color.route_info_control_icon_color_dark :
|
||||
R.color.route_info_control_icon_color_light);
|
||||
if (Build.VERSION.SDK_INT >= 21) {
|
||||
Drawable activeDrawable = getIcon(R.drawable.ic_action_settings, activeColorResId);
|
||||
drawable = AndroidUtils.createPressedStateListDrawable(drawable, activeDrawable);
|
||||
}
|
||||
ivEndBtnIcon.setImageDrawable(drawable);
|
||||
|
||||
basePart.setOnClickListener(listener);
|
||||
endBtn.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (getActivity() != null) {
|
||||
OnlineRoutingEngineFragment.showInstance(getActivity(), getAppMode(), profile.getStringKey());
|
||||
}
|
||||
})
|
||||
.create());
|
||||
dismiss();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
builder.setOnClickListener(listener);
|
||||
}
|
||||
items.add(builder.create());
|
||||
}
|
||||
|
||||
private void addCheckableItem(int titleId,
|
||||
|
@ -363,7 +389,6 @@ public class SelectProfileBottomSheet extends BasePreferenceBottomSheet {
|
|||
|
||||
case NAVIGATION_PROFILE:
|
||||
profiles.addAll(ProfileDataUtils.getSortedRoutingProfiles(app));
|
||||
profiles.addAll(ProfileDataUtils.getOnlineRoutingProfiles(app));
|
||||
break;
|
||||
|
||||
case DEFAULT_PROFILE:
|
||||
|
|
|
@ -22,6 +22,8 @@ import net.osmand.data.LocationPoint;
|
|||
import net.osmand.data.WptLocationPoint;
|
||||
import net.osmand.osm.io.NetworkUtils;
|
||||
import net.osmand.plus.OsmandApplication;
|
||||
import net.osmand.plus.onlinerouting.OnlineRoutingEngine;
|
||||
import net.osmand.plus.onlinerouting.OnlineRoutingHelper;
|
||||
import net.osmand.plus.settings.backend.OsmandSettings;
|
||||
import net.osmand.plus.settings.backend.CommonPreference;
|
||||
import net.osmand.plus.R;
|
||||
|
@ -91,7 +93,8 @@ public class RouteProvider {
|
|||
OSMAND("OsmAnd (offline)"),
|
||||
BROUTER("BRouter (offline)"),
|
||||
STRAIGHT("Straight line"),
|
||||
DIRECT_TO("Direct To");
|
||||
DIRECT_TO("Direct To"),
|
||||
ONLINE("Online engine");
|
||||
|
||||
private final String name;
|
||||
|
||||
|
@ -363,10 +366,8 @@ public class RouteProvider {
|
|||
res = findVectorMapsRoute(params, calcGPXRoute);
|
||||
} else if (params.mode.getRouteService() == RouteService.BROUTER) {
|
||||
res = findBROUTERRoute(params);
|
||||
// } else if (params.type == RouteService.ORS) {
|
||||
// res = findORSRoute(params);
|
||||
// } else if (params.type == RouteService.OSRM) {
|
||||
// res = findOSRMRoute(params);
|
||||
} else if (params.mode.getRouteService() == RouteService.ONLINE) {
|
||||
res = findOnlineRoute(params);
|
||||
} else if (params.mode.getRouteService() == RouteService.STRAIGHT ||
|
||||
params.mode.getRouteService() == RouteService.DIRECT_TO) {
|
||||
res = findStraightRoute(params);
|
||||
|
@ -383,6 +384,8 @@ public class RouteProvider {
|
|||
log.error("Failed to find route ", e); //$NON-NLS-1$
|
||||
} catch (SAXException e) {
|
||||
log.error("Failed to find route ", e); //$NON-NLS-1$
|
||||
} catch (JSONException e) {
|
||||
log.error("Failed to find route ", e); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
return new RouteCalculationResult(null);
|
||||
|
@ -1204,64 +1207,33 @@ public class RouteProvider {
|
|||
return exporter.exportRoute();
|
||||
}
|
||||
|
||||
private void appendOSRMLoc(StringBuilder uri, LatLon il) {
|
||||
uri.append(";").append(il.getLongitude());
|
||||
uri.append(",").append(il.getLatitude());
|
||||
}
|
||||
|
||||
protected RouteCalculationResult findOSRMRoute(RouteCalculationParams params)
|
||||
throws MalformedURLException, IOException, JSONException {
|
||||
// http://router.project-osrm.org/route/v1/driving/4.83,52.28;4.95,52.28
|
||||
List<Location> res = new ArrayList<Location>();
|
||||
StringBuilder uri = new StringBuilder();
|
||||
// possibly hide that API key because it is privacy of osmand
|
||||
// A6421860EBB04234AB5EF2D049F2CD8F key is compromised
|
||||
String scheme = "";
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
|
||||
scheme = "https";
|
||||
} else {
|
||||
scheme = "http";
|
||||
}
|
||||
uri.append(scheme + "://router.project-osrm.org/route/v1/driving/"); //$NON-NLS-1$
|
||||
uri.append(String.valueOf(params.start.getLongitude()));
|
||||
uri.append(",").append(String.valueOf(params.start.getLatitude()));
|
||||
if(params.intermediates != null && params.intermediates.size() > 0) {
|
||||
for(LatLon il : params.intermediates) {
|
||||
appendOSRMLoc(uri, il);
|
||||
private RouteCalculationResult findOnlineRoute(RouteCalculationParams params) throws IOException, JSONException {
|
||||
OnlineRoutingHelper helper = params.ctx.getOnlineRoutingHelper();
|
||||
String stringKey = params.mode.getRoutingProfile();
|
||||
List<LatLon> route = helper.calculateRouteOnline(helper.getEngineByKey(stringKey), getFullPathFromParams(params));
|
||||
if (!route.isEmpty()) {
|
||||
List<Location> res = new ArrayList<>();
|
||||
for (LatLon pt : route) {
|
||||
WptPt wpt = new WptPt();
|
||||
wpt.lat = pt.getLatitude();
|
||||
wpt.lon = pt.getLongitude();
|
||||
res.add(createLocation(wpt));
|
||||
}
|
||||
}
|
||||
appendOSRMLoc(uri, params.end);
|
||||
// to get more waypoints, add overview=full option
|
||||
// uri.append("?overview=full")
|
||||
|
||||
log.info("URL route " + uri);
|
||||
|
||||
URLConnection connection = NetworkUtils.getHttpURLConnection(uri.toString());
|
||||
connection.setRequestProperty("User-Agent", Version.getFullVersion(params.ctx));
|
||||
StringBuilder content = new StringBuilder();
|
||||
BufferedReader rs = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
String s;
|
||||
while((s = rs.readLine()) != null) {
|
||||
content.append(s);
|
||||
}
|
||||
JSONObject obj = new JSONObject(content.toString());
|
||||
try {
|
||||
rs.close();
|
||||
} catch(IOException e){
|
||||
}
|
||||
List<LatLon> route = GeoPolylineParserUtil.parse(obj.getJSONArray("routes").getJSONObject(0).getString("geometry"),
|
||||
GeoPolylineParserUtil.PRECISION_5);
|
||||
if (route.isEmpty()) {
|
||||
params.intermediates = null;
|
||||
return new RouteCalculationResult(res, null, params, null, true);
|
||||
} else {
|
||||
return new RouteCalculationResult("Route is empty");
|
||||
}
|
||||
for (LatLon pt : route) {
|
||||
WptPt wpt = new WptPt();
|
||||
wpt.lat = pt.getLatitude();
|
||||
wpt.lon = pt.getLongitude();
|
||||
res.add(createLocation(wpt));
|
||||
}
|
||||
|
||||
private static List<LatLon> getFullPathFromParams(RouteCalculationParams params) {
|
||||
List<LatLon> points = new ArrayList<>();
|
||||
points.add(new LatLon(params.start.getLatitude(), params.start.getLongitude()));
|
||||
if (Algorithms.isEmpty(params.intermediates)) {
|
||||
points.addAll(params.intermediates);
|
||||
}
|
||||
params.intermediates = null;
|
||||
return new RouteCalculationResult(res, null, params, null, true);
|
||||
points.add(params.end);
|
||||
return points;
|
||||
}
|
||||
|
||||
protected RouteCalculationResult findBROUTERRoute(RouteCalculationParams params) throws MalformedURLException,
|
||||
|
|
|
@ -8,7 +8,7 @@ import androidx.annotation.Nullable;
|
|||
import net.osmand.plus.OsmandApplication;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.onlinerouting.OnlineRoutingEngine;
|
||||
import net.osmand.plus.onlinerouting.OnlineRoutingEngine.EngineParameterType;
|
||||
import net.osmand.plus.onlinerouting.OnlineRoutingEngine.EngineParameter;
|
||||
import net.osmand.plus.onlinerouting.OnlineRoutingHelper;
|
||||
|
||||
import org.json.JSONException;
|
||||
|
@ -93,8 +93,8 @@ public class OnlineRoutingSettingsItem extends CollectionSettingsItem<OnlineRout
|
|||
int number = 0;
|
||||
while (true) {
|
||||
number++;
|
||||
OnlineRoutingEngine renamedItem = OnlineRoutingEngine.createNewEngine(item.getServerType(), item.getVehicleKey(), item.getParams());
|
||||
renamedItem.putParameter(EngineParameterType.CUSTOM_NAME, renamedItem.getName(app) + "_" + number);
|
||||
OnlineRoutingEngine renamedItem = OnlineRoutingEngine.createNewEngine(item.getType(), item.getVehicleKey(), item.getParams());
|
||||
renamedItem.putParameter(EngineParameter.CUSTOM_NAME, renamedItem.getName(app) + "_" + number);
|
||||
if (!isDuplicate(renamedItem)) {
|
||||
return renamedItem;
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@ import java.util.List;
|
|||
import java.util.Set;
|
||||
|
||||
import static net.osmand.plus.importfiles.ImportHelper.ImportType.SETTINGS;
|
||||
import static net.osmand.plus.profiles.SelectProfileBottomSheet.IS_PROFILE_IMPORTED_ARG;
|
||||
import static net.osmand.plus.profiles.SelectProfileBottomSheet.PROFILES_LIST_UPDATED_ARG;
|
||||
import static net.osmand.plus.profiles.SelectProfileBottomSheet.PROFILE_KEY_ARG;
|
||||
|
||||
public class MainSettingsFragment extends BaseSettingsFragment implements OnSelectProfileCallback{
|
||||
|
@ -229,7 +229,7 @@ public class MainSettingsFragment extends BaseSettingsFragment implements OnSele
|
|||
FragmentManager fragmentManager = activity.getSupportFragmentManager();
|
||||
if (fragmentManager != null) {
|
||||
String profileKey = args.getString(PROFILE_KEY_ARG);
|
||||
boolean imported = args.getBoolean(IS_PROFILE_IMPORTED_ARG);
|
||||
boolean imported = args.getBoolean(PROFILES_LIST_UPDATED_ARG);
|
||||
ProfileAppearanceFragment.showInstance(activity, SettingsScreenType.PROFILE_APPEARANCE,
|
||||
profileKey, imported);
|
||||
}
|
||||
|
|
|
@ -10,13 +10,14 @@ import androidx.preference.Preference;
|
|||
import androidx.preference.SwitchPreferenceCompat;
|
||||
|
||||
import net.osmand.plus.measurementtool.MeasurementToolFragment;
|
||||
import net.osmand.plus.profiles.ProfileDataObject;
|
||||
import net.osmand.plus.profiles.ProfileDataUtils;
|
||||
import net.osmand.plus.routepreparationmenu.RouteOptionsBottomSheet;
|
||||
import net.osmand.plus.routepreparationmenu.RouteOptionsBottomSheet.DialogMode;
|
||||
import net.osmand.plus.routing.RouteProvider.RouteService;
|
||||
import net.osmand.plus.settings.backend.ApplicationMode;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.activities.MapActivity;
|
||||
import net.osmand.plus.profiles.RoutingProfileDataObject;
|
||||
import net.osmand.plus.profiles.RoutingProfileDataObject.RoutingProfilesResources;
|
||||
import net.osmand.plus.profiles.SelectProfileBottomSheet;
|
||||
import net.osmand.plus.profiles.SelectProfileBottomSheet.OnSelectProfileCallback;
|
||||
|
@ -26,7 +27,8 @@ import net.osmand.util.Algorithms;
|
|||
|
||||
import java.util.Map;
|
||||
|
||||
import static net.osmand.plus.profiles.SelectProfileBottomSheet.IS_PROFILE_IMPORTED_ARG;
|
||||
import static net.osmand.plus.onlinerouting.OnlineRoutingEngine.ONLINE_ROUTING_ENGINE_PREFIX;
|
||||
import static net.osmand.plus.profiles.SelectProfileBottomSheet.PROFILES_LIST_UPDATED_ARG;
|
||||
import static net.osmand.plus.profiles.SelectProfileBottomSheet.PROFILE_KEY_ARG;
|
||||
import static net.osmand.plus.routepreparationmenu.RouteOptionsBottomSheet.DIALOG_MODE_KEY;
|
||||
|
||||
|
@ -35,13 +37,13 @@ public class NavigationFragment extends BaseSettingsFragment implements OnSelect
|
|||
public static final String TAG = NavigationFragment.class.getSimpleName();
|
||||
public static final String NAVIGATION_TYPE = "navigation_type";
|
||||
|
||||
private Map<String, RoutingProfileDataObject> routingProfileDataObjects;
|
||||
private Map<String, ProfileDataObject> routingProfileDataObjects;
|
||||
private Preference navigationType;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
routingProfileDataObjects = ProfileDataUtils.getRoutingProfiles(app);
|
||||
updateRoutingProfilesDataObjects();
|
||||
setupOnBackPressedCallback();
|
||||
}
|
||||
|
||||
|
@ -90,7 +92,7 @@ public class NavigationFragment extends BaseSettingsFragment implements OnSelect
|
|||
private void setupNavigationTypePref() {
|
||||
String routingProfileKey = getSelectedAppMode().getRoutingProfile();
|
||||
if (!Algorithms.isEmpty(routingProfileKey)) {
|
||||
RoutingProfileDataObject routingProfileDataObject = routingProfileDataObjects.get(routingProfileKey);
|
||||
ProfileDataObject routingProfileDataObject = routingProfileDataObjects.get(routingProfileKey);
|
||||
if (routingProfileDataObject != null) {
|
||||
navigationType.setSummary(routingProfileDataObject.getName());
|
||||
navigationType.setIcon(getActiveIcon(routingProfileDataObject.getIconRes()));
|
||||
|
@ -132,12 +134,16 @@ public class NavigationFragment extends BaseSettingsFragment implements OnSelect
|
|||
return false;
|
||||
}
|
||||
|
||||
private void updateRoutingProfilesDataObjects() {
|
||||
routingProfileDataObjects = ProfileDataUtils.getRoutingProfiles(app);
|
||||
}
|
||||
|
||||
void updateRoutingProfile(String profileKey) {
|
||||
RoutingProfileDataObject selectedRoutingProfileDataObject = routingProfileDataObjects.get(profileKey);
|
||||
ProfileDataObject selectedRoutingProfileDataObject = routingProfileDataObjects.get(profileKey);
|
||||
if (profileKey == null || selectedRoutingProfileDataObject == null) {
|
||||
return;
|
||||
}
|
||||
for (Map.Entry<String, RoutingProfileDataObject> rp : routingProfileDataObjects.entrySet()) {
|
||||
for (Map.Entry<String, ProfileDataObject> rp : routingProfileDataObjects.entrySet()) {
|
||||
boolean selected = profileKey.equals(rp.getKey());
|
||||
rp.getValue().setSelected(selected);
|
||||
}
|
||||
|
@ -152,6 +158,8 @@ public class NavigationFragment extends BaseSettingsFragment implements OnSelect
|
|||
routeService = RouteProvider.RouteService.DIRECT_TO;
|
||||
} else if (profileKey.equals(RoutingProfilesResources.BROUTER_MODE.name())) {
|
||||
routeService = RouteProvider.RouteService.BROUTER;
|
||||
} else if (profileKey.startsWith(ONLINE_ROUTING_ENGINE_PREFIX)) {
|
||||
routeService = RouteService.ONLINE;
|
||||
} else {
|
||||
routeService = RouteProvider.RouteService.OSMAND;
|
||||
}
|
||||
|
@ -174,8 +182,8 @@ public class NavigationFragment extends BaseSettingsFragment implements OnSelect
|
|||
|
||||
@Override
|
||||
public void onProfileSelected(Bundle args) {
|
||||
if (args.getBoolean(IS_PROFILE_IMPORTED_ARG)) {
|
||||
routingProfileDataObjects = ProfileDataUtils.getRoutingProfiles(app);
|
||||
if (args.getBoolean(PROFILES_LIST_UPDATED_ARG)) {
|
||||
updateRoutingProfilesDataObjects();
|
||||
}
|
||||
updateRoutingProfile(args.getString(PROFILE_KEY_ARG));
|
||||
}
|
||||
|
|
|
@ -66,7 +66,7 @@ import java.util.ArrayList;
|
|||
import java.util.Collections;
|
||||
|
||||
import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_SETTINGS_ID;
|
||||
import static net.osmand.plus.profiles.SelectProfileBottomSheet.IS_PROFILE_IMPORTED_ARG;
|
||||
import static net.osmand.plus.profiles.SelectProfileBottomSheet.PROFILES_LIST_UPDATED_ARG;
|
||||
import static net.osmand.plus.profiles.SelectProfileBottomSheet.PROFILE_KEY_ARG;
|
||||
|
||||
public class ProfileAppearanceFragment extends BaseSettingsFragment implements OnSelectProfileCallback {
|
||||
|
@ -934,7 +934,7 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment implements O
|
|||
@Override
|
||||
public void onProfileSelected(Bundle args) {
|
||||
String profileKey = args.getString(PROFILE_KEY_ARG);
|
||||
boolean imported = args.getBoolean(IS_PROFILE_IMPORTED_ARG);
|
||||
boolean imported = args.getBoolean(PROFILES_LIST_UPDATED_ARG);
|
||||
updateParentProfile(profileKey, imported);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue