Fix migration from older versions

This commit is contained in:
Vitaliy 2020-05-04 18:31:26 +03:00
parent c54c142fd2
commit 080f9975ea

View file

@ -259,6 +259,7 @@ public class OsmandSettings {
} }
public void migratePreferences() { public void migratePreferences() {
migrateEnumPreferences();
SharedPreferences globalSharedPreferences = (SharedPreferences) globalPreferences; SharedPreferences globalSharedPreferences = (SharedPreferences) globalPreferences;
Map<String, ?> globalPrefsMap = globalSharedPreferences.getAll(); Map<String, ?> globalPrefsMap = globalSharedPreferences.getAll();
for (String key : globalPrefsMap.keySet()) { for (String key : globalPrefsMap.keySet()) {
@ -317,25 +318,24 @@ public class OsmandSettings {
if (pref instanceof EnumStringPreference) { if (pref instanceof EnumStringPreference) {
EnumStringPreference enumPref = (EnumStringPreference) pref; EnumStringPreference enumPref = (EnumStringPreference) pref;
if (enumPref.isGlobal()) { if (enumPref.isGlobal()) {
migrateEnumPref(enumPref, globalPreferences); migrateEnumPref(enumPref, (SharedPreferences) globalPreferences);
} else { } else {
for (ApplicationMode mode : ApplicationMode.allPossibleValues()) { for (ApplicationMode mode : ApplicationMode.allPossibleValues()) {
migrateEnumPref(enumPref, getProfilePreferences(mode)); migrateEnumPref(enumPref, (SharedPreferences) getProfilePreferences(mode));
} }
} }
} }
} }
} }
private void migrateEnumPref(EnumStringPreference enumPref, Object prefs) { private void migrateEnumPref(EnumStringPreference enumPref, SharedPreferences sharedPreferences) {
try { Object value = sharedPreferences.getAll().get(enumPref.getId());
int enumIndex = settingsAPI.getInt(prefs, enumPref.getId(), -1); if (value instanceof Integer) {
int enumIndex = (int) value;
if (enumIndex >= 0 && enumIndex < enumPref.values.length) { if (enumIndex >= 0 && enumIndex < enumPref.values.length) {
Enum savedValue = enumPref.values[enumIndex]; Enum savedValue = enumPref.values[enumIndex];
enumPref.setValue(prefs, savedValue); enumPref.setValue(sharedPreferences, savedValue);
} }
} catch (ClassCastException ex) {
LOG.error(ex);
} }
} }