From c00c28e81116a28f2a3746e95e3a203256d56e16 Mon Sep 17 00:00:00 2001 From: nazar-kutz Date: Mon, 28 Dec 2020 01:02:46 +0200 Subject: [PATCH] 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(); + } + } +}