Fix update modes params

This commit is contained in:
Vitaliy 2020-01-28 11:12:44 +02:00
parent 61e588fca9
commit b602b3180e
6 changed files with 64 additions and 63 deletions

View file

@ -180,6 +180,7 @@ public class AccessibilitySettingsFragment extends BaseSettingsFragment implemen
OsmandMonitoringPlugin plugin = OsmandPlugin.getPlugin(OsmandMonitoringPlugin.class);
if (plugin != null) {
app.getSettings().copyProfilePreferences(appMode, getSelectedAppMode(), plugin.getPreferences());
updateAllSettings();
}
}
@ -188,6 +189,7 @@ public class AccessibilitySettingsFragment extends BaseSettingsFragment implemen
OsmandMonitoringPlugin plugin = OsmandPlugin.getPlugin(OsmandMonitoringPlugin.class);
if (plugin != null) {
app.getSettings().resetProfilePreferences(appMode, plugin.getPreferences());
updateAllSettings();
}
}

View file

@ -89,48 +89,35 @@ public class ApplicationMode {
* DEFAULT("Browse map"), CAR("Car"), BICYCLE("Bicycle"), PEDESTRIAN("Pedestrian"); NAUTICAL("boat"); PUBLIC_TRANSPORT("Public transport"); AIRCRAFT("Aircraft")
*/
public static final ApplicationMode DEFAULT = createBase(R.string.app_mode_default, "default")
.speed(1.5f, 5).arrivalDistance(90)
.locationIcon(LocationIcon.DEFAULT).navigationIcon(NavigationIcon.DEFAULT)
.icon(R.drawable.ic_world_globe_dark, R.drawable.map_world_globe_dark, "ic_world_globe_dark").reg();
public static final ApplicationMode CAR = createBase(R.string.app_mode_car, "car")
.speed(12.5f, 35)
.locationIcon(LocationIcon.CAR).navigationIcon(NavigationIcon.DEFAULT)
.icon(R.drawable.ic_action_car_dark, R.drawable.map_action_car_dark, "ic_action_car_dark")
.setRoutingProfile("car").description(R.string.base_profile_descr_car).reg();
.description(R.string.base_profile_descr_car).reg();
public static final ApplicationMode BICYCLE = createBase(R.string.app_mode_bicycle, "bicycle")
.speed(2.77f, 15).arrivalDistance(60).offRouteDistance(50)
.locationIcon(LocationIcon.BICYCLE).navigationIcon(NavigationIcon.DEFAULT)
.icon(R.drawable.ic_action_bicycle_dark, R.drawable.map_action_bicycle_dark, "ic_action_bicycle_dark")
.setRoutingProfile("bicycle").description(R.string.base_profile_descr_bicycle).reg();
.description(R.string.base_profile_descr_bicycle).reg();
public static final ApplicationMode PEDESTRIAN = createBase(R.string.app_mode_pedestrian, "pedestrian")
.speed(1.11f, 5).arrivalDistance(45).offRouteDistance(20)
.icon(R.drawable.ic_action_pedestrian_dark, R.drawable.map_action_pedestrian_dark, "ic_action_pedestrian_dark")
.setRoutingProfile("pedestrian").description(R.string.base_profile_descr_pedestrian).reg();
.description(R.string.base_profile_descr_pedestrian).reg();
public static final ApplicationMode PUBLIC_TRANSPORT = createBase(R.string.app_mode_public_transport, "public_transport")
.icon(R.drawable.ic_action_bus_dark, R.drawable.map_action_bus_dark, "ic_action_bus_dark")
.setRoutingProfile("public_transport").description(R.string.base_profile_descr_public_transport).reg();
.description(R.string.base_profile_descr_public_transport).reg();
public static final ApplicationMode BOAT = createBase(R.string.app_mode_boat, "boat")
.speed(1.38f, 20)
.locationIcon(LocationIcon.DEFAULT).navigationIcon(NavigationIcon.NAUTICAL)
.icon(R.drawable.ic_action_sail_boat_dark, R.drawable.map_action_sail_boat_dark, "ic_action_sail_boat_dark")
.setRoutingProfile("boat").description(R.string.base_profile_descr_boat).reg();
.description(R.string.base_profile_descr_boat).reg();
public static final ApplicationMode AIRCRAFT = createBase(R.string.app_mode_aircraft, "aircraft")
.speed(40f, 100)
.locationIcon(LocationIcon.CAR).navigationIcon(NavigationIcon.DEFAULT)
.icon(R.drawable.ic_action_aircraft, R.drawable.map_action_aircraft, "ic_action_aircraft").setRouteService(RouteService.STRAIGHT)
.setRoutingProfile("STRAIGHT_LINE_MODE").description(R.string.base_profile_descr_aircraft).reg();
.description(R.string.base_profile_descr_aircraft).reg();
public static final ApplicationMode SKI = createBase(R.string.app_mode_skiing, "ski")
.speed(1.38f, 15).arrivalDistance(60).offRouteDistance(50)
.locationIcon(LocationIcon.BICYCLE).navigationIcon(NavigationIcon.DEFAULT)
.icon(R.drawable.ic_action_skiing, R.drawable.map_action_skiing, "ic_action_skiing")
.setRoutingProfile("ski").description(R.string.base_profile_descr_ski).reg();
.description(R.string.base_profile_descr_ski).reg();
private static class ApplicationModeBean {
@Expose
@ -320,6 +307,20 @@ public class ApplicationMode {
}
}
private static void updateAppModeOrder(OsmandApplication app, ApplicationMode mode) {
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
Type t = new TypeToken<Map<String, Integer>>() {
}.getType();
Map<String, Integer> appModesOrders = gson.fromJson(app.getSettings().APP_MODES_ORDERS.get(), t);
if (!Algorithms.isEmpty(appModesOrders)) {
Integer order = appModesOrders.get(mode.getStringKey());
if (order != null) {
mode.order = order;
}
}
}
private static ApplicationModeBuilder create(ApplicationMode parent, int key, String stringKey) {
ApplicationModeBuilder builder = new ApplicationModeBuilder();
builder.applicationMode = new ApplicationMode(key, stringKey);
@ -642,31 +643,27 @@ public class ApplicationMode {
}
private static void initModesParams(OsmandApplication app) {
OsmandSettings settings = app.getSettings();
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
Type t = new TypeToken<Map<String, Integer>>() {
}.getType();
Map<String, Integer> appModesOrders = gson.fromJson(app.getSettings().APP_MODES_ORDERS.get(), t);
for (ApplicationMode mode : allPossibleValues()) {
mode.routingProfile = settings.ROUTING_PROFILE.getModeValue(mode);
mode.routeService = settings.ROUTE_SERVICE.getModeValue(mode);
mode.defaultSpeed = settings.DEFAULT_SPEED.getModeValue(mode);
mode.minDistanceForTurn = settings.MIN_DISTANCE_FOR_TURN.getModeValue(mode);
mode.arrivalDistance = settings.ARRIVAL_DISTANCE.getModeValue(mode);
mode.offRouteDistance = settings.OFF_ROUTE_DISTANCE.getModeValue(mode);
mode.userProfileName = settings.USER_PROFILE_NAME.getModeValue(mode);
mode.navigationIcon = settings.NAVIGATION_ICON.getModeValue(mode);
mode.locationIcon = settings.LOCATION_ICON.getModeValue(mode);
mode.iconColor = settings.ICON_COLOR.getModeValue(mode);
Integer order = appModesOrders.get(mode.getStringKey());
if (order != null) {
mode.order = order;
}
updateAppModeIcon(app, settings.ICON_RES_NAME.getModeValue(mode), mode);
initModeParams(app, mode);
}
}
public static void initModeParams(OsmandApplication app, ApplicationMode mode) {
OsmandSettings settings = app.getSettings();
mode.routingProfile = settings.ROUTING_PROFILE.getModeValue(mode);
mode.routeService = settings.ROUTE_SERVICE.getModeValue(mode);
mode.defaultSpeed = settings.DEFAULT_SPEED.getModeValue(mode);
mode.minDistanceForTurn = settings.MIN_DISTANCE_FOR_TURN.getModeValue(mode);
mode.arrivalDistance = settings.ARRIVAL_DISTANCE.getModeValue(mode);
mode.offRouteDistance = settings.OFF_ROUTE_DISTANCE.getModeValue(mode);
mode.userProfileName = settings.USER_PROFILE_NAME.getModeValue(mode);
mode.navigationIcon = settings.NAVIGATION_ICON.getModeValue(mode);
mode.locationIcon = settings.LOCATION_ICON.getModeValue(mode);
mode.iconColor = settings.ICON_COLOR.getModeValue(mode);
updateAppModeOrder(app, mode);
updateAppModeIcon(app, settings.ICON_RES_NAME.getModeValue(mode), mode);
}
public static void reorderAppModes() {
Comparator<ApplicationMode> comparator = new Comparator<ApplicationMode>() {
@Override

View file

@ -424,44 +424,30 @@ public class OsmandSettings {
return false;
}
public boolean copyPreferencesFromProfile(ApplicationMode modeFrom, ApplicationMode modeTo) {
return copyProfilePreferences(modeFrom, modeTo, new ArrayList<OsmandPreference>(registeredPreferences.values()));
public void copyPreferencesFromProfile(ApplicationMode modeFrom, ApplicationMode modeTo) {
copyProfilePreferences(modeFrom, modeTo, new ArrayList<OsmandPreference>(registeredPreferences.values()));
}
public boolean copyProfilePreferences(ApplicationMode modeFrom, ApplicationMode modeTo, List<OsmandPreference> profilePreferences) {
SettingsEditor settingsEditor = settingsAPI.edit(getProfilePreferences(modeTo));
public void copyProfilePreferences(ApplicationMode modeFrom, ApplicationMode modeTo, List<OsmandPreference> profilePreferences) {
for (OsmandPreference pref : profilePreferences) {
if (pref instanceof CommonPreference && !((CommonPreference) pref).global) {
CommonPreference profilePref = (CommonPreference) pref;
Object copiedValue = profilePref.getModeValue(modeFrom);
if (copiedValue instanceof String) {
settingsEditor.putString(pref.getId(), (String) copiedValue);
} else if (copiedValue instanceof Boolean) {
settingsEditor.putBoolean(pref.getId(), (Boolean) copiedValue);
} else if (copiedValue instanceof Float) {
settingsEditor.putFloat(pref.getId(), (Float) copiedValue);
} else if (copiedValue instanceof Integer) {
settingsEditor.putInt(pref.getId(), (Integer) copiedValue);
} else if (copiedValue instanceof Long) {
settingsEditor.putLong(pref.getId(), (Long) copiedValue);
}
profilePref.setModeValue(modeTo, copiedValue);
}
}
return settingsEditor.commit();
}
public boolean resetPreferencesForProfile(ApplicationMode mode) {
return settingsAPI.edit(getProfilePreferences(mode)).clear().commit();
}
public boolean resetProfilePreferences(ApplicationMode mode, List<OsmandPreference> profilePreferences) {
SettingsEditor settingsEditor = settingsAPI.edit(getProfilePreferences(mode));
public void resetProfilePreferences(ApplicationMode mode, List<OsmandPreference> profilePreferences) {
for (OsmandPreference pref : profilePreferences) {
if (pref instanceof CommonPreference && !((CommonPreference) pref).global) {
settingsEditor.remove(pref.getId());
pref.resetModeToDefault(mode);
}
}
return settingsEditor.commit();
}
public ApplicationMode LAST_ROUTING_APPLICATION_MODE = null;

View file

@ -475,6 +475,7 @@ public class MultimediaNotesFragment extends BaseSettingsFragment implements Cop
AudioVideoNotesPlugin plugin = OsmandPlugin.getPlugin(AudioVideoNotesPlugin.class);
if (plugin != null) {
app.getSettings().copyProfilePreferences(appMode, getSelectedAppMode(), plugin.getPreferences());
updateAllSettings();
}
}
@ -483,6 +484,7 @@ public class MultimediaNotesFragment extends BaseSettingsFragment implements Cop
AudioVideoNotesPlugin plugin = OsmandPlugin.getPlugin(AudioVideoNotesPlugin.class);
if (plugin != null) {
app.getSettings().resetProfilePreferences(appMode, plugin.getPreferences());
updateAllSettings();
}
}
}

View file

@ -290,6 +290,7 @@ public class MonitoringSettingsFragment extends BaseSettingsFragment implements
OsmandMonitoringPlugin plugin = OsmandPlugin.getPlugin(OsmandMonitoringPlugin.class);
if (plugin != null) {
app.getSettings().copyProfilePreferences(appMode, getSelectedAppMode(), plugin.getPreferences());
updateAllSettings();
}
}
@ -298,6 +299,7 @@ public class MonitoringSettingsFragment extends BaseSettingsFragment implements
OsmandMonitoringPlugin plugin = OsmandPlugin.getPlugin(OsmandMonitoringPlugin.class);
if (plugin != null) {
app.getSettings().resetProfilePreferences(appMode, plugin.getPreferences());
updateAllSettings();
}
}
}

View file

@ -143,13 +143,22 @@ public class ConfigureProfileFragment extends BaseSettingsFragment implements Co
@Override
protected void updateToolbar() {
super.updateToolbar();
updateToolbarSwitch();
View view = getView();
if (view != null) {
updateToolbarSwitch();
TextView toolbarTitle = view.findViewById(R.id.toolbar_title);
toolbarTitle.setText(getSelectedAppMode().toHumanString(getContext()));
}
}
@Override
public void copyAppModePrefs(ApplicationMode appMode) {
if (appMode != null) {
app.getSettings().copyPreferencesFromProfile(appMode, getSelectedAppMode());
ApplicationMode selectedAppMode = getSelectedAppMode();
app.getSettings().copyPreferencesFromProfile(appMode, selectedAppMode);
ApplicationMode.initModeParams(app, selectedAppMode);
updateToolbar();
updateAllSettings();
}
}
@ -157,6 +166,9 @@ public class ConfigureProfileFragment extends BaseSettingsFragment implements Co
public void resetAppModePrefs(ApplicationMode appMode) {
if (appMode != null) {
app.getSettings().resetPreferencesForProfile(appMode);
ApplicationMode.initModeParams(app, appMode);
updateToolbar();
updateAllSettings();
}
}