diff --git a/CHANGELOG.md b/CHANGELOG.md index e05f4495..dfadb833 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * Pebble: Allow to select the preferred activity tracker via settings activity (Health, Misfit, Morpheuz) * Pebble: Fix wrong(previous) contact being displayed on the pebble * Mi Band: improvements to pairing +* Fix a problem related to shared preferences storage of activity settings. ####Version 0.7.4 * Refactored the settings activity: User details are now generic instead of miband specific. Old settings are preserved. diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java index 9807b9cd..ee33a7de 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java @@ -48,7 +48,7 @@ public class GBApplication extends Application { private static SharedPreferences sharedPrefs; private static final String PREFS_VERSION = "shared_preferences_version"; //if preferences have to be migrated, increment the following and add the migration logic in migratePrefs below; see http://stackoverflow.com/questions/16397848/how-can-i-migrate-android-preferences-with-a-new-version - private static final int CURRENT_PREFS_VERSION = 1; + private static final int CURRENT_PREFS_VERSION = 2; private static LimitedQueue mIDSenderLookup = new LimitedQueue(16); public static final String ACTION_QUIT @@ -251,20 +251,25 @@ public class GBApplication extends Application { } private int getPrefsFileVersion() { - return sharedPrefs.getInt(PREFS_VERSION, 0); //0 is legacy + try { + return Integer.parseInt(sharedPrefs.getString(PREFS_VERSION, "0")); //0 is legacy + } catch (Exception e) { + //in version 1 this was an int + return 1; + } } private void migratePrefs(int oldVersion) { + SharedPreferences.Editor editor = sharedPrefs.edit(); switch (oldVersion) { case 0: - SharedPreferences.Editor editor = sharedPrefs.edit(); String legacyGender = sharedPrefs.getString("mi_user_gender", null); String legacyHeight = sharedPrefs.getString("mi_user_height_cm", null); String legacyWeigth = sharedPrefs.getString("mi_user_weight_kg", null); String legacyYOB = sharedPrefs.getString("mi_user_year_of_birth",null); if(legacyGender != null) { int gender = "male".equals(legacyGender) ? 1 : "female".equals(legacyGender) ? 0 : 2; - editor.putInt(ActivityUser.PREF_USER_GENDER, gender); + editor.putString(ActivityUser.PREF_USER_GENDER, Integer.toString(gender)); editor.remove("mi_user_gender"); } if(legacyHeight != null) { @@ -279,10 +284,18 @@ public class GBApplication extends Application { editor.putString(ActivityUser.PREF_USER_YEAR_OF_BIRTH, legacyYOB); editor.remove("mi_user_year_of_birth"); } - editor.putInt(PREFS_VERSION, CURRENT_PREFS_VERSION); + editor.putString(PREFS_VERSION, Integer.toString(CURRENT_PREFS_VERSION)); editor.commit(); break; + case 1: + Integer legacyGender_1 = sharedPrefs.getInt(ActivityUser.PREF_USER_GENDER, 2); + if(legacyGender_1 != null) { + editor.putString(ActivityUser.PREF_USER_GENDER, Integer.toString(legacyGender_1)); + } + editor.putString(PREFS_VERSION, Integer.toString(CURRENT_PREFS_VERSION)); + break; } + editor.commit(); } public static LimitedQueue getIDSenderLookup() { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/ActivityUser.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/ActivityUser.java index 46e14ea6..f2c87c49 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/ActivityUser.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/ActivityUser.java @@ -70,7 +70,7 @@ public class ActivityUser { private void fetchPreferences() { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(GBApplication.getContext()); - activityUserGender = prefs.getInt(PREF_USER_GENDER, defaultUserGender); + activityUserGender = Integer.parseInt(prefs.getString(PREF_USER_GENDER, Integer.toString(defaultUserGender))); activityUserHeightCm = Integer.parseInt(prefs.getString(PREF_USER_HEIGHT_CM, Integer.toString(defaultUserHeightCm))); activityUserWeightKg = Integer.parseInt(prefs.getString(PREF_USER_WEIGHT_KG, Integer.toString(defaultUserWeightKg))); activityUserYearOfBirth = Integer.parseInt(prefs.getString(PREF_USER_YEAR_OF_BIRTH, Integer.toString(defaultUserYearOfBirth))); diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 53a526ee..cb2a8a6f 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -135,7 +135,7 @@ android:title="@string/activity_prefs_year_birth" />