1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-08 19:27:04 +01:00

Add month and day to date of birth

This commit is contained in:
José Rebelo 2024-08-31 14:40:39 +01:00
parent b59ba76803
commit 4da02fa851
17 changed files with 103 additions and 65 deletions

View File

@ -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<Integer, String> 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();
}

View File

@ -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);

View File

@ -17,6 +17,8 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. */
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();
}

View File

@ -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:

View File

@ -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 AbstractBTLEOperation<CasioGBX10
}
}
if(option == CasioConstants.ConfigurationOption.OPTION_BIRTHDAY || all) {
int year = user.getYearOfBirth();
// Month and Day are not configured in Gadgetbridge!
int month = 1;
int day = 1;
LocalDate dateOfBirth = user.getDateOfBirth();
int year = dateOfBirth.getYear();
int month = dateOfBirth.getMonthValue();
int day = dateOfBirth.getDayOfMonth();
data[6] = BcdUtil.toBcd8(year % 100);
data[7] = BcdUtil.toBcd8((year - (year % 100)) / 100);
data[8] = BcdUtil.toBcd8(month);

View File

@ -491,7 +491,7 @@ public class FitProDeviceSupport extends AbstractBTLEDeviceSupport {
case ActivityUser.PREF_USER_WEIGHT_KG:
case ActivityUser.PREF_USER_GENDER:
case ActivityUser.PREF_USER_HEIGHT_CM:
case ActivityUser.PREF_USER_YEAR_OF_BIRTH:
case ActivityUser.PREF_USER_DATE_OF_BIRTH:
setUserData(builder);
break;
case ActivityUser.PREF_USER_STEPS_GOAL:

View File

@ -123,7 +123,7 @@ public final class HamaFit6900DeviceSupport extends AbstractBTLEDeviceSupport {
case ActivityUser.PREF_USER_WEIGHT_KG:
case ActivityUser.PREF_USER_GENDER:
case ActivityUser.PREF_USER_HEIGHT_CM:
case ActivityUser.PREF_USER_YEAR_OF_BIRTH:
case ActivityUser.PREF_USER_DATE_OF_BIRTH:
case DeviceSettingsPreferenceConst.PREF_USER_FITNESS_GOAL:
sendMessage("update-user-info", makeSetUserInfoMessage());
return;

View File

@ -48,6 +48,7 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.zone.ZoneOffsetTransition;
import java.time.zone.ZoneRules;
@ -285,11 +286,11 @@ import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.VI
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.VIBRATION_PROFILE;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.getNotificationPrefIntValue;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.getNotificationPrefStringValue;
import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_DATE_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_NAME;
import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_WEIGHT_KG;
import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_YEAR_OF_BIRTH;
import static nodomain.freeyourgadget.gadgetbridge.service.btle.GattCharacteristic.UUID_CHARACTERISTIC_ALERT_LEVEL;
public abstract class HuamiSupport extends AbstractBTLEDeviceSupport implements Huami2021Handler {
@ -646,9 +647,10 @@ public abstract class HuamiSupport extends AbstractBTLEDeviceSupport implements
ActivityUser activityUser = new ActivityUser();
int height = activityUser.getHeightCm();
int weight = activityUser.getWeightKg();
int birth_year = activityUser.getYearOfBirth();
byte birth_month = 7; // not in user attributes
byte birth_day = 1; // not in user attributes
LocalDate dateOfBirth = activityUser.getDateOfBirth();
int birth_year = dateOfBirth.getYear();
byte birth_month = (byte) dateOfBirth.getMonthValue();
byte birth_day = (byte) dateOfBirth.getDayOfMonth();
if (alias == null || weight == 0 || height == 0 || birth_year == 0) {
LOG.warn("Unable to set user info, make sure it is set up");
@ -2914,8 +2916,8 @@ public abstract class HuamiSupport extends AbstractBTLEDeviceSupport implements
case PREF_SOUNDS:
setBeepSounds(builder);
break;
case PREF_USER_DATE_OF_BIRTH:
case PREF_USER_NAME:
case PREF_USER_YEAR_OF_BIRTH:
case PREF_USER_WEIGHT_KG:
case PREF_USER_HEIGHT_CM:
case PREF_USER_GENDER:

View File

@ -61,6 +61,7 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
@ -458,9 +459,10 @@ public class ZeppOsSupport extends HuamiSupport implements ZeppOsFileTransferSer
final ActivityUser activityUser = new ActivityUser();
final int height = activityUser.getHeightCm();
final int weight = activityUser.getWeightKg();
final int birthYear = activityUser.getYearOfBirth();
final byte birthMonth = 7; // not in user attributes
final byte birthDay = 1; // not in user attributes
final LocalDate dateOfBirth = activityUser.getDateOfBirth();
final int birthYear = dateOfBirth.getYear();
final byte birthMonth = (byte) dateOfBirth.getMonthValue();
final byte birthDay = (byte) dateOfBirth.getDayOfMonth();
final String region = devicePrefs.getString(DeviceSettingsPreferenceConst.PREF_DEVICE_REGION, "unknown");
if (alias == null || weight == 0 || height == 0 || birthYear == 0) {

View File

@ -1071,7 +1071,7 @@ public class HuaweiSupportProvider {
case ActivityUser.PREF_USER_WEIGHT_KG:
case ActivityUser.PREF_USER_HEIGHT_CM:
case ActivityUser.PREF_USER_GENDER:
case ActivityUser.PREF_USER_YEAR_OF_BIRTH:
case ActivityUser.PREF_USER_DATE_OF_BIRTH:
sendUserInfo();
break;
}

View File

@ -19,8 +19,7 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Calendar;
import java.util.Date;
import java.time.LocalDate;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiPacket;
@ -50,12 +49,10 @@ public class SendFitnessUserInfoRequest extends Request {
gender = 1;
}
Date birthday = activityUser.getUserBirthday();
Calendar cal = Calendar.getInstance();
cal.setTime(birthday);
int birthdayEncoded = cal.get(Calendar.YEAR) << 16;
birthdayEncoded += (cal.get(Calendar.MONTH)+1) << 8;
birthdayEncoded += cal.get(Calendar.DAY_OF_MONTH);
LocalDate dateOfBirth = activityUser.getDateOfBirth();
int birthdayEncoded = dateOfBirth.getYear() << 16;
birthdayEncoded += dateOfBirth.getMonthValue() << 8;
birthdayEncoded += dateOfBirth.getDayOfMonth();
return new UserInfo.Request(paramsProvider,
activityUser.getHeightCm(),

View File

@ -31,6 +31,7 @@ import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
@ -843,7 +844,8 @@ public class SonyWena3DeviceSupport extends AbstractBTLEDeviceSupport {
private void sendActivityGoalSettings(TransactionBuilder b) {
Prefs prefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress()));
ActivityUser user = new ActivityUser();
if(user.getYearOfBirth() < 1920) {
LocalDate dateOfBirth = user.getDateOfBirth();
if(dateOfBirth.getYear() < 1920) {
LOG.error("Device does not support this year of birth");
return;
}
@ -851,8 +853,16 @@ public class SonyWena3DeviceSupport extends AbstractBTLEDeviceSupport {
GenderSetting gender = user.getGender() == ActivityUser.GENDER_FEMALE ? GenderSetting.FEMALE : GenderSetting.MALE;
// Maybe we need to set the full birth date?
BodyPropertiesSetting bodyPropertiesSetting = new BodyPropertiesSetting(gender, (short)user.getYearOfBirth(), (short)0, (short)1, (short)user.getHeightCm(), (short)user.getWeightKg());
BodyPropertiesSetting bodyPropertiesSetting = new BodyPropertiesSetting(
gender,
(short)dateOfBirth.getYear(),
(short)dateOfBirth.getMonthValue(),
(short)dateOfBirth.getDayOfMonth(),
(short)user.getHeightCm(),
(short)user.getWeightKg()
);
GoalStepsSetting stepsSetting = new GoalStepsSetting(stepsNotification, user.getStepsGoal());
b.write(
@ -1034,7 +1044,7 @@ public class SonyWena3DeviceSupport extends AbstractBTLEDeviceSupport {
sendHomeScreenSettings(builder);
break;
case ActivityUser.PREF_USER_YEAR_OF_BIRTH:
case ActivityUser.PREF_USER_DATE_OF_BIRTH:
case ActivityUser.PREF_USER_GENDER:
case ActivityUser.PREF_USER_HEIGHT_CM:
case ActivityUser.PREF_USER_WEIGHT_KG:

View File

@ -25,15 +25,15 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.sony.wena3.protocol.
public class BodyPropertiesSetting implements Wena3Packetable {
public final GenderSetting gender;
public final short yearOfBirth;
public final short monthOfBirthZeroIndexed;
public final short monthOfBirth;
public final short dayOfBirth;
public final short height;
public final short weight;
public BodyPropertiesSetting(GenderSetting gender, short yearOfBirth, short monthOfBirthZeroIndexed, short dayOfBirth, short height, short weight) {
public BodyPropertiesSetting(GenderSetting gender, short yearOfBirth, short monthOfBirth, short dayOfBirth, short height, short weight) {
this.gender = gender;
this.yearOfBirth = yearOfBirth;
this.monthOfBirthZeroIndexed = monthOfBirthZeroIndexed;
this.monthOfBirth = monthOfBirth;
this.dayOfBirth = dayOfBirth;
this.height = height;
this.weight = weight;
@ -46,7 +46,7 @@ public class BodyPropertiesSetting implements Wena3Packetable {
.put((byte) 0x1D)
.put((byte) gender.ordinal())
.putShort(yearOfBirth)
.put((byte) (monthOfBirthZeroIndexed + 1)) // Java uses 0-indexed months
.put((byte) monthOfBirth)
.put((byte) dayOfBirth)
.putShort((short) (height * 10))
.putShort((short) (weight * 10))

View File

@ -29,6 +29,7 @@ import org.slf4j.LoggerFactory;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
@ -209,7 +210,7 @@ public class XiaomiHealthService extends AbstractXiaomiService {
switch (config) {
case ActivityUser.PREF_USER_HEIGHT_CM:
case ActivityUser.PREF_USER_WEIGHT_KG:
case ActivityUser.PREF_USER_YEAR_OF_BIRTH:
case ActivityUser.PREF_USER_DATE_OF_BIRTH:
case ActivityUser.PREF_USER_GENDER:
case ActivityUser.PREF_USER_CALORIES_BURNT:
case ActivityUser.PREF_USER_STEPS_GOAL:
@ -260,14 +261,14 @@ public class XiaomiHealthService extends AbstractXiaomiService {
LOG.debug("Setting user info");
final ActivityUser activityUser = new ActivityUser();
final int birthYear = activityUser.getYearOfBirth();
final byte birthMonth = 7; // not in user attributes
final byte birthDay = 1; // not in user attributes
final LocalDate dateOfBirth = activityUser.getDateOfBirth();
final int birthYear = dateOfBirth.getYear();
final byte birthMonth = (byte) dateOfBirth.getMonthValue();
final byte birthDay = (byte) dateOfBirth.getDayOfMonth();
final int genderInt = activityUser.getGender() != ActivityUser.GENDER_FEMALE ? GENDER_MALE : GENDER_FEMALE; // TODO other gender?
final Calendar now = GregorianCalendar.getInstance();
final int age = now.get(Calendar.YEAR) - birthYear;
final int age = activityUser.getAge();
// Compute the approximate max heart rate from the user age
// TODO max heart rate should be input by the user
int maxHeartRate = (int) Math.round(age <= 40 ? 220 - age : 207 - 0.7 * age);

View File

@ -21,6 +21,7 @@ import android.util.Log;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Calendar;
@ -212,6 +213,24 @@ public class Prefs {
return LocalTime.now();
}
public LocalDate getLocalDate(final String key, final String defaultValue) {
final String time = getString(key, defaultValue);
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.ROOT);
try {
final Date parse = df.parse(time);
final Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(parse);
return LocalDate.of(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
} catch (final Exception e) {
Log.e(TAG, "Error reading localdate preference value: " + key + "; returning default current day", e); // log the first exception
}
return LocalDate.now();
}
private void logReadError(String key, Exception ex) {
Log.e(TAG, "Error reading preference value: " + key + "; returning default value", ex); // log the first exception
}

View File

@ -1160,6 +1160,7 @@
<string name="waiting_for_reconnect">Waiting for reconnect</string>
<string name="activity_prefs_about_you">About you</string>
<string name="activity_prefs_goals">Goals</string>
<string name="activity_prefs_date_birth">Date of birth</string>
<string name="activity_prefs_year_birth">Year of birth</string>
<string name="activity_prefs_gender">Gender</string>
<string name="activity_prefs_height_cm">Height in cm</string>

View File

@ -13,13 +13,10 @@
app:iconSpaceReserved="false"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
<nodomain.freeyourgadget.gadgetbridge.util.XDatePreference
app:iconSpaceReserved="false"
android:inputType="number"
android:key="activity_user_year_of_birth"
android:maxLength="4"
android:title="@string/activity_prefs_year_birth"
app:useSimpleSummaryProvider="true" />
android:key="activity_user_date_of_birth"
android:title="@string/activity_prefs_date_birth" />
<ListPreference
app:iconSpaceReserved="false"