From c00c28e81116a28f2a3746e95e3a203256d56e16 Mon Sep 17 00:00:00 2001 From: nazar-kutz Date: Mon, 28 Dec 2020 01:02:46 +0200 Subject: [PATCH 1/8] Online Routing UI - initial commit --- .../online_routing_engine_preference.xml | 35 ++ .../online_routing_preference_segment.xml | 185 ++++++++ .../preference_toolbar_with_action_button.xml | 95 ++++ OsmAnd/res/values/strings.xml | 11 +- .../profiles/SelectProfileBottomSheet.java | 11 +- .../onlinerouting/ExampleLocation.java | 26 ++ .../OnlineRoutingSegmentCard.java | 182 ++++++++ .../profiles/onlinerouting/ServerType.java | 22 + .../profiles/onlinerouting/VehicleType.java | 25 ++ .../OnlineRoutingEngineFragment.java | 409 ++++++++++++++++++ 10 files changed, 999 insertions(+), 2 deletions(-) create mode 100644 OsmAnd/res/layout/online_routing_engine_preference.xml create mode 100644 OsmAnd/res/layout/online_routing_preference_segment.xml create mode 100644 OsmAnd/res/layout/preference_toolbar_with_action_button.xml create mode 100644 OsmAnd/src/net/osmand/plus/profiles/onlinerouting/ExampleLocation.java create mode 100644 OsmAnd/src/net/osmand/plus/profiles/onlinerouting/OnlineRoutingSegmentCard.java create mode 100644 OsmAnd/src/net/osmand/plus/profiles/onlinerouting/ServerType.java create mode 100644 OsmAnd/src/net/osmand/plus/profiles/onlinerouting/VehicleType.java create mode 100644 OsmAnd/src/net/osmand/plus/settings/fragments/OnlineRoutingEngineFragment.java diff --git a/OsmAnd/res/layout/online_routing_engine_preference.xml b/OsmAnd/res/layout/online_routing_engine_preference.xml new file mode 100644 index 0000000000..69cc042252 --- /dev/null +++ b/OsmAnd/res/layout/online_routing_engine_preference.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/layout/online_routing_preference_segment.xml b/OsmAnd/res/layout/online_routing_preference_segment.xml new file mode 100644 index 0000000000..0b1799e478 --- /dev/null +++ b/OsmAnd/res/layout/online_routing_preference_segment.xml @@ -0,0 +1,185 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/layout/preference_toolbar_with_action_button.xml b/OsmAnd/res/layout/preference_toolbar_with_action_button.xml new file mode 100644 index 0000000000..7c072f7daf --- /dev/null +++ b/OsmAnd/res/layout/preference_toolbar_with_action_button.xml @@ -0,0 +1,95 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index a78818557b..6a66dd03ea 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -11,7 +11,16 @@ Thx - Hardy --> - + Test route calculation + URL with all parameters will look like this: + Keep it empty if not + Enter param + Server URL + API key + Vehicle + Subtype + Edit online routing engine + Add online routing engine Allow intermittent water ways Allow intermittent water ways Allow streams and drains diff --git a/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheet.java b/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheet.java index eaf99b9503..da92070b65 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheet.java @@ -22,7 +22,6 @@ import androidx.annotation.Nullable; import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentActivity; -import androidx.fragment.app.FragmentManager; import net.osmand.AndroidUtils; import net.osmand.CallbackWithObject; @@ -39,6 +38,7 @@ import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem; import net.osmand.plus.helpers.FontCache; import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.settings.bottomsheets.BasePreferenceBottomSheet; +import net.osmand.plus.settings.fragments.OnlineRoutingEngineFragment; import net.osmand.router.RoutingConfiguration; import org.apache.commons.logging.Log; @@ -179,6 +179,15 @@ public class SelectProfileBottomSheet extends BasePreferenceBottomSheet { }); } }); + addButtonItem(R.string.add_online_routing_engine, R.drawable.ic_world_globe_dark, new OnClickListener() { + @Override + public void onClick(View v) { + if (getActivity() != null) { + OnlineRoutingEngineFragment.showInstance(getActivity(), false); + } + dismiss(); + } + }); items.add(new BaseBottomSheetItem.Builder() .setCustomView(bottomSpaceView) .create()); diff --git a/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/ExampleLocation.java b/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/ExampleLocation.java new file mode 100644 index 0000000000..ec1dd2149b --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/ExampleLocation.java @@ -0,0 +1,26 @@ +package net.osmand.plus.profiles.onlinerouting; + +import net.osmand.data.LatLon; + +public enum ExampleLocation { + AMSTERDAM("Amsterdam", new LatLon(52.379189, 4.899431)), + BERLIN("Berlin", new LatLon(52.520008, 13.404954)), + NEW_YORK("New York", new LatLon(43.000000, -75.000000)), + PARIS("Paris", new LatLon(48.864716, 2.349014)); + + ExampleLocation(String title, LatLon latLon) { + this.title = title; + this.latLon = latLon; + } + + private String title; + private LatLon latLon; + + public String getTitle() { + return title; + } + + public LatLon getLatLon() { + return latLon; + } +} diff --git a/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/OnlineRoutingSegmentCard.java b/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/OnlineRoutingSegmentCard.java new file mode 100644 index 0000000000..3ea6c4386f --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/OnlineRoutingSegmentCard.java @@ -0,0 +1,182 @@ +package net.osmand.plus.profiles.onlinerouting; + +import android.text.Editable; +import android.text.TextWatcher; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.EditText; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import net.osmand.CallbackWithObject; +import net.osmand.plus.R; +import net.osmand.plus.UiUtilities; +import net.osmand.plus.UiUtilities.DialogButtonType; +import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.mapcontextmenu.other.HorizontalSelectionAdapter; +import net.osmand.plus.mapcontextmenu.other.HorizontalSelectionAdapter.HorizontalSelectionAdapterListener; +import net.osmand.plus.mapcontextmenu.other.HorizontalSelectionAdapter.HorizontalSelectionItem; +import net.osmand.plus.routepreparationmenu.cards.BaseCard; +import net.osmand.plus.widgets.OsmandTextFieldBoxes; + +import java.util.List; + +public class OnlineRoutingSegmentCard extends BaseCard { + + private View headerContainer; + private TextView tvHeaderTitle; + private TextView tvHeaderSubtitle; + private RecyclerView rvSelectionMenu; + private HorizontalSelectionAdapter adapter; + private TextView tvDescription; + private View fieldBoxContainer; + private OsmandTextFieldBoxes textFieldBoxes; + private EditText editText; + private TextView tvHelperText; + private View bottomDivider; + private View button; + private View resultContainer; + private OnTextChangedListener onTextChangedListener; + + public OnlineRoutingSegmentCard(@NonNull MapActivity mapActivity) { + super(mapActivity); + build(mapActivity); + } + + @Override + public int getCardLayoutId() { + return R.layout.online_routing_preference_segment; + } + + @Override + protected void updateContent() { + headerContainer = view.findViewById(R.id.header); + tvHeaderTitle = view.findViewById(R.id.title); + tvHeaderSubtitle = view.findViewById(R.id.subtitle); + rvSelectionMenu = view.findViewById(R.id.selection_menu); + tvDescription = view.findViewById(R.id.description); + fieldBoxContainer = view.findViewById(R.id.field_box_container); + textFieldBoxes = view.findViewById(R.id.field_box); + editText = view.findViewById(R.id.edit_text); + tvHelperText = view.findViewById(R.id.helper_text); + bottomDivider = view.findViewById(R.id.bottom_divider); + button = view.findViewById(R.id.button); + resultContainer = view.findViewById(R.id.result_container); + + editText.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + } + + @Override + public void afterTextChanged(Editable s) { + if (onTextChangedListener != null) { + boolean editedByUser = editText.getTag() == null; + String text = editText.getText().toString(); + onTextChangedListener.onTextChanged(editedByUser, text); + } + } + }); + } + + public void setHeaderTitle(@NonNull String title) { + showElements(headerContainer, tvHeaderTitle); + tvHeaderTitle.setText(title); + } + + public void setHeaderSubtitle(@NonNull String subtitle) { + showElements(headerContainer, tvHeaderSubtitle); + tvHeaderSubtitle.setText(subtitle); + } + + public void setSelectionMenu(List items, + String selectedItemTitle, + final CallbackWithObject callback) { + showElements(rvSelectionMenu); + rvSelectionMenu.setLayoutManager( + new LinearLayoutManager(app, RecyclerView.HORIZONTAL, false)); + adapter = new HorizontalSelectionAdapter(app, nightMode); + adapter.setItems(items); + adapter.setSelectedItemByTitle(selectedItemTitle); + adapter.setListener(new HorizontalSelectionAdapterListener() { + @Override + public void onItemSelected(HorizontalSelectionItem item) { + if (callback.processResult(item)) { + adapter.setSelectedItem(item); + } + } + }); + rvSelectionMenu.setAdapter(adapter); + } + + public void setDescription(@NonNull String description) { + showElements(tvDescription); + tvDescription.setText(description); + } + + public void setFieldBoxLabelText(@NonNull String labelText) { + showElements(fieldBoxContainer); + textFieldBoxes.setLabelText(labelText); + } + + public void setFieldBoxHelperText(@NonNull String helperText) { + showElements(fieldBoxContainer, tvHelperText); + tvHelperText.setText(helperText); + } + + public void setEditedText(@NonNull String text) { + editText.setTag(""); // needed to indicate that the text was edited programmatically + editText.setText(text); + editText.setTag(null); + } + + public void showDivider() { + showElements(bottomDivider); + } + + public void setButton(OnClickListener listener) { + showElements(button); + button.setOnClickListener(listener); + UiUtilities.setupDialogButton(nightMode, button, + DialogButtonType.PRIMARY, R.string.test_route_calculation); + } + + public void showFieldBox() { + showElements(fieldBoxContainer); + } + + public void hideFieldBox() { + hideElements(fieldBoxContainer); + } + + private void showElements(View... views) { + changeVisibility(View.VISIBLE, views); + } + + private void hideElements(View... views) { + changeVisibility(View.GONE, views); + } + + private void changeVisibility(int visibility, View... views) { + for (View v : views) { + if (visibility != v.getVisibility()) { + v.setVisibility(visibility); + } + } + } + + public void setOnTextChangedListener(OnTextChangedListener onTextChangedListener) { + this.onTextChangedListener = onTextChangedListener; + } + + public interface OnTextChangedListener { + void onTextChanged(boolean editedByUser, String text); + } +} diff --git a/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/ServerType.java b/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/ServerType.java new file mode 100644 index 0000000000..d624aa269e --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/ServerType.java @@ -0,0 +1,22 @@ +package net.osmand.plus.profiles.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; + } +} diff --git a/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/VehicleType.java b/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/VehicleType.java new file mode 100644 index 0000000000..78a0ac1e64 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/VehicleType.java @@ -0,0 +1,25 @@ +package net.osmand.plus.profiles.onlinerouting; + +public enum VehicleType { + CAR("car", "Car"), + BIKE("bike", "Bike"), + FOOT("foot", "Foot"), + DRIVING("driving", "Driving"), + CUSTOM("custom", "Custom"); + + VehicleType(String key, String title) { + this.key = key; + this.title = title; + } + + private String key; + private String title; + + public String getKey() { + return key; + } + + public String getTitle() { + return title; + } +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/OnlineRoutingEngineFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/OnlineRoutingEngineFragment.java new file mode 100644 index 0000000000..8e840f4a1b --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/OnlineRoutingEngineFragment.java @@ -0,0 +1,409 @@ +package net.osmand.plus.settings.fragments; + +import android.os.Build; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.View.OnClickListener; +import android.view.ViewGroup; +import android.widget.FrameLayout; +import android.widget.ImageView; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.widget.Toolbar; +import androidx.fragment.app.FragmentActivity; + +import net.osmand.AndroidUtils; +import net.osmand.CallbackWithObject; +import net.osmand.data.LatLon; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; +import net.osmand.plus.UiUtilities; +import net.osmand.plus.UiUtilities.DialogButtonType; +import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.base.BaseOsmAndFragment; +import net.osmand.plus.mapcontextmenu.other.HorizontalSelectionAdapter.HorizontalSelectionItem; +import net.osmand.plus.profiles.onlinerouting.OnlineRoutingSegmentCard; +import net.osmand.plus.profiles.onlinerouting.OnlineRoutingSegmentCard.OnTextChangedListener; +import net.osmand.plus.profiles.onlinerouting.ServerType; +import net.osmand.plus.profiles.onlinerouting.ExampleLocation; +import net.osmand.plus.profiles.onlinerouting.VehicleType; +import net.osmand.plus.routepreparationmenu.cards.BaseCard; +import net.osmand.util.Algorithms; + +import java.util.ArrayList; +import java.util.List; + +public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { + + public static final String TAG = OnlineRoutingEngineFragment.class.getSimpleName(); + + private static final String ENGINE_NAME_KEY = "engine_name"; + private static final String ENGINE_SERVER_KEY = "engine_server"; + private static final String ENGINE_SERVER_URL_KEY = "engine_server_url"; + private static final String ENGINE_VEHICLE_TYPE_KEY = "engine_vehicle_type"; + private static final String ENGINE_CUSTOM_VEHICLE_KEY = "engine_custom_vehicle"; + private static final String ENGINE_API_KEY_KEY = "engine_api_key"; + private static final String ENGINE_NAME_CHANGED_BY_USER_KEY = "engine_name_changed_by_user_key"; + private static final String EXAMPLE_LOCATION_KEY = "example_location"; + + private OnlineRoutingSegmentCard nameCard; + private OnlineRoutingSegmentCard serverCard; + private OnlineRoutingSegmentCard vehicleCard; + private OnlineRoutingSegmentCard apiKeyCard; + private OnlineRoutingSegmentCard exampleCard; + + private boolean isEditingMode; + private boolean nightMode; + + private OnlineRoutingEngineObject engine; + private ExampleLocation selectedLocation; + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + engine = new OnlineRoutingEngineObject(); + if (savedInstanceState != null) { + restoreState(savedInstanceState); + } else { + initEngineState(); + } + } + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, + @Nullable ViewGroup container, + @Nullable Bundle savedInstanceState) { + OsmandApplication app = requireMyApplication(); + MapActivity mapActivity = getMapActivity(); + if (mapActivity == null) { + return null; + } + nightMode = !app.getSettings().isLightContent(); + + View view = UiUtilities.getInflater(getContext(), nightMode) + .inflate(R.layout.online_routing_engine_preference, container, false); + + if (Build.VERSION.SDK_INT >= 21) { + AndroidUtils.addStatusBarPadding21v(getContext(), view); + } + setupToolbar(view); + + ViewGroup segmentsContainer = (ViewGroup) view.findViewById(R.id.segments_container); + + // create name card + nameCard = new OnlineRoutingSegmentCard(mapActivity); + nameCard.setDescription(getString(R.string.select_nav_profile_dialog_message)); + nameCard.setFieldBoxLabelText(getString(R.string.shared_string_name)); + nameCard.setOnTextChangedListener(new OnTextChangedListener() { + @Override + public void onTextChanged(boolean changedByUser, String text) { + if (!isEditingMode && !engine.wasNameChangedByUser && changedByUser) { + engine.wasNameChangedByUser = true; + } + engine.name = text; + } + }); + nameCard.showDivider(); + segmentsContainer.addView(nameCard.getView()); + + // create server card + serverCard = new OnlineRoutingSegmentCard(mapActivity); + serverCard.setHeaderTitle(getString(R.string.shared_string_type)); + List serverItems = new ArrayList<>(); + for (ServerType server : ServerType.values()) { + serverItems.add(new HorizontalSelectionItem(server.getTitle(), server)); + } + serverCard.setSelectionMenu(serverItems, engine.serverType.getTitle(), + new CallbackWithObject() { + @Override + public boolean processResult(HorizontalSelectionItem result) { + ServerType server = (ServerType) result.getObject(); + if (engine.serverType != server) { + engine.serverType = server; + updateCardViews(nameCard, serverCard, exampleCard); + return true; + } + return false; + } + }); +// serverCard.setOnTextChangedListener(new OnTextChangedListener() { +// @Override +// public void onTextChanged(boolean editedByUser, String text) { +// engine.serverBaseUrl = text; +// } +// }); + serverCard.setFieldBoxLabelText(getString(R.string.shared_string_server_url)); + serverCard.showDivider(); + segmentsContainer.addView(serverCard.getView()); + + // create vehicle card + vehicleCard = new OnlineRoutingSegmentCard(mapActivity); + vehicleCard.setHeaderTitle(getString(R.string.shared_string_vehicle)); + List vehicleItems = new ArrayList<>(); + for (VehicleType vehicle : VehicleType.values()) { + vehicleItems.add(new HorizontalSelectionItem(vehicle.getTitle(), vehicle)); + } + vehicleCard.setSelectionMenu(vehicleItems, engine.vehicleType.getTitle(), + new CallbackWithObject() { + @Override + public boolean processResult(HorizontalSelectionItem result) { + VehicleType vehicle = (VehicleType) result.getObject(); + if (engine.vehicleType != vehicle) { + engine.vehicleType = vehicle; + updateCardViews(nameCard, vehicleCard, exampleCard); + return true; + } + return false; + } + }); + vehicleCard.setFieldBoxLabelText(getString(R.string.shared_string_custom)); + vehicleCard.setOnTextChangedListener(new OnTextChangedListener() { + @Override + public void onTextChanged(boolean editedByUser, String text) { + engine.customVehicleKey = text; + } + }); + vehicleCard.setEditedText(engine.customVehicleKey); + vehicleCard.setFieldBoxHelperText(getString(R.string.shared_string_enter_param)); + vehicleCard.showDivider(); + segmentsContainer.addView(vehicleCard.getView()); + + // create api key card + apiKeyCard = new OnlineRoutingSegmentCard(mapActivity); + apiKeyCard.setHeaderTitle(getString(R.string.shared_string_api_key)); + apiKeyCard.setFieldBoxLabelText(getString(R.string.keep_it_empty_if_not)); + apiKeyCard.setEditedText(engine.apiKey); + apiKeyCard.showDivider(); + apiKeyCard.setOnTextChangedListener(new OnTextChangedListener() { + @Override + public void onTextChanged(boolean editedByUser, String text) { + engine.apiKey = text; + updateCardViews(exampleCard); + } + }); + segmentsContainer.addView(apiKeyCard.getView()); + + // create example card + exampleCard = new OnlineRoutingSegmentCard(mapActivity); + exampleCard.setHeaderTitle(getString(R.string.shared_string_example)); + List locationItems = new ArrayList<>(); + for (ExampleLocation location : ExampleLocation.values()) { + locationItems.add(new HorizontalSelectionItem(location.getTitle(), location)); + } + exampleCard.setSelectionMenu(locationItems, selectedLocation.getTitle(), + new CallbackWithObject() { + @Override + public boolean processResult(HorizontalSelectionItem result) { + ExampleLocation location = (ExampleLocation) result.getObject(); + if (selectedLocation != location) { + selectedLocation = location; + updateCardViews(exampleCard); + return true; + } + return false; + } + }); + exampleCard.setFieldBoxHelperText(getString(R.string.online_routing_example_hint)); + exampleCard.setButton(new View.OnClickListener() { + @Override + public void onClick(View v) { + // make request to the server + } + }); + segmentsContainer.addView(exampleCard.getView()); + + View bottomSpaceView = new View(app); + int space = (int) getResources().getDimension(R.dimen.empty_state_text_button_padding_top); + bottomSpaceView.setLayoutParams( + new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, space)); + segmentsContainer.addView(bottomSpaceView); + + View cancelButton = view.findViewById(R.id.dismiss_button); + UiUtilities.setupDialogButton(nightMode, cancelButton, + DialogButtonType.SECONDARY, R.string.shared_string_cancel); + cancelButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + dismiss(); + } + }); + + view.findViewById(R.id.buttons_divider).setVisibility(View.VISIBLE); + + View applyButton = view.findViewById(R.id.right_bottom_button); + UiUtilities.setupDialogButton(nightMode, applyButton, + UiUtilities.DialogButtonType.PRIMARY, R.string.shared_string_save); + applyButton.setVisibility(View.VISIBLE); + applyButton.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + // save engine to settings + } + }); + + updateCardViews(nameCard, serverCard, vehicleCard, exampleCard); + return view; + } + + private void setupToolbar(View mainView) { + Toolbar toolbar = (Toolbar) mainView.findViewById(R.id.toolbar); + ImageView navigationIcon = toolbar.findViewById(R.id.close_button); + navigationIcon.setImageResource(R.drawable.ic_action_close); + navigationIcon.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + dismiss(); + } + }); + TextView title = toolbar.findViewById(R.id.toolbar_title); + toolbar.findViewById(R.id.toolbar_subtitle).setVisibility(View.GONE); + View actionBtn = toolbar.findViewById(R.id.action_button); + if (isEditingMode) { + title.setText(getString(R.string.edit_online_routing_engine)); + ImageView ivBtn = toolbar.findViewById(R.id.action_button_icon); + ivBtn.setImageDrawable( + getIcon(R.drawable.ic_action_delete_dark, R.color.color_osm_edit_delete)); + actionBtn.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + // delete engine from settings + } + }); + } else { + title.setText(getString(R.string.add_online_routing_engine)); + actionBtn.setVisibility(View.GONE); + } + } + + private void updateCardViews(BaseCard ... cardsToUpdate) { + for (BaseCard card : cardsToUpdate) { + if (nameCard.equals(card)) { + if (!engine.wasNameChangedByUser) { + String name = String.format( + getString(R.string.ltr_or_rtl_combine_via_dash), + engine.serverType.getTitle(), engine.vehicleType.getTitle()); + nameCard.setEditedText(name); + } + + } else if (serverCard.equals(card)) { + serverCard.setHeaderSubtitle(engine.serverType.getTitle()); + serverCard.setEditedText(engine.serverType.getBaseUrl()); + + } else if (vehicleCard.equals(card)) { + vehicleCard.setHeaderSubtitle(engine.vehicleType.getTitle()); + if (engine.vehicleType == VehicleType.CUSTOM) { + vehicleCard.showFieldBox(); + } else { + vehicleCard.hideFieldBox(); + } + + } else if (exampleCard.equals(card)) { + exampleCard.setEditedText(getTestUrl()); + } + } + } + + private String getTestUrl() { + String baseUrl = engine.serverType.getBaseUrl(); + String vehicle = engine.getVehicleKey(); + + LatLon kievLatLon = new LatLon(50.431759, 30.517023); + LatLon destinationLatLon = selectedLocation.getLatLon(); + + if (engine.serverType == ServerType.GRAPHHOPER) { + return baseUrl + "point=" + kievLatLon.getLatitude() + + "," + kievLatLon.getLongitude() + + "&" + "point=" + destinationLatLon.getLatitude() + + "," + destinationLatLon.getLongitude() + + "&" + "vehicle=" + vehicle + + (!Algorithms.isEmpty(engine.apiKey) ? ("&" + "key=" + engine.apiKey) : ""); + } else { + return baseUrl + vehicle + "/" + kievLatLon.getLatitude() + + "," + kievLatLon.getLongitude() + + ";" + destinationLatLon.getLatitude() + + "," + destinationLatLon.getLongitude() + + "?geometries=geojson&overview=full"; + } + } + + public static void showInstance(FragmentActivity activity, boolean isEditingMode) { + OnlineRoutingEngineFragment fragment = new OnlineRoutingEngineFragment(); + fragment.isEditingMode = isEditingMode; + activity.getSupportFragmentManager().beginTransaction() + .add(R.id.fragmentContainer, fragment, TAG) + .addToBackStack(TAG).commitAllowingStateLoss(); + } + + @Override + public void onSaveInstanceState(@NonNull Bundle outState) { + super.onSaveInstanceState(outState); + saveState(outState); + } + + private void saveState(Bundle outState) { + outState.putString(ENGINE_NAME_KEY, engine.name); + outState.putString(ENGINE_SERVER_KEY, engine.serverType.name()); + outState.putString(ENGINE_SERVER_URL_KEY, engine.serverBaseUrl); + outState.putString(ENGINE_VEHICLE_TYPE_KEY, engine.vehicleType.name()); + outState.putString(ENGINE_CUSTOM_VEHICLE_KEY, engine.customVehicleKey); + outState.putString(ENGINE_API_KEY_KEY, engine.apiKey); + outState.putBoolean(ENGINE_NAME_CHANGED_BY_USER_KEY, engine.wasNameChangedByUser); + outState.putString(EXAMPLE_LOCATION_KEY, selectedLocation.name()); + } + + private void restoreState(Bundle savedState) { + engine.name = savedState.getString(ENGINE_NAME_KEY); + engine.serverType = ServerType.valueOf(savedState.getString(ENGINE_SERVER_KEY)); + engine.serverBaseUrl = savedState.getString(ENGINE_SERVER_URL_KEY); + engine.vehicleType = VehicleType.valueOf(savedState.getString(ENGINE_VEHICLE_TYPE_KEY)); + engine.customVehicleKey = savedState.getString(ENGINE_CUSTOM_VEHICLE_KEY); + engine.apiKey = savedState.getString(ENGINE_API_KEY_KEY); + engine.wasNameChangedByUser = savedState.getBoolean(ENGINE_NAME_CHANGED_BY_USER_KEY); + selectedLocation = ExampleLocation.valueOf(savedState.getString(EXAMPLE_LOCATION_KEY)); + } + + + private void initEngineState() { + engine.serverType = ServerType.values()[0]; + engine.vehicleType = VehicleType.values()[0]; + selectedLocation = ExampleLocation.values()[0]; + } + + private void dismiss() { + FragmentActivity activity = getActivity(); + if (activity != null) { + activity.onBackPressed(); + } + } + + @Nullable + private MapActivity getMapActivity() { + FragmentActivity activity = getActivity(); + if (activity instanceof MapActivity) { + return (MapActivity) activity; + } else { + return null; + } + } + + private static class OnlineRoutingEngineObject { + private String name; + private ServerType serverType; + private String serverBaseUrl; + private VehicleType vehicleType; + private String customVehicleKey; + private String apiKey; + private boolean wasNameChangedByUser; + + public String getVehicleKey() { + if (vehicleType == VehicleType.CUSTOM) { + return customVehicleKey; + } + return vehicleType.getKey(); + } + } +} From 677a2c888b848b6b4ffbec88c550ba27940d0ce6 Mon Sep 17 00:00:00 2001 From: nazar-kutz Date: Mon, 28 Dec 2020 10:45:53 +0200 Subject: [PATCH 2/8] rename "save" button --- .../settings/fragments/OnlineRoutingEngineFragment.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/OnlineRoutingEngineFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/OnlineRoutingEngineFragment.java index 8e840f4a1b..44924f7352 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/OnlineRoutingEngineFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/OnlineRoutingEngineFragment.java @@ -234,11 +234,11 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { view.findViewById(R.id.buttons_divider).setVisibility(View.VISIBLE); - View applyButton = view.findViewById(R.id.right_bottom_button); - UiUtilities.setupDialogButton(nightMode, applyButton, + View saveButton = view.findViewById(R.id.right_bottom_button); + UiUtilities.setupDialogButton(nightMode, saveButton, UiUtilities.DialogButtonType.PRIMARY, R.string.shared_string_save); - applyButton.setVisibility(View.VISIBLE); - applyButton.setOnClickListener(new OnClickListener() { + saveButton.setVisibility(View.VISIBLE); + saveButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // save engine to settings From 079da7b219729721ceb421d5805b00f31d623982 Mon Sep 17 00:00:00 2001 From: nazar-kutz Date: Mon, 28 Dec 2020 12:42:31 +0200 Subject: [PATCH 3/8] Online Routing UI - refactoring part 1 --- .../online_routing_preference_segment.xml | 1 + .../osmand/plus/helpers/AndroidUiHelper.java | 8 +++ .../profiles/SelectProfileBottomSheet.java | 4 +- .../OnlineRoutingSegmentCard.java | 17 ++----- .../BasePreferenceBottomSheet.java | 2 +- .../fragments/GlobalSettingsFragment.java | 5 +- .../fragments/MainSettingsFragment.java | 5 +- .../fragments/NavigationFragment.java | 2 +- .../OnlineRoutingEngineFragment.java | 49 +++++++++++++------ .../fragments/ProfileAppearanceFragment.java | 2 +- 10 files changed, 59 insertions(+), 36 deletions(-) diff --git a/OsmAnd/res/layout/online_routing_preference_segment.xml b/OsmAnd/res/layout/online_routing_preference_segment.xml index 0b1799e478..bb52d533af 100644 --- a/OsmAnd/res/layout/online_routing_preference_segment.xml +++ b/OsmAnd/res/layout/online_routing_preference_segment.xml @@ -110,6 +110,7 @@ android:layout_height="wrap_content" android:inputType="textMultiLine" android:maxLines="4" + android:saveEnabled="false" android:scrollHorizontally="false" tools:text="Text" /> diff --git a/OsmAnd/src/net/osmand/plus/helpers/AndroidUiHelper.java b/OsmAnd/src/net/osmand/plus/helpers/AndroidUiHelper.java index 45f5fe3ff4..9a038b787f 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/AndroidUiHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/AndroidUiHelper.java @@ -102,6 +102,14 @@ public class AndroidUiHelper { } } } + + public static void setVisibility(int visibility, View ... views) { + for (View view : views) { + if (view != null && view.getVisibility() != visibility) { + view.setVisibility(visibility); + } + } + } public static boolean isXLargeDevice(@NonNull Activity ctx) { int lt = (ctx.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK); diff --git a/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheet.java b/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheet.java index da92070b65..d86e1945f7 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheet.java @@ -183,7 +183,7 @@ public class SelectProfileBottomSheet extends BasePreferenceBottomSheet { @Override public void onClick(View v) { if (getActivity() != null) { - OnlineRoutingEngineFragment.showInstance(getActivity(), false); + OnlineRoutingEngineFragment.showInstance(getActivity(), getAppMode(), false); } dismiss(); } @@ -367,6 +367,7 @@ public class SelectProfileBottomSheet extends BasePreferenceBottomSheet { public static void showInstance(@NonNull FragmentActivity activity, @NonNull DialogMode dialogMode, @Nullable Fragment target, + ApplicationMode appMode, String selectedItemKey, boolean usedOnMap) { SelectProfileBottomSheet fragment = new SelectProfileBottomSheet(); @@ -375,6 +376,7 @@ public class SelectProfileBottomSheet extends BasePreferenceBottomSheet { args.putString(SELECTED_KEY, selectedItemKey); fragment.setArguments(args); fragment.setUsedOnMap(usedOnMap); + fragment.setAppMode(appMode); fragment.setTargetFragment(target, 0); fragment.show(activity.getSupportFragmentManager(), TAG); } diff --git a/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/OnlineRoutingSegmentCard.java b/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/OnlineRoutingSegmentCard.java index 3ea6c4386f..75d90202b7 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/OnlineRoutingSegmentCard.java +++ b/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/OnlineRoutingSegmentCard.java @@ -16,6 +16,7 @@ import net.osmand.plus.R; import net.osmand.plus.UiUtilities; import net.osmand.plus.UiUtilities.DialogButtonType; import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.mapcontextmenu.other.HorizontalSelectionAdapter; import net.osmand.plus.mapcontextmenu.other.HorizontalSelectionAdapter.HorizontalSelectionAdapterListener; import net.osmand.plus.mapcontextmenu.other.HorizontalSelectionAdapter.HorizontalSelectionItem; @@ -41,9 +42,9 @@ public class OnlineRoutingSegmentCard extends BaseCard { private View resultContainer; private OnTextChangedListener onTextChangedListener; - public OnlineRoutingSegmentCard(@NonNull MapActivity mapActivity) { + public OnlineRoutingSegmentCard(@NonNull MapActivity mapActivity, boolean nightMode) { super(mapActivity); - build(mapActivity); + this.nightMode = nightMode; } @Override @@ -157,19 +158,11 @@ public class OnlineRoutingSegmentCard extends BaseCard { } private void showElements(View... views) { - changeVisibility(View.VISIBLE, views); + AndroidUiHelper.setVisibility(View.VISIBLE, views); } private void hideElements(View... views) { - changeVisibility(View.GONE, views); - } - - private void changeVisibility(int visibility, View... views) { - for (View v : views) { - if (visibility != v.getVisibility()) { - v.setVisibility(visibility); - } - } + AndroidUiHelper.setVisibility(View.GONE, views); } public void setOnTextChangedListener(OnTextChangedListener onTextChangedListener) { diff --git a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/BasePreferenceBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/BasePreferenceBottomSheet.java index 6a71538ff1..67e2bfcfa0 100644 --- a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/BasePreferenceBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/BasePreferenceBottomSheet.java @@ -38,12 +38,12 @@ public abstract class BasePreferenceBottomSheet extends MenuBottomSheetDialogFra @Override public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); if (savedInstanceState != null) { appMode = ApplicationMode.valueOfStringKey(savedInstanceState.getString(APP_MODE_KEY), null); applyQueryType = ApplyQueryType.valueOf(savedInstanceState.getString(APPLY_QUERY_TYPE)); profileDependent = savedInstanceState.getBoolean(PROFILE_DEPENDENT, false); } + super.onCreate(savedInstanceState); } @Override diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/GlobalSettingsFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/GlobalSettingsFragment.java index e957eb7748..fa7383228c 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/GlobalSettingsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/GlobalSettingsFragment.java @@ -125,8 +125,9 @@ public class GlobalSettingsFragment extends BaseSettingsFragment if (prefId.equals(settings.DEFAULT_APPLICATION_MODE.getId())) { if (getActivity() != null) { String defaultModeKey = settings.DEFAULT_APPLICATION_MODE.get().getStringKey(); - SelectProfileBottomSheet.showInstance(getActivity(), - DialogMode.DEFAULT_PROFILE, this, defaultModeKey, false); + SelectProfileBottomSheet.showInstance( + getActivity(), DialogMode.DEFAULT_PROFILE, this, + getSelectedAppMode(), defaultModeKey, false); } } else if (settings.SPEED_CAMERAS_UNINSTALLED.getId().equals(prefId) && !settings.SPEED_CAMERAS_UNINSTALLED.get()) { FragmentManager fm = getFragmentManager(); diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/MainSettingsFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/MainSettingsFragment.java index f7ae122851..fdd5508352 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/MainSettingsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/MainSettingsFragment.java @@ -119,8 +119,9 @@ public class MainSettingsFragment extends BaseSettingsFragment implements OnSele return true; } else if (CREATE_PROFILE.equals(prefId)) { if (getActivity() != null) { - SelectProfileBottomSheet.showInstance(getActivity(), - DialogMode.BASE_PROFILE, this, null, false); + SelectProfileBottomSheet.showInstance( + getActivity(), DialogMode.BASE_PROFILE, this, + getSelectedAppMode(), null, false); } } else if (IMPORT_PROFILE.equals(prefId)) { final MapActivity mapActivity = getMapActivity(); diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/NavigationFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/NavigationFragment.java index 0720dc7714..de8481f222 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/NavigationFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/NavigationFragment.java @@ -126,7 +126,7 @@ public class NavigationFragment extends BaseSettingsFragment implements OnSelect if (getActivity() != null) { SelectProfileBottomSheet.showInstance( getActivity(), SelectProfileBottomSheet.DialogMode.NAVIGATION_PROFILE, - this, routingProfileKey, false); + this, getSelectedAppMode(), routingProfileKey, false); } } return false; diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/OnlineRoutingEngineFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/OnlineRoutingEngineFragment.java index 44924f7352..07460805a2 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/OnlineRoutingEngineFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/OnlineRoutingEngineFragment.java @@ -31,6 +31,7 @@ import net.osmand.plus.profiles.onlinerouting.ServerType; import net.osmand.plus.profiles.onlinerouting.ExampleLocation; import net.osmand.plus.profiles.onlinerouting.VehicleType; import net.osmand.plus.routepreparationmenu.cards.BaseCard; +import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.util.Algorithms; import java.util.ArrayList; @@ -48,6 +49,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { private static final String ENGINE_API_KEY_KEY = "engine_api_key"; private static final String ENGINE_NAME_CHANGED_BY_USER_KEY = "engine_name_changed_by_user_key"; private static final String EXAMPLE_LOCATION_KEY = "example_location"; + private static final String APP_MODE_KEY = "app_mode"; private OnlineRoutingSegmentCard nameCard; private OnlineRoutingSegmentCard serverCard; @@ -56,7 +58,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { private OnlineRoutingSegmentCard exampleCard; private boolean isEditingMode; - private boolean nightMode; + private ApplicationMode appMode; private OnlineRoutingEngineObject engine; private ExampleLocation selectedLocation; @@ -82,7 +84,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { if (mapActivity == null) { return null; } - nightMode = !app.getSettings().isLightContent(); + boolean nightMode = isNightMode(app); View view = UiUtilities.getInflater(getContext(), nightMode) .inflate(R.layout.online_routing_engine_preference, container, false); @@ -95,7 +97,8 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { ViewGroup segmentsContainer = (ViewGroup) view.findViewById(R.id.segments_container); // create name card - nameCard = new OnlineRoutingSegmentCard(mapActivity); + nameCard = new OnlineRoutingSegmentCard(mapActivity, nightMode); + nameCard.build(mapActivity); nameCard.setDescription(getString(R.string.select_nav_profile_dialog_message)); nameCard.setFieldBoxLabelText(getString(R.string.shared_string_name)); nameCard.setOnTextChangedListener(new OnTextChangedListener() { @@ -111,7 +114,8 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { segmentsContainer.addView(nameCard.getView()); // create server card - serverCard = new OnlineRoutingSegmentCard(mapActivity); + serverCard = new OnlineRoutingSegmentCard(mapActivity, nightMode); + serverCard.build(mapActivity); serverCard.setHeaderTitle(getString(R.string.shared_string_type)); List serverItems = new ArrayList<>(); for (ServerType server : ServerType.values()) { @@ -141,7 +145,8 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { segmentsContainer.addView(serverCard.getView()); // create vehicle card - vehicleCard = new OnlineRoutingSegmentCard(mapActivity); + vehicleCard = new OnlineRoutingSegmentCard(mapActivity, nightMode); + vehicleCard.build(mapActivity); vehicleCard.setHeaderTitle(getString(R.string.shared_string_vehicle)); List vehicleItems = new ArrayList<>(); for (VehicleType vehicle : VehicleType.values()) { @@ -173,7 +178,8 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { segmentsContainer.addView(vehicleCard.getView()); // create api key card - apiKeyCard = new OnlineRoutingSegmentCard(mapActivity); + apiKeyCard = new OnlineRoutingSegmentCard(mapActivity, nightMode); + apiKeyCard.build(mapActivity); apiKeyCard.setHeaderTitle(getString(R.string.shared_string_api_key)); apiKeyCard.setFieldBoxLabelText(getString(R.string.keep_it_empty_if_not)); apiKeyCard.setEditedText(engine.apiKey); @@ -188,7 +194,8 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { segmentsContainer.addView(apiKeyCard.getView()); // create example card - exampleCard = new OnlineRoutingSegmentCard(mapActivity); + exampleCard = new OnlineRoutingSegmentCard(mapActivity, nightMode); + exampleCard.build(mapActivity); exampleCard.setHeaderTitle(getString(R.string.shared_string_example)); List locationItems = new ArrayList<>(); for (ExampleLocation location : ExampleLocation.values()) { @@ -330,14 +337,6 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { } } - public static void showInstance(FragmentActivity activity, boolean isEditingMode) { - OnlineRoutingEngineFragment fragment = new OnlineRoutingEngineFragment(); - fragment.isEditingMode = isEditingMode; - activity.getSupportFragmentManager().beginTransaction() - .add(R.id.fragmentContainer, fragment, TAG) - .addToBackStack(TAG).commitAllowingStateLoss(); - } - @Override public void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); @@ -353,6 +352,9 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { outState.putString(ENGINE_API_KEY_KEY, engine.apiKey); outState.putBoolean(ENGINE_NAME_CHANGED_BY_USER_KEY, engine.wasNameChangedByUser); outState.putString(EXAMPLE_LOCATION_KEY, selectedLocation.name()); + if (appMode != null) { + outState.putString(APP_MODE_KEY, appMode.getStringKey()); + } } private void restoreState(Bundle savedState) { @@ -364,9 +366,9 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { engine.apiKey = savedState.getString(ENGINE_API_KEY_KEY); engine.wasNameChangedByUser = savedState.getBoolean(ENGINE_NAME_CHANGED_BY_USER_KEY); selectedLocation = ExampleLocation.valueOf(savedState.getString(EXAMPLE_LOCATION_KEY)); + appMode = ApplicationMode.valueOfStringKey(savedState.getString(APP_MODE_KEY), null); } - private void initEngineState() { engine.serverType = ServerType.values()[0]; engine.vehicleType = VehicleType.values()[0]; @@ -380,6 +382,10 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { } } + protected boolean isNightMode(@NonNull OsmandApplication app) { + return !app.getSettings().isLightContentForMode(appMode); + } + @Nullable private MapActivity getMapActivity() { FragmentActivity activity = getActivity(); @@ -390,6 +396,17 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { } } + public static void showInstance(@NonNull FragmentActivity activity, + @NonNull ApplicationMode appMode, + boolean isEditingMode) { + OnlineRoutingEngineFragment fragment = new OnlineRoutingEngineFragment(); + fragment.isEditingMode = isEditingMode; + fragment.appMode = appMode; + activity.getSupportFragmentManager().beginTransaction() + .add(R.id.fragmentContainer, fragment, TAG) + .addToBackStack(TAG).commitAllowingStateLoss(); + } + private static class OnlineRoutingEngineObject { private String name; private ServerType serverType; diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/ProfileAppearanceFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/ProfileAppearanceFragment.java index 6913770773..7a42818745 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/ProfileAppearanceFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/ProfileAppearanceFragment.java @@ -403,7 +403,7 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment implements O if (getActivity() != null) { SelectProfileBottomSheet.showInstance( getActivity(), DialogMode.BASE_PROFILE, ProfileAppearanceFragment.this, - selectedAppModeKey, false); + getSelectedAppMode(), selectedAppModeKey, false); } } } From ce32f41f3eb19eb5af8ab5299f4f2ef5cbfcaa0d Mon Sep 17 00:00:00 2001 From: nazar-kutz Date: Mon, 28 Dec 2020 17:56:30 +0200 Subject: [PATCH 4/8] Online Routing UI - refactoring part 2 --- ...xml => online_routing_engine_fragment.xml} | 0 .../online_routing_preference_segment.xml | 2 +- OsmAnd/res/values/strings.xml | 4 + .../OnlineRoutingCard.java} | 14 +- .../OnlineRoutingEngineFragment.java | 147 ++++++++++++------ .../onlinerouting/ServerType.java | 4 +- .../plus/onlinerouting/VehicleType.java | 29 ++++ .../profiles/SelectProfileBottomSheet.java | 2 +- .../onlinerouting/ExampleLocation.java | 26 ---- .../profiles/onlinerouting/VehicleType.java | 25 --- 10 files changed, 146 insertions(+), 107 deletions(-) rename OsmAnd/res/layout/{online_routing_engine_preference.xml => online_routing_engine_fragment.xml} (100%) rename OsmAnd/src/net/osmand/plus/{profiles/onlinerouting/OnlineRoutingSegmentCard.java => onlinerouting/OnlineRoutingCard.java} (95%) rename OsmAnd/src/net/osmand/plus/{settings/fragments => onlinerouting}/OnlineRoutingEngineFragment.java (79%) rename OsmAnd/src/net/osmand/plus/{profiles => }/onlinerouting/ServerType.java (73%) create mode 100644 OsmAnd/src/net/osmand/plus/onlinerouting/VehicleType.java delete mode 100644 OsmAnd/src/net/osmand/plus/profiles/onlinerouting/ExampleLocation.java delete mode 100644 OsmAnd/src/net/osmand/plus/profiles/onlinerouting/VehicleType.java diff --git a/OsmAnd/res/layout/online_routing_engine_preference.xml b/OsmAnd/res/layout/online_routing_engine_fragment.xml similarity index 100% rename from OsmAnd/res/layout/online_routing_engine_preference.xml rename to OsmAnd/res/layout/online_routing_engine_fragment.xml diff --git a/OsmAnd/res/layout/online_routing_preference_segment.xml b/OsmAnd/res/layout/online_routing_preference_segment.xml index bb52d533af..5f095e126c 100644 --- a/OsmAnd/res/layout/online_routing_preference_segment.xml +++ b/OsmAnd/res/layout/online_routing_preference_segment.xml @@ -108,7 +108,7 @@ android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" - android:inputType="textMultiLine" + android:inputType="textMultiLine|textNoSuggestions" android:maxLines="4" android:saveEnabled="false" android:scrollHorizontally="false" diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 6a66dd03ea..cd6690f74a 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -11,6 +11,10 @@ Thx - Hardy --> + Car + Bike + Foot + Driving Test route calculation URL with all parameters will look like this: Keep it empty if not diff --git a/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/OnlineRoutingSegmentCard.java b/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingCard.java similarity index 95% rename from OsmAnd/src/net/osmand/plus/profiles/onlinerouting/OnlineRoutingSegmentCard.java rename to OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingCard.java index 75d90202b7..48069e3f41 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/OnlineRoutingSegmentCard.java +++ b/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingCard.java @@ -1,4 +1,4 @@ -package net.osmand.plus.profiles.onlinerouting; +package net.osmand.plus.onlinerouting; import android.text.Editable; import android.text.TextWatcher; @@ -25,7 +25,7 @@ import net.osmand.plus.widgets.OsmandTextFieldBoxes; import java.util.List; -public class OnlineRoutingSegmentCard extends BaseCard { +public class OnlineRoutingCard extends BaseCard { private View headerContainer; private TextView tvHeaderTitle; @@ -42,7 +42,7 @@ public class OnlineRoutingSegmentCard extends BaseCard { private View resultContainer; private OnTextChangedListener onTextChangedListener; - public OnlineRoutingSegmentCard(@NonNull MapActivity mapActivity, boolean nightMode) { + public OnlineRoutingCard(@NonNull MapActivity mapActivity, boolean nightMode) { super(mapActivity); this.nightMode = nightMode; } @@ -149,6 +149,14 @@ public class OnlineRoutingSegmentCard extends BaseCard { DialogButtonType.PRIMARY, R.string.test_route_calculation); } + public void show() { + showElements(view); + } + + public void hide() { + hideElements(view); + } + public void showFieldBox() { showElements(fieldBoxContainer); } diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/OnlineRoutingEngineFragment.java b/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingEngineFragment.java similarity index 79% rename from OsmAnd/src/net/osmand/plus/settings/fragments/OnlineRoutingEngineFragment.java rename to OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingEngineFragment.java index 07460805a2..71bfe6b09f 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/OnlineRoutingEngineFragment.java +++ b/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingEngineFragment.java @@ -1,4 +1,4 @@ -package net.osmand.plus.settings.fragments; +package net.osmand.plus.onlinerouting; import android.os.Build; import android.os.Bundle; @@ -14,6 +14,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.widget.Toolbar; import androidx.fragment.app.FragmentActivity; +import androidx.fragment.app.FragmentManager; import net.osmand.AndroidUtils; import net.osmand.CallbackWithObject; @@ -25,11 +26,7 @@ import net.osmand.plus.UiUtilities.DialogButtonType; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.base.BaseOsmAndFragment; import net.osmand.plus.mapcontextmenu.other.HorizontalSelectionAdapter.HorizontalSelectionItem; -import net.osmand.plus.profiles.onlinerouting.OnlineRoutingSegmentCard; -import net.osmand.plus.profiles.onlinerouting.OnlineRoutingSegmentCard.OnTextChangedListener; -import net.osmand.plus.profiles.onlinerouting.ServerType; -import net.osmand.plus.profiles.onlinerouting.ExampleLocation; -import net.osmand.plus.profiles.onlinerouting.VehicleType; +import net.osmand.plus.onlinerouting.OnlineRoutingCard.OnTextChangedListener; import net.osmand.plus.routepreparationmenu.cards.BaseCard; import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.util.Algorithms; @@ -51,11 +48,13 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { private static final String EXAMPLE_LOCATION_KEY = "example_location"; private static final String APP_MODE_KEY = "app_mode"; - private OnlineRoutingSegmentCard nameCard; - private OnlineRoutingSegmentCard serverCard; - private OnlineRoutingSegmentCard vehicleCard; - private OnlineRoutingSegmentCard apiKeyCard; - private OnlineRoutingSegmentCard exampleCard; + private OsmandApplication app; + + private OnlineRoutingCard nameCard; + private OnlineRoutingCard serverCard; + private OnlineRoutingCard vehicleCard; + private OnlineRoutingCard apiKeyCard; + private OnlineRoutingCard exampleCard; private boolean isEditingMode; private ApplicationMode appMode; @@ -63,9 +62,51 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { private OnlineRoutingEngineObject engine; private ExampleLocation selectedLocation; + private enum ExampleLocation { + + AMSTERDAM("Amsterdam", + new LatLon(52.379189, 4.899431), + new LatLon( 52.308056, 4.764167)), + + BERLIN("Berlin", + new LatLon(52.520008, 13.404954), + new LatLon(52.3666652, 13.501997992)), + + NEW_YORK("New York", + new LatLon(43.000000, -75.000000), + new LatLon( 40.641766, -73.780968)), + + PARIS("Paris", + new LatLon(48.864716, 2.349014), + new LatLon(48.948437, 2.434931)); + + ExampleLocation(String name, LatLon cityCenterLatLon, LatLon cityAirportLatLon) { + this.name = name; + this.cityCenterLatLon = cityCenterLatLon; + this.cityAirportLatLon = cityAirportLatLon; + } + + private String name; + private LatLon cityCenterLatLon; + private LatLon cityAirportLatLon; + + public String getName() { + return name; + } + + public LatLon getCityCenterLatLon() { + return cityCenterLatLon; + } + + public LatLon getCityAirportLatLon() { + return cityAirportLatLon; + } + } + @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); + app = requireMyApplication(); engine = new OnlineRoutingEngineObject(); if (savedInstanceState != null) { restoreState(savedInstanceState); @@ -79,15 +120,14 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - OsmandApplication app = requireMyApplication(); MapActivity mapActivity = getMapActivity(); if (mapActivity == null) { return null; } - boolean nightMode = isNightMode(app); + boolean nightMode = isNightMode(); View view = UiUtilities.getInflater(getContext(), nightMode) - .inflate(R.layout.online_routing_engine_preference, container, false); + .inflate(R.layout.online_routing_engine_fragment, container, false); if (Build.VERSION.SDK_INT >= 21) { AndroidUtils.addStatusBarPadding21v(getContext(), view); @@ -97,9 +137,10 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { ViewGroup segmentsContainer = (ViewGroup) view.findViewById(R.id.segments_container); // create name card - nameCard = new OnlineRoutingSegmentCard(mapActivity, nightMode); + nameCard = new OnlineRoutingCard(mapActivity, nightMode); nameCard.build(mapActivity); nameCard.setDescription(getString(R.string.select_nav_profile_dialog_message)); + nameCard.setEditedText(engine.name); nameCard.setFieldBoxLabelText(getString(R.string.shared_string_name)); nameCard.setOnTextChangedListener(new OnTextChangedListener() { @Override @@ -114,7 +155,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { segmentsContainer.addView(nameCard.getView()); // create server card - serverCard = new OnlineRoutingSegmentCard(mapActivity, nightMode); + serverCard = new OnlineRoutingCard(mapActivity, nightMode); serverCard.build(mapActivity); serverCard.setHeaderTitle(getString(R.string.shared_string_type)); List serverItems = new ArrayList<>(); @@ -134,25 +175,25 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { return false; } }); -// serverCard.setOnTextChangedListener(new OnTextChangedListener() { -// @Override -// public void onTextChanged(boolean editedByUser, String text) { -// engine.serverBaseUrl = text; -// } -// }); + serverCard.setOnTextChangedListener(new OnTextChangedListener() { + @Override + public void onTextChanged(boolean editedByUser, String text) { + engine.serverBaseUrl = text; + } + }); serverCard.setFieldBoxLabelText(getString(R.string.shared_string_server_url)); serverCard.showDivider(); segmentsContainer.addView(serverCard.getView()); // create vehicle card - vehicleCard = new OnlineRoutingSegmentCard(mapActivity, nightMode); + vehicleCard = new OnlineRoutingCard(mapActivity, nightMode); vehicleCard.build(mapActivity); vehicleCard.setHeaderTitle(getString(R.string.shared_string_vehicle)); List vehicleItems = new ArrayList<>(); for (VehicleType vehicle : VehicleType.values()) { - vehicleItems.add(new HorizontalSelectionItem(vehicle.getTitle(), vehicle)); + vehicleItems.add(new HorizontalSelectionItem(vehicle.toHumanString(app), vehicle)); } - vehicleCard.setSelectionMenu(vehicleItems, engine.vehicleType.getTitle(), + vehicleCard.setSelectionMenu(vehicleItems, engine.vehicleType.toHumanString(app), new CallbackWithObject() { @Override public boolean processResult(HorizontalSelectionItem result) { @@ -178,7 +219,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { segmentsContainer.addView(vehicleCard.getView()); // create api key card - apiKeyCard = new OnlineRoutingSegmentCard(mapActivity, nightMode); + apiKeyCard = new OnlineRoutingCard(mapActivity, nightMode); apiKeyCard.build(mapActivity); apiKeyCard.setHeaderTitle(getString(R.string.shared_string_api_key)); apiKeyCard.setFieldBoxLabelText(getString(R.string.keep_it_empty_if_not)); @@ -194,14 +235,14 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { segmentsContainer.addView(apiKeyCard.getView()); // create example card - exampleCard = new OnlineRoutingSegmentCard(mapActivity, nightMode); + exampleCard = new OnlineRoutingCard(mapActivity, nightMode); exampleCard.build(mapActivity); exampleCard.setHeaderTitle(getString(R.string.shared_string_example)); List locationItems = new ArrayList<>(); for (ExampleLocation location : ExampleLocation.values()) { - locationItems.add(new HorizontalSelectionItem(location.getTitle(), location)); + locationItems.add(new HorizontalSelectionItem(location.getName(), location)); } - exampleCard.setSelectionMenu(locationItems, selectedLocation.getTitle(), + exampleCard.setSelectionMenu(locationItems, selectedLocation.getName(), new CallbackWithObject() { @Override public boolean processResult(HorizontalSelectionItem result) { @@ -292,16 +333,21 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { if (!engine.wasNameChangedByUser) { String name = String.format( getString(R.string.ltr_or_rtl_combine_via_dash), - engine.serverType.getTitle(), engine.vehicleType.getTitle()); + engine.serverType.getTitle(), engine.vehicleType.toHumanString(app)); nameCard.setEditedText(name); } } else if (serverCard.equals(card)) { serverCard.setHeaderSubtitle(engine.serverType.getTitle()); serverCard.setEditedText(engine.serverType.getBaseUrl()); + if (engine.serverType == ServerType.GRAPHHOPER) { + apiKeyCard.show(); + } else { + apiKeyCard.hide(); + } } else if (vehicleCard.equals(card)) { - vehicleCard.setHeaderSubtitle(engine.vehicleType.getTitle()); + vehicleCard.setHeaderSubtitle(engine.vehicleType.toHumanString(app)); if (engine.vehicleType == VehicleType.CUSTOM) { vehicleCard.showFieldBox(); } else { @@ -318,22 +364,22 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { String baseUrl = engine.serverType.getBaseUrl(); String vehicle = engine.getVehicleKey(); - LatLon kievLatLon = new LatLon(50.431759, 30.517023); - LatLon destinationLatLon = selectedLocation.getLatLon(); + LatLon startPoint = selectedLocation.getCityCenterLatLon(); + LatLon endPoint = selectedLocation.getCityAirportLatLon(); if (engine.serverType == ServerType.GRAPHHOPER) { - return baseUrl + "point=" + kievLatLon.getLatitude() - + "," + kievLatLon.getLongitude() - + "&" + "point=" + destinationLatLon.getLatitude() - + "," + destinationLatLon.getLongitude() + 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 + "/" + kievLatLon.getLatitude() - + "," + kievLatLon.getLongitude() - + ";" + destinationLatLon.getLatitude() - + "," + destinationLatLon.getLongitude() - + "?geometries=geojson&overview=full"; + return baseUrl + vehicle + "/" + startPoint.getLatitude() + + "," + startPoint.getLongitude() + + ";" + endPoint.getLatitude() + + "," + endPoint.getLongitude() + + "?" + "geometries=geojson"; } } @@ -382,7 +428,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { } } - protected boolean isNightMode(@NonNull OsmandApplication app) { + private boolean isNightMode() { return !app.getSettings().isLightContentForMode(appMode); } @@ -399,12 +445,15 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { public static void showInstance(@NonNull FragmentActivity activity, @NonNull ApplicationMode appMode, boolean isEditingMode) { - OnlineRoutingEngineFragment fragment = new OnlineRoutingEngineFragment(); - fragment.isEditingMode = isEditingMode; - fragment.appMode = appMode; - activity.getSupportFragmentManager().beginTransaction() - .add(R.id.fragmentContainer, fragment, TAG) - .addToBackStack(TAG).commitAllowingStateLoss(); + FragmentManager fm = activity.getSupportFragmentManager(); + if (!fm.isStateSaved() && fm.findFragmentByTag(OnlineRoutingEngineFragment.TAG) == null) { + OnlineRoutingEngineFragment fragment = new OnlineRoutingEngineFragment(); + fragment.isEditingMode = isEditingMode; + fragment.appMode = appMode; + fm.beginTransaction() + .add(R.id.fragmentContainer, fragment, TAG) + .addToBackStack(TAG).commitAllowingStateLoss(); + } } private static class OnlineRoutingEngineObject { diff --git a/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/ServerType.java b/OsmAnd/src/net/osmand/plus/onlinerouting/ServerType.java similarity index 73% rename from OsmAnd/src/net/osmand/plus/profiles/onlinerouting/ServerType.java rename to OsmAnd/src/net/osmand/plus/onlinerouting/ServerType.java index d624aa269e..7eb30798d5 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/ServerType.java +++ b/OsmAnd/src/net/osmand/plus/onlinerouting/ServerType.java @@ -1,7 +1,7 @@ -package net.osmand.plus.profiles.onlinerouting; +package net.osmand.plus.onlinerouting; public enum ServerType { - GRAPHHOPER("Graphhoper", "https://graphhopper.com/api/1/route?"), + GRAPHHOPER("Graphhoper", "https://graphhopper.com/api/1/route"), OSRM("OSRM", "https://zlzk.biz/route/v1/"); ServerType(String title, String baseUrl) { diff --git a/OsmAnd/src/net/osmand/plus/onlinerouting/VehicleType.java b/OsmAnd/src/net/osmand/plus/onlinerouting/VehicleType.java new file mode 100644 index 0000000000..7a4d44b714 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/onlinerouting/VehicleType.java @@ -0,0 +1,29 @@ +package net.osmand.plus.onlinerouting; + +import android.content.Context; + +import net.osmand.plus.R; + +public enum VehicleType { + CAR("car", R.string.routing_engine_vehicle_type_car), + BIKE("bike", R.string.routing_engine_vehicle_type_bike), + FOOT("foot", R.string.routing_engine_vehicle_type_foot), + DRIVING("driving", R.string.routing_engine_vehicle_type_driving), + CUSTOM("-", R.string.shared_string_custom); + + VehicleType(String key, int titleId) { + this.key = key; + this.titleId = titleId; + } + + private String key; + private int titleId; + + public String getKey() { + return key; + } + + public String toHumanString(Context ctx) { + return ctx.getString(titleId); + } +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheet.java b/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheet.java index d86e1945f7..af1289e85b 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheet.java @@ -38,7 +38,7 @@ import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem; import net.osmand.plus.helpers.FontCache; import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.settings.bottomsheets.BasePreferenceBottomSheet; -import net.osmand.plus.settings.fragments.OnlineRoutingEngineFragment; +import net.osmand.plus.onlinerouting.OnlineRoutingEngineFragment; import net.osmand.router.RoutingConfiguration; import org.apache.commons.logging.Log; diff --git a/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/ExampleLocation.java b/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/ExampleLocation.java deleted file mode 100644 index ec1dd2149b..0000000000 --- a/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/ExampleLocation.java +++ /dev/null @@ -1,26 +0,0 @@ -package net.osmand.plus.profiles.onlinerouting; - -import net.osmand.data.LatLon; - -public enum ExampleLocation { - AMSTERDAM("Amsterdam", new LatLon(52.379189, 4.899431)), - BERLIN("Berlin", new LatLon(52.520008, 13.404954)), - NEW_YORK("New York", new LatLon(43.000000, -75.000000)), - PARIS("Paris", new LatLon(48.864716, 2.349014)); - - ExampleLocation(String title, LatLon latLon) { - this.title = title; - this.latLon = latLon; - } - - private String title; - private LatLon latLon; - - public String getTitle() { - return title; - } - - public LatLon getLatLon() { - return latLon; - } -} diff --git a/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/VehicleType.java b/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/VehicleType.java deleted file mode 100644 index 78a0ac1e64..0000000000 --- a/OsmAnd/src/net/osmand/plus/profiles/onlinerouting/VehicleType.java +++ /dev/null @@ -1,25 +0,0 @@ -package net.osmand.plus.profiles.onlinerouting; - -public enum VehicleType { - CAR("car", "Car"), - BIKE("bike", "Bike"), - FOOT("foot", "Foot"), - DRIVING("driving", "Driving"), - CUSTOM("custom", "Custom"); - - VehicleType(String key, String title) { - this.key = key; - this.title = title; - } - - private String key; - private String title; - - public String getKey() { - return key; - } - - public String getTitle() { - return title; - } -} \ No newline at end of file From 1649c066ff986011857e91c0c35910151ac1ec05 Mon Sep 17 00:00:00 2001 From: nazar-kutz Date: Tue, 29 Dec 2020 00:58:26 +0200 Subject: [PATCH 5/8] Fix HorizontalSelectionAdapter selected item has wrong background width --- .../mapcontextmenu/other/HorizontalSelectionAdapter.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/HorizontalSelectionAdapter.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/HorizontalSelectionAdapter.java index 2299a62c7a..b26a377266 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/HorizontalSelectionAdapter.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/HorizontalSelectionAdapter.java @@ -67,8 +67,9 @@ public class HorizontalSelectionAdapter extends RecyclerView.Adapter Date: Wed, 30 Dec 2020 10:09:17 +0200 Subject: [PATCH 6/8] Online Routing UI - Implement Test button --- .../bottom_sheet_item_with_descr_64dp.xml | 56 +++++++++++++++++ OsmAnd/res/values/strings.xml | 1 + .../plus/onlinerouting/OnlineRoutingCard.java | 11 ++-- .../OnlineRoutingEngineFragment.java | 63 +++++++++++++++++-- 4 files changed, 120 insertions(+), 11 deletions(-) create mode 100644 OsmAnd/res/layout/bottom_sheet_item_with_descr_64dp.xml diff --git a/OsmAnd/res/layout/bottom_sheet_item_with_descr_64dp.xml b/OsmAnd/res/layout/bottom_sheet_item_with_descr_64dp.xml new file mode 100644 index 0000000000..2ff6a1bf58 --- /dev/null +++ b/OsmAnd/res/layout/bottom_sheet_item_with_descr_64dp.xml @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index cd6690f74a..f10d9be2f0 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -11,6 +11,7 @@ Thx - Hardy --> + Error, recheck parameters Car Bike Foot diff --git a/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingCard.java b/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingCard.java index 48069e3f41..bd4ca4c735 100644 --- a/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingCard.java +++ b/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingCard.java @@ -39,7 +39,6 @@ public class OnlineRoutingCard extends BaseCard { private TextView tvHelperText; private View bottomDivider; private View button; - private View resultContainer; private OnTextChangedListener onTextChangedListener; public OnlineRoutingCard(@NonNull MapActivity mapActivity, boolean nightMode) { @@ -65,7 +64,6 @@ public class OnlineRoutingCard extends BaseCard { tvHelperText = view.findViewById(R.id.helper_text); bottomDivider = view.findViewById(R.id.bottom_divider); button = view.findViewById(R.id.button); - resultContainer = view.findViewById(R.id.result_container); editText.addTextChangedListener(new TextWatcher() { @Override @@ -138,15 +136,18 @@ public class OnlineRoutingCard extends BaseCard { editText.setTag(null); } + public String getEditedText() { + return editText.getText().toString(); + } + public void showDivider() { showElements(bottomDivider); } - public void setButton(OnClickListener listener) { + public void setButton(String title, OnClickListener listener) { showElements(button); button.setOnClickListener(listener); - UiUtilities.setupDialogButton(nightMode, button, - DialogButtonType.PRIMARY, R.string.test_route_calculation); + UiUtilities.setupDialogButton(nightMode, button, DialogButtonType.PRIMARY, title); } public void show() { diff --git a/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingEngineFragment.java b/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingEngineFragment.java index 71bfe6b09f..3d5b90444c 100644 --- a/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingEngineFragment.java +++ b/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingEngineFragment.java @@ -16,6 +16,8 @@ import androidx.appcompat.widget.Toolbar; import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; +import net.osmand.AndroidNetworkUtils; +import net.osmand.AndroidNetworkUtils.OnRequestResultListener; import net.osmand.AndroidUtils; import net.osmand.CallbackWithObject; import net.osmand.data.LatLon; @@ -31,6 +33,9 @@ import net.osmand.plus.routepreparationmenu.cards.BaseCard; import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.util.Algorithms; +import org.json.JSONException; +import org.json.JSONObject; + import java.util.ArrayList; import java.util.List; @@ -55,6 +60,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { private OnlineRoutingCard vehicleCard; private OnlineRoutingCard apiKeyCard; private OnlineRoutingCard exampleCard; + private View testResultsContainer; private boolean isEditingMode; private ApplicationMode appMode; @@ -125,10 +131,9 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { return null; } boolean nightMode = isNightMode(); - - View view = UiUtilities.getInflater(getContext(), nightMode) - .inflate(R.layout.online_routing_engine_fragment, container, false); - + inflater = UiUtilities.getInflater(getContext(), nightMode); + View view = inflater.inflate( + R.layout.online_routing_engine_fragment, container, false); if (Build.VERSION.SDK_INT >= 21) { AndroidUtils.addStatusBarPadding21v(getContext(), view); } @@ -256,14 +261,19 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { } }); exampleCard.setFieldBoxHelperText(getString(R.string.online_routing_example_hint)); - exampleCard.setButton(new View.OnClickListener() { + exampleCard.setButton(getString(R.string.test_route_calculation), new View.OnClickListener() { @Override public void onClick(View v) { - // make request to the server + testEngineWork(); } }); segmentsContainer.addView(exampleCard.getView()); + testResultsContainer = inflater.inflate( + R.layout.bottom_sheet_item_with_descr_64dp, segmentsContainer, false); + testResultsContainer.setVisibility(View.GONE); + segmentsContainer.addView(testResultsContainer); + View bottomSpaceView = new View(app); int space = (int) getResources().getDimension(R.dimen.empty_state_text_button_padding_top); bottomSpaceView.setLayoutParams( @@ -383,6 +393,47 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { } } + private void testEngineWork() { + final ServerType server = engine.serverType; + final ExampleLocation location = selectedLocation; + AndroidNetworkUtils.sendRequestAsync(app, exampleCard.getEditedText(), null, + null, false, false, new OnRequestResultListener() { + @Override + public void onResult(String response) { + boolean resultOk = false; + if (response != null) { + try { + JSONObject obj = new JSONObject(response); + + if (server == ServerType.GRAPHHOPER) { + resultOk = obj.has("paths"); + } else if (server == ServerType.OSRM) { + resultOk = obj.has("routes"); + } + } catch (JSONException e) { + + } + } + showTestResults(resultOk, location); + } + }); + } + + private void showTestResults(boolean resultOk, ExampleLocation location) { + testResultsContainer.setVisibility(View.VISIBLE); + ImageView ivImage = testResultsContainer.findViewById(R.id.icon); + TextView tvTitle = testResultsContainer.findViewById(R.id.title); + TextView tvDescription = testResultsContainer.findViewById(R.id.description); + if (resultOk) { + ivImage.setImageDrawable(getContentIcon(R.drawable.ic_action_gdirections_dark)); + tvTitle.setText(getString(R.string.shared_string_ok)); + } else { + ivImage.setImageDrawable(getContentIcon(R.drawable.ic_action_alert)); + tvTitle.setText(getString(R.string.message_error_recheck_parameters)); + } + tvDescription.setText(location.getName()); + } + @Override public void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); From 802221dbc11881aceb09f18b4c828036182b7637 Mon Sep 17 00:00:00 2001 From: nazar-kutz Date: Wed, 30 Dec 2020 22:56:50 +0200 Subject: [PATCH 7/8] Online Routing UI - Add / Delete / Save in settings --- ..._item_with_descr_and_radio_and_eng_btn.xml | 111 +++++++++++++ ...om_sheet_item_with_descr_and_radio_btn.xml | 6 +- .../src/net/osmand/plus/AppInitializer.java | 2 + .../net/osmand/plus/OsmandApplication.java | 6 + .../onlinerouting/OnlineRoutingEngine.java | 85 ++++++++++ .../OnlineRoutingEngineFragment.java | 90 ++++++++-- .../onlinerouting/OnlineRoutingHelper.java | 157 ++++++++++++++++++ .../plus/onlinerouting/VehicleType.java | 10 ++ .../OnlineRoutingEngineDataObject.java | 13 ++ .../plus/profiles/ProfileDataUtils.java | 41 +++-- .../profiles/SelectProfileBottomSheet.java | 39 +++-- .../plus/settings/backend/OsmandSettings.java | 3 + 12 files changed, 516 insertions(+), 47 deletions(-) create mode 100644 OsmAnd/res/layout/bottom_sheet_item_with_descr_and_radio_and_eng_btn.xml create mode 100644 OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingEngine.java create mode 100644 OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingHelper.java create mode 100644 OsmAnd/src/net/osmand/plus/profiles/OnlineRoutingEngineDataObject.java diff --git a/OsmAnd/res/layout/bottom_sheet_item_with_descr_and_radio_and_eng_btn.xml b/OsmAnd/res/layout/bottom_sheet_item_with_descr_and_radio_and_eng_btn.xml new file mode 100644 index 0000000000..d81614fbb1 --- /dev/null +++ b/OsmAnd/res/layout/bottom_sheet_item_with_descr_and_radio_and_eng_btn.xml @@ -0,0 +1,111 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/OsmAnd/res/layout/bottom_sheet_item_with_descr_and_radio_btn.xml b/OsmAnd/res/layout/bottom_sheet_item_with_descr_and_radio_btn.xml index 4bace93e9f..a346825302 100644 --- a/OsmAnd/res/layout/bottom_sheet_item_with_descr_and_radio_btn.xml +++ b/OsmAnd/res/layout/bottom_sheet_item_with_descr_and_radio_btn.xml @@ -9,7 +9,7 @@ diff --git a/OsmAnd/src/net/osmand/plus/AppInitializer.java b/OsmAnd/src/net/osmand/plus/AppInitializer.java index eda57e3c28..424855f2a0 100644 --- a/OsmAnd/src/net/osmand/plus/AppInitializer.java +++ b/OsmAnd/src/net/osmand/plus/AppInitializer.java @@ -44,6 +44,7 @@ import net.osmand.plus.mapmarkers.MapMarkersDbHelper; import net.osmand.plus.mapmarkers.MapMarkersHelper; import net.osmand.plus.monitoring.LiveMonitoringHelper; import net.osmand.plus.monitoring.OsmandMonitoringPlugin; +import net.osmand.plus.onlinerouting.OnlineRoutingHelper; import net.osmand.plus.osmedit.oauth.OsmOAuthHelper; import net.osmand.plus.poi.PoiFiltersHelper; import net.osmand.plus.quickaction.QuickActionRegistry; @@ -467,6 +468,7 @@ public class AppInitializer implements IProgress { app.settingsHelper = startupInit(new SettingsHelper(app), SettingsHelper.class); app.quickActionRegistry = startupInit(new QuickActionRegistry(app.getSettings()), QuickActionRegistry.class); app.osmOAuthHelper = startupInit(new OsmOAuthHelper(app), OsmOAuthHelper.class); + app.onlineRoutingHelper = startupInit(new OnlineRoutingHelper(app), OnlineRoutingHelper.class); initOpeningHoursParser(); } diff --git a/OsmAnd/src/net/osmand/plus/OsmandApplication.java b/OsmAnd/src/net/osmand/plus/OsmandApplication.java index db7c0d6433..8deb9c81a8 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandApplication.java +++ b/OsmAnd/src/net/osmand/plus/OsmandApplication.java @@ -66,6 +66,7 @@ import net.osmand.plus.mapmarkers.MapMarkersDbHelper; import net.osmand.plus.mapmarkers.MapMarkersHelper; import net.osmand.plus.measurementtool.MeasurementEditingContext; import net.osmand.plus.monitoring.LiveMonitoringHelper; +import net.osmand.plus.onlinerouting.OnlineRoutingHelper; import net.osmand.plus.osmedit.oauth.OsmOAuthHelper; import net.osmand.plus.poi.PoiFiltersHelper; import net.osmand.plus.quickaction.QuickActionRegistry; @@ -158,6 +159,7 @@ public class OsmandApplication extends MultiDexApplication { QuickActionRegistry quickActionRegistry; OsmOAuthHelper osmOAuthHelper; MeasurementEditingContext measurementEditingContext; + OnlineRoutingHelper onlineRoutingHelper; private Resources localizedResources; private Map customRoutingConfigs = new ConcurrentHashMap<>(); @@ -475,6 +477,10 @@ public class OsmandApplication extends MultiDexApplication { this.measurementEditingContext = context; } + public OnlineRoutingHelper getOnlineRoutingHelper() { + return onlineRoutingHelper; + } + public TransportRoutingHelper getTransportRoutingHelper() { return transportRoutingHelper; } diff --git a/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingEngine.java b/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingEngine.java new file mode 100644 index 0000000000..164b56439c --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingEngine.java @@ -0,0 +1,85 @@ +package net.osmand.plus.onlinerouting; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import net.osmand.util.Algorithms; + +import java.util.HashMap; +import java.util.Map; + +public class OnlineRoutingEngine { + + public enum EngineParameterType { + CUSTOM_SERVER_URL, + API_KEY + } + + private String stringKey; + private String name; + private ServerType serverType; + private String vehicleKey; + private Map params; + + public OnlineRoutingEngine(@Nullable String stringKey, + @NonNull String name, + @NonNull ServerType serverType, + @NonNull String vehicleKey, + Map params) { + if (stringKey == null) { + stringKey = generateKey(vehicleKey); + } + this.stringKey = stringKey; + this.name = name; + this.serverType = serverType; + this.vehicleKey = vehicleKey; + this.params = params; + } + + public String getStringKey() { + return stringKey; + } + + public String getName() { + return name; + } + + public ServerType getServerType() { + return serverType; + } + + public String getVehicleKey() { + return vehicleKey; + } + + public Map getParams() { + 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(EngineParameterType paramType) { + if (params != null) { + return params.get(paramType.name()); + } + return null; + } + + public void putParameter(EngineParameterType paramType, String paramValue) { + if (params == null) { + params = new HashMap<>(); + } + params.put(paramType.name(), paramValue); + } + + private static String generateKey(String vehicleKey) { + return "online_" + vehicleKey + "_" + System.currentTimeMillis(); + } +} diff --git a/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingEngineFragment.java b/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingEngineFragment.java index 3d5b90444c..e777fa6609 100644 --- a/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingEngineFragment.java +++ b/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingEngineFragment.java @@ -29,6 +29,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.routepreparationmenu.cards.BaseCard; import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.util.Algorithms; @@ -52,8 +53,10 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { private static final String ENGINE_NAME_CHANGED_BY_USER_KEY = "engine_name_changed_by_user_key"; private static final String EXAMPLE_LOCATION_KEY = "example_location"; private static final String APP_MODE_KEY = "app_mode"; + private static final String EDITED_ENGINE_KEY = "edited_engine_key"; private OsmandApplication app; + private OnlineRoutingHelper helper; private OnlineRoutingCard nameCard; private OnlineRoutingCard serverCard; @@ -62,17 +65,17 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { private OnlineRoutingCard exampleCard; private View testResultsContainer; - private boolean isEditingMode; private ApplicationMode appMode; private OnlineRoutingEngineObject engine; private ExampleLocation selectedLocation; + private String editedEngineKey; private enum ExampleLocation { AMSTERDAM("Amsterdam", new LatLon(52.379189, 4.899431), - new LatLon( 52.308056, 4.764167)), + new LatLon(52.308056, 4.764167)), BERLIN("Berlin", new LatLon(52.520008, 13.404954), @@ -80,11 +83,11 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { NEW_YORK("New York", new LatLon(43.000000, -75.000000), - new LatLon( 40.641766, -73.780968)), + new LatLon(40.641766, -73.780968)), PARIS("Paris", new LatLon(48.864716, 2.349014), - new LatLon(48.948437, 2.434931)); + new LatLon(48.948437, 2.434931)); ExampleLocation(String name, LatLon cityCenterLatLon, LatLon cityAirportLatLon) { this.name = name; @@ -113,11 +116,12 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); app = requireMyApplication(); + helper = app.getOnlineRoutingHelper(); engine = new OnlineRoutingEngineObject(); if (savedInstanceState != null) { restoreState(savedInstanceState); } else { - initEngineState(); + initState(); } } @@ -150,7 +154,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { nameCard.setOnTextChangedListener(new OnTextChangedListener() { @Override public void onTextChanged(boolean changedByUser, String text) { - if (!isEditingMode && !engine.wasNameChangedByUser && changedByUser) { + if (!isEditingMode() && !engine.wasNameChangedByUser && changedByUser) { engine.wasNameChangedByUser = true; } engine.name = text; @@ -183,7 +187,9 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { serverCard.setOnTextChangedListener(new OnTextChangedListener() { @Override public void onTextChanged(boolean editedByUser, String text) { - engine.serverBaseUrl = text; + if (editedByUser) { + engine.customServerUrl = text; + } } }); serverCard.setFieldBoxLabelText(getString(R.string.shared_string_server_url)); @@ -299,7 +305,8 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { saveButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - // save engine to settings + saveChanges(); + dismiss(); } }); @@ -320,7 +327,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { TextView title = toolbar.findViewById(R.id.toolbar_title); toolbar.findViewById(R.id.toolbar_subtitle).setVisibility(View.GONE); View actionBtn = toolbar.findViewById(R.id.action_button); - if (isEditingMode) { + if (isEditingMode()) { title.setText(getString(R.string.edit_online_routing_engine)); ImageView ivBtn = toolbar.findViewById(R.id.action_button_icon); ivBtn.setImageDrawable( @@ -328,7 +335,8 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { actionBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - // delete engine from settings + deleteEngine(); + dismiss(); } }); } else { @@ -337,7 +345,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { } } - private void updateCardViews(BaseCard ... cardsToUpdate) { + private void updateCardViews(BaseCard... cardsToUpdate) { for (BaseCard card : cardsToUpdate) { if (nameCard.equals(card)) { if (!engine.wasNameChangedByUser) { @@ -349,7 +357,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { } else if (serverCard.equals(card)) { serverCard.setHeaderSubtitle(engine.serverType.getTitle()); - serverCard.setEditedText(engine.serverType.getBaseUrl()); + serverCard.setEditedText(engine.getBaseUrl()); if (engine.serverType == ServerType.GRAPHHOPER) { apiKeyCard.show(); } else { @@ -360,6 +368,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { vehicleCard.setHeaderSubtitle(engine.vehicleType.toHumanString(app)); if (engine.vehicleType == VehicleType.CUSTOM) { vehicleCard.showFieldBox(); + vehicleCard.setEditedText(engine.getVehicleKey()); } else { vehicleCard.hideFieldBox(); } @@ -370,6 +379,22 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { } } + private void saveChanges() { + OnlineRoutingEngine engineToSave = new OnlineRoutingEngine(editedEngineKey, engine.name, + engine.serverType, engine.getVehicleKey(), null); + + engineToSave.putParameter(EngineParameterType.CUSTOM_SERVER_URL, engine.customServerUrl); + if (engine.serverType == ServerType.GRAPHHOPER) { + engineToSave.putParameter(EngineParameterType.API_KEY, engine.apiKey); + } + + helper.saveEngine(engineToSave); + } + + private void deleteEngine() { + helper.deleteEngine(editedEngineKey); + } + private String getTestUrl() { String baseUrl = engine.serverType.getBaseUrl(); String vehicle = engine.getVehicleKey(); @@ -434,6 +459,10 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { tvDescription.setText(location.getName()); } + private boolean isEditingMode() { + return editedEngineKey != null; + } + @Override public void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); @@ -443,7 +472,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { private void saveState(Bundle outState) { outState.putString(ENGINE_NAME_KEY, engine.name); outState.putString(ENGINE_SERVER_KEY, engine.serverType.name()); - outState.putString(ENGINE_SERVER_URL_KEY, engine.serverBaseUrl); + 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); outState.putString(ENGINE_API_KEY_KEY, engine.apiKey); @@ -452,24 +481,45 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { if (appMode != null) { outState.putString(APP_MODE_KEY, appMode.getStringKey()); } + outState.putString(EDITED_ENGINE_KEY, editedEngineKey); } private void restoreState(Bundle savedState) { engine.name = savedState.getString(ENGINE_NAME_KEY); engine.serverType = ServerType.valueOf(savedState.getString(ENGINE_SERVER_KEY)); - engine.serverBaseUrl = savedState.getString(ENGINE_SERVER_URL_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); engine.apiKey = savedState.getString(ENGINE_API_KEY_KEY); engine.wasNameChangedByUser = savedState.getBoolean(ENGINE_NAME_CHANGED_BY_USER_KEY); selectedLocation = ExampleLocation.valueOf(savedState.getString(EXAMPLE_LOCATION_KEY)); appMode = ApplicationMode.valueOfStringKey(savedState.getString(APP_MODE_KEY), null); + editedEngineKey = savedState.getString(EDITED_ENGINE_KEY); } - private void initEngineState() { + private void initState() { engine.serverType = ServerType.values()[0]; engine.vehicleType = VehicleType.values()[0]; selectedLocation = ExampleLocation.values()[0]; + + if (isEditingMode()) { + OnlineRoutingEngine editedEngine = helper.getEngineByKey(editedEngineKey); + if (editedEngine != null) { + engine.name = editedEngine.getName(); + engine.wasNameChangedByUser = true; + engine.serverType = editedEngine.getServerType(); + engine.customServerUrl = editedEngine.getParameter(EngineParameterType.CUSTOM_SERVER_URL); + String vehicleKey = editedEngine.getVehicleKey(); + if (vehicleKey != null) { + VehicleType vehicleType = VehicleType.getVehicleByKey(vehicleKey); + if (vehicleType == VehicleType.CUSTOM) { + engine.customVehicleKey = vehicleKey; + } + engine.vehicleType = vehicleType; + } + engine.apiKey = editedEngine.getParameter(EngineParameterType.API_KEY); + } + } } private void dismiss() { @@ -495,12 +545,12 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { public static void showInstance(@NonNull FragmentActivity activity, @NonNull ApplicationMode appMode, - boolean isEditingMode) { + String editedEngineKey) { FragmentManager fm = activity.getSupportFragmentManager(); if (!fm.isStateSaved() && fm.findFragmentByTag(OnlineRoutingEngineFragment.TAG) == null) { OnlineRoutingEngineFragment fragment = new OnlineRoutingEngineFragment(); - fragment.isEditingMode = isEditingMode; fragment.appMode = appMode; + fragment.editedEngineKey = editedEngineKey; fm.beginTransaction() .add(R.id.fragmentContainer, fragment, TAG) .addToBackStack(TAG).commitAllowingStateLoss(); @@ -510,7 +560,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { private static class OnlineRoutingEngineObject { private String name; private ServerType serverType; - private String serverBaseUrl; + private String customServerUrl; private VehicleType vehicleType; private String customVehicleKey; private String apiKey; @@ -522,5 +572,9 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { } return vehicleType.getKey(); } + + public String getBaseUrl() { + return customServerUrl != null ? customServerUrl : serverType.getBaseUrl(); + } } } diff --git a/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingHelper.java b/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingHelper.java new file mode 100644 index 0000000000..cc652f35b2 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingHelper.java @@ -0,0 +1,157 @@ +package net.osmand.plus.onlinerouting; + +import androidx.annotation.NonNull; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; + +import net.osmand.PlatformUtil; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.settings.backend.OsmandSettings; +import net.osmand.util.Algorithms; + +import org.apache.commons.logging.Log; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class OnlineRoutingHelper { + + private static final Log LOG = PlatformUtil.getLog(OnlineRoutingHelper.class); + + private OsmandApplication app; + private OsmandSettings settings; + private List cachedEngines; + private Map cachedEnginesMap; + + public OnlineRoutingHelper(OsmandApplication app) { + this.app = app; + this.settings = app.getSettings(); + loadFromSettings(); + } + + @NonNull + public List getEngines() { + return cachedEngines; + } + + public OnlineRoutingEngine getEngineByKey(String stringKey) { + return cachedEnginesMap.get(stringKey); + } + + 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); + saveToSettings(); + } + + public void deleteEngine(@NonNull String stringKey) { + OnlineRoutingEngine engine = getEngineByKey(stringKey); + if (engine != null) { + deleteEngine(engine); + } + } + + public void deleteEngine(@NonNull OnlineRoutingEngine engine) { + String stringKey = engine.getStringKey(); + if (cachedEnginesMap.containsKey(stringKey)) { + OnlineRoutingEngine existedEngine = cachedEnginesMap.remove(stringKey); + cachedEngines.remove(existedEngine); + saveToSettings(); + } + } + + private void loadFromSettings() { + cachedEngines = readFromSettings(); + cachedEnginesMap = new HashMap<>(); + for (OnlineRoutingEngine engine : cachedEngines) { + cachedEnginesMap.put(engine.getStringKey(), engine); + } + } + + @NonNull + private List readFromSettings() { + List engines = new ArrayList<>(); + String jsonString = settings.ONLINE_ROUTING_ENGINES.get(); + if (!Algorithms.isEmpty(jsonString)) { + try { + JSONObject json = new JSONObject(jsonString); + readFromJson(json, engines); + } catch (JSONException e) { + LOG.debug("Error when read setting from JSON"); + } + } + return engines; + } + + private void saveToSettings() { + if (!Algorithms.isEmpty(cachedEngines)) { + JSONObject json = new JSONObject(); + if (writeToJson(json, cachedEngines)) { + settings.ONLINE_ROUTING_ENGINES.set(json.toString()); + } + } else { + settings.ONLINE_ROUTING_ENGINES.set(null); + } + } + + private void readFromJson(JSONObject json, List engines) { + try { + if (!json.has("items")) { + return; + } + Gson gson = new Gson(); + Type type = new TypeToken>() { + }.getType(); + JSONArray itemsJson = json.getJSONArray("items"); + for (int i = 0; i < itemsJson.length(); i++) { + JSONObject object = itemsJson.getJSONObject(i); + String key = object.getString("key"); + String name = object.getString("name"); + String vehicleKey = object.getString("vehicle"); + ServerType serverType = ServerType.valueOf(object.getString("serverType")); + String paramsString = object.getString("params"); + HashMap params = gson.fromJson(paramsString, type); + engines.add(new OnlineRoutingEngine(key, name, serverType, vehicleKey, params)); + } + } catch (JSONException e) { + LOG.debug("Error when read setting from JSON internal"); + } + } + + private boolean writeToJson(JSONObject json, List engines) { + JSONArray jsonArray = new JSONArray(); + Gson gson = new Gson(); + Type type = new TypeToken>() { + }.getType(); + try { + for (OnlineRoutingEngine engine : engines) { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("key", engine.getStringKey()); + jsonObject.put("name", engine.getName()); + jsonObject.put("serverType", engine.getServerType().name()); + jsonObject.put("vehicle", engine.getVehicleKey()); + jsonObject.put("params", gson.toJson(engine.getParams(), type)); + jsonArray.put(jsonObject); + } + json.put("items", jsonArray); + return true; + } catch (JSONException e) { + LOG.debug("Error when write engines to JSON"); + return false; + } + } +} diff --git a/OsmAnd/src/net/osmand/plus/onlinerouting/VehicleType.java b/OsmAnd/src/net/osmand/plus/onlinerouting/VehicleType.java index 7a4d44b714..a3ca17681c 100644 --- a/OsmAnd/src/net/osmand/plus/onlinerouting/VehicleType.java +++ b/OsmAnd/src/net/osmand/plus/onlinerouting/VehicleType.java @@ -3,6 +3,7 @@ package net.osmand.plus.onlinerouting; import android.content.Context; import net.osmand.plus.R; +import net.osmand.util.Algorithms; public enum VehicleType { CAR("car", R.string.routing_engine_vehicle_type_car), @@ -26,4 +27,13 @@ public enum VehicleType { public String toHumanString(Context ctx) { return ctx.getString(titleId); } + + public static VehicleType getVehicleByKey(String key) { + for (VehicleType v : values()) { + if (Algorithms.objectEquals(v.getKey(), key)) { + return v; + } + } + return CUSTOM; + } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/profiles/OnlineRoutingEngineDataObject.java b/OsmAnd/src/net/osmand/plus/profiles/OnlineRoutingEngineDataObject.java new file mode 100644 index 0000000000..cd812b3363 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/profiles/OnlineRoutingEngineDataObject.java @@ -0,0 +1,13 @@ +package net.osmand.plus.profiles; + +public class OnlineRoutingEngineDataObject extends ProfileDataObject { + + public OnlineRoutingEngineDataObject(String name, + String description, + String stringKey, + int iconRes, + boolean isSelected, + ProfileIconColors iconColor) { + super(name, description, stringKey, iconRes, isSelected, iconColor); + } +} diff --git a/OsmAnd/src/net/osmand/plus/profiles/ProfileDataUtils.java b/OsmAnd/src/net/osmand/plus/profiles/ProfileDataUtils.java index 4cbc9b1940..fd146778a7 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/ProfileDataUtils.java +++ b/OsmAnd/src/net/osmand/plus/profiles/ProfileDataUtils.java @@ -5,6 +5,8 @@ import android.content.Context; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; import net.osmand.plus.R; +import net.osmand.plus.onlinerouting.OnlineRoutingEngine; +import net.osmand.plus.profiles.RoutingProfileDataObject.RoutingProfilesResources; import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.router.GeneralRouter; import net.osmand.router.RoutingConfiguration; @@ -66,6 +68,15 @@ public class ProfileDataUtils { return result; } + public static List getOnlineRoutingProfiles(OsmandApplication app) { + List objects = new ArrayList<>(); + for (OnlineRoutingEngine engine : app.getOnlineRoutingHelper().getEngines()) { + objects.add(new OnlineRoutingEngineDataObject(engine.getName(), + engine.getBaseUrl(), engine.getStringKey(), R.drawable.ic_world_globe_dark, false, null)); + } + return objects; + } + public static Map> getRoutingProfilesByFileNames(OsmandApplication app) { Map> result = new HashMap<>(); for (final RoutingProfileDataObject profile : getRoutingProfiles(app).values()) { @@ -83,24 +94,24 @@ public class ProfileDataUtils { public static Map getRoutingProfiles(OsmandApplication context) { Map profilesObjects = new HashMap<>(); - profilesObjects.put(RoutingProfileDataObject.RoutingProfilesResources.STRAIGHT_LINE_MODE.name(), new RoutingProfileDataObject( - RoutingProfileDataObject.RoutingProfilesResources.STRAIGHT_LINE_MODE.name(), - context.getString(RoutingProfileDataObject.RoutingProfilesResources.STRAIGHT_LINE_MODE.getStringRes()), + profilesObjects.put(RoutingProfilesResources.STRAIGHT_LINE_MODE.name(), new RoutingProfileDataObject( + RoutingProfilesResources.STRAIGHT_LINE_MODE.name(), + context.getString(RoutingProfilesResources.STRAIGHT_LINE_MODE.getStringRes()), context.getString(R.string.special_routing_type), - RoutingProfileDataObject.RoutingProfilesResources.STRAIGHT_LINE_MODE.getIconRes(), + RoutingProfilesResources.STRAIGHT_LINE_MODE.getIconRes(), false, null)); - profilesObjects.put(RoutingProfileDataObject.RoutingProfilesResources.DIRECT_TO_MODE.name(), new RoutingProfileDataObject( - RoutingProfileDataObject.RoutingProfilesResources.DIRECT_TO_MODE.name(), - context.getString(RoutingProfileDataObject.RoutingProfilesResources.DIRECT_TO_MODE.getStringRes()), + profilesObjects.put(RoutingProfilesResources.DIRECT_TO_MODE.name(), new RoutingProfileDataObject( + RoutingProfilesResources.DIRECT_TO_MODE.name(), + context.getString(RoutingProfilesResources.DIRECT_TO_MODE.getStringRes()), context.getString(R.string.special_routing_type), - RoutingProfileDataObject.RoutingProfilesResources.DIRECT_TO_MODE.getIconRes(), + RoutingProfilesResources.DIRECT_TO_MODE.getIconRes(), false, null)); if (context.getBRouterService() != null) { - profilesObjects.put(RoutingProfileDataObject.RoutingProfilesResources.BROUTER_MODE.name(), new RoutingProfileDataObject( - RoutingProfileDataObject.RoutingProfilesResources.BROUTER_MODE.name(), - context.getString(RoutingProfileDataObject.RoutingProfilesResources.BROUTER_MODE.getStringRes()), + profilesObjects.put(RoutingProfilesResources.BROUTER_MODE.name(), new RoutingProfileDataObject( + RoutingProfilesResources.BROUTER_MODE.name(), + context.getString(RoutingProfilesResources.BROUTER_MODE.getStringRes()), context.getString(R.string.third_party_routing_type), - RoutingProfileDataObject.RoutingProfilesResources.BROUTER_MODE.getIconRes(), + RoutingProfilesResources.BROUTER_MODE.getIconRes(), false, null)); } @@ -123,9 +134,9 @@ public class ProfileDataUtils { String fileName = router.getFilename(); if (!Algorithms.isEmpty(fileName)) { description = fileName; - } else if (RoutingProfileDataObject.RoutingProfilesResources.isRpValue(name.toUpperCase())) { - iconRes = RoutingProfileDataObject.RoutingProfilesResources.valueOf(name.toUpperCase()).getIconRes(); - name = app.getString(RoutingProfileDataObject.RoutingProfilesResources.valueOf(name.toUpperCase()).getStringRes()); + } else if (RoutingProfilesResources.isRpValue(name.toUpperCase())) { + iconRes = RoutingProfilesResources.valueOf(name.toUpperCase()).getIconRes(); + name = app.getString(RoutingProfilesResources.valueOf(name.toUpperCase()).getStringRes()); } profilesObjects.put(routerKey, new RoutingProfileDataObject(routerKey, name, description, iconRes, false, fileName)); diff --git a/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheet.java b/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheet.java index af1289e85b..ebc554040a 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheet.java @@ -149,17 +149,23 @@ public class SelectProfileBottomSheet extends BasePreferenceBottomSheet { items.add(new TitleItem(getString(R.string.select_nav_profile_dialog_title))); items.add(new LongDescriptionItem(getString(R.string.select_nav_profile_dialog_message))); for (int i = 0; i < profiles.size(); i++) { - final RoutingProfileDataObject profile = (RoutingProfileDataObject) profiles.get(i); boolean showBottomDivider = false; - if (i < profiles.size() - 1) { - RoutingProfileDataObject nextProfile = (RoutingProfileDataObject) profiles.get(i + 1); - if (profile.getFileName() == null) { - showBottomDivider = nextProfile.getFileName() != null; - } else { - showBottomDivider = !profile.getFileName().equals(nextProfile.getFileName()); + if (profiles.get(i) instanceof RoutingProfileDataObject) { + final RoutingProfileDataObject profile = (RoutingProfileDataObject) profiles.get(i); + if (i < profiles.size() - 1) { + if (profiles.get(i + 1) instanceof RoutingProfileDataObject) { + RoutingProfileDataObject nextProfile = (RoutingProfileDataObject) profiles.get(i + 1); + if (profile.getFileName() == null) { + showBottomDivider = nextProfile.getFileName() != null; + } else { + showBottomDivider = !profile.getFileName().equals(nextProfile.getFileName()); + } + } else { + showBottomDivider = true; + } } } - addProfileItem(profile, showBottomDivider); + addProfileItem(profiles.get(i), showBottomDivider); } items.add(new DividerItem(app)); items.add(new LongDescriptionItem(app.getString(R.string.osmand_routing_promo))); @@ -183,7 +189,7 @@ public class SelectProfileBottomSheet extends BasePreferenceBottomSheet { @Override public void onClick(View v) { if (getActivity() != null) { - OnlineRoutingEngineFragment.showInstance(getActivity(), getAppMode(), false); + OnlineRoutingEngineFragment.showInstance(getActivity(), getAppMode(), null); } dismiss(); } @@ -229,7 +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; - View itemView = UiUtilities.getInflater(getContext(), nightMode).inflate(R.layout.bottom_sheet_item_with_descr_and_radio_btn, null); + View itemView = UiUtilities.getInflater(getContext(), nightMode).inflate( + profile instanceof OnlineRoutingEngineDataObject ? + R.layout.bottom_sheet_item_with_descr_and_radio_and_eng_btn : + R.layout.bottom_sheet_item_with_descr_and_radio_btn, null); TextView tvTitle = itemView.findViewById(R.id.title); TextView tvDescription = itemView.findViewById(R.id.description); ImageView ivIcon = itemView.findViewById(R.id.icon); @@ -262,7 +271,14 @@ public class SelectProfileBottomSheet extends BasePreferenceBottomSheet { args.putString(PROFILE_KEY_ARG, profile.getStringKey()); Fragment target = getTargetFragment(); if (target instanceof OnSelectProfileCallback) { - ((OnSelectProfileCallback) target).onProfileSelected(args); + if (profile instanceof OnlineRoutingEngineDataObject) { + if (getActivity() != null) { + OnlineRoutingEngineFragment.showInstance(getActivity(), getAppMode(), profile.getStringKey()); + } + dismiss(); + } else { + ((OnSelectProfileCallback) target).onProfileSelected(args); + } } dismiss(); } @@ -347,6 +363,7 @@ public class SelectProfileBottomSheet extends BasePreferenceBottomSheet { case NAVIGATION_PROFILE: profiles.addAll(ProfileDataUtils.getSortedRoutingProfiles(app)); + profiles.addAll(ProfileDataUtils.getOnlineRoutingProfiles(app)); break; case DEFAULT_PROFILE: diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java index b3cd8fb9a6..0019b7bffb 100644 --- a/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java @@ -48,6 +48,7 @@ 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; @@ -1015,6 +1016,8 @@ public class OsmandSettings { ROUTE_SERVICE.setModeDefaultValue(ApplicationMode.AIRCRAFT, RouteService.STRAIGHT); } + public final CommonPreference ONLINE_ROUTING_ENGINES = new StringPreference(this, "online_routing_engines", null); + public final CommonPreference NAVIGATION_ICON = new EnumStringPreference<>(this, "navigation_icon", NavigationIcon.DEFAULT, NavigationIcon.values()).makeProfile().cache(); { From 38a3e8c6fea11a4a01d4751f7804f2ea20553b95 Mon Sep 17 00:00:00 2001 From: nazar-kutz Date: Sun, 3 Jan 2021 20:28:47 +0200 Subject: [PATCH 8/8] Online Routing UI - refactoring part 3 --- ...et_item_with_descr_radio_and_icon_btn.xml} | 0 .../plus/onlinerouting/OnlineRoutingCard.java | 12 ++ .../onlinerouting/OnlineRoutingEngine.java | 69 ++++--- .../OnlineRoutingEngineFragment.java | 176 ++++++++++-------- .../onlinerouting/OnlineRoutingHelper.java | 14 +- .../plus/onlinerouting/VehicleType.java | 15 +- .../OnlineRoutingEngineDataObject.java | 9 +- .../plus/profiles/ProfileDataUtils.java | 4 +- .../profiles/SelectProfileBottomSheet.java | 2 +- 9 files changed, 186 insertions(+), 115 deletions(-) rename OsmAnd/res/layout/{bottom_sheet_item_with_descr_and_radio_and_eng_btn.xml => bottom_sheet_item_with_descr_radio_and_icon_btn.xml} (100%) diff --git a/OsmAnd/res/layout/bottom_sheet_item_with_descr_and_radio_and_eng_btn.xml b/OsmAnd/res/layout/bottom_sheet_item_with_descr_radio_and_icon_btn.xml similarity index 100% rename from OsmAnd/res/layout/bottom_sheet_item_with_descr_and_radio_and_eng_btn.xml rename to OsmAnd/res/layout/bottom_sheet_item_with_descr_radio_and_icon_btn.xml diff --git a/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingCard.java b/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingCard.java index bd4ca4c735..1b9e7f3b29 100644 --- a/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingCard.java +++ b/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingCard.java @@ -4,6 +4,7 @@ import android.text.Editable; import android.text.TextWatcher; import android.view.View; import android.view.View.OnClickListener; +import android.view.View.OnFocusChangeListener; import android.widget.EditText; import android.widget.TextView; @@ -11,6 +12,7 @@ import androidx.annotation.NonNull; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import net.osmand.AndroidUtils; import net.osmand.CallbackWithObject; import net.osmand.plus.R; import net.osmand.plus.UiUtilities; @@ -83,6 +85,16 @@ public class OnlineRoutingCard extends BaseCard { } } }); + + editText.setOnFocusChangeListener(new OnFocusChangeListener() { + @Override + public void onFocusChange(View v, boolean hasFocus) { + if (hasFocus) { + editText.setSelection(editText.getText().length()); + AndroidUtils.showSoftKeyboard(getMapActivity(), editText); + } + } + }); } public void setHeaderTitle(@NonNull String title) { diff --git a/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingEngine.java b/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingEngine.java index 164b56439c..10ba169cb1 100644 --- a/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingEngine.java +++ b/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingEngine.java @@ -1,8 +1,10 @@ package net.osmand.plus.onlinerouting; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; +import android.content.Context; +import androidx.annotation.NonNull; + +import net.osmand.plus.R; import net.osmand.util.Algorithms; import java.util.HashMap; @@ -10,40 +12,39 @@ import java.util.Map; public class OnlineRoutingEngine { + public final static String ONLINE_ROUTING_ENGINE_PREFIX = "online_routing_engine_"; + public enum EngineParameterType { CUSTOM_SERVER_URL, + CUSTOM_NAME, API_KEY } private String stringKey; - private String name; private ServerType serverType; private String vehicleKey; - private Map params; + private Map params = new HashMap<>(); - public OnlineRoutingEngine(@Nullable String stringKey, - @NonNull String name, + public OnlineRoutingEngine(@NonNull String stringKey, @NonNull ServerType serverType, @NonNull String vehicleKey, Map params) { - if (stringKey == null) { - stringKey = generateKey(vehicleKey); - } + this(stringKey, serverType, vehicleKey); + this.params = params; + } + + public OnlineRoutingEngine(@NonNull String stringKey, + @NonNull ServerType serverType, + @NonNull String vehicleKey) { this.stringKey = stringKey; - this.name = name; this.serverType = serverType; this.vehicleKey = vehicleKey; - this.params = params; } public String getStringKey() { return stringKey; } - public String getName() { - return name; - } - public ServerType getServerType() { return serverType; } @@ -66,20 +67,40 @@ public class OnlineRoutingEngine { } public String getParameter(EngineParameterType paramType) { - if (params != null) { - return params.get(paramType.name()); - } - return null; + return params.get(paramType.name()); } public void putParameter(EngineParameterType paramType, String paramValue) { - if (params == null) { - params = new HashMap<>(); - } params.put(paramType.name(), paramValue); } - private static String generateKey(String vehicleKey) { - return "online_" + vehicleKey + "_" + System.currentTimeMillis(); + public String getName(@NonNull Context ctx) { + String customName = getParameter(EngineParameterType.CUSTOM_NAME); + if (customName != null) { + return customName; + } else { + return getStandardName(ctx); + } + } + + private String getStandardName(@NonNull Context ctx) { + return getStandardName(ctx, serverType, vehicleKey); + } + + public static String getStandardName(@NonNull Context ctx, + @NonNull ServerType serverType, + @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); + } + + public static OnlineRoutingEngine createNewEngine(@NonNull ServerType serverType, + @NonNull String vehicleKey) { + return new OnlineRoutingEngine(generateKey(), serverType, vehicleKey); + } + + private static String generateKey() { + return ONLINE_ROUTING_ENGINE_PREFIX + System.currentTimeMillis(); } } diff --git a/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingEngineFragment.java b/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingEngineFragment.java index e777fa6609..18b24a8762 100644 --- a/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingEngineFragment.java +++ b/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingEngineFragment.java @@ -1,5 +1,6 @@ package net.osmand.plus.onlinerouting; +import android.content.Context; import android.os.Build; import android.os.Bundle; import android.view.LayoutInflater; @@ -50,14 +51,16 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { private static final String ENGINE_VEHICLE_TYPE_KEY = "engine_vehicle_type"; private static final String ENGINE_CUSTOM_VEHICLE_KEY = "engine_custom_vehicle"; private static final String ENGINE_API_KEY_KEY = "engine_api_key"; - private static final String ENGINE_NAME_CHANGED_BY_USER_KEY = "engine_name_changed_by_user_key"; private static final String EXAMPLE_LOCATION_KEY = "example_location"; private static final String APP_MODE_KEY = "app_mode"; private static final String EDITED_ENGINE_KEY = "edited_engine_key"; private OsmandApplication app; + private MapActivity mapActivity; private OnlineRoutingHelper helper; + private View view; + private ViewGroup segmentsContainer; private OnlineRoutingCard nameCard; private OnlineRoutingCard serverCard; private OnlineRoutingCard vehicleCard; @@ -116,6 +119,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); app = requireMyApplication(); + mapActivity = getMapActivity(); helper = app.getOnlineRoutingHelper(); engine = new OnlineRoutingEngineObject(); if (savedInstanceState != null) { @@ -130,41 +134,48 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - MapActivity mapActivity = getMapActivity(); - if (mapActivity == null) { - return null; - } - boolean nightMode = isNightMode(); - inflater = UiUtilities.getInflater(getContext(), nightMode); - View view = inflater.inflate( + view = getInflater().inflate( R.layout.online_routing_engine_fragment, container, false); + segmentsContainer = (ViewGroup) view.findViewById(R.id.segments_container); if (Build.VERSION.SDK_INT >= 21) { AndroidUtils.addStatusBarPadding21v(getContext(), view); } - setupToolbar(view); + setupToolbar((Toolbar) view.findViewById(R.id.toolbar)); - ViewGroup segmentsContainer = (ViewGroup) view.findViewById(R.id.segments_container); + setupNameCard(); + setupServerCard(); + setupVehicleCard(); + setupApiKeyCard(); + setupExampleCard(); + setupResultsContainer(); + addSpaceSegment(); - // create name card - nameCard = new OnlineRoutingCard(mapActivity, nightMode); + setupButtons(); + + updateCardViews(nameCard, serverCard, vehicleCard, exampleCard); + return view; + } + + private void setupNameCard() { + nameCard = new OnlineRoutingCard(mapActivity, isNightMode()); nameCard.build(mapActivity); nameCard.setDescription(getString(R.string.select_nav_profile_dialog_message)); - nameCard.setEditedText(engine.name); + nameCard.setEditedText(engine.getName(app)); nameCard.setFieldBoxLabelText(getString(R.string.shared_string_name)); nameCard.setOnTextChangedListener(new OnTextChangedListener() { @Override public void onTextChanged(boolean changedByUser, String text) { - if (!isEditingMode() && !engine.wasNameChangedByUser && changedByUser) { - engine.wasNameChangedByUser = true; + if (changedByUser) { + engine.customName = text; } - engine.name = text; } }); nameCard.showDivider(); segmentsContainer.addView(nameCard.getView()); + } - // create server card - serverCard = new OnlineRoutingCard(mapActivity, nightMode); + private void setupServerCard() { + serverCard = new OnlineRoutingCard(mapActivity, isNightMode()); serverCard.build(mapActivity); serverCard.setHeaderTitle(getString(R.string.shared_string_type)); List serverItems = new ArrayList<>(); @@ -195,16 +206,17 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { serverCard.setFieldBoxLabelText(getString(R.string.shared_string_server_url)); serverCard.showDivider(); segmentsContainer.addView(serverCard.getView()); + } - // create vehicle card - vehicleCard = new OnlineRoutingCard(mapActivity, nightMode); + private void setupVehicleCard() { + vehicleCard = new OnlineRoutingCard(mapActivity, isNightMode()); vehicleCard.build(mapActivity); vehicleCard.setHeaderTitle(getString(R.string.shared_string_vehicle)); List vehicleItems = new ArrayList<>(); for (VehicleType vehicle : VehicleType.values()) { - vehicleItems.add(new HorizontalSelectionItem(vehicle.toHumanString(app), vehicle)); + vehicleItems.add(new HorizontalSelectionItem(vehicle.getTitle(app), vehicle)); } - vehicleCard.setSelectionMenu(vehicleItems, engine.vehicleType.toHumanString(app), + vehicleCard.setSelectionMenu(vehicleItems, engine.vehicleType.getTitle(app), new CallbackWithObject() { @Override public boolean processResult(HorizontalSelectionItem result) { @@ -221,16 +233,20 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { vehicleCard.setOnTextChangedListener(new OnTextChangedListener() { @Override public void onTextChanged(boolean editedByUser, String text) { - engine.customVehicleKey = text; + if (editedByUser) { + engine.customVehicleKey = text; + updateCardViews(nameCard, exampleCard); + } } }); vehicleCard.setEditedText(engine.customVehicleKey); vehicleCard.setFieldBoxHelperText(getString(R.string.shared_string_enter_param)); vehicleCard.showDivider(); segmentsContainer.addView(vehicleCard.getView()); + } - // create api key card - apiKeyCard = new OnlineRoutingCard(mapActivity, nightMode); + private void setupApiKeyCard() { + apiKeyCard = new OnlineRoutingCard(mapActivity, isNightMode()); apiKeyCard.build(mapActivity); apiKeyCard.setHeaderTitle(getString(R.string.shared_string_api_key)); apiKeyCard.setFieldBoxLabelText(getString(R.string.keep_it_empty_if_not)); @@ -244,9 +260,10 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { } }); segmentsContainer.addView(apiKeyCard.getView()); + } - // create example card - exampleCard = new OnlineRoutingCard(mapActivity, nightMode); + private void setupExampleCard() { + exampleCard = new OnlineRoutingCard(mapActivity, isNightMode()); exampleCard.build(mapActivity); exampleCard.setHeaderTitle(getString(R.string.shared_string_example)); List locationItems = new ArrayList<>(); @@ -274,48 +291,24 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { } }); segmentsContainer.addView(exampleCard.getView()); + } - testResultsContainer = inflater.inflate( + private void setupResultsContainer() { + testResultsContainer = getInflater().inflate( R.layout.bottom_sheet_item_with_descr_64dp, segmentsContainer, false); testResultsContainer.setVisibility(View.GONE); segmentsContainer.addView(testResultsContainer); + } - View bottomSpaceView = new View(app); + private void addSpaceSegment() { int space = (int) getResources().getDimension(R.dimen.empty_state_text_button_padding_top); + View bottomSpaceView = new View(app); bottomSpaceView.setLayoutParams( new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, space)); segmentsContainer.addView(bottomSpaceView); - - View cancelButton = view.findViewById(R.id.dismiss_button); - UiUtilities.setupDialogButton(nightMode, cancelButton, - DialogButtonType.SECONDARY, R.string.shared_string_cancel); - cancelButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - dismiss(); - } - }); - - view.findViewById(R.id.buttons_divider).setVisibility(View.VISIBLE); - - View saveButton = view.findViewById(R.id.right_bottom_button); - UiUtilities.setupDialogButton(nightMode, saveButton, - UiUtilities.DialogButtonType.PRIMARY, R.string.shared_string_save); - saveButton.setVisibility(View.VISIBLE); - saveButton.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - saveChanges(); - dismiss(); - } - }); - - updateCardViews(nameCard, serverCard, vehicleCard, exampleCard); - return view; } - private void setupToolbar(View mainView) { - Toolbar toolbar = (Toolbar) mainView.findViewById(R.id.toolbar); + private void setupToolbar(Toolbar toolbar) { ImageView navigationIcon = toolbar.findViewById(R.id.close_button); navigationIcon.setImageResource(R.drawable.ic_action_close); navigationIcon.setOnClickListener(new OnClickListener() { @@ -348,10 +341,8 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { private void updateCardViews(BaseCard... cardsToUpdate) { for (BaseCard card : cardsToUpdate) { if (nameCard.equals(card)) { - if (!engine.wasNameChangedByUser) { - String name = String.format( - getString(R.string.ltr_or_rtl_combine_via_dash), - engine.serverType.getTitle(), engine.vehicleType.toHumanString(app)); + if (Algorithms.isEmpty(engine.customName)) { + String name = OnlineRoutingEngine.getStandardName(app, engine.serverType, engine.getVehicleKey()); nameCard.setEditedText(name); } @@ -365,7 +356,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { } } else if (vehicleCard.equals(card)) { - vehicleCard.setHeaderSubtitle(engine.vehicleType.toHumanString(app)); + vehicleCard.setHeaderSubtitle(engine.vehicleType.getTitle(app)); if (engine.vehicleType == VehicleType.CUSTOM) { vehicleCard.showFieldBox(); vehicleCard.setEditedText(engine.getVehicleKey()); @@ -379,11 +370,43 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { } } + private void setupButtons() { + boolean nightMode = isNightMode(); + View cancelButton = view.findViewById(R.id.dismiss_button); + UiUtilities.setupDialogButton(nightMode, cancelButton, + DialogButtonType.SECONDARY, R.string.shared_string_cancel); + cancelButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + dismiss(); + } + }); + + view.findViewById(R.id.buttons_divider).setVisibility(View.VISIBLE); + + View saveButton = view.findViewById(R.id.right_bottom_button); + UiUtilities.setupDialogButton(nightMode, saveButton, + UiUtilities.DialogButtonType.PRIMARY, R.string.shared_string_save); + saveButton.setVisibility(View.VISIBLE); + saveButton.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + saveChanges(); + dismiss(); + } + }); + } + private void saveChanges() { - OnlineRoutingEngine engineToSave = new OnlineRoutingEngine(editedEngineKey, engine.name, - engine.serverType, engine.getVehicleKey(), null); + OnlineRoutingEngine engineToSave; + if (isEditingMode()) { + engineToSave = new OnlineRoutingEngine(editedEngineKey, engine.serverType, engine.getVehicleKey()); + } else { + engineToSave = OnlineRoutingEngine.createNewEngine(engine.serverType, 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); } @@ -470,13 +493,12 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { } private void saveState(Bundle outState) { - outState.putString(ENGINE_NAME_KEY, engine.name); + outState.putString(ENGINE_NAME_KEY, engine.customName); outState.putString(ENGINE_SERVER_KEY, engine.serverType.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); outState.putString(ENGINE_API_KEY_KEY, engine.apiKey); - outState.putBoolean(ENGINE_NAME_CHANGED_BY_USER_KEY, engine.wasNameChangedByUser); outState.putString(EXAMPLE_LOCATION_KEY, selectedLocation.name()); if (appMode != null) { outState.putString(APP_MODE_KEY, appMode.getStringKey()); @@ -485,13 +507,12 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { } private void restoreState(Bundle savedState) { - engine.name = savedState.getString(ENGINE_NAME_KEY); + engine.customName = savedState.getString(ENGINE_NAME_KEY); engine.serverType = ServerType.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); engine.apiKey = savedState.getString(ENGINE_API_KEY_KEY); - engine.wasNameChangedByUser = savedState.getBoolean(ENGINE_NAME_CHANGED_BY_USER_KEY); selectedLocation = ExampleLocation.valueOf(savedState.getString(EXAMPLE_LOCATION_KEY)); appMode = ApplicationMode.valueOfStringKey(savedState.getString(APP_MODE_KEY), null); editedEngineKey = savedState.getString(EDITED_ENGINE_KEY); @@ -505,8 +526,7 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { if (isEditingMode()) { OnlineRoutingEngine editedEngine = helper.getEngineByKey(editedEngineKey); if (editedEngine != null) { - engine.name = editedEngine.getName(); - engine.wasNameChangedByUser = true; + engine.customName = editedEngine.getParameter(EngineParameterType.CUSTOM_NAME); engine.serverType = editedEngine.getServerType(); engine.customServerUrl = editedEngine.getParameter(EngineParameterType.CUSTOM_SERVER_URL); String vehicleKey = editedEngine.getVehicleKey(); @@ -543,6 +563,10 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { } } + private LayoutInflater getInflater() { + return UiUtilities.getInflater(mapActivity, isNightMode()); + } + public static void showInstance(@NonNull FragmentActivity activity, @NonNull ApplicationMode appMode, String editedEngineKey) { @@ -558,13 +582,12 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { } private static class OnlineRoutingEngineObject { - private String name; + private String customName; private ServerType serverType; private String customServerUrl; private VehicleType vehicleType; private String customVehicleKey; private String apiKey; - private boolean wasNameChangedByUser; public String getVehicleKey() { if (vehicleType == VehicleType.CUSTOM) { @@ -576,5 +599,12 @@ public class OnlineRoutingEngineFragment extends BaseOsmAndFragment { 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()); + } } } diff --git a/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingHelper.java b/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingHelper.java index cc652f35b2..8a9e42bd54 100644 --- a/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingHelper.java +++ b/OsmAnd/src/net/osmand/plus/onlinerouting/OnlineRoutingHelper.java @@ -91,7 +91,7 @@ public class OnlineRoutingHelper { JSONObject json = new JSONObject(jsonString); readFromJson(json, engines); } catch (JSONException e) { - LOG.debug("Error when read setting from JSON"); + LOG.debug("Error when create a new JSONObject: " + e.toString()); } } return engines; @@ -108,7 +108,7 @@ public class OnlineRoutingHelper { } } - private void readFromJson(JSONObject json, List engines) { + private static void readFromJson(JSONObject json, List engines) { try { if (!json.has("items")) { return; @@ -120,19 +120,18 @@ public class OnlineRoutingHelper { for (int i = 0; i < itemsJson.length(); i++) { JSONObject object = itemsJson.getJSONObject(i); String key = object.getString("key"); - String name = object.getString("name"); String vehicleKey = object.getString("vehicle"); ServerType serverType = ServerType.valueOf(object.getString("serverType")); String paramsString = object.getString("params"); HashMap params = gson.fromJson(paramsString, type); - engines.add(new OnlineRoutingEngine(key, name, serverType, vehicleKey, params)); + engines.add(new OnlineRoutingEngine(key, serverType, vehicleKey, params)); } } catch (JSONException e) { - LOG.debug("Error when read setting from JSON internal"); + LOG.debug("Error when reading engines from JSON: " + e.toString()); } } - private boolean writeToJson(JSONObject json, List engines) { + private static boolean writeToJson(JSONObject json, List engines) { JSONArray jsonArray = new JSONArray(); Gson gson = new Gson(); Type type = new TypeToken>() { @@ -141,7 +140,6 @@ public class OnlineRoutingHelper { for (OnlineRoutingEngine engine : engines) { JSONObject jsonObject = new JSONObject(); jsonObject.put("key", engine.getStringKey()); - jsonObject.put("name", engine.getName()); jsonObject.put("serverType", engine.getServerType().name()); jsonObject.put("vehicle", engine.getVehicleKey()); jsonObject.put("params", gson.toJson(engine.getParams(), type)); @@ -150,7 +148,7 @@ public class OnlineRoutingHelper { json.put("items", jsonArray); return true; } catch (JSONException e) { - LOG.debug("Error when write engines to JSON"); + LOG.debug("Error when writing engines to JSON: " + e.toString()); return false; } } diff --git a/OsmAnd/src/net/osmand/plus/onlinerouting/VehicleType.java b/OsmAnd/src/net/osmand/plus/onlinerouting/VehicleType.java index a3ca17681c..3e3fea6660 100644 --- a/OsmAnd/src/net/osmand/plus/onlinerouting/VehicleType.java +++ b/OsmAnd/src/net/osmand/plus/onlinerouting/VehicleType.java @@ -2,6 +2,8 @@ package net.osmand.plus.onlinerouting; import android.content.Context; +import androidx.annotation.NonNull; + import net.osmand.plus.R; import net.osmand.util.Algorithms; @@ -10,7 +12,7 @@ public enum VehicleType { BIKE("bike", R.string.routing_engine_vehicle_type_bike), FOOT("foot", R.string.routing_engine_vehicle_type_foot), DRIVING("driving", R.string.routing_engine_vehicle_type_driving), - CUSTOM("-", R.string.shared_string_custom); + CUSTOM("", R.string.shared_string_custom); VehicleType(String key, int titleId) { this.key = key; @@ -24,10 +26,19 @@ public enum VehicleType { return key; } - public String toHumanString(Context ctx) { + public String getTitle(Context ctx) { return ctx.getString(titleId); } + public static String toHumanString(@NonNull Context ctx, + @NonNull String key) { + VehicleType vehicleType = getVehicleByKey(key); + if (vehicleType == CUSTOM) { + return Algorithms.capitalizeFirstLetter(key); + } + return vehicleType.getTitle(ctx); + } + public static VehicleType getVehicleByKey(String key) { for (VehicleType v : values()) { if (Algorithms.objectEquals(v.getKey(), key)) { diff --git a/OsmAnd/src/net/osmand/plus/profiles/OnlineRoutingEngineDataObject.java b/OsmAnd/src/net/osmand/plus/profiles/OnlineRoutingEngineDataObject.java index cd812b3363..46a35ea67e 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/OnlineRoutingEngineDataObject.java +++ b/OsmAnd/src/net/osmand/plus/profiles/OnlineRoutingEngineDataObject.java @@ -1,13 +1,12 @@ package net.osmand.plus.profiles; +import net.osmand.plus.R; + public class OnlineRoutingEngineDataObject extends ProfileDataObject { public OnlineRoutingEngineDataObject(String name, String description, - String stringKey, - int iconRes, - boolean isSelected, - ProfileIconColors iconColor) { - super(name, description, stringKey, iconRes, isSelected, iconColor); + String stringKey) { + super(name, description, stringKey, R.drawable.ic_world_globe_dark, false, null); } } diff --git a/OsmAnd/src/net/osmand/plus/profiles/ProfileDataUtils.java b/OsmAnd/src/net/osmand/plus/profiles/ProfileDataUtils.java index fd146778a7..f769f69bdb 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/ProfileDataUtils.java +++ b/OsmAnd/src/net/osmand/plus/profiles/ProfileDataUtils.java @@ -71,8 +71,8 @@ public class ProfileDataUtils { public static List getOnlineRoutingProfiles(OsmandApplication app) { List objects = new ArrayList<>(); for (OnlineRoutingEngine engine : app.getOnlineRoutingHelper().getEngines()) { - objects.add(new OnlineRoutingEngineDataObject(engine.getName(), - engine.getBaseUrl(), engine.getStringKey(), R.drawable.ic_world_globe_dark, false, null)); + objects.add(new OnlineRoutingEngineDataObject( + engine.getName(app), engine.getBaseUrl(), engine.getStringKey())); } return objects; } diff --git a/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheet.java b/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheet.java index ebc554040a..df2bd8b879 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheet.java @@ -237,7 +237,7 @@ public class SelectProfileBottomSheet extends BasePreferenceBottomSheet { View itemView = UiUtilities.getInflater(getContext(), nightMode).inflate( profile instanceof OnlineRoutingEngineDataObject ? - R.layout.bottom_sheet_item_with_descr_and_radio_and_eng_btn : + 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); TextView tvDescription = itemView.findViewById(R.id.description);