From d20e6516c80cadb6d2e2d42540f1d770a643ff18 Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Sat, 7 Nov 2020 10:19:28 +0100 Subject: [PATCH] Humai: Implement menu sorting for remaining devices (except Mi Band 2) --- .../huami/amazfitbip/AmazfitBipService.java | 2 - .../huami/amazfitcor/AmazfitCorService.java | 25 ------- .../devices/huami/miband3/MiBand3Service.java | 3 - .../service/devices/huami/HuamiSupport.java | 52 +++++++++++++- .../huami/amazfitbip/AmazfitBipSupport.java | 68 ++++--------------- .../huami/amazfitcor/AmazfitCorSupport.java | 63 ++++------------- .../devices/huami/miband3/MiBand3Support.java | 66 ++++-------------- app/src/main/res/values/arrays.xml | 18 +++-- app/src/main/res/values/strings.xml | 2 - app/src/main/res/values/values.xml | 2 - .../res/xml/devicesettings_amazfitbip.xml | 5 +- .../res/xml/devicesettings_amazfitcor.xml | 5 +- .../main/res/xml/devicesettings_miband3.xml | 5 +- 13 files changed, 112 insertions(+), 204 deletions(-) delete mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/amazfitcor/AmazfitCorService.java 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 325ddccfc..120cca1be 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 @@ -37,6 +37,4 @@ 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, 0x10, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/amazfitcor/AmazfitCorService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/amazfitcor/AmazfitCorService.java deleted file mode 100644 index ba1b7027c..000000000 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/amazfitcor/AmazfitCorService.java +++ /dev/null @@ -1,25 +0,0 @@ -/* Copyright (C) 2015-2020 Andreas Shimokawa, Carsten Pfeiffer - - This file is part of Gadgetbridge. - - Gadgetbridge is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Gadgetbridge is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . */ - -package nodomain.freeyourgadget.gadgetbridge.devices.huami.amazfitcor; - -import static nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiService.DISPLAY_ITEM_BIT_CLOCK; -import static nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiService.ENDPOINT_DISPLAY_ITEMS; - -public class AmazfitCorService { - public static final byte[] COMMAND_CHANGE_SCREENS = new byte[]{ENDPOINT_DISPLAY_ITEMS, DISPLAY_ITEM_BIT_CLOCK, 0x20, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}; -} 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 e2284c7e1..5352c1f27 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 @@ -17,12 +17,9 @@ 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, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00}; 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}; public static final byte[] COMMAND_NIGHT_MODE_OFF = new byte[]{0x1a, 0x00}; 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 31c31eabc..052595a12 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 @@ -156,6 +156,8 @@ import static nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiConst.PREF import static nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiConst.PREF_DEVICE_ACTION_START_NON_WEAR_SELECTION; import static nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiConst.PREF_DEVICE_ACTION_WOKE_UP_BROADCAST; import static nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiConst.PREF_DEVICE_ACTION_WOKE_UP_SELECTION; +import static nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiService.DISPLAY_ITEM_BIT_CLOCK; +import static nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiService.ENDPOINT_DISPLAY_ITEMS; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.DEFAULT_VALUE_VIBRATION_COUNT; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.DEFAULT_VALUE_VIBRATION_PROFILE; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.VIBRATION_COUNT; @@ -2315,6 +2317,54 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport { return this; } + protected HuamiSupport setDisplayItemsOld(TransactionBuilder builder, boolean isShortcuts, int defaultSettings, Map keyPosMap) { + + SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(gbDevice.getAddress()); + String pages; + List enabledList; + if (isShortcuts) { + pages = prefs.getString(HuamiConst.PREF_SHORTCUTS_SORTABLE, null); + LOG.info("Setting shortcuts"); + } else { + pages = prefs.getString(HuamiConst.PREF_DISPLAY_ITEMS_SORTABLE, null); + LOG.info("Setting menu items"); + } + if (pages == null) { + enabledList = Arrays.asList(getContext().getResources().getStringArray(defaultSettings)); + } else { + enabledList = Arrays.asList(pages.split(",")); + } + LOG.info("enabled items" + enabledList); + + byte[] command = new byte[keyPosMap.size() + 4]; + command[0] = ENDPOINT_DISPLAY_ITEMS; + byte index = 1; + int enabled_mask = DISPLAY_ITEM_BIT_CLOCK; + // it seem that we first have to put all ENABLED items into the array, oder does matter + for (String key : enabledList) { + Integer id = keyPosMap.get(key); + if (id != null) { + enabled_mask |= (1 << id.byteValue()); + command[3 + id] = index++; + } + } + // And then all DISABLED ones, order does not matter + for (Map.Entry entry : keyPosMap.entrySet()) { + String key = entry.getKey(); + int id = entry.getValue(); + + if (!enabledList.contains(key)) { + command[3 + id] = index++; + } + } + + command[1] = (byte) (enabled_mask & 0xff); + command[2] = (byte) ((enabled_mask >> 8 & 0xff)); + + builder.write(getCharacteristic(HuamiService.UUID_CHARACTERISTIC_3_CONFIGURATION), command); + return this; + } + protected HuamiSupport setDisplayItemsNew(TransactionBuilder builder, boolean isShortcuts, int defaultSettings, Map keyIdMap) { SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(gbDevice.getAddress()); String pages; @@ -2358,7 +2408,7 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport { if (!enabledList.contains(key)) { command[pos++] = (byte) index++; command[pos++] = 0x01; - command[pos++] = (byte) menuType; + command[pos++] = menuType; command[pos++] = (byte) id; } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/amazfitbip/AmazfitBipSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/amazfitbip/AmazfitBipSupport.java index 7552c4951..82f346d47 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/amazfitbip/AmazfitBipSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/amazfitbip/AmazfitBipSupport.java @@ -18,24 +18,19 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.huami.amazfitbip; import android.content.Context; -import android.content.SharedPreferences; import android.net.Uri; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; +import java.util.LinkedHashMap; +import java.util.Map; -import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; -import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiConst; import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiFWHelper; import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiService; import nodomain.freeyourgadget.gadgetbridge.devices.huami.amazfitbip.AmazfitBipFWHelper; -import nodomain.freeyourgadget.gadgetbridge.devices.huami.amazfitbip.AmazfitBipService; import nodomain.freeyourgadget.gadgetbridge.model.CallSpec; import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec; import nodomain.freeyourgadget.gadgetbridge.model.RecordedDataTypes; @@ -74,55 +69,18 @@ public class AmazfitBipSupport extends HuamiSupport { @Override protected AmazfitBipSupport setDisplayItems(TransactionBuilder builder) { - if (gbDevice.getFirmwareVersion() == null) { - LOG.warn("Device not initialized yet, won't set menu items"); - return this; - } - - SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(gbDevice.getAddress()); - Set pages = prefs.getStringSet(HuamiConst.PREF_DISPLAY_ITEMS, new HashSet<>(Arrays.asList(getContext().getResources().getStringArray(R.array.pref_bip_display_items_default)))); - - LOG.info("Setting display items to " + (pages == null ? "none" : pages)); - byte[] command = AmazfitBipService.COMMAND_CHANGE_SCREENS.clone(); - - boolean shortcut_weather = false; - boolean shortcut_alipay = false; - - 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; - } - if (pages.contains("shortcut_weather")) { - shortcut_weather = true; - } - if (pages.contains("shortcut_alipay")) { - shortcut_alipay = true; - } - builder.write(getCharacteristic(HuamiService.UUID_CHARACTERISTIC_3_CONFIGURATION), command); - setShortcuts(builder, shortcut_weather, shortcut_alipay); - } + Map keyPosMap = new LinkedHashMap<>(); + keyPosMap.put("status", 1); + keyPosMap.put("activity", 2); + keyPosMap.put("weather", 3); + keyPosMap.put("alarm", 4); + keyPosMap.put("timer", 5); + keyPosMap.put("compass", 6); + keyPosMap.put("settings", 7); + keyPosMap.put("alipay", 8); + setDisplayItemsOld(builder, false, R.array.pref_bip_display_items_default, keyPosMap); + //setShortcuts(builder, shortcut_weather, shortcut_alipay); return this; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/amazfitcor/AmazfitCorSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/amazfitcor/AmazfitCorSupport.java index 1786053ce..c2cc7f664 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/amazfitcor/AmazfitCorSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/amazfitcor/AmazfitCorSupport.java @@ -17,72 +17,35 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.huami.amazfitcor; import android.content.Context; -import android.content.SharedPreferences; import android.net.Uri; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.io.IOException; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; +import java.util.LinkedHashMap; +import java.util.Map; -import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; -import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiConst; import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiFWHelper; -import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiService; import nodomain.freeyourgadget.gadgetbridge.devices.huami.amazfitcor.AmazfitCorFWHelper; -import nodomain.freeyourgadget.gadgetbridge.devices.huami.amazfitcor.AmazfitCorService; import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec; import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder; import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.amazfitbip.AmazfitBipSupport; public class AmazfitCorSupport extends AmazfitBipSupport { - private static final Logger LOG = LoggerFactory.getLogger(AmazfitCorSupport.class); - @Override protected AmazfitCorSupport setDisplayItems(TransactionBuilder builder) { - SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(gbDevice.getAddress()); - Set pages = prefs.getStringSet(HuamiConst.PREF_DISPLAY_ITEMS, new HashSet<>(Arrays.asList(getContext().getResources().getStringArray(R.array.pref_cor_display_items_default)))); - LOG.info("Setting display items to " + (pages == null ? "none" : pages)); - - byte[] command = AmazfitCorService.COMMAND_CHANGE_SCREENS.clone(); - - if (pages != null) { - if (pages.contains("status")) { - command[1] |= 0x02; - } - if (pages.contains("notifications")) { - command[1] |= 0x04; - } - if (pages.contains("activity")) { - command[1] |= 0x08; - } - if (pages.contains("weather")) { - command[1] |= 0x10; - } - if (pages.contains("alarm")) { - command[1] |= 0x20; - } - if (pages.contains("timer")) { - command[1] |= 0x40; - } - if (pages.contains("settings")) { - command[1] |= 0x80; - } - if (pages.contains("alipay")) { - command[2] |= 0x01; - } - if (pages.contains("music")) { - command[2] |= 0x02; - } - - builder.write(getCharacteristic(HuamiService.UUID_CHARACTERISTIC_3_CONFIGURATION), command); - } + Map keyPosMap = new LinkedHashMap<>(); + keyPosMap.put("status", 1); + keyPosMap.put("notifications", 2); + keyPosMap.put("activity", 3); + keyPosMap.put("weather", 4); + keyPosMap.put("alarm", 5); + keyPosMap.put("timer", 6); + keyPosMap.put("settings", 7); + keyPosMap.put("alipay", 8); + keyPosMap.put("music", 9); + setDisplayItemsOld(builder, false, R.array.pref_cor_display_items_default, keyPosMap); return this; } 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 4bc4b33e5..c960da486 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 @@ -17,7 +17,6 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.huami.miband3; import android.content.Context; -import android.content.SharedPreferences; import android.net.Uri; import android.widget.Toast; @@ -25,16 +24,13 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; -import java.util.Arrays; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; -import java.util.HashSet; -import java.util.Set; +import java.util.LinkedHashMap; +import java.util.Map; -import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; -import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiConst; import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiFWHelper; import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiService; import nodomain.freeyourgadget.gadgetbridge.devices.huami.miband3.MiBand3Coordinator; @@ -56,55 +52,17 @@ public class MiBand3Support extends AmazfitBipSupport { @Override protected MiBand3Support setDisplayItems(TransactionBuilder builder) { - SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(gbDevice.getAddress()); - Set pages = prefs.getStringSet(HuamiConst.PREF_DISPLAY_ITEMS, new HashSet<>(Arrays.asList(getContext().getResources().getStringArray(R.array.pref_miband3_display_items_default)))); - - LOG.info("Setting display items to " + (pages == null ? "none" : pages)); - byte[] command = MiBand3Service.COMMAND_CHANGE_SCREENS.clone(); - - byte pos = 1; - if (pages != null) { - if (pages.contains("notifications")) { - command[1] |= 0x02; - command[4] = pos++; - } - if (pages.contains("weather")) { - command[1] |= 0x04; - command[5] = pos++; - } - if (pages.contains("activity")) { - command[1] |= 0x08; - command[6] = pos++; - } - if (pages.contains("more")) { - command[1] |= 0x10; - command[7] = pos++; - } - if (pages.contains("status")) { - command[1] |= 0x20; - command[8] = pos++; - } - if (pages.contains("heart_rate")) { - command[1] |= 0x40; - command[9] = pos++; - } - if (pages.contains("timer")) { - command[1] |= 0x80; - command[10] = pos++; - } - if (pages.contains("nfc")) { - command[2] |= 0x01; - command[11] = pos++; - } - for (int i = 4; i <= 11; i++) { - if (command[i] == 0) { - command[i] = pos++; - } - } - - builder.write(getCharacteristic(HuamiService.UUID_CHARACTERISTIC_3_CONFIGURATION), command); - } + Map keyPosMap = new LinkedHashMap<>(); + keyPosMap.put("notifications", 1); + keyPosMap.put("weather", 2); + keyPosMap.put("activity", 3); + keyPosMap.put("more", 4); + keyPosMap.put("status", 5); + keyPosMap.put("heart_rate", 6); + keyPosMap.put("timer", 7); + keyPosMap.put("nfc", 8); + setDisplayItemsOld(builder, false, R.array.pref_miband3_display_items_default, keyPosMap); return this; } diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index c6a426053..e7d8ddf0b 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -401,8 +401,6 @@ - @string/menuitem_shortcut_alipay - @string/menuitem_shortcut_weather @string/menuitem_status @string/menuitem_activity @string/menuitem_weather @@ -414,8 +412,6 @@ - @string/p_menuitem_shortcut_alipay - @string/p_menuitem_shortcut_weather @string/p_menuitem_status @string/p_menuitem_activity @string/p_menuitem_weather @@ -436,6 +432,20 @@ @string/p_menuitem_settings + + @string/menuitem_alipay + @string/menuitem_weather + + + + @string/p_menuitem_alipay + @string/p_menuitem_weather + + + + @string/p_menuitem_weather + + @string/menuitem_status @string/menuitem_notifications diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 70a67471c..298a57ad6 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -812,8 +812,6 @@ Lemfo SG2 Lefun - Alipay (Shortcut) - Weather (Shortcut) Status Notifications Activity diff --git a/app/src/main/res/values/values.xml b/app/src/main/res/values/values.xml index 3709203ff..eefaf6191 100644 --- a/app/src/main/res/values/values.xml +++ b/app/src/main/res/values/values.xml @@ -19,8 +19,6 @@ heart_rate battery - shortcut_alipay - shortcut_weather status activity weather diff --git a/app/src/main/res/xml/devicesettings_amazfitbip.xml b/app/src/main/res/xml/devicesettings_amazfitbip.xml index d2133e176..b5d7323fc 100644 --- a/app/src/main/res/xml/devicesettings_amazfitbip.xml +++ b/app/src/main/res/xml/devicesettings_amazfitbip.xml @@ -1,12 +1,13 @@ - - -