From 348f459e3fa118452eec7a1e538115fc581ced69 Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Thu, 27 May 2021 23:11:00 +0200 Subject: [PATCH] Huami: Send "about you" preferences to device immediately when changing values Closes #2297 --- .../activities/AboutUserPreferencesActivity.java | 10 ++++++++++ .../activities/AbstractSettingsActivity.java | 15 +++++++++++++++ .../gadgetbridge/devices/miband/MiBandConst.java | 2 -- .../devices/miband/MiBandCoordinator.java | 7 +++++-- .../devices/miband/MiBandPreferencesActivity.java | 4 ++-- .../devices/zetime/ZeTimePreferenceActivity.java | 11 ----------- .../service/devices/huami/HuamiSupport.java | 14 +++++++++++++- 7 files changed, 45 insertions(+), 18 deletions(-) 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 069203e39..f6547299b 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AboutUserPreferencesActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AboutUserPreferencesActivity.java @@ -19,12 +19,17 @@ package nodomain.freeyourgadget.gadgetbridge.activities; import android.os.Bundle; +import androidx.preference.Preference; + +import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; 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_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_NAME; import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_SLEEP_DURATION; import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_STEPS_GOAL; import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_STEP_LENGTH_CM; @@ -36,6 +41,11 @@ public class AboutUserPreferencesActivity extends AbstractSettingsActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.about_user); + addPreferenceHandlerFor(PREF_USER_NAME); + addPreferenceHandlerFor(PREF_USER_YEAR_OF_BIRTH); + addPreferenceHandlerFor(PREF_USER_HEIGHT_CM); + addPreferenceHandlerFor(PREF_USER_WEIGHT_KG); + addPreferenceHandlerFor(PREF_USER_GENDER); } @Override diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AbstractSettingsActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AbstractSettingsActivity.java index 470bf8ab1..afb41e865 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AbstractSettingsActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AbstractSettingsActivity.java @@ -228,4 +228,19 @@ public abstract class AbstractSettingsActivity extends AppCompatPreferenceActivi } AndroidUtils.setLanguage(this, language); } + + protected void addPreferenceHandlerFor(final String preferenceKey) { + Preference pref = findPreference(preferenceKey); + if (pref != null) { + pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newVal) { + GBApplication.deviceService().onSendConfiguration(preferenceKey); + return true; + } + }); + } else { + LOG.warn("Could not find preference " + preferenceKey); + } + } } 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 79bb77c64..bbd390e46 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 @@ -21,8 +21,6 @@ import nodomain.freeyourgadget.gadgetbridge.util.Prefs; import nodomain.freeyourgadget.gadgetbridge.util.Version; public final class MiBandConst { - - public static final String PREF_USER_ALIAS = "mi_user_alias"; public static final String PREF_MIBAND_ADDRESS = "development_miaddr"; // FIXME: should be prefixed mi_ public static final String PREF_MIBAND_ALARMS = "mi_alarms"; public static final String PREF_MIBAND_DONT_ACK_TRANSFER = "mi_dont_ack_transfer"; 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 a6096192f..de579f243 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 @@ -26,13 +26,14 @@ import android.net.Uri; import android.os.Build; import android.os.ParcelUuid; +import androidx.annotation.NonNull; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Collection; import java.util.Collections; -import androidx.annotation.NonNull; import de.greenrobot.dao.query.QueryBuilder; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBException; @@ -51,6 +52,8 @@ import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser; import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; import nodomain.freeyourgadget.gadgetbridge.util.Prefs; +import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_NAME; + public class MiBandCoordinator extends AbstractDeviceCoordinator { private static final Logger LOG = LoggerFactory.getLogger(MiBandCoordinator.class); @@ -218,7 +221,7 @@ public class MiBandCoordinator extends AbstractDeviceCoordinator { UserInfo info = UserInfo.create( miBandAddress, - prefs.getString(MiBandConst.PREF_USER_ALIAS, null), + prefs.getString(PREF_USER_NAME, null), activityUser.getGender(), activityUser.getAge(), activityUser.getHeightCm(), 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 9a4b10126..8ccdc77f8 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 @@ -49,9 +49,9 @@ import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PR import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MI2_INACTIVITY_WARNINGS_THRESHOLD; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MIBAND_ADDRESS; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MIBAND_USE_HR_FOR_SLEEP_DETECTION; -import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_USER_ALIAS; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.VIBRATION_COUNT; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.getNotificationPrefKey; +import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_NAME; public class MiBandPreferencesActivity extends AbstractSettingsActivity { @Override @@ -261,7 +261,7 @@ public class MiBandPreferencesActivity extends AbstractSettingsActivity { @Override protected String[] getPreferenceKeysWithSummary() { Set prefKeys = new HashSet<>(); - prefKeys.add(PREF_USER_ALIAS); + prefKeys.add(PREF_USER_NAME); prefKeys.add(PREF_MIBAND_ADDRESS); prefKeys.add(ActivityUser.PREF_USER_STEPS_GOAL); prefKeys.add(PREF_MI2_INACTIVITY_WARNINGS_THRESHOLD); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/zetime/ZeTimePreferenceActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/zetime/ZeTimePreferenceActivity.java index d112a0d01..bc9bbcedb 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/zetime/ZeTimePreferenceActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/zetime/ZeTimePreferenceActivity.java @@ -22,7 +22,6 @@ import android.preference.Preference; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.activities.AbstractSettingsActivity; -import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst; public class ZeTimePreferenceActivity extends AbstractSettingsActivity { @Override @@ -93,16 +92,6 @@ public class ZeTimePreferenceActivity extends AbstractSettingsActivity { addPreferenceHandlerFor(ZeTimeConstants.PREF_USER_ACTIVETIME_GOAL); } - private void addPreferenceHandlerFor(final String preferenceKey) { - Preference pref = findPreference(preferenceKey); - pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override public boolean onPreferenceChange(Preference preference, Object newVal) { - GBApplication.deviceService().onSendConfiguration(preferenceKey); - return true; - } - }); - } - /** * delayed execution so that the preferences are applied first */ diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiSupport.java index 2332d77e7..d927f16e4 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiSupport.java @@ -169,6 +169,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_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 class HuamiSupport extends AbstractBTLEDeviceSupport { @@ -448,7 +453,7 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport { LOG.info("Attempting to set user info..."); Prefs prefs = GBApplication.getPrefs(); - String alias = prefs.getString(MiBandConst.PREF_USER_ALIAS, null); + String alias = prefs.getString(PREF_USER_NAME, null); ActivityUser activityUser = new ActivityUser(); int height = activityUser.getHeightCm(); int weight = activityUser.getWeightKg(); @@ -2011,6 +2016,13 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport { case PREF_SOUNDS: setBeepSounds(builder); break; + case PREF_USER_NAME: + case PREF_USER_YEAR_OF_BIRTH: + case PREF_USER_WEIGHT_KG: + case PREF_USER_HEIGHT_CM: + case PREF_USER_GENDER: + setUserInfo(builder); + break; } builder.queue(getQueue()); } catch (IOException e) {