1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-23 18:36:50 +01:00

Zepp OS: Add shortcut cards preference

This commit is contained in:
José Rebelo 2023-04-24 19:12:22 +01:00
parent ad8069b38d
commit 3c3936bf6a
9 changed files with 353 additions and 0 deletions

View File

@ -249,6 +249,8 @@ public class DeviceSettingsPreferenceConst {
public static final String MORNING_UPDATES_ENABLED = "morning_updates_enabled";
public static final String MORNING_UPDATES_CATEGORIES_SORTABLE = "morning_updates_categories";
public static final String SHORTCUT_CARDS_SORTABLE = "shortcut_cards_sortable";
public static final String FTP_SERVER_ROOT_DIR = "ftp_server_root_dir";
public static final String FTP_SERVER_ADDRESS = "ftp_server_address";
public static final String FTP_SERVER_USERNAME = "ftp_server_username";

View File

@ -501,6 +501,8 @@ public class DeviceSpecificSettingsFragment extends PreferenceFragmentCompat imp
addPreferenceHandlerFor(MORNING_UPDATES_ENABLED);
addPreferenceHandlerFor(MORNING_UPDATES_CATEGORIES_SORTABLE);
addPreferenceHandlerFor(SHORTCUT_CARDS_SORTABLE);
addPreferenceHandlerFor(PREF_SONY_AMBIENT_SOUND_CONTROL);
addPreferenceHandlerFor(PREF_SONY_AMBIENT_SOUND_CONTROL_BUTTON_MODE);
addPreferenceHandlerFor(PREF_SONY_FOCUS_VOICE);

View File

@ -42,6 +42,7 @@ import nodomain.freeyourgadget.gadgetbridge.entities.Device;
import nodomain.freeyourgadget.gadgetbridge.entities.HuamiExtendedActivitySampleDao;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySummaryParser;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.zeppos.services.ZeppOsShortcutCardsService;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.zeppos.services.ZeppOsConfigService;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.HuamiLanguageType;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.HuamiVibrationPatternNotificationType;
@ -194,6 +195,9 @@ public abstract class Huami2021Coordinator extends HuamiCoordinator {
if (supportsControlCenter()) {
settings.add(R.xml.devicesettings_huami2021_control_center);
}
if (supportsShortcutCards(device)) {
settings.add(R.xml.devicesettings_huami2021_shortcut_cards);
}
settings.add(R.xml.devicesettings_nightmode);
settings.add(R.xml.devicesettings_sleep_mode);
settings.add(R.xml.devicesettings_liftwrist_display_sensitivity_with_smart);
@ -370,6 +374,10 @@ public abstract class Huami2021Coordinator extends HuamiCoordinator {
return ZeppOsPhoneService.isSupported(getPrefs(device));
}
public boolean supportsShortcutCards(final GBDevice device) {
return ZeppOsShortcutCardsService.isSupported(getPrefs(device));
}
private boolean supportsConfig(final GBDevice device, final ZeppOsConfigService.ConfigArg config) {
return ZeppOsConfigService.deviceHasConfig(getPrefs(device), config);
}

View File

@ -63,6 +63,7 @@ public class Huami2021SettingsCustomizer extends HuamiSettingsCustomizer {
removeUnsupportedElementsFromListPreference(HuamiConst.PREF_DISPLAY_ITEMS_SORTABLE, handler, prefs);
removeUnsupportedElementsFromListPreference(HuamiConst.PREF_SHORTCUTS_SORTABLE, handler, prefs);
removeUnsupportedElementsFromListPreference(HuamiConst.PREF_CONTROL_CENTER_SORTABLE, handler, prefs);
removeUnsupportedElementsFromListPreference(DeviceSettingsPreferenceConst.SHORTCUT_CARDS_SORTABLE, handler, prefs);
removeUnsupportedElementsFromListPreference(DeviceSettingsPreferenceConst.MORNING_UPDATES_CATEGORIES_SORTABLE, handler, prefs);
for (final ZeppOsConfigService.ConfigArg config : ZeppOsConfigService.ConfigArg.values()) {

View File

@ -17,6 +17,7 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.huami;
import static org.apache.commons.lang3.ArrayUtils.subarray;
import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.SHORTCUT_CARDS_SORTABLE;
import static nodomain.freeyourgadget.gadgetbridge.devices.huami.Huami2021Service.*;
import static nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiService.SUCCESS;
import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_NAME;
@ -119,6 +120,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.operations.Upd
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.zeppos.AbstractZeppOsService;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.zeppos.operations.ZeppOsAgpsUpdateOperation;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.zeppos.services.ZeppOsAgpsService;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.zeppos.services.ZeppOsShortcutCardsService;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.zeppos.services.ZeppOsConfigService;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.zeppos.services.ZeppOsContactsService;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.zeppos.services.ZeppOsFileUploadService;
@ -157,6 +159,7 @@ public abstract class Huami2021Support extends HuamiSupport {
private final ZeppOsContactsService contactsService = new ZeppOsContactsService(this);
private final ZeppOsMorningUpdatesService morningUpdatesService = new ZeppOsMorningUpdatesService(this);
private final ZeppOsPhoneService phoneService = new ZeppOsPhoneService(this);
private final ZeppOsShortcutCardsService shortcutCardsService = new ZeppOsShortcutCardsService(this);
private final Map<Short, AbstractZeppOsService> mServiceMap = new HashMap<Short, AbstractZeppOsService>() {{
put(fileUploadService.getEndpoint(), fileUploadService);
@ -167,6 +170,7 @@ public abstract class Huami2021Support extends HuamiSupport {
put(contactsService.getEndpoint(), contactsService);
put(morningUpdatesService.getEndpoint(), morningUpdatesService);
put(phoneService.getEndpoint(), phoneService);
put(shortcutCardsService.getEndpoint(), shortcutCardsService);
}};
public Huami2021Support() {
@ -270,6 +274,15 @@ public abstract class Huami2021Support extends HuamiSupport {
return;
}
// shortcutCardsService preferences, they do not use the configService
switch (config) {
case DeviceSettingsPreferenceConst.SHORTCUT_CARDS_SORTABLE:
final List<String> shortcutCards = prefs.getList(SHORTCUT_CARDS_SORTABLE, Collections.emptyList());
LOG.info("Setting shortcut cards to {}", shortcutCards);
shortcutCardsService.setShortcutCards(shortcutCards);
return;
}
// Other preferences
switch (config) {
case HuamiConst.PREF_CONTROL_CENTER_SORTABLE:
@ -1530,6 +1543,8 @@ public abstract class Huami2021Support extends HuamiSupport {
//contactsService.requestCapabilities(builder);
morningUpdatesService.getEnabled(builder);
morningUpdatesService.getCategories(builder);
shortcutCardsService.requestCapabilities(builder);
shortcutCardsService.requestShortcutCards(builder);
}
@Override

View File

@ -0,0 +1,251 @@
/* Copyright (C) 2023 José Rebelo
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 <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.service.devices.huami.zeppos.services;
import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.SHORTCUT_CARDS_SORTABLE;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventUpdatePreferences;
import nodomain.freeyourgadget.gadgetbridge.devices.huami.Huami2021Coordinator;
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.Huami2021Support;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.zeppos.AbstractZeppOsService;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
public class ZeppOsShortcutCardsService extends AbstractZeppOsService {
private static final Logger LOG = LoggerFactory.getLogger(ZeppOsShortcutCardsService.class);
private static final short ENDPOINT = 0x0009;
public static final byte CMD_CAPABILITIES_REQUEST = 0x01;
public static final byte CMD_CAPABILITIES_RESPONSE = 0x02;
public static final byte CMD_LIST_GET = 0x07;
public static final byte CMD_LIST_RET = 0x08;
public static final byte CMD_ENABLED_SET = 0x09;
public static final byte CMD_ENABLED_SET_ACK = 0x0a;
public static final String PREF_VERSION = "zepp_os_shortcut_cards_version";
public enum ShortcutCard {
WEATHER("2", "1"),
FORECAST("2", "3"),
PAI("3", "1"),
ALARM("4", "1"),
SLEEP("5", "1"),
HEARTRATE("6", "1"),
ACTIVITY("7", "1"),
SPO2("8", "1"),
PHONE("9", "1"),
EVENTS("10", "1"),
STRESS("11", "1"),
WORLDCLOCK("13", "1"),
TODO("17", "1"),
COUNTDOWN("18", "1"),
LAST_WORKOUT("19", "1"),
TOTAL_WORKOUT("19", "2"),
WORKOUT_STATUS("19", "3"),
VO2_MAX("19", "4"),
MUSIC("20", "1"),
CYCLE_TRACKING("21", "1"),
ONE_TAP_MEASURING("22", "1"),
BREATHING("24", "1"),
STOPWATCH("25", "1"),
RECOMMENDATION("28", "1"),
;
private final String appNum;
private final String cardNum;
ShortcutCard(final String appNum, final String cardNum) {
this.appNum = appNum;
this.cardNum = cardNum;
}
public String getAppNum() {
return appNum;
}
public String getCardNum() {
return cardNum;
}
public static ShortcutCard fromCodes(final String appNum, final String cardNum) {
for (ShortcutCard value : ShortcutCard.values()) {
if (value.getAppNum().equals(appNum) && value.getCardNum().equals(cardNum)) {
return value;
}
}
return null;
}
}
private int version = 0;
private int maxCards = 0;
public ZeppOsShortcutCardsService(final Huami2021Support support) {
super(support);
}
@Override
public short getEndpoint() {
return ENDPOINT;
}
@Override
public boolean isEncrypted() {
return true;
}
@Override
public void handlePayload(final byte[] payload) {
switch (payload[0]) {
case CMD_CAPABILITIES_RESPONSE:
version = payload[1];
getSupport().evaluateGBDeviceEvent(new GBDeviceEventUpdatePreferences(PREF_VERSION, version));
if (version != 3) {
LOG.warn("Unsupported shortcut cards service version {}", version);
return;
}
maxCards = payload[2] & 0xFF;
LOG.info("Contacts version={}, maxCards={}", version, maxCards);
break;
case CMD_LIST_RET:
LOG.info("Got shortcut cards list");
parseShortcutCards(payload);
break;
case CMD_ENABLED_SET_ACK:
LOG.info("Got enabled shortcut cards ack, status = {}", payload[1]);
break;
default:
LOG.warn("Unexpected contacts byte {}", String.format("0x%02x", payload[0]));
}
}
public void requestCapabilities(final TransactionBuilder builder) {
write(builder, CMD_CAPABILITIES_REQUEST);
}
public void requestShortcutCards(final TransactionBuilder builder) {
write(builder, CMD_LIST_GET);
}
public void parseShortcutCards(final byte[] payload) {
final ByteBuffer buf = ByteBuffer.wrap(payload).order(ByteOrder.LITTLE_ENDIAN);
buf.get(); // discard the command byte
final int numCards = buf.get() & 0xFF;
final List<String> allCards = new ArrayList<>();
final List<String> enabledCards = new ArrayList<>();
for (int i = 0; i < numCards; i++) {
final String appNum = StringUtils.untilNullTerminator(buf);
final String cardNum = StringUtils.untilNullTerminator(buf);
final boolean enabled = buf.get() == 0x01;
final byte b = buf.get();
if (b != 0) {
LOG.warn("Unexpected byte {} at pos {}", b, buf.position() - 1);
return;
}
final ShortcutCard card = ShortcutCard.fromCodes(appNum, cardNum);
final String cardPrefValue;
if (card != null) {
cardPrefValue = card.name().toLowerCase(Locale.ROOT);
} else {
LOG.warn("Unknown shortcut card [{}, {}]", appNum, cardNum);
cardPrefValue = appNum + "/" + cardNum;
}
allCards.add(cardPrefValue);
if (enabled) {
enabledCards.add(cardPrefValue);
}
}
final GBDeviceEventUpdatePreferences evt = new GBDeviceEventUpdatePreferences()
.withPreference(SHORTCUT_CARDS_SORTABLE, String.join(",", enabledCards))
.withPreference(Huami2021Coordinator.getPrefPossibleValuesKey(SHORTCUT_CARDS_SORTABLE), String.join(",", allCards));
getSupport().evaluateGBDeviceEvent(evt);
}
public void setShortcutCards(final List<String> cardsPrefValue) {
if (maxCards == 0) {
LOG.warn("maxCards == 0, refusing");
return;
}
final List<String> cards = new ArrayList<>(cardsPrefValue);
if (cards.size() > maxCards) {
LOG.warn("Number of cards {} > maxCards {}, truncating", cards.size(), maxCards);
cards.subList(maxCards, cards.size()).clear();
}
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(CMD_ENABLED_SET);
baos.write(cards.size());
for (final String cardPrefValue : cards) {
String appNum;
String cardNum;
try {
final ShortcutCard card = ShortcutCard.valueOf(cardPrefValue.toUpperCase(Locale.ROOT));
appNum = card.getAppNum();
cardNum = card.getCardNum();
} catch (final IllegalArgumentException e) {
// attempt to parse as appNum/cardNum
final Matcher matcher = Pattern.compile("^([0-9a-fA-F]+)/([0-9a-fA-F]+)$").matcher(cardPrefValue);
if (matcher.find()) {
appNum = matcher.group(1);
cardNum = matcher.group(2);
} else {
LOG.warn("Unexpected format for shortcut cards pref value {}", cardPrefValue);
return;
}
}
try {
baos.write(appNum.getBytes(StandardCharsets.UTF_8));
baos.write(0);
baos.write(cardNum.getBytes(StandardCharsets.UTF_8));
baos.write(0);
baos.write(1); // enabled
baos.write(0); // ?
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
write("set enabled shortcut cards", baos.toByteArray());
}
public static boolean isSupported(final Prefs devicePrefs) {
return devicePrefs.getInt(PREF_VERSION, 0) == 3;
}
}

View File

@ -846,6 +846,60 @@
<item>more</item>
</string-array>
<string-array name="pref_zepp_os_shortcut_cards">
<item>@string/menuitem_weather</item>
<item>@string/menuitem_forecast</item>
<item>@string/menuitem_pai</item>
<item>@string/menuitem_alarm</item>
<item>@string/menuitem_sleep</item>
<item>@string/menuitem_hr</item>
<item>@string/menuitem_activity</item>
<item>@string/menuitem_spo2</item>
<item>@string/menuitem_phone</item>
<item>@string/menuitem_events</item>
<item>@string/menuitem_stress</item>
<item>@string/menuitem_worldclock</item>
<item>@string/menuitem_todo</item>
<item>@string/menuitem_countdown</item>
<item>@string/menuitem_last_workout</item>
<item>@string/menuitem_total_workout</item>
<item>@string/menuitem_workout_status</item>
<item>@string/menuitem_vo2_max</item>
<item>@string/menuitem_music</item>
<item>@string/menuitem_cycles</item>
<item>@string/menuitem_one_tap_measuring</item>
<item>@string/menuitem_breathing</item>
<item>@string/menuitem_stopwatch</item>
<item>@string/menuitem_recommendation</item>
</string-array>
<string-array name="pref_zepp_os_shortcut_cards_values">
<item>weather</item>
<item>forecast</item>
<item>pai</item>
<item>alarm</item>
<item>sleep</item>
<item>heartrate</item>
<item>activity</item>
<item>spo2</item>
<item>phone</item>
<item>events</item>
<item>stress</item>
<item>worldclock</item>
<item>todo</item>
<item>countdown</item>
<item>last_workout</item>
<item>total_workout</item>
<item>workout_status</item>
<item>vo2_max</item>
<item>music</item>
<item>cycle_tracking</item>
<item>one_tap_measuring</item>
<item>breathing</item>
<item>stopwatch</item>
<item>recommendation</item>
</string-array>
<string-array name="pref_huami2021_control_center">
<item>@string/battery</item>
<item>@string/menuitem_dnd</item>

View File

@ -914,6 +914,8 @@
<string name="mi3_prefs_night_mode_summary">Lower band screen brightness automatically at night</string>
<string name="bip_prefs_shortcuts">Shortcuts</string>
<string name="bip_prefs_shotcuts_summary">Choose the shortcuts on the band screen</string>
<string name="prefs_shortcut_cards">Shortcut Cards</string>
<string name="prefs_shortcut_cards_summary">Shortcut cards seen when swiping right on the watchface. When an app is running, auto-generated cards are not affected by this setting.</string>
<string name="prefs_control_center">Control Center</string>
<string name="prefs_control_center_summary">Choose the items on the control center dropdown</string>
<string name="prefs_activate_display_on_lift_sensitivity">Sensitivity</string>
@ -1295,6 +1297,11 @@
<string name="menuitem_notifications">Notifications</string>
<string name="menuitem_activity">Workout History</string>
<string name="menuitem_weather">Weather</string>
<string name="menuitem_forecast">Forecast</string>
<string name="menuitem_last_workout">Last workout</string>
<string name="menuitem_total_workout">Total workout</string>
<string name="menuitem_vo2_max">VO₂ Max</string>
<string name="menuitem_recommendation">Recommendation</string>
<string name="menuitem_breathing">Breathing</string>
<string name="menuitem_cycles">Cycle Tracking</string>
<string name="menuitem_alarm">Alarm</string>

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<com.mobeta.android.dslv.DragSortListPreference
android:defaultValue="@array/pref_huami2021_empty_array"
android:dialogTitle="@string/bip_prefs_shortcuts"
android:entries="@array/pref_zepp_os_shortcut_cards"
android:entryValues="@array/pref_zepp_os_shortcut_cards_values"
android:icon="@drawable/ic_shortcut"
android:key="shortcut_cards_sortable"
android:persistent="true"
android:summary="@string/prefs_shortcut_cards_summary"
android:title="@string/prefs_shortcut_cards" />
</androidx.preference.PreferenceScreen>