From e844f8105652b8995802b0326d43946f072bee83 Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Tue, 12 May 2020 22:42:19 +0200 Subject: [PATCH] Huami: If no menu items were ever configured with Gadgetbridge use our default and do not disable everything on connect Fixes #1853 --- .../devices/huami/HuamiCoordinator.java | 6 ----- .../service/devices/huami/HuamiSupport.java | 9 +++++-- .../huami/amazfitbip/AmazfitBipSupport.java | 15 ++++++++---- .../huami/amazfitcor/AmazfitCorSupport.java | 15 ++++++++---- .../huami/amazfitgts/AmazfitGTSSupport.java | 13 ++++++---- .../devices/huami/miband3/MiBand3Support.java | 24 +++++++++++-------- 6 files changed, 52 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/HuamiCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/HuamiCoordinator.java index 5a752e2b0..2f3110744 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/HuamiCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/HuamiCoordinator.java @@ -35,7 +35,6 @@ import java.text.SimpleDateFormat; import java.util.Collection; import java.util.Collections; import java.util.Date; -import java.util.Set; import de.greenrobot.dao.query.QueryBuilder; import nodomain.freeyourgadget.gadgetbridge.GBApplication; @@ -192,11 +191,6 @@ public abstract class HuamiCoordinator extends AbstractDeviceCoordinator { return getTimePreference(HuamiConst.PREF_DISCONNECT_NOTIFICATION_END, "00:00", deviceAddress); } - public static Set getDisplayItems(String deviceAddress) { - SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(deviceAddress); - return prefs.getStringSet(HuamiConst.PREF_DISPLAY_ITEMS, null); - } - public static boolean getUseCustomFont(String deviceAddress) { SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(deviceAddress); return prefs.getBoolean(HuamiConst.PREF_USE_CUSTOM_FONT, false); 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 55a443bfa..a8cdbe394 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 @@ -22,6 +22,7 @@ import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCharacteristic; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.net.Uri; @@ -37,9 +38,11 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.ArrayList; +import java.util.Arrays; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; +import java.util.HashSet; import java.util.List; import java.util.Locale; import java.util.Set; @@ -2134,7 +2137,9 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport { } protected HuamiSupport setDisplayItems(TransactionBuilder builder) { - Set pages = HuamiCoordinator.getDisplayItems(gbDevice.getAddress()); + SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(gbDevice.getAddress()); + Set pages = prefs.getStringSet(HuamiConst.PREF_DISPLAY_ITEMS, new HashSet<>(Arrays.asList(getContext().getResources().getStringArray(R.array.pref_mi2_display_items_default)))); + LOG.info("Setting display items to " + (pages == null ? "none" : pages)); byte[] data = HuamiService.COMMAND_CHANGE_SCREENS.clone(); @@ -2155,9 +2160,9 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport { if (pages.contains(MiBandConst.PREF_MI2_DISPLAY_ITEM_BATTERY)) { data[HuamiService.SCREEN_CHANGE_BYTE] |= HuamiService.DISPLAY_ITEM_BIT_BATTERY; } + builder.write(getCharacteristic(HuamiService.UUID_CHARACTERISTIC_3_CONFIGURATION), data); } - builder.write(getCharacteristic(HuamiService.UUID_CHARACTERISTIC_3_CONFIGURATION), data); return this; } 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 5b82823cb..7552c4951 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,15 +18,20 @@ 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 nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiCoordinator; +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; @@ -74,7 +79,9 @@ public class AmazfitBipSupport extends HuamiSupport { return this; } - Set pages = HuamiCoordinator.getDisplayItems(gbDevice.getAddress()); + 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(); @@ -112,9 +119,9 @@ public class AmazfitBipSupport extends HuamiSupport { if (pages.contains("shortcut_alipay")) { shortcut_alipay = true; } + builder.write(getCharacteristic(HuamiService.UUID_CHARACTERISTIC_3_CONFIGURATION), command); + setShortcuts(builder, shortcut_weather, shortcut_alipay); } - builder.write(getCharacteristic(HuamiService.UUID_CHARACTERISTIC_3_CONFIGURATION), command); - 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 cc0729094..1ccbe9484 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,15 +17,20 @@ 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 nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiCoordinator; +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; @@ -39,9 +44,10 @@ public class AmazfitCorSupport extends AmazfitBipSupport { @Override protected AmazfitCorSupport setDisplayItems(TransactionBuilder builder) { - - Set pages = HuamiCoordinator.getDisplayItems(getDevice().getAddress()); + 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) { @@ -72,8 +78,9 @@ public class AmazfitCorSupport extends AmazfitBipSupport { if (pages.contains("music")) { command[2] |= 0x02; } + + builder.write(getCharacteristic(HuamiService.UUID_CHARACTERISTIC_3_CONFIGURATION), command); } - builder.write(getCharacteristic(HuamiService.UUID_CHARACTERISTIC_3_CONFIGURATION), command); return this; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/amazfitgts/AmazfitGTSSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/amazfitgts/AmazfitGTSSupport.java index 6247b29f5..6f5987df5 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/amazfitgts/AmazfitGTSSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/amazfitgts/AmazfitGTSSupport.java @@ -17,17 +17,21 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.huami.amazfitgts; 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 nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiCoordinator; +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.amazfitgts.AmazfitGTSFWHelper; import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec; import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder; @@ -70,7 +74,8 @@ public class AmazfitGTSSupport extends AmazfitBipSupport { return this; } - Set pages = HuamiCoordinator.getDisplayItems(gbDevice.getAddress()); + SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(gbDevice.getAddress()); + Set pages = prefs.getStringSet(HuamiConst.PREF_DISPLAY_ITEMS, new HashSet<>(Arrays.asList(getContext().getResources().getStringArray(R.array.pref_gts_display_items_default)))); LOG.info("Setting display items to " + (pages == null ? "none" : pages)); byte[] command = new byte[]{ 0x1E, @@ -114,8 +119,8 @@ public class AmazfitGTSSupport extends AmazfitBipSupport { pos += 4; } } + writeToChunked(builder, 2, command); } - writeToChunked(builder, 2, command); 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 ad54a33f1..4bc4b33e5 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,6 +17,7 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.huami.miband3; import android.content.Context; +import android.content.SharedPreferences; import android.net.Uri; import android.widget.Toast; @@ -24,19 +25,22 @@ 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 nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiCoordinator; +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; 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.model.NotificationSpec; import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder; import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.amazfitbip.AmazfitBipSupport; import nodomain.freeyourgadget.gadgetbridge.util.GB; @@ -52,7 +56,8 @@ public class MiBand3Support extends AmazfitBipSupport { @Override protected MiBand3Support setDisplayItems(TransactionBuilder builder) { - Set pages = HuamiCoordinator.getDisplayItems(gbDevice.getAddress()); + 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(); @@ -91,15 +96,14 @@ public class MiBand3Support extends AmazfitBipSupport { command[2] |= 0x01; command[11] = pos++; } - } - - for (int i = 4; i <= 11; i++) { - if (command[i] == 0) { - command[i] = pos++; + for (int i = 4; i <= 11; i++) { + if (command[i] == 0) { + command[i] = pos++; + } } - } - builder.write(getCharacteristic(HuamiService.UUID_CHARACTERISTIC_3_CONFIGURATION), command); + builder.write(getCharacteristic(HuamiService.UUID_CHARACTERISTIC_3_CONFIGURATION), command); + } return this; }