Fix app mode params migration

This commit is contained in:
Vitaliy 2020-01-27 19:04:05 +02:00
parent 366c6657b7
commit 440411001e
3 changed files with 61 additions and 47 deletions

View file

@ -621,7 +621,7 @@ public class ApplicationMode {
return order; return order;
} }
public void setOrder(OsmandApplication app, int order) { public void setOrder(int order) {
this.order = order; this.order = order;
} }
@ -637,15 +637,17 @@ public class ApplicationMode {
private static void initModesParams(OsmandApplication app) { private static void initModesParams(OsmandApplication app) {
OsmandSettings settings = app.getSettings(); OsmandSettings settings = app.getSettings();
for (ApplicationMode mode : allPossibleValues()) { 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.defaultSpeed = settings.DEFAULT_SPEED.getModeValue(mode);
updateAppModeIcon(app, settings.ICON_RES_NAME.getModeValue(mode), mode);
mode.iconColor = settings.ICON_COLOR.getModeValue(mode);
mode.minDistanceForTurn = settings.MIN_DISTANCE_FOR_TURN.getModeValue(mode); mode.minDistanceForTurn = settings.MIN_DISTANCE_FOR_TURN.getModeValue(mode);
mode.arrivalDistance = settings.ARRIVAL_DISTANCE.getModeValue(mode); mode.arrivalDistance = settings.ARRIVAL_DISTANCE.getModeValue(mode);
mode.offRouteDistance = settings.OFF_ROUTE_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.navigationIcon = settings.NAVIGATION_ICON.getModeValue(mode);
mode.locationIcon = settings.LOCATION_ICON.getModeValue(mode); mode.locationIcon = settings.LOCATION_ICON.getModeValue(mode);
// mode.order = settings.APP_MODE_ORDER.getModeValue(mode); mode.iconColor = settings.ICON_COLOR.getModeValue(mode);
updateAppModeIcon(app, settings.ICON_RES_NAME.getModeValue(mode), mode);
} }
} }
@ -664,7 +666,7 @@ public class ApplicationMode {
private static void updateAppModesOrder(OsmandApplication app) { private static void updateAppModesOrder(OsmandApplication app) {
for (int i = 0; i < values.size(); i++) { for (int i = 0; i < values.size(); i++) {
values.get(i).setOrder(app, i); values.get(i).setOrder(i);
} }
} }
@ -717,7 +719,6 @@ public class ApplicationMode {
settings.USER_PROFILE_NAME.setModeValue(mode, modeBean.userProfileName); settings.USER_PROFILE_NAME.setModeValue(mode, modeBean.userProfileName);
settings.ROUTING_PROFILE.setModeValue(mode, modeBean.routingProfile); settings.ROUTING_PROFILE.setModeValue(mode, modeBean.routingProfile);
settings.ROUTE_SERVICE.setModeValue(mode, modeBean.routeService); settings.ROUTE_SERVICE.setModeValue(mode, modeBean.routeService);
// settings.APP_MODE_ORDER.setModeValue(mode, modeBean.order);
if (modeBean.locIcon != null) { if (modeBean.locIcon != null) {
settings.LOCATION_ICON.setModeValue(mode, modeBean.locIcon); settings.LOCATION_ICON.setModeValue(mode, modeBean.locIcon);
} }
@ -738,50 +739,46 @@ public class ApplicationMode {
Type t = new TypeToken<ArrayList<ApplicationModeBean>>() { Type t = new TypeToken<ArrayList<ApplicationModeBean>>() {
}.getType(); }.getType();
List<ApplicationModeBean> customProfiles = gson.fromJson(app.getSettings().CUSTOM_APP_PROFILES.get(), t); List<ApplicationModeBean> customProfiles = gson.fromJson(app.getSettings().CUSTOM_APP_PROFILES.get(), t);
List<String> customModesKeys = new ArrayList<>();
if (!Algorithms.isEmpty(customProfiles)) { if (!Algorithms.isEmpty(customProfiles)) {
for (ApplicationModeBean m : customProfiles) { for (ApplicationModeBean m : customProfiles) {
customModesKeys.add(m.stringKey);
ApplicationMode parentMode = valueOfStringKey(m.parent, CAR); ApplicationMode parentMode = valueOfStringKey(m.parent, CAR);
createCustomMode(parentMode, m.userProfileName, m.stringKey) ApplicationModeBuilder builder = createCustomMode(parentMode, m.userProfileName, m.stringKey)
.setRouteService(m.routeService) .setRouteService(m.routeService)
.setRoutingProfile(m.routingProfile) .setRoutingProfile(m.routingProfile)
.icon(app, m.iconName) .icon(app, m.iconName)
.setColor(m.iconColor) .setColor(m.iconColor)
.locationIcon(m.locIcon) .locationIcon(m.locIcon)
.navigationIcon(m.navIcon) .navigationIcon(m.navIcon)
.setOrder(m.order) .setOrder(m.order);
.customReg(); saveProfile(builder, app);
} }
} }
settings.CUSTOM_APP_MODES_KEYS.set(gson.toJson(customModesKeys));
settings.CUSTOM_APP_PROFILES.resetToDefault(); settings.CUSTOM_APP_PROFILES.resetToDefault();
} }
} if (settings.CUSTOM_APP_MODES_KEYS.isSet()) {
Set<String> customModesKeys = settings.getCustomAppModesKeys();
private static void saveAppModesToSettings(OsmandSettings settings) { for (String appModeKey : customModesKeys) {
List<ApplicationModeBean> modeBeans = createApplicationModeBeans(getCustomValues()); Object profilePreferences = settings.getProfilePreferences(appModeKey);
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); String parent = settings.PARENT_APP_MODE.getValue(profilePreferences, null);
settings.CUSTOM_APP_PROFILES.set(gson.toJson(modeBeans)); String userProfileName = settings.USER_PROFILE_NAME.getValue(profilePreferences, "");
} createCustomMode(valueOfStringKey(parent, CAR), userProfileName, appModeKey).customReg();
}
private static List<ApplicationModeBean> createApplicationModeBeans(List<ApplicationMode> applicationModes) {
List<ApplicationModeBean> modeBeans = new ArrayList<>();
for (ApplicationMode mode : applicationModes) {
ApplicationModeBean mb = new ApplicationModeBean();
mb.userProfileName = mode.userProfileName;
mb.iconColor = mode.iconColor;
mb.iconName = mode.iconResName;
mb.parent = mode.parentAppMode != null ? mode.parentAppMode.getStringKey() : null;
mb.stringKey = mode.stringKey;
mb.routeService = mode.routeService;
mb.routingProfile = mode.routingProfile;
mb.order = mode.order;
mb.locIcon = mode.locationIcon;
mb.navIcon = mode.navigationIcon;
modeBeans.add(mb);
} }
return modeBeans; }
private static boolean saveCustomAppModesToSettings(OsmandSettings settings) {
StringBuilder stringBuilder = new StringBuilder();
Iterator<ApplicationMode> it = ApplicationMode.getCustomValues().iterator();
while (it.hasNext()) {
stringBuilder.append(it.next().getStringKey());
if (it.hasNext()) {
stringBuilder.append(",");
}
}
if (!stringBuilder.toString().equals(settings.CUSTOM_APP_MODES_KEYS.get())) {
return settings.CUSTOM_APP_MODES_KEYS.set(stringBuilder.toString());
}
return false;
} }
public static ApplicationMode saveProfile(ApplicationModeBuilder builder, OsmandApplication app) { public static ApplicationMode saveProfile(ApplicationModeBuilder builder, OsmandApplication app) {
@ -799,9 +796,9 @@ public class ApplicationMode {
mode.setIconColor(app, builder.applicationMode.iconColor); mode.setIconColor(app, builder.applicationMode.iconColor);
mode.setLocationIcon(app, builder.applicationMode.locationIcon); mode.setLocationIcon(app, builder.applicationMode.locationIcon);
mode.setNavigationIcon(app, builder.applicationMode.navigationIcon); mode.setNavigationIcon(app, builder.applicationMode.navigationIcon);
mode.setOrder(app, builder.applicationMode.order); mode.setOrder(builder.applicationMode.order);
saveAppModesToSettings(app.getSettings()); saveCustomAppModesToSettings(app.getSettings());
return mode; return mode;
} }
@ -818,7 +815,7 @@ public class ApplicationMode {
settings.APPLICATION_MODE.resetToDefault(); settings.APPLICATION_MODE.resetToDefault();
} }
cachedFilteredValues.removeAll(modes); cachedFilteredValues.removeAll(modes);
saveAppModesToSettings(app.getSettings()); saveCustomAppModesToSettings(app.getSettings());
} }
public static boolean changeProfileAvailability(ApplicationMode mode, boolean isSelected, OsmandApplication app) { public static boolean changeProfileAvailability(ApplicationMode mode, boolean isSelected, OsmandApplication app) {

View file

@ -30,9 +30,6 @@ import net.osmand.map.ITileSource;
import net.osmand.map.TileSourceManager; import net.osmand.map.TileSourceManager;
import net.osmand.map.TileSourceManager.TileSourceTemplate; import net.osmand.map.TileSourceManager.TileSourceTemplate;
import net.osmand.osm.io.NetworkUtils; import net.osmand.osm.io.NetworkUtils;
import net.osmand.plus.profiles.LocationIcon;
import net.osmand.plus.profiles.NavigationIcon;
import net.osmand.plus.profiles.ProfileIconColors;
import net.osmand.plus.access.AccessibilityMode; import net.osmand.plus.access.AccessibilityMode;
import net.osmand.plus.access.RelativeDirectionStyle; import net.osmand.plus.access.RelativeDirectionStyle;
import net.osmand.plus.api.SettingsAPI; import net.osmand.plus.api.SettingsAPI;
@ -42,6 +39,9 @@ import net.osmand.plus.dialogs.RateUsBottomSheetDialogFragment;
import net.osmand.plus.helpers.SearchHistoryHelper; import net.osmand.plus.helpers.SearchHistoryHelper;
import net.osmand.plus.mapillary.MapillaryPlugin; import net.osmand.plus.mapillary.MapillaryPlugin;
import net.osmand.plus.mapmarkers.CoordinateInputFormats.Format; import net.osmand.plus.mapmarkers.CoordinateInputFormats.Format;
import net.osmand.plus.profiles.LocationIcon;
import net.osmand.plus.profiles.NavigationIcon;
import net.osmand.plus.profiles.ProfileIconColors;
import net.osmand.plus.render.RendererRegistry; import net.osmand.plus.render.RendererRegistry;
import net.osmand.plus.routing.RouteProvider.RouteService; import net.osmand.plus.routing.RouteProvider.RouteService;
import net.osmand.plus.voice.CommandPlayer; import net.osmand.plus.voice.CommandPlayer;
@ -218,11 +218,16 @@ public class OsmandSettings {
} }
public static String getSharedPreferencesName(ApplicationMode mode) { public static String getSharedPreferencesName(ApplicationMode mode) {
String modeKey = mode != null ? mode.getStringKey() : null;
return getSharedPreferencesNameForKey(modeKey);
}
public static String getSharedPreferencesNameForKey(String modeKey) {
String sharedPreferencesName = !Algorithms.isEmpty(CUSTOM_SHARED_PREFERENCES_NAME) ? CUSTOM_SHARED_PREFERENCES_NAME : SHARED_PREFERENCES_NAME; String sharedPreferencesName = !Algorithms.isEmpty(CUSTOM_SHARED_PREFERENCES_NAME) ? CUSTOM_SHARED_PREFERENCES_NAME : SHARED_PREFERENCES_NAME;
if (mode == null) { if (modeKey == null) {
return sharedPreferencesName; return sharedPreferencesName;
} else { } else {
return sharedPreferencesName + "." + mode.getStringKey().toLowerCase(); return sharedPreferencesName + "." + modeKey.toLowerCase();
} }
} }
@ -301,6 +306,10 @@ public class OsmandSettings {
return settingsAPI.getPreferenceObject(getSharedPreferencesName(mode)); return settingsAPI.getPreferenceObject(getSharedPreferencesName(mode));
} }
public Object getProfilePreferences(String modeKey) {
return settingsAPI.getPreferenceObject(getSharedPreferencesNameForKey(modeKey));
}
public OsmandPreference getPreference(String key) { public OsmandPreference getPreference(String key) {
return registeredPreferences.get(key); return registeredPreferences.get(key);
} }
@ -1560,8 +1569,6 @@ public class OsmandSettings {
LOCATION_ICON.setModeDefaultValue(ApplicationMode.SKI, LocationIcon.BICYCLE); LOCATION_ICON.setModeDefaultValue(ApplicationMode.SKI, LocationIcon.BICYCLE);
} }
// public final CommonPreference<Integer> APP_MODE_ORDER = new IntPreference("app_mode_order", 0).makeProfile().cache();
public final OsmandPreference<Float> SWITCH_MAP_DIRECTION_TO_COMPASS = public final OsmandPreference<Float> SWITCH_MAP_DIRECTION_TO_COMPASS =
new FloatPreference("speed_for_map_to_direction_of_movement", 0f).makeProfile(); new FloatPreference("speed_for_map_to_direction_of_movement", 0f).makeProfile();
@ -3392,6 +3399,16 @@ public class OsmandSettings {
public final CommonPreference<String> CUSTOM_APP_MODES_KEYS = public final CommonPreference<String> CUSTOM_APP_MODES_KEYS =
new StringPreference("custom_app_modes_keys", "").makeGlobal().cache(); new StringPreference("custom_app_modes_keys", "").makeGlobal().cache();
public Set<String> getCustomAppModesKeys() {
String appModesKeys = CUSTOM_APP_MODES_KEYS.get();
StringTokenizer toks = new StringTokenizer(appModesKeys, ",");
Set<String> res = new LinkedHashSet<String>();
while (toks.hasMoreTokens()) {
res.add(toks.nextToken());
}
return res;
}
public enum DayNightMode { public enum DayNightMode {
AUTO(R.string.daynight_mode_auto, R.drawable.ic_action_map_sunset), AUTO(R.string.daynight_mode_auto, R.drawable.ic_action_map_sunset),
DAY(R.string.daynight_mode_day, R.drawable.ic_action_map_day), DAY(R.string.daynight_mode_day, R.drawable.ic_action_map_day),

View file

@ -166,7 +166,7 @@ public class EditProfilesFragment extends BaseOsmAndFragment {
if (order == null) { if (order == null) {
order = mode.getOrder(); order = mode.getOrder();
} }
mode.setOrder(app, order); mode.setOrder(order);
} }
ApplicationMode.reorderAppModes(app); ApplicationMode.reorderAppModes(app);
mapActivity.onBackPressed(); mapActivity.onBackPressed();