Implement Online routing backend
This commit is contained in:
parent
e74c72af73
commit
170deedad4
16 changed files with 469 additions and 202 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()));
|
||||
|
|
|
@ -4,40 +4,51 @@ import android.content.Context;
|
|||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import net.osmand.data.LatLon;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.onlinerouting.type.EngineType;
|
||||
import net.osmand.util.Algorithms;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
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 customUrl;
|
||||
private String vehicleKey;
|
||||
private Map<String, String> params = new HashMap<>();
|
||||
|
||||
private OnlineRoutingEngine() {};
|
||||
|
||||
public OnlineRoutingEngine(@NonNull String stringKey,
|
||||
@NonNull ServerType serverType,
|
||||
@NonNull EngineType type,
|
||||
@NonNull String vehicleKey,
|
||||
Map<String, String> params) {
|
||||
this(stringKey, serverType, vehicleKey);
|
||||
this(stringKey, type, vehicleKey);
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
public OnlineRoutingEngine(@NonNull String stringKey,
|
||||
@NonNull ServerType serverType,
|
||||
@NonNull EngineType type,
|
||||
@NonNull String vehicleKey) {
|
||||
this(type, vehicleKey);
|
||||
this.stringKey = stringKey;
|
||||
this.serverType = serverType;
|
||||
}
|
||||
|
||||
private OnlineRoutingEngine(@NonNull EngineType type,
|
||||
@NonNull String vehicleKey) {
|
||||
this.type = type;
|
||||
this.vehicleKey = vehicleKey;
|
||||
}
|
||||
|
||||
|
@ -45,8 +56,15 @@ public class OnlineRoutingEngine {
|
|||
return stringKey;
|
||||
}
|
||||
|
||||
public ServerType getServerType() {
|
||||
return serverType;
|
||||
public EngineType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public String getBaseUrl() {
|
||||
if (Algorithms.isEmpty(customUrl)) {
|
||||
return type.getStandardUrl();
|
||||
}
|
||||
return customUrl;
|
||||
}
|
||||
|
||||
public String getVehicleKey() {
|
||||
|
@ -57,25 +75,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 {
|
||||
|
@ -83,21 +92,30 @@ public class OnlineRoutingEngine {
|
|||
}
|
||||
}
|
||||
|
||||
public String createFullUrl(@NonNull List<LatLon> path) {
|
||||
return type.createFullUrl(this, path);
|
||||
}
|
||||
|
||||
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,
|
||||
public static OnlineRoutingEngine createNewEngine(@NonNull EngineType type,
|
||||
@NonNull String vehicleKey) {
|
||||
return new OnlineRoutingEngine(generateKey(), serverType, vehicleKey);
|
||||
return new OnlineRoutingEngine(generateKey(), type, vehicleKey);
|
||||
}
|
||||
|
||||
public static OnlineRoutingEngine createTmpEngine(@NonNull EngineType type,
|
||||
@NonNull String vehicleKey) {
|
||||
return new OnlineRoutingEngine(type, vehicleKey);
|
||||
}
|
||||
|
||||
private static String generateKey() {
|
||||
|
|
|
@ -30,7 +30,10 @@ 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.onlinerouting.type.EngineType;
|
||||
import net.osmand.plus.onlinerouting.type.GraphhoperEngine;
|
||||
import net.osmand.plus.onlinerouting.type.OsrmEngine;
|
||||
import net.osmand.plus.routepreparationmenu.cards.BaseCard;
|
||||
import net.osmand.plus.settings.backend.ApplicationMode;
|
||||
import net.osmand.util.Algorithms;
|
||||
|
@ -62,7 +65,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;
|
||||
|
@ -143,7 +146,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
|
|||
setupToolbar((Toolbar) view.findViewById(R.id.toolbar));
|
||||
|
||||
setupNameCard();
|
||||
setupServerCard();
|
||||
setupTypeCard();
|
||||
setupVehicleCard();
|
||||
setupApiKeyCard();
|
||||
setupExampleCard();
|
||||
|
@ -152,7 +155,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
|
|||
|
||||
setupButtons();
|
||||
|
||||
updateCardViews(nameCard, serverCard, vehicleCard, exampleCard);
|
||||
updateCardViews(nameCard, typeCard, vehicleCard, exampleCard);
|
||||
return view;
|
||||
}
|
||||
|
||||
|
@ -174,38 +177,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 (!Algorithms.objectEquals(engine.type.getStringKey(), type.getStringKey())) {
|
||||
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 +346,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 instanceof GraphhoperEngine) {
|
||||
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 +410,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());
|
||||
engineToSave = OnlineRoutingEngine.createNewEngine(engine.type, engine.getVehicleKey());
|
||||
}
|
||||
|
||||
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 instanceof GraphhoperEngine) {
|
||||
engineToSave.putParameter(EngineParameter.API_KEY, engine.apiKey);
|
||||
}
|
||||
|
||||
helper.saveEngine(engineToSave);
|
||||
|
@ -419,30 +429,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.createTmpEngine(engine.type, engine.getVehicleKey());
|
||||
tmpEngine.putParameter(EngineParameter.CUSTOM_URL, engine.customServerUrl);
|
||||
tmpEngine.putParameter(EngineParameter.API_KEY, engine.apiKey);
|
||||
return tmpEngine.createFullUrl(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,9 +451,9 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
|
|||
try {
|
||||
JSONObject obj = new JSONObject(response);
|
||||
|
||||
if (server == ServerType.GRAPHHOPER) {
|
||||
if (type instanceof GraphhoperEngine) {
|
||||
resultOk = obj.has("paths");
|
||||
} else if (server == ServerType.OSRM) {
|
||||
} else if (type instanceof OsrmEngine) {
|
||||
resultOk = obj.has("routes");
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
|
@ -494,7 +492,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.getStringKey());
|
||||
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 +506,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 +517,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 +534,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment {
|
|||
}
|
||||
engine.vehicleType = vehicleType;
|
||||
}
|
||||
engine.apiKey = editedEngine.getParameter(EngineParameterType.API_KEY);
|
||||
engine.apiKey = editedEngine.getParameter(EngineParameter.API_KEY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -583,7 +580,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 +593,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,7 +6,11 @@ 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.type.EngineType;
|
||||
import net.osmand.plus.settings.backend.OsmandSettings;
|
||||
import net.osmand.util.Algorithms;
|
||||
|
||||
|
@ -15,9 +19,14 @@ 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 +36,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 +46,39 @@ 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 = engine.createFullUrl(path);
|
||||
String content = makeRequest(fullUrl);
|
||||
return engine.getType().parseResponse(content);
|
||||
}
|
||||
|
||||
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 +91,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
|
||||
|
@ -100,7 +123,7 @@ public class OnlineRoutingHelper {
|
|||
private void saveToSettings() {
|
||||
if (!Algorithms.isEmpty(cachedEngines)) {
|
||||
JSONObject json = new JSONObject();
|
||||
if (writeToJson(json, cachedEngines)) {
|
||||
if (writeToJson(json, getEngines())) {
|
||||
settings.ONLINE_ROUTING_ENGINES.set(json.toString());
|
||||
}
|
||||
} else {
|
||||
|
@ -121,10 +144,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));
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
LOG.debug("Error when reading engines from JSON: " + e.toString());
|
||||
|
@ -140,7 +163,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().getStringKey());
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
package net.osmand.plus.onlinerouting.type;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import net.osmand.data.LatLon;
|
||||
import net.osmand.plus.onlinerouting.OnlineRoutingEngine;
|
||||
import net.osmand.util.Algorithms;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class EngineType {
|
||||
|
||||
public abstract String getStringKey();
|
||||
|
||||
public abstract String getTitle();
|
||||
|
||||
public abstract String getStandardUrl();
|
||||
|
||||
public String createFullUrl(OnlineRoutingEngine engine, List<LatLon> path) {
|
||||
StringBuilder sb = new StringBuilder(engine.getBaseUrl());
|
||||
createFullUrl(sb, engine, path);
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
protected abstract void createFullUrl(StringBuilder sb,
|
||||
OnlineRoutingEngine engine,
|
||||
List<LatLon> path);
|
||||
|
||||
public List<LatLon> parseResponse(@NonNull String content) throws JSONException {
|
||||
return parseResponse(new JSONObject(content), content);
|
||||
}
|
||||
|
||||
protected abstract List<LatLon> parseResponse(JSONObject obj, @NonNull String content) throws JSONException;
|
||||
|
||||
public static EngineType[] values() {
|
||||
EngineType[] types = new EngineType[] {
|
||||
new GraphhoperEngine(),
|
||||
new OsrmEngine()
|
||||
};
|
||||
return types;
|
||||
}
|
||||
|
||||
public static EngineType valueOf(String key) {
|
||||
EngineType[] values = values();
|
||||
for (EngineType type : values) {
|
||||
if (Algorithms.objectEquals(type.getStringKey(), key)) {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
return values[0];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package net.osmand.plus.onlinerouting.type;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import net.osmand.data.LatLon;
|
||||
import net.osmand.plus.onlinerouting.OnlineRoutingEngine;
|
||||
import net.osmand.plus.onlinerouting.OnlineRoutingEngine.EngineParameter;
|
||||
import net.osmand.util.Algorithms;
|
||||
import net.osmand.util.GeoPolylineParserUtil;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class GraphhoperEngine extends EngineType {
|
||||
|
||||
@Override
|
||||
public String getStringKey() {
|
||||
return "graphhoper";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTitle() {
|
||||
return "Graphhoper";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getStandardUrl() {
|
||||
return "https://graphhopper.com/api/1/route";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void createFullUrl(StringBuilder sb,
|
||||
OnlineRoutingEngine engine,
|
||||
List<LatLon> path) {
|
||||
sb.append("?");
|
||||
for (LatLon point : path) {
|
||||
sb.append("point=")
|
||||
.append(point.getLatitude())
|
||||
.append(',')
|
||||
.append(point.getLongitude())
|
||||
.append('&');
|
||||
}
|
||||
sb.append("vehicle=").append(engine.getVehicleKey());
|
||||
|
||||
String apiKey = engine.getParameter(EngineParameter.API_KEY);
|
||||
if (!Algorithms.isEmpty(apiKey)) {
|
||||
sb.append('&').append("key=").append(apiKey);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<LatLon> parseResponse(JSONObject obj, @NonNull String content) throws JSONException {
|
||||
return GeoPolylineParserUtil.parse(
|
||||
obj.getJSONArray("routes").getJSONObject(0).getString("geometry"),
|
||||
GeoPolylineParserUtil.PRECISION_5);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package net.osmand.plus.onlinerouting.type;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import net.osmand.data.LatLon;
|
||||
import net.osmand.plus.onlinerouting.OnlineRoutingEngine;
|
||||
import net.osmand.util.GeoPolylineParserUtil;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OsrmEngine extends EngineType {
|
||||
|
||||
@Override
|
||||
public String getStringKey() {
|
||||
return "osrm";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTitle() {
|
||||
return "OSRM";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getStandardUrl() {
|
||||
return "https://router.project-osrm.org/route/v1/";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void createFullUrl(StringBuilder sb,
|
||||
OnlineRoutingEngine engine,
|
||||
List<LatLon> path) {
|
||||
sb.append(engine.getVehicleKey()).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(';');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<LatLon> parseResponse(@NonNull JSONObject obj, @NonNull String content) throws JSONException {
|
||||
return GeoPolylineParserUtil.parse(
|
||||
obj.getJSONArray("routes").getJSONObject(0).getString("geometry"),
|
||||
GeoPolylineParserUtil.PRECISION_5);
|
||||
}
|
||||
|
||||
}
|
|
@ -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,6 +366,8 @@ public class RouteProvider {
|
|||
res = findVectorMapsRoute(params, calcGPXRoute);
|
||||
} else if (params.mode.getRouteService() == RouteService.BROUTER) {
|
||||
res = findBROUTERRoute(params);
|
||||
} else if (params.mode.getRouteService() == RouteService.ONLINE) {
|
||||
res = findOnlineRoute(params);
|
||||
// } else if (params.type == RouteService.ORS) {
|
||||
// res = findORSRoute(params);
|
||||
// } else if (params.type == RouteService.OSRM) {
|
||||
|
@ -383,6 +388,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,6 +1211,35 @@ public class RouteProvider {
|
|||
return exporter.exportRoute();
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
params.intermediates = null;
|
||||
return new RouteCalculationResult(res, null, params, null, true);
|
||||
} else {
|
||||
return new RouteCalculationResult("Route is empty");
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
points.add(params.end);
|
||||
return points;
|
||||
}
|
||||
|
||||
private void appendOSRMLoc(StringBuilder uri, LatLon il) {
|
||||
uri.append(";").append(il.getLongitude());
|
||||
uri.append(",").append(il.getLatitude());
|
||||
|
|
|
@ -48,7 +48,6 @@ import net.osmand.plus.helpers.enums.TracksSortByMode;
|
|||
import net.osmand.plus.mapillary.MapillaryPlugin;
|
||||
import net.osmand.plus.mapmarkers.CoordinateInputFormats.Format;
|
||||
import net.osmand.plus.mapmarkers.MapMarkersMode;
|
||||
import net.osmand.plus.onlinerouting.OnlineRoutingEngine;
|
||||
import net.osmand.plus.profiles.LocationIcon;
|
||||
import net.osmand.plus.profiles.NavigationIcon;
|
||||
import net.osmand.plus.profiles.ProfileIconColors;
|
||||
|
|
|
@ -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