From e1ef2455dce9b0be79189f6c630826e2ecb511a4 Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Fri, 27 Apr 2018 09:28:33 +0200 Subject: [PATCH] Amazfit Bip: allow to set displayed menu item in firmware >=0.1.1.14 NOTE: - You will have to be connected while doing the changes in preferences (FIXME) - It might not work at all since I did not test after the latest changes :P --- .../activities/SettingsActivity.java | 20 +++++-- .../huami/amazfitbip/AmazfitBipService.java | 4 ++ .../devices/amazfitbip/AmazfitBipSupport.java | 53 ++++++++++++++++--- app/src/main/res/values/arrays.xml | 31 +++++++++++ app/src/main/res/values/strings.xml | 12 +++++ app/src/main/res/values/values.xml | 9 ++++ app/src/main/res/xml/preferences.xml | 8 +++ 7 files changed, 125 insertions(+), 12 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 ec1c97f05..fb243cac4 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/SettingsActivity.java @@ -18,7 +18,6 @@ package nodomain.freeyourgadget.gadgetbridge.activities; import android.Manifest; -import android.content.ContentUris; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; @@ -29,16 +28,13 @@ import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.net.Uri; -import android.os.Build; import android.os.Bundle; -import android.os.Environment; import android.preference.EditTextPreference; import android.preference.ListPreference; import android.preference.Preference; import android.preference.PreferenceCategory; import android.preference.PreferenceManager; import android.provider.DocumentsContract; -import android.provider.MediaStore; import android.support.v4.app.ActivityCompat; import android.support.v4.content.LocalBroadcastManager; import android.widget.Toast; @@ -47,7 +43,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; -import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -65,6 +60,7 @@ import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs; import nodomain.freeyourgadget.gadgetbridge.util.Prefs; +import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MI2_DISPLAY_ITEMS; 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; @@ -330,6 +326,20 @@ public class SettingsActivity extends AbstractSettingsActivity { } }); + final Preference displayPages = findPreference("bip_display_items"); + displayPages.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newVal) { + invokeLater(new Runnable() { + @Override + public void run() { + GBApplication.deviceService().onSendConfiguration(PREF_MI2_DISPLAY_ITEMS); + } + }); + return true; + } + }); + // Get all receivers of Media Buttons Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/amazfitbip/AmazfitBipService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/amazfitbip/AmazfitBipService.java index 38b81f3f8..6b627b074 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/amazfitbip/AmazfitBipService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/amazfitbip/AmazfitBipService.java @@ -19,7 +19,9 @@ package nodomain.freeyourgadget.gadgetbridge.devices.huami.amazfitbip; import java.util.UUID; +import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBand2Service.DISPLAY_ITEM_BIT_CLOCK; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBand2Service.ENDPOINT_DISPLAY; +import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBand2Service.ENDPOINT_DISPLAY_ITEMS; public class AmazfitBipService { public static final UUID UUID_CHARACTERISTIC_WEATHER = UUID.fromString("0000000e-0000-3512-2118-0009af100700"); @@ -40,4 +42,6 @@ public class AmazfitBipService { public static final byte COMMAND_ACTIVITY_DATA_TYPE_SPORTS_DETAILS = 0x06; public static final byte[] COMMAND_ACK_FIND_PHONE_IN_PROGRESS = new byte[]{ENDPOINT_DISPLAY, 0x14, 0x00, 0x00}; + + public static final byte[] COMMAND_CHANGE_SCREENS = new byte[]{ENDPOINT_DISPLAY_ITEMS, DISPLAY_ITEM_BIT_CLOCK, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/amazfitbip/AmazfitBipSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/amazfitbip/AmazfitBipSupport.java index a853132a9..a13a12a20 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/amazfitbip/AmazfitBipSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/amazfitbip/AmazfitBipSupport.java @@ -28,6 +28,7 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.Locale; +import java.util.Set; import java.util.SimpleTimeZone; import java.util.UUID; @@ -55,6 +56,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.NotificationS import nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.MiBand2Support; import nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.operations.FetchActivityOperation; import nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.operations.FetchSportsSummaryOperation; +import nodomain.freeyourgadget.gadgetbridge.util.Prefs; import nodomain.freeyourgadget.gadgetbridge.util.StringUtils; import nodomain.freeyourgadget.gadgetbridge.util.Version; @@ -129,14 +131,52 @@ public class AmazfitBipSupport extends MiBand2Support { @Override protected AmazfitBipSupport setDisplayItems(TransactionBuilder builder) { - /* - LOG.info("Enabling all display items"); + if (gbDevice.getType() != DeviceType.AMAZFITBIP) { + return this; // Disable for Cor for now + } + if (gbDevice.getFirmwareVersion() == null) { + LOG.warn("Device not initialized yet, won't set menu items"); + return this; + } - // This will brick the watch, don't enable it! - byte[] data = new byte[]{ENDPOINT_DISPLAY_ITEMS, (byte) 0xff, 0x01, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; + Version version = new Version(gbDevice.getFirmwareVersion()); + if (version.compareTo(new Version("0.1.1.14")) <= 0) { + LOG.warn("Won't set menu items since firmware is too low to be safe"); + return this; + } - builder.write(getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_3_CONFIGURATION), data); - */ + Prefs prefs = GBApplication.getPrefs(); + Set pages = prefs.getStringSet("bip_display_items", null); + LOG.info("Setting display items to " + (pages == null ? "none" : pages)); + byte[] command = AmazfitBipService.COMMAND_CHANGE_SCREENS.clone(); + + if (pages != null) { + if (pages.contains("status")) { + command[1] |= 0x02; + } + if (pages.contains("activity")) { + command[1] |= 0x04; + } + if (pages.contains("weather")) { + command[1] |= 0x08; + } + if (pages.contains("alarm")) { + command[1] |= 0x10; + } + if (pages.contains("timer")) { + command[1] |= 0x20; + } + if (pages.contains("compass")) { + command[1] |= 0x40; + } + if (pages.contains("settings")) { + command[1] |= 0x80; + } + if (pages.contains("alipay")) { + command[2] |= 0x01; + } + } + builder.write(getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_3_CONFIGURATION), command); return this; } @@ -145,7 +185,6 @@ public class AmazfitBipSupport extends MiBand2Support { if (gbDevice.getFirmwareVersion() == null) { LOG.warn("Device not initialized yet, so not sending weather info"); return; - } boolean supportsConditionString = false; diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 4a6bd3794..fa9cde984 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -183,6 +183,37 @@ @string/p_battery + + @string/menuitem_status + @string/menuitem_activity + @string/menuitem_weather + @string/menuitem_alarm + @string/menuitem_timer + @string/menuitem_compass + @string/menuitem_settings + @string/menuitem_alipay + + + + @string/p_menuitem_status + @string/p_menuitem_activity + @string/p_menuitem_weather + @string/p_menuitem_alarm + @string/p_menuitem_timer + @string/p_menuitem_compass + @string/p_menuitem_settings + @string/p_menuitem_alipay + + + @string/p_menuitem_status + @string/p_menuitem_activity + @string/p_menuitem_weather + @string/p_menuitem_alarm + @string/p_menuitem_timer + @string/p_menuitem_compass + @string/p_menuitem_settings + + @string/unit_metric @string/unit_imperial diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6f9afd11a..c16faf20e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -344,6 +344,8 @@ Clock Heart rate Battery + + Live activity Steps today, target: %1$s Do not ACK activity data transfer @@ -552,4 +554,14 @@ Choose export location Gadgetbridge notifications + + + Status + Activity + Weather + Alarm + Timer + Compass + Settings + Alipay diff --git a/app/src/main/res/values/values.xml b/app/src/main/res/values/values.xml index 3de882530..42a7f2b8f 100644 --- a/app/src/main/res/values/values.xml +++ b/app/src/main/res/values/values.xml @@ -19,6 +19,15 @@ heart_rate battery + status + activity + weather + alarm + timer + compass + settings + alipay + off on automatic diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index f8faff22c..6769d7daa 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -193,6 +193,14 @@ android:key="amazfitbip_language" android:summary="%s" android:title="@string/pref_title_language" /> +