From f9783297113b3a384d0162e09930da06a7dab722 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Rebelo?= Date: Fri, 6 Oct 2023 17:07:15 +0100 Subject: [PATCH] Mi Band 8: Display items (wip, needs chunked) --- .../devices/xiaomi/XiaomiCoordinator.java | 2 +- .../devices/xiaomi/XiaomiPreferences.java | 7 +++ .../service/devices/xiaomi/XiaomiSupport.java | 2 +- .../xiaomi/services/XiaomiSystemService.java | 53 +++++++++++++++- app/src/main/res/values/arrays.xml | 60 +++++++++++++++++++ app/src/main/res/values/strings.xml | 4 ++ .../devicesettings_xiaomi_displayitems.xml | 13 ++++ 7 files changed, 137 insertions(+), 4 deletions(-) create mode 100644 app/src/main/res/xml/devicesettings_xiaomi_displayitems.xml diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/xiaomi/XiaomiCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/xiaomi/XiaomiCoordinator.java index 2b1b5cc19..ab59df635 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/xiaomi/XiaomiCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/xiaomi/XiaomiCoordinator.java @@ -303,7 +303,7 @@ public abstract class XiaomiCoordinator extends AbstractBLEDeviceCoordinator { // Display // settings.add(R.xml.devicesettings_header_display); - settings.add(R.xml.devicesettings_huami2021_displayitems); + settings.add(R.xml.devicesettings_xiaomi_displayitems); settings.add(R.xml.devicesettings_password); // diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xiaomi/XiaomiPreferences.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xiaomi/XiaomiPreferences.java index 76fbccf0f..2f7a3122a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xiaomi/XiaomiPreferences.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xiaomi/XiaomiPreferences.java @@ -41,4 +41,11 @@ public final class XiaomiPreferences { .setMinute(calendar.get(Calendar.MINUTE)) .build(); } + + /** + * Returns the preference key where to save the list of possible value for a preference, comma-separated. + */ + public static String getPrefPossibleValuesKey(final String key) { + return String.format(Locale.ROOT, "%s_possible_values", key); + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xiaomi/XiaomiSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xiaomi/XiaomiSupport.java index be4583572..5fe46e649 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xiaomi/XiaomiSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xiaomi/XiaomiSupport.java @@ -239,7 +239,7 @@ public class XiaomiSupport extends AbstractBTLEDeviceSupport { @Override public void onTestNewFunction() { final TransactionBuilder builder = createTransactionBuilder("test new function"); - + sendCommand(builder, 2, 29); builder.queue(getQueue()); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xiaomi/services/XiaomiSystemService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xiaomi/services/XiaomiSystemService.java index 172cfba12..b33e35f9f 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xiaomi/services/XiaomiSystemService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xiaomi/services/XiaomiSystemService.java @@ -19,8 +19,11 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.xiaomi.services; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; import java.util.Calendar; +import java.util.Collections; import java.util.GregorianCalendar; +import java.util.List; import java.util.TimeZone; import nodomain.freeyourgadget.gadgetbridge.GBApplication; @@ -31,12 +34,16 @@ import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventFindPhone; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventUpdateDeviceInfo; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventUpdatePreferences; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInfo; +import nodomain.freeyourgadget.gadgetbridge.devices.huami.Huami2021Coordinator; +import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiConst; import nodomain.freeyourgadget.gadgetbridge.model.BatteryState; import nodomain.freeyourgadget.gadgetbridge.proto.xiaomi.XiaomiProto; import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder; +import nodomain.freeyourgadget.gadgetbridge.service.devices.xiaomi.XiaomiPreferences; import nodomain.freeyourgadget.gadgetbridge.service.devices.xiaomi.XiaomiSupport; import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs; import nodomain.freeyourgadget.gadgetbridge.util.Prefs; +import nodomain.freeyourgadget.gadgetbridge.util.StringUtils; public class XiaomiSystemService extends AbstractXiaomiService { private static final Logger LOG = LoggerFactory.getLogger(XiaomiSystemService.class); @@ -62,7 +69,7 @@ public class XiaomiSystemService extends AbstractXiaomiService { getSupport().sendCommand(builder, COMMAND_TYPE, CMD_DEVICE_INFO); getSupport().sendCommand(builder, COMMAND_TYPE, CMD_BATTERY); getSupport().sendCommand(builder, COMMAND_TYPE, CMD_PASSWORD_GET); - getSupport().sendCommand(builder, COMMAND_TYPE, CMD_DISPLAY_ITEMS_GET); + // FIXME i think this needs chunked getSupport().sendCommand(builder, COMMAND_TYPE, CMD_DISPLAY_ITEMS_GET); } @Override @@ -97,7 +104,7 @@ public class XiaomiSystemService extends AbstractXiaomiService { return; } - LOG.warn("Unknown config command {}", cmd.getSubtype()); + LOG.warn("Unknown system command {}", cmd.getSubtype()); } @Override @@ -233,8 +240,50 @@ public class XiaomiSystemService extends AbstractXiaomiService { getSupport().evaluateGBDeviceEvent(eventUpdatePreferences); } + private void setDisplayItems(final TransactionBuilder builder) { + final Prefs prefs = getDevicePrefs(); + final ArrayList allScreens = new ArrayList<>(prefs.getList(XiaomiPreferences.getPrefPossibleValuesKey(HuamiConst.PREF_DISPLAY_ITEMS_SORTABLE), Collections.emptyList())); + final ArrayList enabledScreens = new ArrayList<>(prefs.getList(HuamiConst.PREF_DISPLAY_ITEMS_SORTABLE, Collections.emptyList())); + if (allScreens.isEmpty()) { + LOG.warn("No list of all screens"); + return; + } + if (!enabledScreens.contains("setting")) { + enabledScreens.add("setting"); + } + // TODO i think this needs chunked + } + private void handleDisplayItems(final XiaomiProto.DisplayItems displayItems) { LOG.debug("Got {} display items", displayItems.getDisplayItemCount()); + final List allScreens = new ArrayList<>(); + final List mainScreens = new ArrayList<>(); + final List moreScreens = new ArrayList<>(); + for (final XiaomiProto.DisplayItem displayItem : displayItems.getDisplayItemList()) { + allScreens.add(displayItem.getCode()); + if (!displayItem.getDisabled()) { + if (displayItem.getInMoreSection()) { + moreScreens.add(displayItem.getCode()); + } else { + mainScreens.add(displayItem.getCode()); + } + } + } + + final List enabledScreens = new ArrayList<>(mainScreens); + if (!moreScreens.isEmpty()) { + enabledScreens.add("more"); + enabledScreens.addAll(moreScreens); + } + + final String allScreensPrefValue = StringUtils.join(",", allScreens.toArray(new String[0])).toString(); + final String prefValue = StringUtils.join(",", enabledScreens.toArray(new String[0])).toString(); + + final GBDeviceEventUpdatePreferences eventUpdatePreferences = new GBDeviceEventUpdatePreferences() + .withPreference(XiaomiPreferences.getPrefPossibleValuesKey(HuamiConst.PREF_DISPLAY_ITEMS_SORTABLE), allScreensPrefValue) + .withPreference(HuamiConst.PREF_DISPLAY_ITEMS_SORTABLE, prefValue); + + getSupport().evaluateGBDeviceEvent(eventUpdatePreferences); } public void onFindPhone(final boolean start) { diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index f832b4e14..f273c9bf3 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -1022,6 +1022,66 @@ headphone + + @string/menuitem_stats + @string/menuitem_workout + @string/menuitem_activity + @string/menuitem_running + @string/menuitem_status + @string/menuitem_hr + @string/menuitem_pai + @string/menuitem_spo2 + @string/menuitem_sleep + @string/menuitem_stress + @string/menuitem_weather + @string/menuitem_alarm + @string/menuitem_settings + @string/menuitem_more + @string/menuitem_alerts + @string/menuitem_events + @string/menuitem_breathing + @string/menuitem_female_health + @string/menuitem_stopwatch + @string/menuitem_music + @string/menuitem_findphone + @string/menuitem_worldclock + @string/menuitem_mutephone + @string/menuitem_takephoto + @string/menuitem_timer + @string/menuitem_flashlight + @string/menuitem_pomodoro + + + + today_act + sport + sport_record + sport_course + sport_state + heart + pai + blood_ox + sleep + press + weather + alarm + setting + more + schedule + event_reminder + breath + fm_health + stopwatch + music + find_phone + world_clock + phone_mute + phone_remote + count_down + flash_light + focus + + @string/activity_type_outdoor_running @string/activity_type_walking diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f4d8495e9..e79b237aa 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -2407,4 +2407,8 @@ OsmAnd(+) Google Maps Serial Number + Stats + Running + Alerts + Focus diff --git a/app/src/main/res/xml/devicesettings_xiaomi_displayitems.xml b/app/src/main/res/xml/devicesettings_xiaomi_displayitems.xml new file mode 100644 index 000000000..08ad3f301 --- /dev/null +++ b/app/src/main/res/xml/devicesettings_xiaomi_displayitems.xml @@ -0,0 +1,13 @@ + + + +