1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-06-18 11:00:09 +02:00

Amazfit Band 5: Allow setting Sp=2

Also unify code to set display items on newer devices
This commit is contained in:
Andreas Shimokawa 2020-10-20 20:12:08 +02:00
parent d3f0cfd593
commit 4131f19f8d
10 changed files with 192 additions and 170 deletions

View File

@ -87,7 +87,7 @@ public class AmazfitBand5Coordinator extends HuamiCoordinator {
@Override
public int[] getSupportedDeviceSpecificSettings(GBDevice device) {
return new int[]{
R.xml.devicesettings_miband5,
R.xml.devicesettings_amazfitband5,
R.xml.devicesettings_wearlocation,
R.xml.devicesettings_custom_emoji_font,
R.xml.devicesettings_timeformat,

View File

@ -48,8 +48,10 @@ import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.SimpleTimeZone;
import java.util.SortedMap;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
@ -2312,6 +2314,51 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport {
return this;
}
protected HuamiSupport setDisplayItemsNew(TransactionBuilder builder, int defaultSettings, Map<String, Integer> keyIdMap) {
if (gbDevice.getFirmwareVersion() == null) {
LOG.warn("Device not initialized yet, won't set menu items");
return this;
}
SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(gbDevice.getAddress());
Set<String> pages = prefs.getStringSet(HuamiConst.PREF_DISPLAY_ITEMS, new HashSet<>(Arrays.asList(getContext().getResources().getStringArray(defaultSettings))));
LOG.info("Setting display items to " + (pages == null ? "none" : pages));
if (pages != null) {
byte[] command = new byte[keyIdMap.size() * 4 + 1];
command[0] = 0x1e;
// it seem that we first have to put all ENABLED items into the array
int pos = 1;
int index = 0;
for (Map.Entry<String, Integer> entry : keyIdMap.entrySet()) {
String key = entry.getKey();
int id = entry.getValue();
if (pages.contains(key)) {
command[pos++] = (byte) index++;
command[pos++] = 0x00;
command[pos++] = (byte) 0xff;
command[pos++] = (byte) id;
}
}
// And then all DISABLED ones
for (Map.Entry<String, Integer> entry : keyIdMap.entrySet()) {
String key = entry.getKey();
int id = entry.getValue();
if (!pages.contains(key)) {
command[pos++] = (byte) index++;
command[pos++] = 0x01;
command[pos++] = (byte) 0xff;
command[pos++] = (byte) id;
}
}
writeToChunked(builder, 2, command);
}
return this;
}
protected HuamiSupport setShortcuts(TransactionBuilder builder) {
return this;
}

View File

@ -19,13 +19,41 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.huami.amazfitband5;
import android.content.Context;
import android.net.Uri;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiFWHelper;
import nodomain.freeyourgadget.gadgetbridge.devices.huami.amazfitband5.AmazfitBand5FWHelper;
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.miband5.MiBand5Support;
public class AmazfitBand5Support extends MiBand5Support {
private static final Logger LOG = LoggerFactory.getLogger(AmazfitBand5Support.class);
@Override
protected AmazfitBand5Support setDisplayItems(TransactionBuilder builder) {
Map<String, Integer> keyIdMap = new LinkedHashMap<>();
keyIdMap.put("status", 0x01);
keyIdMap.put("pai", 0x19);
keyIdMap.put("hr", 0x02);
keyIdMap.put("spo2", 0x24);
keyIdMap.put("notifications", 0x06);
keyIdMap.put("breathing", 0x33);
keyIdMap.put("eventreminder", 0x15);
keyIdMap.put("weather", 0x04);
keyIdMap.put("workout", 0x03);
keyIdMap.put("more", 0x07);
keyIdMap.put("stress", 0x1c);
keyIdMap.put("cycles", 0x1d);
setDisplayItemsNew(builder, R.array.pref_amazfitband5_display_items_default, keyIdMap);
return this;
}
@Override
public HuamiFWHelper createFWHelper(Uri uri, Context context) throws IOException {

View File

@ -28,6 +28,8 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
@ -115,59 +117,22 @@ public class AmazfitBipSSupport extends AmazfitBipSupport {
@Override
protected AmazfitBipSSupport 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<String> pages = prefs.getStringSet(HuamiConst.PREF_DISPLAY_ITEMS, new HashSet<>(Arrays.asList(getContext().getResources().getStringArray(R.array.pref_bips_display_items_default))));
LOG.info("Setting display items to " + (pages == null ? "none" : pages));
byte[] command = new byte[]{
0x1E,
0x00, 0x00, (byte) 0xFF, 0x01, // Status
0x01, 0x00, (byte) 0xFF, 0x02, // HR
0x02, 0x00, (byte) 0xFF, 0x19, // PAI
0x03, 0x00, (byte) 0xFF, 0x03, // Workout
0x04, 0x00, (byte) 0xFF, 0x11, // Alipay
0x05, 0x00, (byte) 0xFF, 0x10, // NFC
0x06, 0x00, (byte) 0xFF, 0x04, // Weather
0x07, 0x00, (byte) 0xFF, 0x09, // Alarm
0x08, 0x00, (byte) 0xFF, 0x1B, // Timer
0x09, 0x00, (byte) 0xFF, 0x16, // Compass
0x0A, 0x00, (byte) 0xFF, 0x1A, // World clock
0x0B, 0x00, (byte) 0xFF, 0x0B, // Music
0x0C, 0x00, (byte) 0xFF, 0x13 // Settings
};
String[] keys = {"status", "hr", "pai", "workout", "alipay", "nfc", "weather", "alarm", "timer", "compass", "worldclock", "music", "settings"};
byte[] ids = {1, 2, 25, 3, 17, 16, 4, 9, 27, 22, 26, 11, 19};
if (pages != null) {
// it seem that we first have to put all ENABLED items into the array
int pos = 1;
for (int i = 0; i < keys.length; i++) {
String key = keys[i];
byte id = ids[i];
if (pages.contains(key)) {
command[pos + 1] = 0x00;
command[pos + 3] = id;
pos += 4;
}
}
// And then all DISABLED ones
for (int i = 0; i < keys.length; i++) {
String key = keys[i];
byte id = ids[i];
if (!pages.contains(key)) {
command[pos + 1] = 0x01;
command[pos + 3] = id;
pos += 4;
}
}
writeToChunked(builder, 2, command);
}
Map<String, Integer> keyIdMap = new LinkedHashMap<>();
keyIdMap.put("status", 0x01);
keyIdMap.put("hr", 0x02);
keyIdMap.put("pai", 0x19);
keyIdMap.put("workout", 0x03);
keyIdMap.put("alipay", 0x11);
keyIdMap.put("nfc", 0x10);
keyIdMap.put("weather", 0x04);
keyIdMap.put("alarm", 0x09);
keyIdMap.put("timer", 0x1b);
keyIdMap.put("compass", 0x16);
keyIdMap.put("worldclock", 0x1a);
keyIdMap.put("music", 0x0b);
keyIdMap.put("settings", 0x13);
setDisplayItemsNew(builder, R.array.pref_bips_display_items_default, keyIdMap);
return this;
}

View File

@ -17,20 +17,16 @@
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 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.amazfitgts.AmazfitGTSFWHelper;
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
@ -70,59 +66,21 @@ public class AmazfitGTSSupport extends AmazfitBipSupport {
@Override
protected AmazfitGTSSupport 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<String> 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,
0x00, 0x00, (byte) 0xFF, 0x01, // Status
0x01, 0x00, (byte) 0xFF, 0x19, // PAI
0x02, 0x00, (byte) 0xFF, 0x02, // HR
0x03, 0x00, (byte) 0xFF, 0x03, // Workout
0x04, 0x00, (byte) 0xFF, 0x14, // Activities
0x05, 0x00, (byte) 0xFF, 0x04, // Weather
0x06, 0x00, (byte) 0xFF, 0x0B, // Music
0x07, 0x00, (byte) 0xFF, 0x06, // Notifications
0x08, 0x00, (byte) 0xFF, 0x09, // Alarm
0x09, 0x00, (byte) 0xFF, 0x15, // Event reminder
0x0A, 0x00, (byte) 0xFF, 0x07, // More
0x0B, 0x00, (byte) 0xFF, 0x13 // Settings
};
String[] keys = {"status", "pai", "hr", "workout", "activity", "weather", "music", "notifications", "alarm", "eventreminder", "more", "settings"};
byte[] ids = {1, 25, 2, 3, 20, 4, 11, 6, 9, 21, 7, 19};
if (pages != null) {
pages.add("settings");
// it seem that we first have to put all ENABLED items into the array
int pos = 1;
for (int i = 0; i < keys.length; i++) {
String key = keys[i];
byte id = ids[i];
if (pages.contains(key)) {
command[pos + 1] = 0x00;
command[pos + 3] = id;
pos += 4;
}
}
// And then all DISABLED ones
for (int i = 0; i < keys.length; i++) {
String key = keys[i];
byte id = ids[i];
if (!pages.contains(key)) {
command[pos + 1] = 0x01;
command[pos + 3] = id;
pos += 4;
}
}
writeToChunked(builder, 2, command);
}
Map<String, Integer> keyIdMap = new LinkedHashMap<>();
keyIdMap.put("status", 0x01);
keyIdMap.put("pai", 0x19);
keyIdMap.put("hr", 0x02);
keyIdMap.put("workout", 0x03);
keyIdMap.put("activity", 0x14);
keyIdMap.put("weather", 0x04);
keyIdMap.put("music", 0x0b);
keyIdMap.put("notifications", 0x06);
keyIdMap.put("alarm", 0x09);
keyIdMap.put("eventreminder", 0x15);
keyIdMap.put("more", 0x07);
keyIdMap.put("settings", 0x13);
setDisplayItemsNew(builder, R.array.pref_gts_display_items_default, keyIdMap);
return this;
}

View File

@ -17,20 +17,16 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.huami.miband5;
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.miband5.MiBand5FWHelper;
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
@ -41,59 +37,20 @@ public class MiBand5Support extends MiBand4Support {
@Override
protected MiBand5Support 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<String> pages = prefs.getStringSet(HuamiConst.PREF_DISPLAY_ITEMS, new HashSet<>(Arrays.asList(getContext().getResources().getStringArray(R.array.pref_miband5_display_items_default))));
LOG.info("Setting display items to " + (pages == null ? "none" : pages));
byte[] command = new byte[]{
0x1E,
0x00, 0x00, (byte) 0xFF, 0x12, // Display clock?
0x01, 0x00, (byte) 0xFF, 0x01, // Status
0x02, 0x00, (byte) 0xFF, 0x19, // PAI
0x03, 0x00, (byte) 0xFF, 0x02, // HR
0x04, 0x00, (byte) 0xFF, 0x06, // Notifications
0x05, 0x00, (byte) 0xFF, 0x33, // Breathing
0x06, 0x00, (byte) 0xFF, 0x15, // Events
0x07, 0x00, (byte) 0xFF, 0x04, // Weather
0x08, 0x00, (byte) 0xFF, 0x03, // Workout
0x09, 0x00, (byte) 0xFF, 0x07, // More
0x0A, 0x00, (byte) 0xFF, 0x1c, // Stress
0x0B, 0x00, (byte) 0xFF, 0x1d // Cycles
};
String[] keys = {"displayclock", "status", "pai", "hr", "notifications", "breathing", "eventreminder", "weather", "workout", "more", "stress", "cycles"};
byte[] ids = {0x12, 0x01, 0x19, 0x02, 0x06, 0x33, 0x15, 0x04, 0x03, 0x07, 0x1c, 0x1d};
if (pages != null) {
pages.add("displayclock");
// it seem that we first have to put all ENABLED items into the array
int pos = 1;
for (int i = 0; i < keys.length; i++) {
String key = keys[i];
byte id = ids[i];
if (pages.contains(key)) {
command[pos + 1] = 0x00;
command[pos + 3] = id;
pos += 4;
}
}
// And then all DISABLED ones
for (int i = 0; i < keys.length; i++) {
String key = keys[i];
byte id = ids[i];
if (!pages.contains(key)) {
command[pos + 1] = 0x01;
command[pos + 3] = id;
pos += 4;
}
}
writeToChunked(builder, 2, command);
}
Map<String, Integer> keyIdMap = new LinkedHashMap<>();
keyIdMap.put("status", 0x01);
keyIdMap.put("pai", 0x19);
keyIdMap.put("hr", 0x02);
keyIdMap.put("notifications", 0x06);
keyIdMap.put("breathing", 0x33);
keyIdMap.put("eventreminder", 0x15);
keyIdMap.put("weather", 0x04);
keyIdMap.put("workout", 0x03);
keyIdMap.put("more", 0x07);
keyIdMap.put("stress", 0x1c);
keyIdMap.put("cycles", 0x1d);
setDisplayItemsNew(builder, R.array.pref_miband5_display_items_default, keyIdMap);
return this;
}

View File

@ -355,6 +355,51 @@
<item>@string/p_menuitem_cycles</item>
</string-array>
<string-array name="pref_amazfitband5_display_items">
<item>@string/menuitem_status</item>
<item>@string/menuitem_pai</item>
<item>@string/menuitem_hr</item>
<item>@string/menuitem_spo2</item>
<item>@string/menuitem_notifications</item>
<item>@string/menuitem_breathing</item>
<item>@string/menuitem_eventreminder</item>
<item>@string/menuitem_weather</item>
<item>@string/menuitem_workout</item>
<item>@string/menuitem_more</item>
<item>@string/menuitem_stress</item>
<item>@string/menuitem_cycles</item>
</string-array>
<string-array name="pref_amazfitband5_display_items_values">
<item>@string/p_menuitem_status</item>
<item>@string/p_menuitem_pai</item>
<item>@string/p_menuitem_hr</item>
<item>@string/p_menuitem_spo2</item>
<item>@string/p_menuitem_notifications</item>
<item>@string/p_menuitem_breathing</item>
<item>@string/p_menuitem_eventreminder</item>
<item>@string/p_menuitem_weather</item>
<item>@string/p_menuitem_workout</item>
<item>@string/p_menuitem_more</item>
<item>@string/p_menuitem_stress</item>
<item>@string/p_menuitem_cycles</item>
</string-array>
<string-array name="pref_amazfitband5_display_items_default">
<item>@string/p_menuitem_status</item>
<item>@string/p_menuitem_pai</item>
<item>@string/p_menuitem_hr</item>
<item>@string/p_menuitem_spo2</item>
<item>@string/p_menuitem_notifications</item>
<item>@string/p_menuitem_breathing</item>
<item>@string/p_menuitem_eventreminder</item>
<item>@string/p_menuitem_weather</item>
<item>@string/p_menuitem_workout</item>
<item>@string/p_menuitem_more</item>
<item>@string/p_menuitem_stress</item>
<item>@string/p_menuitem_cycles</item>
</string-array>
<string-array name="pref_bip_display_items">
<item>@string/menuitem_shortcut_alipay</item>
<item>@string/menuitem_shortcut_weather</item>

View File

@ -828,6 +828,7 @@
<string name="menuitem_stress">Stress</string>
<string name="menuitem_pai">PAI</string>
<string name="menuitem_hr">Heart Rate</string>
<string name="menuitem_spo2">SpO2</string>
<string name="menuitem_eventreminder">Event Reminder</string>
<string name="menuitem_workout">Workout</string>
<string name="menuitem_unknown">Unknown</string>

View File

@ -35,6 +35,7 @@
<item name="p_menuitem_more" type="string">more</item>
<item name="p_menuitem_nfc" type="string">nfc</item>
<item name="p_menuitem_hr" type="string">hr</item>
<item name="p_menuitem_spo2" type="string">spo2</item>
<item name="p_menuitem_pai" type="string">pai</item>
<item name="p_menuitem_stress" type="string">stress</item>
<item name="p_menuitem_cycles" type="string">cycles</item>

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<MultiSelectListPreference
android:icon="@drawable/ic_widgets"
android:defaultValue="@array/pref_amazfitband5_display_items_default"
android:dialogTitle="@string/mi2_prefs_display_items"
android:entries="@array/pref_amazfitband5_display_items"
android:entryValues="@array/pref_amazfitband5_display_items_values"
android:key="display_items"
android:summary="@string/mi2_prefs_display_items_summary"
android:title="@string/mi2_prefs_display_items" />
<ListPreference
android:icon="@drawable/ic_language"
android:defaultValue="auto"
android:entries="@array/pref_miband5_language"
android:entryValues="@array/pref_miband5_language_values"
android:key="language"
android:summary="%s"
android:title="@string/pref_title_language" />
</androidx.preference.PreferenceScreen>