From 2593ffea4e1664fd81ce77b865da112ffc46f44f Mon Sep 17 00:00:00 2001 From: Chumva Date: Fri, 8 Nov 2019 18:05:47 +0200 Subject: [PATCH] Fix #7773 --- .../SettingsNavigationActivity.java | 9 ++ .../settings/RouteParametersFragment.java | 131 +++++++++++++++--- .../settings/VehicleParametersFragment.java | 16 ++- .../BooleanPreferenceBottomSheet.java | 6 + .../EditTextPreferenceBottomSheet.java | 6 + .../MultiSelectPreferencesBottomSheet.java | 6 + .../MultiSelectBooleanPreference.java | 8 +- 7 files changed, 158 insertions(+), 24 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/activities/SettingsNavigationActivity.java b/OsmAnd/src/net/osmand/plus/activities/SettingsNavigationActivity.java index 1c39792cf2..be36276183 100644 --- a/OsmAnd/src/net/osmand/plus/activities/SettingsNavigationActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/SettingsNavigationActivity.java @@ -45,6 +45,7 @@ import net.osmand.plus.download.DownloadActivityType; import net.osmand.plus.helpers.FileNameTranslationHelper; import net.osmand.plus.routepreparationmenu.RoutingOptionsHelper; import net.osmand.plus.routing.RouteProvider.RouteService; +import net.osmand.plus.routing.RoutingHelper; import net.osmand.plus.voice.CommandPlayer; import net.osmand.router.GeneralRouter; import net.osmand.router.GeneralRouter.GeneralRouterProfile; @@ -788,6 +789,10 @@ public class SettingsNavigationActivity extends SettingsBaseActivity { settings.MIN_SPEED.set(minValue[0] / ratio[0]); settings.MAX_SPEED.set(maxValue[0] / ratio[0]); } + RoutingHelper routingHelper = app.getRoutingHelper(); + if (mode.equals(routingHelper.getAppMode()) && (routingHelper.isRouteCalculated() || routingHelper.isRouteBeingCalculated())) { + routingHelper.recalculateRouteDueToSettingsChange(); + } } }); builder.setNegativeButton(R.string.shared_string_cancel, null); @@ -799,6 +804,10 @@ public class SettingsNavigationActivity extends SettingsBaseActivity { settings.MIN_SPEED.set(0f); settings.MAX_SPEED.set(0f); } + RoutingHelper routingHelper = app.getRoutingHelper(); + if (mode.equals(routingHelper.getAppMode()) && (routingHelper.isRouteCalculated() || routingHelper.isRouteBeingCalculated())) { + routingHelper.recalculateRouteDueToSettingsChange(); + } } }); diff --git a/OsmAnd/src/net/osmand/plus/settings/RouteParametersFragment.java b/OsmAnd/src/net/osmand/plus/settings/RouteParametersFragment.java index d98864fdd5..b2de0d6a8c 100644 --- a/OsmAnd/src/net/osmand/plus/settings/RouteParametersFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/RouteParametersFragment.java @@ -2,12 +2,14 @@ package net.osmand.plus.settings; import android.graphics.drawable.Drawable; import android.os.Build; +import android.os.Bundle; import android.support.v4.content.ContextCompat; import android.support.v7.preference.Preference; import android.support.v7.preference.PreferenceScreen; import android.support.v7.preference.PreferenceViewHolder; import android.widget.ImageView; +import net.osmand.StateChangedListener; import net.osmand.plus.ApplicationMode; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; @@ -16,10 +18,13 @@ import net.osmand.plus.R; import net.osmand.plus.activities.SettingsBaseActivity; import net.osmand.plus.activities.SettingsNavigationActivity; import net.osmand.plus.routing.RouteProvider; +import net.osmand.plus.routing.RoutingHelper; import net.osmand.plus.settings.preferences.ListPreferenceEx; import net.osmand.plus.settings.preferences.MultiSelectBooleanPreference; import net.osmand.plus.settings.preferences.SwitchPreferenceEx; import net.osmand.router.GeneralRouter; +import net.osmand.router.GeneralRouter.RoutingParameter; +import net.osmand.router.GeneralRouter.RoutingParameterType; import net.osmand.util.Algorithms; import java.util.ArrayList; @@ -31,7 +36,7 @@ import java.util.Set; import static net.osmand.plus.activities.SettingsNavigationActivity.getRouter; import static net.osmand.plus.routepreparationmenu.RoutingOptionsHelper.DRIVING_STYLE; -public class RouteParametersFragment extends BaseSettingsFragment { +public class RouteParametersFragment extends BaseSettingsFragment implements OnPreferenceChanged { public static final String TAG = RouteParametersFragment.class.getSimpleName(); @@ -41,12 +46,32 @@ public class RouteParametersFragment extends BaseSettingsFragment { private static final String ROUTE_PARAMETERS_IMAGE = "route_parameters_image"; private static final String RELIEF_SMOOTHNESS_FACTOR = "relief_smoothness_factor"; - private List avoidParameters = new ArrayList(); - private List preferParameters = new ArrayList(); - private List drivingStyleParameters = new ArrayList(); - private List reliefFactorParameters = new ArrayList(); - private List otherRoutingParameters = new ArrayList(); + private List avoidParameters = new ArrayList(); + private List preferParameters = new ArrayList(); + private List drivingStyleParameters = new ArrayList(); + private List reliefFactorParameters = new ArrayList(); + private List otherRoutingParameters = new ArrayList(); + private StateChangedListener booleanRoutingPrefListener; + private StateChangedListener customRoutingPrefListener; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + booleanRoutingPrefListener = new StateChangedListener() { + @Override + public void stateChanged(Boolean change) { + recalculateRoute(); + } + }; + customRoutingPrefListener = new StateChangedListener() { + @Override + public void stateChanged(String change) { + recalculateRoute(); + } + }; + } @Override protected void setupPreferences() { @@ -118,13 +143,13 @@ public class RouteParametersFragment extends BaseSettingsFragment { GeneralRouter router = getRouter(getMyApplication().getRoutingConfig(), am); clearParameters(); if (router != null) { - Map parameters = router.getParameters(); + Map parameters = router.getParameters(); if (!am.isDerivedRoutingFrom(ApplicationMode.CAR)) { screen.addPreference(fastRoute); } - for (Map.Entry e : parameters.entrySet()) { + for (Map.Entry e : parameters.entrySet()) { String param = e.getKey(); - GeneralRouter.RoutingParameter routingParameter = e.getValue(); + RoutingParameter routingParameter = e.getValue(); if (param.startsWith(AVOID_ROUTING_PARAMETER_PREFIX)) { avoidParameters.add(routingParameter); } else if (param.startsWith(PREFER_ROUTING_PARAMETER_PREFIX)) { @@ -162,11 +187,11 @@ public class RouteParametersFragment extends BaseSettingsFragment { screen.addPreference(reliefFactorRouting); } - for (GeneralRouter.RoutingParameter p : otherRoutingParameters) { + for (RoutingParameter p : otherRoutingParameters) { String title = SettingsBaseActivity.getRoutingStringPropertyName(app, p.getId(), p.getName()); String description = SettingsBaseActivity.getRoutingStringPropertyDescription(app, p.getId(), p.getDescription()); - if (p.getType() == GeneralRouter.RoutingParameterType.BOOLEAN) { + if (p.getType() == RoutingParameterType.BOOLEAN) { OsmandSettings.OsmandPreference pref = settings.getCustomRoutingBooleanProperty(p.getId(), p.getDefaultBoolean()); SwitchPreferenceEx switchPreferenceEx = (SwitchPreferenceEx) createSwitchPreferenceEx(pref.getId(), title, description, R.layout.preference_with_descr_dialog_and_switch); @@ -184,7 +209,7 @@ public class RouteParametersFragment extends BaseSettingsFragment { for (Object o : vls) { svlss[i++] = o.toString(); } - OsmandSettings.OsmandPreference pref = settings.getCustomRoutingProperty(p.getId(), p.getType() == GeneralRouter.RoutingParameterType.NUMERIC ? "0.0" : "-"); + OsmandSettings.OsmandPreference pref = settings.getCustomRoutingProperty(p.getId(), p.getType() == RoutingParameterType.NUMERIC ? "0.0" : "-"); ListPreferenceEx listPreferenceEx = (ListPreferenceEx) createListPreferenceEx(pref.getId(), p.getPossibleValueDescriptions(), svlss, title, R.layout.preference_with_descr); listPreferenceEx.setDescription(description); @@ -198,24 +223,82 @@ public class RouteParametersFragment extends BaseSettingsFragment { } } + @Override + public void onResume() { + super.onResume(); + addRoutingPrefListeners(); + } + + @Override + public void onPause() { + super.onPause(); + removeRoutingPrefListeners(); + } + + @Override + public void onAppModeChanged() { + removeRoutingPrefListeners(); + super.onAppModeChanged(); + addRoutingPrefListeners(); + } + + private void addRoutingPrefListeners() { + settings.FAST_ROUTE_MODE.addListener(booleanRoutingPrefListener); + settings.ENABLE_TIME_CONDITIONAL_ROUTING.addListener(booleanRoutingPrefListener); + + for (RoutingParameter parameter : otherRoutingParameters) { + if (parameter.getType() == RoutingParameterType.BOOLEAN) { + OsmandSettings.CommonPreference pref = settings.getCustomRoutingBooleanProperty(parameter.getId(), parameter.getDefaultBoolean()); + pref.addListener(booleanRoutingPrefListener); + } else { + OsmandSettings.CommonPreference pref = settings.getCustomRoutingProperty(parameter.getId(), parameter.getType() == RoutingParameterType.NUMERIC ? "0.0" : "-"); + pref.addListener(customRoutingPrefListener); + } + } + } + + private void removeRoutingPrefListeners() { + settings.FAST_ROUTE_MODE.removeListener(booleanRoutingPrefListener); + settings.ENABLE_TIME_CONDITIONAL_ROUTING.removeListener(booleanRoutingPrefListener); + + for (RoutingParameter parameter : otherRoutingParameters) { + if (parameter.getType() == RoutingParameterType.BOOLEAN) { + OsmandSettings.CommonPreference pref = settings.getCustomRoutingBooleanProperty(parameter.getId(), parameter.getDefaultBoolean()); + pref.removeListener(booleanRoutingPrefListener); + } else { + OsmandSettings.CommonPreference pref = settings.getCustomRoutingProperty(parameter.getId(), parameter.getType() == RoutingParameterType.NUMERIC ? "0.0" : "-"); + pref.removeListener(customRoutingPrefListener); + } + } + } + @Override public boolean onPreferenceChange(Preference preference, Object newValue) { String key = preference.getKey(); if ((RELIEF_SMOOTHNESS_FACTOR.equals(key) || DRIVING_STYLE.equals(key)) && newValue instanceof String) { + ApplicationMode appMode = getSelectedAppMode(); String selectedParameterId = (String) newValue; - List routingParameters = DRIVING_STYLE.equals(key) ? drivingStyleParameters : reliefFactorParameters; - for (GeneralRouter.RoutingParameter parameter : routingParameters) { - String parameterId = parameter.getId(); - SettingsNavigationActivity.setRoutingParameterSelected(settings, getSelectedAppMode(), parameterId, parameter.getDefaultBoolean(), parameterId.equals(selectedParameterId)); + List routingParameters = DRIVING_STYLE.equals(key) ? drivingStyleParameters : reliefFactorParameters; + for (RoutingParameter p : routingParameters) { + String parameterId = p.getId(); + SettingsNavigationActivity.setRoutingParameterSelected(settings, appMode, parameterId, p.getDefaultBoolean(), parameterId.equals(selectedParameterId)); } + recalculateRoute(); return true; } return super.onPreferenceChange(preference, newValue); } - private ListPreferenceEx createRoutingBooleanListPreference(String groupKey, List routingParameters) { + @Override + public void onPreferenceChanged(String prefId) { + if (AVOID_ROUTING_PARAMETER_PREFIX.equals(prefId) || PREFER_ROUTING_PARAMETER_PREFIX.equals(prefId)) { + recalculateRoute(); + } + } + + private ListPreferenceEx createRoutingBooleanListPreference(String groupKey, List routingParameters) { String defaultTitle = Algorithms.capitalizeFirstLetterAndLowercase(groupKey.replace('_', ' ')); String title = SettingsBaseActivity.getRoutingStringPropertyName(app, groupKey, defaultTitle); ApplicationMode am = settings.getApplicationMode(); @@ -225,7 +308,7 @@ public class RouteParametersFragment extends BaseSettingsFragment { String selectedParameterId = null; for (int i = 0; i < routingParameters.size(); i++) { - GeneralRouter.RoutingParameter parameter = routingParameters.get(i); + RoutingParameter parameter = routingParameters.get(i); entryValues[i] = parameter.getId(); entries[i] = SettingsNavigationActivity.getRoutinParameterTitle(app, parameter); if (SettingsNavigationActivity.isRoutingParameterSelected(settings, am, parameter)) { @@ -241,7 +324,7 @@ public class RouteParametersFragment extends BaseSettingsFragment { return routingListPref; } - private MultiSelectBooleanPreference createRoutingBooleanMultiSelectPref(String groupKey, String title, String descr, List routingParameters) { + private MultiSelectBooleanPreference createRoutingBooleanMultiSelectPref(String groupKey, String title, String descr, List routingParameters) { MultiSelectBooleanPreference multiSelectPref = new MultiSelectBooleanPreference(app); multiSelectPref.setKey(groupKey); multiSelectPref.setTitle(title); @@ -255,7 +338,7 @@ public class RouteParametersFragment extends BaseSettingsFragment { Set enabledPrefsIds = new HashSet<>(); for (int i = 0; i < routingParameters.size(); i++) { - GeneralRouter.RoutingParameter p = routingParameters.get(i); + RoutingParameter p = routingParameters.get(i); BooleanPreference booleanRoutingPref = (BooleanPreference) settings.getCustomRoutingBooleanProperty(p.getId(), p.getDefaultBoolean()); entries[i] = SettingsBaseActivity.getRoutingStringPropertyName(app, p.getId(), p.getName()); @@ -273,6 +356,14 @@ public class RouteParametersFragment extends BaseSettingsFragment { return multiSelectPref; } + private void recalculateRoute() { + RoutingHelper routingHelper = app.getRoutingHelper(); + if (getSelectedAppMode().equals(routingHelper.getAppMode()) + && (routingHelper.isRouteCalculated() || routingHelper.isRouteBeingCalculated())) { + routingHelper.recalculateRouteDueToSettingsChange(); + } + } + private void clearParameters() { avoidParameters.clear(); preferParameters.clear(); diff --git a/OsmAnd/src/net/osmand/plus/settings/VehicleParametersFragment.java b/OsmAnd/src/net/osmand/plus/settings/VehicleParametersFragment.java index fdfb29d7b6..63ae76cae6 100644 --- a/OsmAnd/src/net/osmand/plus/settings/VehicleParametersFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/VehicleParametersFragment.java @@ -9,6 +9,7 @@ import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; import net.osmand.plus.activities.SettingsBaseActivity; import net.osmand.plus.routing.RouteProvider.RouteService; +import net.osmand.plus.routing.RoutingHelper; import net.osmand.plus.settings.preferences.ListPreferenceEx; import net.osmand.router.GeneralRouter; @@ -17,7 +18,7 @@ import java.util.Map; import static net.osmand.plus.activities.SettingsNavigationActivity.getRouter; import static net.osmand.plus.activities.SettingsNavigationActivity.showSeekbarSettingsDialog; -public class VehicleParametersFragment extends BaseSettingsFragment { +public class VehicleParametersFragment extends BaseSettingsFragment implements OnPreferenceChanged { public static final String TAG = VehicleParametersFragment.class.getSimpleName(); @@ -107,6 +108,19 @@ public class VehicleParametersFragment extends BaseSettingsFragment { return super.onPreferenceClick(preference); } + @Override + public void onPreferenceChanged(String prefId) { + recalculateRoute(); + } + + private void recalculateRoute() { + RoutingHelper routingHelper = app.getRoutingHelper(); + if (getSelectedAppMode().equals(routingHelper.getAppMode()) + && (routingHelper.isRouteCalculated() || routingHelper.isRouteBeingCalculated())) { + routingHelper.recalculateRouteDueToSettingsChange(); + } + } + private Drawable getPreferenceIcon(String prefId) { switch (prefId) { case GeneralRouter.DEFAULT_SPEED: diff --git a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/BooleanPreferenceBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/BooleanPreferenceBottomSheet.java index 1e4a9f5b9e..d5a9bcbc68 100644 --- a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/BooleanPreferenceBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/BooleanPreferenceBottomSheet.java @@ -22,6 +22,7 @@ import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton; import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithDescription; import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem; +import net.osmand.plus.settings.OnPreferenceChanged; import net.osmand.plus.settings.preferences.SwitchPreferenceEx; import org.apache.commons.logging.Log; @@ -68,6 +69,11 @@ public class BooleanPreferenceBottomSheet extends BasePreferenceBottomSheet { switchPreference.setChecked(newValue); preferenceBtn[0].setTitle(newValue ? on : off); preferenceBtn[0].setChecked(newValue); + + Fragment target = getTargetFragment(); + if (target instanceof OnPreferenceChanged) { + ((OnPreferenceChanged) target).onPreferenceChanged(switchPreference.getKey()); + } } } }) diff --git a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/EditTextPreferenceBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/EditTextPreferenceBottomSheet.java index 13aa966fcb..8df24170dd 100644 --- a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/EditTextPreferenceBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/EditTextPreferenceBottomSheet.java @@ -11,6 +11,7 @@ import net.osmand.plus.R; import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.LongDescriptionItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem; +import net.osmand.plus.settings.OnPreferenceChanged; import net.osmand.plus.settings.preferences.EditTextPreferenceEx; import net.osmand.util.Algorithms; @@ -71,6 +72,11 @@ public class EditTextPreferenceBottomSheet extends BasePreferenceBottomSheet { String value = editText.getText().toString(); if (editTextPreference.callChangeListener(value)) { editTextPreference.setText(value); + + Fragment target = getTargetFragment(); + if (target instanceof OnPreferenceChanged) { + ((OnPreferenceChanged) target).onPreferenceChanged(editTextPreference.getKey()); + } } } diff --git a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/MultiSelectPreferencesBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/MultiSelectPreferencesBottomSheet.java index b6a92b2fff..106edb7c3f 100644 --- a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/MultiSelectPreferencesBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/MultiSelectPreferencesBottomSheet.java @@ -13,6 +13,7 @@ import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton; import net.osmand.plus.base.bottomsheetmenu.simpleitems.LongDescriptionItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem; +import net.osmand.plus.settings.OnPreferenceChanged; import net.osmand.plus.settings.preferences.MultiSelectBooleanPreference; import net.osmand.util.Algorithms; @@ -119,6 +120,11 @@ public class MultiSelectPreferencesBottomSheet extends BasePreferenceBottomSheet final Set values = enabledPrefs; if (multiSelectBooleanPreference.callChangeListener(values)) { multiSelectBooleanPreference.setValues(values); + + Fragment target = getTargetFragment(); + if (target instanceof OnPreferenceChanged) { + ((OnPreferenceChanged) target).onPreferenceChanged(multiSelectBooleanPreference.getKey()); + } } } prefChanged = false; diff --git a/OsmAnd/src/net/osmand/plus/settings/preferences/MultiSelectBooleanPreference.java b/OsmAnd/src/net/osmand/plus/settings/preferences/MultiSelectBooleanPreference.java index 6328b66e55..6fa605c033 100644 --- a/OsmAnd/src/net/osmand/plus/settings/preferences/MultiSelectBooleanPreference.java +++ b/OsmAnd/src/net/osmand/plus/settings/preferences/MultiSelectBooleanPreference.java @@ -43,10 +43,12 @@ public class MultiSelectBooleanPreference extends MultiSelectListPreference { } public void setValues(Set values) { - getValues().clear(); - getValues().addAll(values); + if (!getValues().equals(values)) { + getValues().clear(); + getValues().addAll(values); - persistBooleanPrefs(); + persistBooleanPrefs(); + } } public String getDescription() {