diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java
index 05c1ac7a9..9807b9cd1 100644
--- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java
+++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java
@@ -26,6 +26,7 @@ import nodomain.freeyourgadget.gadgetbridge.database.ActivityDatabaseHandler;
import nodomain.freeyourgadget.gadgetbridge.database.DBConstants;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceService;
+import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceService;
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
@@ -45,6 +46,9 @@ public class GBApplication extends Application {
private static final Lock dbLock = new ReentrantLock();
private static DeviceService deviceService;
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 LimitedQueue mIDSenderLookup = new LimitedQueue(16);
public static final String ACTION_QUIT
@@ -84,6 +88,10 @@ public class GBApplication extends Application {
// slf4j may be implicitly initialized before we properly configured it.
setupLogging();
+ if (getPrefsFileVersion() != CURRENT_PREFS_VERSION) {
+ migratePrefs(getPrefsFileVersion());
+ }
+
setupExceptionHandler();
// For debugging problems with the logback configuration
// LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
@@ -242,6 +250,41 @@ public class GBApplication extends Application {
return result;
}
+ private int getPrefsFileVersion() {
+ return sharedPrefs.getInt(PREFS_VERSION, 0); //0 is legacy
+ }
+
+ private void migratePrefs(int oldVersion) {
+ 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.remove("mi_user_gender");
+ }
+ if(legacyHeight != null) {
+ editor.putString(ActivityUser.PREF_USER_HEIGHT_CM, legacyHeight);
+ editor.remove("mi_user_height_cm");
+ }
+ if(legacyWeigth != null) {
+ editor.putString(ActivityUser.PREF_USER_WEIGHT_KG, legacyWeigth);
+ editor.remove("mi_user_weight_kg");
+ }
+ if(legacyYOB != null) {
+ editor.putString(ActivityUser.PREF_USER_YEAR_OF_BIRTH, legacyYOB);
+ editor.remove("mi_user_year_of_birth");
+ }
+ editor.putInt(PREFS_VERSION, CURRENT_PREFS_VERSION);
+ editor.commit();
+ break;
+ }
+ }
+
public static LimitedQueue getIDSenderLookup() {
return mIDSenderLookup;
}
diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java
index 7ab5d03f1..2c37c9141 100644
--- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java
+++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java
@@ -13,10 +13,10 @@ import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandPreferencesActivity;
-import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_USER_GENDER;
-import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_USER_HEIGHT_CM;
-import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_USER_WEIGHT_KG;
-import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_USER_YEAR_OF_BIRTH;
+import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_GENDER;
+import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_HEIGHT_CM;
+import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_WEIGHT_KG;
+import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_YEAR_OF_BIRTH;
public class SettingsActivity extends AbstractSettingsActivity {
@Override
diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandConst.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandConst.java
index 200616743..6f3aa77f3 100644
--- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandConst.java
+++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandConst.java
@@ -9,10 +9,6 @@ public final class MiBandConst {
private static final Logger LOG = LoggerFactory.getLogger(MiBandConst.class);
public static final String PREF_USER_ALIAS = "mi_user_alias";
- public static final String PREF_USER_YEAR_OF_BIRTH = "mi_user_year_of_birth";
- public static final String PREF_USER_GENDER = "mi_user_gender";
- public static final String PREF_USER_HEIGHT_CM = "mi_user_height_cm";
- public static final String PREF_USER_WEIGHT_KG = "mi_user_weight_kg";
public static final String PREF_MIBAND_WEARSIDE = "mi_wearside";
public static final String PREF_MIBAND_ADDRESS = "development_miaddr"; // FIXME: should be prefixed mi_
public static final String PREF_MIBAND_ALARMS = "mi_alarms";
diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandCoordinator.java
index 7b3af1b8f..868cda131 100644
--- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandCoordinator.java
+++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandCoordinator.java
@@ -9,8 +9,6 @@ import android.preference.PreferenceManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.util.Calendar;
-
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsActivity;
import nodomain.freeyourgadget.gadgetbridge.devices.AbstractDeviceCoordinator;
@@ -18,6 +16,7 @@ import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate;
+import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
public class MiBandCoordinator extends AbstractDeviceCoordinator {
@@ -107,22 +106,16 @@ public class MiBandCoordinator extends AbstractDeviceCoordinator {
* @throws IllegalArgumentException when the user info can not be created
*/
public static UserInfo getConfiguredUserInfo(String miBandAddress) throws IllegalArgumentException {
+ ActivityUser activityUser = new ActivityUser();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(GBApplication.getContext());
- int userYear = Integer.parseInt(prefs.getString(MiBandConst.PREF_USER_YEAR_OF_BIRTH, "0"));
- int age = 25;
- if (userYear > 1900) {
- age = Calendar.getInstance().get(Calendar.YEAR) - userYear;
- if (age <= 0) {
- age = 25;
- }
- }
+
UserInfo info = UserInfo.create(
miBandAddress,
prefs.getString(MiBandConst.PREF_USER_ALIAS, null),
- ("male".equals(prefs.getString(MiBandConst.PREF_USER_GENDER, null)) ? 1 : 0),
- age,
- Integer.parseInt(prefs.getString(MiBandConst.PREF_USER_HEIGHT_CM, "175")),
- Integer.parseInt(prefs.getString(MiBandConst.PREF_USER_WEIGHT_KG, "70")),
+ activityUser.getActivityUserGender(),
+ activityUser.getActivityUserAge(),
+ activityUser.getActivityUserHeightCm(),
+ activityUser.getActivityUserWeightKg(),
0
);
return info;
diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandPreferencesActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandPreferencesActivity.java
index 6f469e344..3cb8343fc 100644
--- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandPreferencesActivity.java
+++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandPreferencesActivity.java
@@ -20,10 +20,6 @@ import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PR
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MIBAND_RESERVE_ALARM_FOR_CALENDAR;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MIBAND_WEARSIDE;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_USER_ALIAS;
-import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_USER_GENDER;
-import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_USER_HEIGHT_CM;
-import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_USER_WEIGHT_KG;
-import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_USER_YEAR_OF_BIRTH;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.VIBRATION_COUNT;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.VIBRATION_PROFILE;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.getNotificationPrefKey;
diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/UserInfo.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/UserInfo.java
index 48439da7d..9aa098bc8 100644
--- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/UserInfo.java
+++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/UserInfo.java
@@ -1,5 +1,6 @@
package nodomain.freeyourgadget.gadgetbridge.devices.miband;
+import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.DeviceInfo;
import nodomain.freeyourgadget.gadgetbridge.util.CheckSums;
@@ -23,7 +24,7 @@ public class UserInfo {
* @param btAddress the address of the MI Band to connect to.
*/
public static UserInfo getDefault(String btAddress) {
- return new UserInfo(btAddress, "1550050550", 0, 25, 175, 70, 0);
+ return new UserInfo(btAddress, "1550050550", ActivityUser.defaultUserGender, ActivityUser.defaultUserAge, ActivityUser.defaultUserHeightCm, ActivityUser.defaultUserWeightKg, 0);
}
/**
diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/ActivityUser.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/ActivityUser.java
new file mode 100644
index 000000000..46e14ea65
--- /dev/null
+++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/ActivityUser.java
@@ -0,0 +1,78 @@
+package nodomain.freeyourgadget.gadgetbridge.model;
+
+import android.content.SharedPreferences;
+import android.preference.PreferenceManager;
+
+import java.util.Calendar;
+
+import nodomain.freeyourgadget.gadgetbridge.GBApplication;
+
+/**
+ * Class holding the common user information needed by most activity trackers
+ */
+public class ActivityUser {
+
+ private Integer activityUserGender;
+ private Integer activityUserYearOfBirth;
+ private Integer activityUserHeightCm;
+ private Integer activityUserWeightKg;
+
+ public static final int defaultUserGender = 0;
+ public static final int defaultUserYearOfBirth = 0;
+ public static final int defaultUserAge = 0;
+ public static final int defaultUserHeightCm = 175;
+ public static final int defaultUserWeightKg = 70;
+
+ public static final String PREF_USER_YEAR_OF_BIRTH = "activity_user_year_of_birth";
+ public static final String PREF_USER_GENDER = "activity_user_gender";
+ public static final String PREF_USER_HEIGHT_CM = "activity_user_height_cm";
+ public static final String PREF_USER_WEIGHT_KG = "activity_user_weight_kg";
+
+ public int getActivityUserWeightKg() {
+ if(activityUserWeightKg == null) {
+ fetchPreferences();
+ }
+ return activityUserWeightKg;
+ }
+
+ public int getActivityUserGender() {
+ if(activityUserGender == null) {
+ fetchPreferences();
+ }
+ return activityUserGender;
+ }
+
+ public int getActivityUserYearOfBirth() {
+ if(activityUserYearOfBirth == null) {
+ fetchPreferences();
+ }
+ return activityUserYearOfBirth;
+ }
+
+ public int getActivityUserHeightCm() {
+ if(activityUserHeightCm == null) {
+ fetchPreferences();
+ }
+ return activityUserHeightCm;
+ }
+
+ public int getActivityUserAge() {
+ int userYear = getActivityUserYearOfBirth();
+ int age = 25;
+ if (userYear > 1900) {
+ age = Calendar.getInstance().get(Calendar.YEAR) - userYear;
+ if (age <= 0) {
+ age = 25;
+ }
+ }
+ return age;
+ }
+
+ private void fetchPreferences() {
+ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(GBApplication.getContext());
+ activityUserGender = prefs.getInt(PREF_USER_GENDER, 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/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java
index 612ff073b..8276fca86 100644
--- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java
+++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java
@@ -1,7 +1,5 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble;
-import android.content.SharedPreferences;
-import android.preference.PreferenceManager;
import android.util.Base64;
import android.util.Pair;
@@ -14,7 +12,6 @@ import org.slf4j.LoggerFactory;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
-import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
@@ -32,10 +29,10 @@ import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventNotificati
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventScreenshot;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInfo;
-import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst;
import nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebbleColor;
import nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebbleIconID;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceApp;
+import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
import nodomain.freeyourgadget.gadgetbridge.model.NotificationType;
import nodomain.freeyourgadget.gadgetbridge.model.ServiceCommand;
@@ -688,27 +685,16 @@ public class PebbleProtocol extends GBDeviceProtocol {
ByteBuffer buf = ByteBuffer.allocate(9);
buf.order(ByteOrder.LITTLE_ENDIAN);
- SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(GBApplication.getContext());
-
- Integer heightMm = Integer.parseInt(prefs.getString(MiBandConst.PREF_USER_HEIGHT_CM, "175")) * 10;
+ ActivityUser activityUser = new ActivityUser();
+ Integer heightMm = activityUser.getActivityUserHeightCm() * 10;
buf.putShort(heightMm.shortValue());
- Integer weigthDag = Integer.parseInt(prefs.getString(MiBandConst.PREF_USER_WEIGHT_KG, "70")) * 100;
+ Integer weigthDag = activityUser.getActivityUserWeightKg() * 100;
buf.putShort(weigthDag.shortValue());
buf.put((byte)0x01); //activate tracking
buf.put((byte)0x01); //activity Insights
buf.put((byte)0x01); //sleep Insights
- int userYear = Integer.parseInt(prefs.getString(MiBandConst.PREF_USER_YEAR_OF_BIRTH, "0"));
- int age = 25;
- if (userYear > 1900) {
- age = Calendar.getInstance().get(Calendar.YEAR) - userYear;
- if (age <= 0) {
- age = 25;
- }
- }
- buf.put((byte)age);
-
- int gender = ("male".equals(prefs.getString(MiBandConst.PREF_USER_GENDER, null)) ? 1 : 0);
- buf.put((byte)gender);
+ buf.put((byte)activityUser.getActivityUserAge());
+ buf.put((byte)activityUser.getActivityUserGender());
//blob = new byte[]{0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02};
blob = buf.array();
diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml
index c18572139..cc31c187c 100644
--- a/app/src/main/res/values/arrays.xml
+++ b/app/src/main/res/values/arrays.xml
@@ -21,14 +21,14 @@
- - @string/male
- - @string/female
- - @string/other
+ - @string/male
+ - @string/female
+ - @string/other
- - male
- - female
- - other
+ - 1
+ - 0
+ - 2
- @string/left