From 338251597b647eb09d14a609dc2ef779452a839b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Rebelo?= Date: Mon, 6 Aug 2018 09:59:38 +0100 Subject: [PATCH] Mi Band 3: Band screen unlock --- .../activities/SettingsActivity.java | 16 +++++++ .../huami/miband3/MiBand3Coordinator.java | 8 ++++ .../devices/huami/miband3/MiBand3Service.java | 3 ++ .../devices/miband/MiBandConst.java | 1 + .../devices/huami/miband3/MiBand3Support.java | 43 +++++++++++++++++++ app/src/main/res/values/strings.xml | 3 +- app/src/main/res/xml/preferences.xml | 10 ++++- 7 files changed, 81 insertions(+), 3 deletions(-) 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 10d079d61..073b40c48 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java @@ -63,6 +63,7 @@ import nodomain.freeyourgadget.gadgetbridge.util.Prefs; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MI2_DATEFORMAT; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MI2_DISPLAY_ITEMS; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MI2_ENABLE_TEXT_NOTIFICATIONS; +import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MI3_BAND_SCREEN_UNLOCK; import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_HEIGHT_CM; import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_SLEEP_DURATION; import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_STEPS_GOAL; @@ -390,6 +391,21 @@ public class SettingsActivity extends AbstractSettingsActivity { } }); + final Preference miBand3ScreenUnlock = findPreference(PREF_MI3_BAND_SCREEN_UNLOCK); + miBand3ScreenUnlock.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newVal) { + invokeLater(new Runnable() { + @Override + public void run() { + GBApplication.deviceService().onSendConfiguration(PREF_MI3_BAND_SCREEN_UNLOCK); + } + }); + return true; + } + }); + + /* final Preference miBand3DisplayItems = findPreference("miband3_display_items"); miBand3DisplayItems.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/miband3/MiBand3Coordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/miband3/MiBand3Coordinator.java index 91bb24ce2..39035982b 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/miband3/MiBand3Coordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/miband3/MiBand3Coordinator.java @@ -24,12 +24,15 @@ import android.support.annotation.NonNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler; import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiConst; import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiCoordinator; +import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate; import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; +import nodomain.freeyourgadget.gadgetbridge.util.Prefs; public class MiBand3Coordinator extends HuamiCoordinator { private static final Logger LOG = LoggerFactory.getLogger(MiBand3Coordinator.class); @@ -70,4 +73,9 @@ public class MiBand3Coordinator extends HuamiCoordinator { public boolean supportsWeather() { return true; } + + public static boolean getBandScreenUnlock() { + Prefs prefs = GBApplication.getPrefs(); + return prefs.getBoolean(MiBandConst.PREF_MI3_BAND_SCREEN_UNLOCK, false); + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/miband3/MiBand3Service.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/miband3/MiBand3Service.java index 7b8f1b69b..e8bf6dd9b 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/miband3/MiBand3Service.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/miband3/MiBand3Service.java @@ -18,8 +18,11 @@ package nodomain.freeyourgadget.gadgetbridge.devices.huami.miband3; import static nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiService.DISPLAY_ITEM_BIT_CLOCK; +import static nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiService.ENDPOINT_DISPLAY; import static nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiService.ENDPOINT_DISPLAY_ITEMS; public class MiBand3Service { public static final byte[] COMMAND_CHANGE_SCREENS = new byte[]{ENDPOINT_DISPLAY_ITEMS, DISPLAY_ITEM_BIT_CLOCK, 0x30, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; + public static final byte[] COMMAND_ENABLE_BAND_SCREEN_UNLOCK = new byte[]{ENDPOINT_DISPLAY, 0x16, 0x00, 0x01}; + public static final byte[] COMMAND_DISABLE_BAND_SCREEN_UNLOCK = new byte[]{ENDPOINT_DISPLAY, 0x16, 0x00, 0x00}; } 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 b9d5ae507..0358dfb07 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 @@ -69,6 +69,7 @@ public final class MiBandConst { public static final String PREF_MI2_INACTIVITY_WARNINGS_DND_END = "mi2_inactivity_warnings_dnd_end"; public static final String PREF_MIBAND_SETUP_BT_PAIRING = "mi_setup_bt_pairing"; + public static final String PREF_MI3_BAND_SCREEN_UNLOCK = "mi3_band_screen_unlock"; public static final String ORIGIN_INCOMING_CALL = "incoming_call"; public static final String ORIGIN_ALARM_CLOCK = "alarm_clock"; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/miband3/MiBand3Support.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/miband3/MiBand3Support.java index b09eabc6d..faeed4a05 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/miband3/MiBand3Support.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/miband3/MiBand3Support.java @@ -18,6 +18,7 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.huami.miband3; import android.content.Context; import android.net.Uri; +import android.widget.Toast; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -28,10 +29,13 @@ import java.util.Set; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiFWHelper; import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiService; +import nodomain.freeyourgadget.gadgetbridge.devices.huami.miband3.MiBand3Coordinator; import nodomain.freeyourgadget.gadgetbridge.devices.huami.miband3.MiBand3FWHelper; import nodomain.freeyourgadget.gadgetbridge.devices.huami.miband3.MiBand3Service; +import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst; import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder; import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.amazfitbip.AmazfitBipSupport; +import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.Prefs; public class MiBand3Support extends AmazfitBipSupport { @@ -71,6 +75,45 @@ public class MiBand3Support extends AmazfitBipSupport { return this; } + @Override + public void onSendConfiguration(String config) { + TransactionBuilder builder; + try { + builder = performInitialized("Sending configuration for option: " + config); + switch (config) { + case MiBandConst.PREF_MI3_BAND_SCREEN_UNLOCK: + setBandScreenUnlock(builder); + break; + default: + super.onSendConfiguration(config); + return; + } + builder.queue(getQueue()); + } catch (IOException e) { + GB.toast("Error setting configuration", Toast.LENGTH_LONG, GB.ERROR, e); + } + } + + private MiBand3Support setBandScreenUnlock(TransactionBuilder builder) { + boolean enable = MiBand3Coordinator.getBandScreenUnlock(); + LOG.info("Setting band screen unlock to " + enable); + + if (enable) { + builder.write(getCharacteristic(HuamiService.UUID_CHARACTERISTIC_3_CONFIGURATION), MiBand3Service.COMMAND_ENABLE_BAND_SCREEN_UNLOCK); + } else { + builder.write(getCharacteristic(HuamiService.UUID_CHARACTERISTIC_3_CONFIGURATION), MiBand3Service.COMMAND_DISABLE_BAND_SCREEN_UNLOCK); + } + + return this; + } + + @Override + public void phase2Initialize(TransactionBuilder builder) { + super.phase2Initialize(builder); + LOG.info("phase2Initialize..."); + setBandScreenUnlock(builder); + } + @Override public HuamiFWHelper createFWHelper(Uri uri, Context context) throws IOException { return new MiBand3FWHelper(uri, context); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5a8d22d89..9442876eb 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -426,7 +426,8 @@ Disable the inactivity warnings for a time interval Start time End time - + Band screen unlock" + Swipe up to unlock the band\'s screen Automatic Simplified Chinese Traditional Chinese diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 0a8d4bf3b..ea71718ee 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -208,10 +208,16 @@ android:summary="@string/mi2_prefs_display_items_summary" android:title="@string/mi2_prefs_display_items"/> - +