Fix issue where routing profiles with equal names being overwritten

This commit is contained in:
madwasp79 2019-05-14 15:42:00 +03:00
parent 5a41dbb962
commit 998e04181c
3 changed files with 76 additions and 53 deletions

View file

@ -45,6 +45,7 @@ public class GeneralRouter implements VehicleRouter {
private boolean heightObstacles; private boolean heightObstacles;
private boolean allowPrivate; private boolean allowPrivate;
private String filename = null; private String filename = null;
private String profileName = "";
private Map<RouteRegion, Map<Integer, Integer>> regionConvert = new LinkedHashMap<RouteRegion, Map<Integer,Integer>>(); private Map<RouteRegion, Map<Integer, Integer>> regionConvert = new LinkedHashMap<RouteRegion, Map<Integer,Integer>>();
@ -130,6 +131,14 @@ public class GeneralRouter implements VehicleRouter {
this.filename = filename; this.filename = filename;
} }
public String getProfileName() {
return profileName;
}
public void setProfileName(String profileName) {
this.profileName = profileName;
}
public GeneralRouter(GeneralRouter parent, Map<String, String> params) { public GeneralRouter(GeneralRouter parent, Map<String, String> params) {
this.profile = parent.profile; this.profile = parent.profile;
this.attributes = new LinkedHashMap<String, String>(); this.attributes = new LinkedHashMap<String, String>();

View file

@ -308,7 +308,7 @@ public class RoutingConfiguration {
} }
} }
private static int count = 0;
private static GeneralRouter parseRoutingProfile(XmlPullParser parser, final RoutingConfiguration.Builder config, String filename) { private static GeneralRouter parseRoutingProfile(XmlPullParser parser, final RoutingConfiguration.Builder config, String filename) {
String currentSelectedRouter = parser.getAttributeValue("", "name"); String currentSelectedRouter = parser.getAttributeValue("", "name");
@ -319,9 +319,14 @@ public class RoutingConfiguration {
GeneralRouterProfile c = Algorithms.parseEnumValue(GeneralRouterProfile.values(), GeneralRouterProfile c = Algorithms.parseEnumValue(GeneralRouterProfile.values(),
parser.getAttributeValue("", "baseProfile"), GeneralRouterProfile.CAR); parser.getAttributeValue("", "baseProfile"), GeneralRouterProfile.CAR);
GeneralRouter currentRouter = new GeneralRouter(c, attrs); GeneralRouter currentRouter = new GeneralRouter(c, attrs);
currentRouter.setProfileName(currentSelectedRouter);
if (filename != null) { if (filename != null) {
currentRouter.setFilename(filename); currentRouter.setFilename(filename);
} }
if (Builder.routers.containsKey(currentSelectedRouter)) {
count++;
currentSelectedRouter = currentSelectedRouter + currentRouter.hashCode();
}
Builder.routers.put(currentSelectedRouter, currentRouter); Builder.routers.put(currentSelectedRouter, currentRouter);
return currentRouter; return currentRouter;
} }

View file

@ -102,7 +102,6 @@ public class EditProfileFragment extends BaseOsmAndFragment {
private Button saveButtonSV; private Button saveButtonSV;
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -175,7 +174,8 @@ public class EditProfileFragment extends BaseOsmAndFragment {
isDataChanged = false; isDataChanged = false;
} else if (isNew) { } else if (isNew) {
isDataChanged = true; isDataChanged = true;
title = String.format("Custom %s", getResources().getString(profile.parent.getStringResource())); title = String
.format("Custom %s", getResources().getString(profile.parent.getStringResource()));
startIconId = profile.getParent().getSmallIconDark(); startIconId = profile.getParent().getSmallIconDark();
profile.setIconId(startIconId); profile.setIconId(startIconId);
} else if (profile.getKey() != -1) { } else if (profile.getKey() != -1) {
@ -188,9 +188,10 @@ public class EditProfileFragment extends BaseOsmAndFragment {
profile.setUserProfileTitle(title); profile.setUserProfileTitle(title);
if (profile.parent != null) { if (profile.parent != null) {
setupBaseProfileView(profile.parent.getStringKey()); setupBaseProfileView(profile.parent.getStringKey());
} else if (profile.getKey() != -1){ } else if (profile.getKey() != -1) {
baseModeTitle.setText(profile.getKey()); baseModeTitle.setText(profile.getKey());
baseModeIcon.setImageDrawable(app.getUIUtilities().getIcon(profile.getIconId(), R.color.icon_color)); baseModeIcon.setImageDrawable(
app.getUIUtilities().getIcon(profile.getIconId(), R.color.icon_color));
} }
typeSelectionBtn.setOnClickListener(new OnClickListener() { typeSelectionBtn.setOnClickListener(new OnClickListener() {
@Override @Override
@ -203,11 +204,13 @@ public class EditProfileFragment extends BaseOsmAndFragment {
String key = SettingsProfileFragment.getBaseProfiles(getMyApplication()) String key = SettingsProfileFragment.getBaseProfiles(getMyApplication())
.get(pos).getStringKey(); .get(pos).getStringKey();
setupBaseProfileView(key); setupBaseProfileView(key);
profile.parent = ApplicationMode.valueOfStringKey(key, ApplicationMode.DEFAULT); profile.parent = ApplicationMode
.valueOfStringKey(key, ApplicationMode.DEFAULT);
} }
}); });
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putParcelableArrayList(TYPE_APP_PROFILE, SettingsProfileFragment.getBaseProfiles(getMyApplication())); bundle.putParcelableArrayList(TYPE_APP_PROFILE,
SettingsProfileFragment.getBaseProfiles(getMyApplication()));
dialog.setArguments(bundle); dialog.setArguments(bundle);
if (getActivity() != null) { if (getActivity() != null) {
@ -303,10 +306,12 @@ public class EditProfileFragment extends BaseOsmAndFragment {
profileNameEt.addTextChangedListener(new TextWatcher() { profileNameEt.addTextChangedListener(new TextWatcher() {
@Override @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override @Override
public void onTextChanged(CharSequence s, int start, int before, int count) { } public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override @Override
public void afterTextChanged(Editable s) { public void afterTextChanged(Editable s) {
@ -324,7 +329,7 @@ public class EditProfileFragment extends BaseOsmAndFragment {
selectNavTypeBtn.setOnClickListener(new View.OnClickListener() { selectNavTypeBtn.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if(isNew || isUserProfile) { if (isNew || isUserProfile) {
final ProfileBottomSheetDialogFragment fragment = new ProfileBottomSheetDialogFragment(); final ProfileBottomSheetDialogFragment fragment = new ProfileBottomSheetDialogFragment();
fragment.setProfileTypeListener(profileTypeDialogListener); fragment.setProfileTypeListener(profileTypeDialogListener);
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
@ -471,7 +476,6 @@ public class EditProfileFragment extends BaseOsmAndFragment {
} }
@Override @Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
@ -503,9 +507,10 @@ public class EditProfileFragment extends BaseOsmAndFragment {
} }
private void setupBaseProfileView(String stringKey) { private void setupBaseProfileView(String stringKey) {
for(ApplicationMode am : ApplicationMode.getDefaultValues()) { for (ApplicationMode am : ApplicationMode.getDefaultValues()) {
if (am.getStringKey().equals(stringKey)) { if (am.getStringKey().equals(stringKey)) {
baseModeIcon.setImageDrawable(app.getUIUtilities().getIcon(am.getSmallIconDark(), R.color.icon_color)); baseModeIcon.setImageDrawable(
app.getUIUtilities().getIcon(am.getSmallIconDark(), R.color.icon_color));
baseModeTitle.setText(Algorithms.capitalizeFirstLetter(am.toHumanString(app))); baseModeTitle.setText(Algorithms.capitalizeFirstLetter(am.toHumanString(app)));
isDataChanged = false; isDataChanged = false;
} }
@ -542,7 +547,8 @@ public class EditProfileFragment extends BaseOsmAndFragment {
return false; return false;
} }
for (ApplicationMode m : ApplicationMode.allPossibleValues()) { for (ApplicationMode m : ApplicationMode.allPossibleValues()) {
if (m.getUserProfileName() != null && m.getUserProfileName().equals(profile.getUserProfileTitle()) if (m.getUserProfileName() != null && m.getUserProfileName()
.equals(profile.getUserProfileTitle())
&& getActivity() != null) { && getActivity() != null) {
AlertDialog.Builder bld = new AlertDialog.Builder(getActivity()); AlertDialog.Builder bld = new AlertDialog.Builder(getActivity());
bld.setTitle("Duplicate Name"); bld.setTitle("Duplicate Name");
@ -630,13 +636,15 @@ public class EditProfileFragment extends BaseOsmAndFragment {
AlertDialog.Builder bld = new AlertDialog.Builder(getActivity()); AlertDialog.Builder bld = new AlertDialog.Builder(getActivity());
bld.setTitle("Delete Profile"); bld.setTitle("Delete Profile");
bld.setMessage(String bld.setMessage(String
.format("Are you sure you want to delete %s profile", profile.userProfileTitle)); .format("Are you sure you want to delete %s profile",
profile.userProfileTitle));
bld.setPositiveButton(R.string.shared_string_delete, bld.setPositiveButton(R.string.shared_string_delete,
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
ApplicationMode ApplicationMode
.deleteCustomMode(profile.getUserProfileTitle(), getMyApplication()); .deleteCustomMode(profile.getUserProfileTitle(),
getMyApplication());
if (getActivity() != null) { if (getActivity() != null) {
getActivity().onBackPressed(); getActivity().onBackPressed();
} }
@ -645,7 +653,8 @@ public class EditProfileFragment extends BaseOsmAndFragment {
bld.setNegativeButton(R.string.shared_string_dismiss, null); bld.setNegativeButton(R.string.shared_string_dismiss, null);
bld.show(); bld.show();
} else { } else {
Toast.makeText(getActivity(), "You cannot delete OsmAnd base profiles", Toast.LENGTH_SHORT).show(); Toast.makeText(getActivity(), "You cannot delete OsmAnd base profiles",
Toast.LENGTH_SHORT).show();
} }
} }
@ -653,44 +662,44 @@ public class EditProfileFragment extends BaseOsmAndFragment {
private ArrayList<RoutingProfile> getRoutingProfiles() { private ArrayList<RoutingProfile> getRoutingProfiles() {
ArrayList<RoutingProfile> profilesObjects = new ArrayList<>(); ArrayList<RoutingProfile> profilesObjects = new ArrayList<>();
Map<String, GeneralRouter> inputProfiles = getMyApplication().getDefaultRoutingConfig().getAllRoutes(); Map<String, GeneralRouter> inputProfiles = getMyApplication().getDefaultRoutingConfig()
.getAllRoutes();
for (Entry<String, GeneralRouter> e : inputProfiles.entrySet()) { for (Entry<String, GeneralRouter> e : inputProfiles.entrySet()) {
String name; int iconRes = R.drawable.ic_action_world_globe;
String description = ""; String name = e.getValue().getProfileName();
int iconRes; String description = "Custom profile";
switch (e.getKey()) {
case "car": if (e.getValue().getFilename() == null) {
iconRes = R.drawable.ic_action_car_dark; switch (e.getValue().getProfileName()) {
name = getString(R.string.rendering_value_car_name); case "car":
break; iconRes = R.drawable.ic_action_car_dark;
case "pedestrian": name = getString(R.string.rendering_value_car_name);
iconRes = R.drawable.map_action_pedestrian_dark; break;
name = getString(R.string.rendering_value_pedestrian_name); case "pedestrian":
break; iconRes = R.drawable.map_action_pedestrian_dark;
case "bicycle": name = getString(R.string.rendering_value_pedestrian_name);
iconRes = R.drawable.map_action_bicycle_dark; break;
name = getString(R.string.rendering_value_bicycle_name); case "bicycle":
break; iconRes = R.drawable.map_action_bicycle_dark;
case "public_transport": name = getString(R.string.rendering_value_bicycle_name);
iconRes = R.drawable.map_action_bus_dark; break;
name = getString(R.string.app_mode_public_transport); case "public_transport":
break; iconRes = R.drawable.map_action_bus_dark;
case "boat": name = getString(R.string.app_mode_public_transport);
iconRes = R.drawable.map_action_sail_boat_dark; break;
name = getString(R.string.app_mode_boat); case "boat":
break; iconRes = R.drawable.map_action_sail_boat_dark;
case "geocoding": name = getString(R.string.app_mode_boat);
iconRes = R.drawable.ic_action_world_globe; break;
name = "Geocoding"; case "geocoding":
break; iconRes = R.drawable.ic_action_world_globe;
default: name = "Geocoding";
iconRes = R.drawable.ic_action_world_globe; break;
name = Algorithms }
.capitalizeFirstLetterAndLowercase(e.getKey().replace("_", " "));
description = "Custom profile";
break;
} }
profilesObjects.add(new RoutingProfile(e.getKey(), name, description, iconRes, false, e.getValue().getFilename()));
profilesObjects.add(new RoutingProfile(e.getKey(), name, description, iconRes, false,
e.getValue().getFilename()));
} }
return profilesObjects; return profilesObjects;
} }