Use strings to store activity shared preferences.

System has trouble with accessing integer in the preferences, so let's not use them.
This commit is contained in:
danielegobbetti 2016-02-21 13:04:32 +01:00
parent c436c4c055
commit b858e50804
4 changed files with 21 additions and 7 deletions

View File

@ -6,6 +6,7 @@
* Pebble: Allow to select the preferred activity tracker via settings activity (Health, Misfit, Morpheuz) * Pebble: Allow to select the preferred activity tracker via settings activity (Health, Misfit, Morpheuz)
* Pebble: Fix wrong(previous) contact being displayed on the pebble * Pebble: Fix wrong(previous) contact being displayed on the pebble
* Mi Band: improvements to pairing * Mi Band: improvements to pairing
* Fix a problem related to shared preferences storage of activity settings.
####Version 0.7.4 ####Version 0.7.4
* Refactored the settings activity: User details are now generic instead of miband specific. Old settings are preserved. * Refactored the settings activity: User details are now generic instead of miband specific. Old settings are preserved.

View File

@ -48,7 +48,7 @@ public class GBApplication extends Application {
private static SharedPreferences sharedPrefs; private static SharedPreferences sharedPrefs;
private static final String PREFS_VERSION = "shared_preferences_version"; 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 //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); private static LimitedQueue mIDSenderLookup = new LimitedQueue(16);
public static final String ACTION_QUIT public static final String ACTION_QUIT
@ -251,20 +251,25 @@ public class GBApplication extends Application {
} }
private int getPrefsFileVersion() { 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) { private void migratePrefs(int oldVersion) {
SharedPreferences.Editor editor = sharedPrefs.edit();
switch (oldVersion) { switch (oldVersion) {
case 0: case 0:
SharedPreferences.Editor editor = sharedPrefs.edit();
String legacyGender = sharedPrefs.getString("mi_user_gender", null); String legacyGender = sharedPrefs.getString("mi_user_gender", null);
String legacyHeight = sharedPrefs.getString("mi_user_height_cm", null); String legacyHeight = sharedPrefs.getString("mi_user_height_cm", null);
String legacyWeigth = sharedPrefs.getString("mi_user_weight_kg", null); String legacyWeigth = sharedPrefs.getString("mi_user_weight_kg", null);
String legacyYOB = sharedPrefs.getString("mi_user_year_of_birth",null); String legacyYOB = sharedPrefs.getString("mi_user_year_of_birth",null);
if(legacyGender != null) { if(legacyGender != null) {
int gender = "male".equals(legacyGender) ? 1 : "female".equals(legacyGender) ? 0 : 2; 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"); editor.remove("mi_user_gender");
} }
if(legacyHeight != null) { if(legacyHeight != null) {
@ -279,10 +284,18 @@ public class GBApplication extends Application {
editor.putString(ActivityUser.PREF_USER_YEAR_OF_BIRTH, legacyYOB); editor.putString(ActivityUser.PREF_USER_YEAR_OF_BIRTH, legacyYOB);
editor.remove("mi_user_year_of_birth"); 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(); editor.commit();
break; 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() { public static LimitedQueue getIDSenderLookup() {

View File

@ -70,7 +70,7 @@ public class ActivityUser {
private void fetchPreferences() { private void fetchPreferences() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(GBApplication.getContext()); 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))); activityUserHeightCm = Integer.parseInt(prefs.getString(PREF_USER_HEIGHT_CM, Integer.toString(defaultUserHeightCm)));
activityUserWeightKg = Integer.parseInt(prefs.getString(PREF_USER_WEIGHT_KG, Integer.toString(defaultUserWeightKg))); 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))); activityUserYearOfBirth = Integer.parseInt(prefs.getString(PREF_USER_YEAR_OF_BIRTH, Integer.toString(defaultUserYearOfBirth)));

View File

@ -135,7 +135,7 @@
android:title="@string/activity_prefs_year_birth" /> android:title="@string/activity_prefs_year_birth" />
<ListPreference <ListPreference
android:defaultValue="male" android:defaultValue="2"
android:entries="@array/gender" android:entries="@array/gender"
android:entryValues="@array/gender_values" android:entryValues="@array/gender_values"
android:key="activity_user_gender" android:key="activity_user_gender"