Amazfit Bip S: unify shortcuts code with display items code

This commit is contained in:
Andreas Shimokawa 2020-10-21 21:43:48 +02:00
parent 50f99a5b59
commit 1224cfeffc
5 changed files with 27 additions and 55 deletions

View File

@ -2313,15 +2313,24 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport {
return this;
}
protected HuamiSupport setDisplayItemsNew(TransactionBuilder builder, int defaultSettings, Map<String, Integer> keyIdMap) {
protected HuamiSupport setDisplayItemsNew(TransactionBuilder builder, boolean isShortcuts, 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));
Set<String> pages;
byte menuType;
if (isShortcuts) {
menuType = (byte) 0xfd;
pages = prefs.getStringSet(HuamiConst.PREF_SHORTCUTS, new HashSet<>(Arrays.asList(getContext().getResources().getStringArray(R.array.pref_bips_shortcuts_default))));
LOG.info("Setting shortcuts to " + (pages == null ? "none" : pages));
} else {
menuType = (byte) 0xff;
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];
@ -2336,7 +2345,7 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport {
if (pages.contains(key)) {
command[pos++] = (byte) index++;
command[pos++] = 0x00;
command[pos++] = (byte) 0xff;
command[pos++] = menuType;
command[pos++] = (byte) id;
}
}
@ -2348,7 +2357,7 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport {
if (!pages.contains(key)) {
command[pos++] = (byte) index++;
command[pos++] = 0x01;
command[pos++] = (byte) 0xff;
command[pos++] = (byte) menuType;
command[pos++] = (byte) id;
}
}

View File

@ -51,7 +51,7 @@ public class AmazfitBand5Support extends MiBand5Support {
keyIdMap.put("stress", 0x1c);
keyIdMap.put("cycles", 0x1d);
setDisplayItemsNew(builder, R.array.pref_amazfitband5_display_items_default, keyIdMap);
setDisplayItemsNew(builder, false, R.array.pref_amazfitband5_display_items_default, keyIdMap);
return this;
}

View File

@ -132,59 +132,22 @@ public class AmazfitBipSSupport extends AmazfitBipSupport {
keyIdMap.put("music", 0x0b);
keyIdMap.put("settings", 0x13);
setDisplayItemsNew(builder, R.array.pref_bips_display_items_default, keyIdMap);
setDisplayItemsNew(builder, false, R.array.pref_bips_display_items_default, keyIdMap);
return this;
}
@Override
protected AmazfitBipSSupport setShortcuts(TransactionBuilder builder) {
if (gbDevice.getFirmwareVersion() == null) {
LOG.warn("Device not initialized yet, won't set shortcuts");
return this;
}
SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(gbDevice.getAddress());
Set<String> pages = prefs.getStringSet(HuamiConst.PREF_SHORTCUTS, new HashSet<>(Arrays.asList(getContext().getResources().getStringArray(R.array.pref_bips_shortcuts_default))));
LOG.info("Setting shortcuts to " + (pages == null ? "none" : pages));
byte[] command = new byte[]{
0x1E,
0x00, 0x00, (byte) 0xFD, 0x01, // Status
0x01, 0x00, (byte) 0xFD, 0x11, // Alipay
0x02, 0x00, (byte) 0xFD, 0x10, // NFC
0x03, 0x00, (byte) 0xFD, 0x19, // PAI
0x04, 0x00, (byte) 0xFD, 0x02, // HR
0x05, 0x00, (byte) 0xFD, 0x0B, // Music
0x06, 0x00, (byte) 0xFD, 0x04, // Weather
};
String[] keys = {"status", "alipay", "nfc", "pai", "hr", "music", "weather"};
byte[] ids = {1, 17, 16, 25, 2, 11, 4};
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("alipay", 0x11);
keyIdMap.put("nfc", 0x10);
keyIdMap.put("pai", 0x19);
keyIdMap.put("hr", 0x02);
keyIdMap.put("music", 0x0b);
keyIdMap.put("weather", 0x04);
setDisplayItemsNew(builder, true, R.array.pref_bips_display_items_default, keyIdMap);
return this;
}

View File

@ -80,7 +80,7 @@ public class AmazfitGTSSupport extends AmazfitBipSupport {
keyIdMap.put("more", 0x07);
keyIdMap.put("settings", 0x13);
setDisplayItemsNew(builder, R.array.pref_gts_display_items_default, keyIdMap);
setDisplayItemsNew(builder, false, R.array.pref_gts_display_items_default, keyIdMap);
return this;
}

View File

@ -50,7 +50,7 @@ public class MiBand5Support extends MiBand4Support {
keyIdMap.put("stress", 0x1c);
keyIdMap.put("cycles", 0x1d);
setDisplayItemsNew(builder, R.array.pref_miband5_display_items_default, keyIdMap);
setDisplayItemsNew(builder, false, R.array.pref_miband5_display_items_default, keyIdMap);
return this;
}