From 38a3e8c6fea11a4a01d4751f7804f2ea20553b95 Mon Sep 17 00:00:00 2001 From: nazar-kutz Date: Sun, 3 Jan 2021 20:28:47 +0200 Subject: [PATCH] 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);