From 4da02fa85183e534a929d1909b765af695a1bf3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Rebelo?= Date: Sat, 31 Aug 2024 14:40:39 +0100 Subject: [PATCH] Add month and day to date of birth --- .../gadgetbridge/GBApplication.java | 12 +++++-- .../AboutUserPreferencesActivity.java | 5 ++- .../gadgetbridge/model/ActivityUser.java | 33 ++++++++++--------- .../gbx100/CasioGBX100DeviceSupport.java | 4 +-- .../gbx100/SetConfigurationOperation.java | 9 ++--- .../devices/fitpro/FitProDeviceSupport.java | 2 +- .../fit6900/HamaFit6900DeviceSupport.java | 2 +- .../service/devices/huami/HuamiSupport.java | 12 ++++--- .../devices/huami/zeppos/ZeppOsSupport.java | 8 +++-- .../devices/huawei/HuaweiSupportProvider.java | 2 +- .../requests/SendFitnessUserInfoRequest.java | 13 +++----- .../sony/wena3/SonyWena3DeviceSupport.java | 16 +++++++-- .../settings/BodyPropertiesSetting.java | 8 ++--- .../xiaomi/services/XiaomiHealthService.java | 13 ++++---- .../gadgetbridge/util/Prefs.java | 19 +++++++++++ app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/about_user.xml | 9 ++--- 17 files changed, 103 insertions(+), 65 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java index 7b04a82d7..2ac909614 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java @@ -123,7 +123,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 = 37; + private static final int CURRENT_PREFS_VERSION = 38; private static final LimitedQueue mIDSenderLookup = new LimitedQueue<>(16); private static GBPrefs prefs; @@ -735,7 +735,7 @@ public class GBApplication extends Application { editor.remove("mi_user_weight_kg"); } if (legacyYOB != null) { - editor.putString(ActivityUser.PREF_USER_YEAR_OF_BIRTH, legacyYOB); + editor.putString("activity_user_year_of_birth", legacyYOB); editor.remove("mi_user_year_of_birth"); } } @@ -1726,6 +1726,14 @@ public class GBApplication extends Application { } } + if (oldVersion < 38) { + // Migrate year of birth to date of birth + final String yearOfBirth = sharedPrefs.getString("activity_user_year_of_birth", null); + if (StringUtils.isNotBlank(yearOfBirth)) { + editor.putString("activity_user_date_of_birth", String.format(Locale.ROOT, "%s-01-01", yearOfBirth.trim())); + } + } + editor.putString(PREFS_VERSION, Integer.toString(CURRENT_PREFS_VERSION)); editor.apply(); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AboutUserPreferencesActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AboutUserPreferencesActivity.java index 914a76763..ca6dea58f 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AboutUserPreferencesActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AboutUserPreferencesActivity.java @@ -18,6 +18,7 @@ package nodomain.freeyourgadget.gadgetbridge.activities; import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_ACTIVETIME_MINUTES; import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_CALORIES_BURNT; +import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_DATE_OF_BIRTH; import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_DISTANCE_METERS; import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_GENDER; import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_GOAL_FAT_BURN_TIME_MINUTES; @@ -29,7 +30,6 @@ import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_ import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_STEPS_GOAL; import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_STEP_LENGTH_CM; import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_WEIGHT_KG; -import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_YEAR_OF_BIRTH; import android.content.Intent; import android.os.Bundle; @@ -62,7 +62,7 @@ public class AboutUserPreferencesActivity extends AbstractSettingsActivityV2 { setPreferencesFromResource(R.xml.about_user, rootKey); addPreferenceHandlerFor(PREF_USER_NAME, true, false); - addPreferenceHandlerFor(PREF_USER_YEAR_OF_BIRTH, true, false); + addPreferenceHandlerFor(PREF_USER_DATE_OF_BIRTH, true, false); addPreferenceHandlerFor(PREF_USER_HEIGHT_CM, true, true); addPreferenceHandlerFor(PREF_USER_WEIGHT_KG, true, false); addPreferenceHandlerFor(PREF_USER_GENDER, true, false); @@ -74,7 +74,6 @@ public class AboutUserPreferencesActivity extends AbstractSettingsActivityV2 { addPreferenceHandlerFor(PREF_USER_STEP_LENGTH_CM, false, true); addPreferenceHandlerFor(PREF_USER_DISTANCE_METERS, false, true); - setInputTypeFor(PREF_USER_YEAR_OF_BIRTH, InputType.TYPE_CLASS_NUMBER); setInputTypeFor(PREF_USER_HEIGHT_CM, InputType.TYPE_CLASS_NUMBER); setInputTypeFor(PREF_USER_WEIGHT_KG, InputType.TYPE_CLASS_NUMBER); setInputTypeFor(PREF_USER_STEPS_GOAL, InputType.TYPE_CLASS_NUMBER); 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 e4765119f..e9f218a68 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/ActivityUser.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/ActivityUser.java @@ -17,6 +17,8 @@ along with this program. If not, see . */ package nodomain.freeyourgadget.gadgetbridge.model; +import java.time.LocalDate; +import java.time.Period; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; @@ -36,7 +38,7 @@ public class ActivityUser { private String activityUserName; private int activityUserGender; - private int activityUserYearOfBirth; + private LocalDate activityUserDateOfBirth; private int activityUserHeightCm; private int activityUserWeightKg; private int activityUserSleepDurationGoal; @@ -49,7 +51,7 @@ public class ActivityUser { private static final String defaultUserName = "gadgetbridge-user"; public static final int defaultUserGender = GENDER_FEMALE; - public static final int defaultUserYearOfBirth = 1970; + public static final String defaultUserDateOfBirth = "1970-01-01"; public static final int defaultUserAge = 0; public static final int defaultUserHeightCm = 175; public static final int defaultUserWeightKg = 70; @@ -64,7 +66,7 @@ public class ActivityUser { public static final int defaultUserFatBurnTimeMinutes = 30; public static final String PREF_USER_NAME = "mi_user_alias"; - public static final String PREF_USER_YEAR_OF_BIRTH = "activity_user_year_of_birth"; + public static final String PREF_USER_DATE_OF_BIRTH = "activity_user_date_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"; @@ -99,8 +101,8 @@ public class ActivityUser { return activityUserGender; } - public int getYearOfBirth() { - return activityUserYearOfBirth; + public LocalDate getDateOfBirth() { + return activityUserDateOfBirth; } /** @@ -145,15 +147,7 @@ public class ActivityUser { } public int getAge() { - int userYear = getYearOfBirth(); - int age = 25; - if (userYear > 1900) { - age = Calendar.getInstance().get(Calendar.YEAR) - userYear; - if (age <= 0) { - age = 25; - } - } - return age; + return Period.between(getDateOfBirth(), LocalDate.now()).getYears(); } private void fetchPreferences() { @@ -162,7 +156,7 @@ public class ActivityUser { activityUserGender = prefs.getInt(PREF_USER_GENDER, defaultUserGender); activityUserHeightCm = prefs.getInt(PREF_USER_HEIGHT_CM, defaultUserHeightCm); activityUserWeightKg = prefs.getInt(PREF_USER_WEIGHT_KG, defaultUserWeightKg); - activityUserYearOfBirth = prefs.getInt(PREF_USER_YEAR_OF_BIRTH, defaultUserYearOfBirth); + activityUserDateOfBirth = prefs.getLocalDate(PREF_USER_DATE_OF_BIRTH, defaultUserDateOfBirth); activityUserSleepDurationGoal = prefs.getInt(PREF_USER_SLEEP_DURATION, defaultUserSleepDurationGoal); activityUserStepsGoal = prefs.getInt(PREF_USER_STEPS_GOAL, defaultUserStepsGoal); activityUserCaloriesBurntGoal = prefs.getInt(PREF_USER_CALORIES_BURNT, defaultUserCaloriesBurntGoal); @@ -172,9 +166,16 @@ public class ActivityUser { activityUserStepLengthCm = prefs.getInt(PREF_USER_STEP_LENGTH_CM, defaultUserStepLengthCm); } + /** + * @deprecated use {@link #getDateOfBirth()}. + */ + @Deprecated public Date getUserBirthday() { + final LocalDate dateOfBirth = getDateOfBirth(); Calendar cal = DateTimeUtils.getCalendarUTC(); - cal.set(GregorianCalendar.YEAR, getYearOfBirth()); + cal.set(GregorianCalendar.YEAR, dateOfBirth.getYear()); + cal.set(GregorianCalendar.MONTH, dateOfBirth.getMonthValue() - 1); + cal.set(GregorianCalendar.DAY_OF_MONTH, dateOfBirth.getDayOfMonth()); return cal.getTime(); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/casio/gbx100/CasioGBX100DeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/casio/gbx100/CasioGBX100DeviceSupport.java index 31efb77b4..0626da017 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/casio/gbx100/CasioGBX100DeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/casio/gbx100/CasioGBX100DeviceSupport.java @@ -69,12 +69,12 @@ import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.Dev import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_PREVIEW_MESSAGE_IN_TITLE; import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_TIMEFORMAT; import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_ACTIVETIME_MINUTES; +import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_DATE_OF_BIRTH; import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_DISTANCE_METERS; 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_STEPS_GOAL; 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 CasioGBX100DeviceSupport extends Casio2C2DSupport implements SharedPreferences.OnSharedPreferenceChangeListener { private static final Logger LOG = LoggerFactory.getLogger(CasioGBX100DeviceSupport.class); @@ -676,7 +676,7 @@ public class CasioGBX100DeviceSupport extends Casio2C2DSupport implements Shared case PREF_USER_WEIGHT_KG: new SetConfigurationOperation(this, CasioConstants.ConfigurationOption.OPTION_WEIGHT).perform(); break; - case PREF_USER_YEAR_OF_BIRTH: + case PREF_USER_DATE_OF_BIRTH: new SetConfigurationOperation(this, CasioConstants.ConfigurationOption.OPTION_BIRTHDAY).perform(); break; case PREF_TIMEFORMAT: diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/casio/gbx100/SetConfigurationOperation.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/casio/gbx100/SetConfigurationOperation.java index 3dd4650e8..02e3c3f95 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/casio/gbx100/SetConfigurationOperation.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/casio/gbx100/SetConfigurationOperation.java @@ -24,6 +24,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; +import java.time.LocalDate; import java.util.Arrays; import java.util.UUID; @@ -120,10 +121,10 @@ public class SetConfigurationOperation extends AbstractBTLEOperationWaiting for reconnect About you Goals + Date of birth Year of birth Gender Height in cm diff --git a/app/src/main/res/xml/about_user.xml b/app/src/main/res/xml/about_user.xml index 452f9d07b..08d3f626a 100644 --- a/app/src/main/res/xml/about_user.xml +++ b/app/src/main/res/xml/about_user.xml @@ -13,13 +13,10 @@ app:iconSpaceReserved="false" app:useSimpleSummaryProvider="true" /> - + android:key="activity_user_date_of_birth" + android:title="@string/activity_prefs_date_birth" />