1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-27 20:36:51 +01:00

Zepp OS: Refactor services config and init logic

This commit is contained in:
José Rebelo 2023-04-30 11:42:34 +01:00
parent 94c7b43ad4
commit 1d0b10ed0f
9 changed files with 192 additions and 92 deletions

View File

@ -69,6 +69,7 @@ import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
@ -163,7 +164,7 @@ public abstract class Huami2021Support extends HuamiSupport {
private final ZeppOsShortcutCardsService shortcutCardsService = new ZeppOsShortcutCardsService(this);
private final ZeppOsWatchfaceService watchfaceService = new ZeppOsWatchfaceService(this);
private final Map<Short, AbstractZeppOsService> mServiceMap = new HashMap<Short, AbstractZeppOsService>() {{
private final Map<Short, AbstractZeppOsService> mServiceMap = new LinkedHashMap<Short, AbstractZeppOsService>() {{
put(fileUploadService.getEndpoint(), fileUploadService);
put(configService.getEndpoint(), configService);
put(agpsService.getEndpoint(), agpsService);
@ -208,91 +209,11 @@ public abstract class Huami2021Support extends HuamiSupport {
final ZeppOsConfigService.ConfigSetter configSetter = configService.newSetter();
final Prefs prefs = getDevicePrefs();
// Handle button presses - these are not preferences
// See Huami2021SettingsCustomizer
switch (config) {
case DeviceSettingsPreferenceConst.PREF_BLUETOOTH_CALLS_PAIR:
if (!phoneService.isSupported()) {
GB.toast(getContext(), "Phone service is not supported.", Toast.LENGTH_LONG, GB.ERROR);
// Check if any of the services handles this config
for (AbstractZeppOsService service : mServiceMap.values()) {
if (service.onSendConfiguration(config, prefs)) {
return;
}
phoneService.startPairing();
return;
case DeviceSettingsPreferenceConst.WIFI_HOTSPOT_START:
final String ssid = getDevicePrefs().getString(DeviceSettingsPreferenceConst.WIFI_HOTSPOT_SSID, "");
if (StringUtils.isNullOrEmpty(ssid)) {
LOG.error("Wi-Fi hotspot SSID not specified");
return;
}
final String password = getDevicePrefs().getString(DeviceSettingsPreferenceConst.WIFI_HOTSPOT_PASSWORD, "");
if (StringUtils.isNullOrEmpty(password) || password.length() < 8) {
LOG.error("Wi-Fi hotspot password is not valid");
return;
}
wifiService.startWifiHotspot(ssid, password);
return;
case DeviceSettingsPreferenceConst.WIFI_HOTSPOT_STOP:
wifiService.stopWifiHotspot();
return;
case DeviceSettingsPreferenceConst.FTP_SERVER_START:
ftpServerService.startFtpServer(getDevicePrefs().getString(DeviceSettingsPreferenceConst.FTP_SERVER_ROOT_DIR, ""));
return;
case DeviceSettingsPreferenceConst.FTP_SERVER_STOP:
ftpServerService.stopFtpServer();
return;
case DeviceSettingsPreferenceConst.WIFI_HOTSPOT_SSID:
case DeviceSettingsPreferenceConst.WIFI_HOTSPOT_PASSWORD:
case DeviceSettingsPreferenceConst.WIFI_HOTSPOT_STATUS:
case DeviceSettingsPreferenceConst.FTP_SERVER_ROOT_DIR:
case DeviceSettingsPreferenceConst.FTP_SERVER_ADDRESS:
case DeviceSettingsPreferenceConst.FTP_SERVER_USERNAME:
case DeviceSettingsPreferenceConst.FTP_SERVER_STATUS:
// Ignore preferences that are not reloadable
return;
}
// morning updates preferences, they do not use the configService
switch (config) {
case DeviceSettingsPreferenceConst.MORNING_UPDATES_ENABLED:
final boolean morningUpdatesEnabled = prefs.getBoolean(config, false);
LOG.info("Setting morning updates enabled = {}", morningUpdatesEnabled);
morningUpdatesService.setEnabled(morningUpdatesEnabled);
return;
case DeviceSettingsPreferenceConst.MORNING_UPDATES_CATEGORIES_SORTABLE:
final List<String> categories = new ArrayList<>(prefs.getList(config, Collections.emptyList()));
final List<String> allCategories = new ArrayList<>(prefs.getList(Huami2021Coordinator.getPrefPossibleValuesKey(config), Collections.emptyList()));
LOG.info("Setting morning updates categories = {}", categories);
morningUpdatesService.setCategories(categories, allCategories);
return;
}
// phoneService preferences, they do not use the configService
switch (config) {
case DeviceSettingsPreferenceConst.PREF_BLUETOOTH_CALLS_ENABLED:
final boolean bluetoothCallsEnabled = prefs.getBoolean(DeviceSettingsPreferenceConst.PREF_BLUETOOTH_CALLS_ENABLED, false);
LOG.info("Setting bluetooth calls enabled = {}", bluetoothCallsEnabled);
phoneService.setEnabled(bluetoothCallsEnabled);
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;
}
// watchfaceService preferences, they do not use the configService
switch (config) {
case DeviceSettingsPreferenceConst.PREF_WATCHFACE:
final String watchface = prefs.getString(DeviceSettingsPreferenceConst.PREF_WATCHFACE, null);
LOG.info("Setting watchface to {}", watchface);
watchfaceService.setWatchface(watchface);
return;
}
// Other preferences
@ -1537,7 +1458,6 @@ public abstract class Huami2021Support extends HuamiSupport {
configService.requestAllConfigs(builder);
requestCapabilityReminders(builder);
fileUploadService.requestCapability(builder);
for (final HuamiVibrationPatternNotificationType type : coordinator.getVibrationPatternNotificationTypes(gbDevice)) {
// FIXME: Can we read these from the band?
@ -1553,17 +1473,16 @@ public abstract class Huami2021Support extends HuamiSupport {
}
requestAlarms(builder);
//requestReminders(builder);
for (AbstractZeppOsService service : mServiceMap.values()) {
service.initialize(builder);
}
if (coordinator.supportsBluetoothPhoneCalls(gbDevice)) {
phoneService.requestCapabilities(builder);
phoneService.requestEnabled(builder);
}
//contactsService.requestCapabilities(builder);
morningUpdatesService.getEnabled(builder);
morningUpdatesService.getCategories(builder);
shortcutCardsService.requestCapabilities(builder);
shortcutCardsService.requestShortcutCards(builder);
watchfaceService.requestWatchfaces(builder);
watchfaceService.requestCurrentWatchface(builder);
}
@Override

View File

@ -18,6 +18,7 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.huami.zeppos;
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.Huami2021Support;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
public abstract class AbstractZeppOsService {
private final Huami2021Support mSupport;
@ -32,6 +33,20 @@ public abstract class AbstractZeppOsService {
public abstract void handlePayload(final byte[] payload);
/**
* Handle a preference change.
* @param config the preference key
* @param prefs the device preferences
* @return true if the preference was handled, false otherwise
*/
public boolean onSendConfiguration(final String config, final Prefs prefs) {
return false;
}
public void initialize(final TransactionBuilder builder) {
// Do nothing by default
}
protected Huami2021Support getSupport() {
return mSupport;
}

View File

@ -31,6 +31,7 @@ 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.CheckSums;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
public class ZeppOsFileUploadService extends AbstractZeppOsService {
private static final Logger LOG = LoggerFactory.getLogger(ZeppOsFileUploadService.class);
@ -117,6 +118,11 @@ public class ZeppOsFileUploadService extends AbstractZeppOsService {
}
}
@Override
public void initialize(final TransactionBuilder builder) {
requestCapability(builder);
}
public void requestCapability(final TransactionBuilder builder) {
write(builder, new byte[]{CMD_CAPABILITIES_REQUEST});
}

View File

@ -26,9 +26,11 @@ import java.nio.charset.StandardCharsets;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventUpdatePreferences;
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.GB;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
public class ZeppOsFtpServerService extends AbstractZeppOsService {
@ -115,6 +117,30 @@ public class ZeppOsFtpServerService extends AbstractZeppOsService {
}
}
@Override
public boolean onSendConfiguration(final String config, final Prefs prefs) {
switch (config) {
case DeviceSettingsPreferenceConst.FTP_SERVER_START:
startFtpServer(prefs.getString(DeviceSettingsPreferenceConst.FTP_SERVER_ROOT_DIR, ""));
return true;
case DeviceSettingsPreferenceConst.FTP_SERVER_STOP:
stopFtpServer();
return true;
case DeviceSettingsPreferenceConst.FTP_SERVER_ROOT_DIR:
case DeviceSettingsPreferenceConst.FTP_SERVER_ADDRESS:
case DeviceSettingsPreferenceConst.FTP_SERVER_USERNAME:
case DeviceSettingsPreferenceConst.FTP_SERVER_STATUS:
// Ignore preferences that are not reloadable
return true;
}
return false;
}
@Override
public void initialize(final TransactionBuilder builder) {
}
public void setCallback(final Callback callback) {
mCallback = callback;
}

View File

@ -22,6 +22,7 @@ import org.slf4j.LoggerFactory;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -35,6 +36,7 @@ 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.MapUtils;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
public class ZeppOsMorningUpdatesService extends AbstractZeppOsService {
@ -109,6 +111,31 @@ public class ZeppOsMorningUpdatesService extends AbstractZeppOsService {
}
}
@Override
public boolean onSendConfiguration(final String config, final Prefs prefs) {
switch (config) {
case DeviceSettingsPreferenceConst.MORNING_UPDATES_ENABLED:
final boolean morningUpdatesEnabled = prefs.getBoolean(config, false);
LOG.info("Setting morning updates enabled = {}", morningUpdatesEnabled);
setEnabled(morningUpdatesEnabled);
return true;
case DeviceSettingsPreferenceConst.MORNING_UPDATES_CATEGORIES_SORTABLE:
final List<String> categories = new ArrayList<>(prefs.getList(config, Collections.emptyList()));
final List<String> allCategories = new ArrayList<>(prefs.getList(Huami2021Coordinator.getPrefPossibleValuesKey(config), Collections.emptyList()));
LOG.info("Setting morning updates categories = {}", categories);
setCategories(categories, allCategories);
return true;
}
return false;
}
@Override
public void initialize(final TransactionBuilder builder) {
getEnabled(builder);
getCategories(builder);
}
public void getEnabled(final TransactionBuilder builder) {
write(builder, CMD_ENABLED_GET);
}

View File

@ -17,6 +17,7 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.huami.zeppos.services;
import android.bluetooth.BluetoothAdapter;
import android.widget.Toast;
import androidx.annotation.Nullable;
@ -32,6 +33,7 @@ import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventUpdatePref
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.GB;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
public class ZeppOsPhoneService extends AbstractZeppOsService {
@ -113,6 +115,31 @@ public class ZeppOsPhoneService extends AbstractZeppOsService {
}
}
@Override
public boolean onSendConfiguration(final String config, final Prefs prefs) {
switch (config) {
case DeviceSettingsPreferenceConst.PREF_BLUETOOTH_CALLS_PAIR:
if (!isSupported()) {
LOG.warn("Phone service is not supported.");
return false;
}
startPairing();
return true;
case DeviceSettingsPreferenceConst.PREF_BLUETOOTH_CALLS_ENABLED:
final boolean bluetoothCallsEnabled = prefs.getBoolean(DeviceSettingsPreferenceConst.PREF_BLUETOOTH_CALLS_ENABLED, false);
LOG.info("Setting bluetooth calls enabled = {}", bluetoothCallsEnabled);
setEnabled(bluetoothCallsEnabled);
return true;
}
return false;
}
@Override
public void initialize(final TransactionBuilder builder) {
}
public boolean isSupported() {
return version == 1;
}

View File

@ -27,11 +27,13 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventUpdatePreferences;
import nodomain.freeyourgadget.gadgetbridge.devices.huami.Huami2021Coordinator;
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
@ -150,6 +152,25 @@ public class ZeppOsShortcutCardsService extends AbstractZeppOsService {
}
}
@Override
public boolean onSendConfiguration(final String config, final Prefs prefs) {
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);
setShortcutCards(shortcutCards);
return true;
}
return false;
}
@Override
public void initialize(final TransactionBuilder builder) {
requestCapabilities(builder);
requestShortcutCards(builder);
}
public void requestCapabilities(final TransactionBuilder builder) {
write(builder, CMD_CAPABILITIES_REQUEST);
}

View File

@ -29,12 +29,14 @@ import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventUpdatePreferences;
import nodomain.freeyourgadget.gadgetbridge.devices.huami.Huami2021Coordinator;
import nodomain.freeyourgadget.gadgetbridge.service.btle.BLETypeConversions;
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;
public class ZeppOsWatchfaceService extends AbstractZeppOsService {
private static final Logger LOG = LoggerFactory.getLogger(ZeppOsWatchfaceService.class);
@ -122,6 +124,25 @@ public class ZeppOsWatchfaceService extends AbstractZeppOsService {
}
}
@Override
public boolean onSendConfiguration(final String config, final Prefs prefs) {
switch (config) {
case DeviceSettingsPreferenceConst.PREF_WATCHFACE:
final String watchface = prefs.getString(DeviceSettingsPreferenceConst.PREF_WATCHFACE, null);
LOG.info("Setting watchface to {}", watchface);
setWatchface(watchface);
return true;
}
return false;
}
@Override
public void initialize(final TransactionBuilder builder) {
requestWatchfaces(builder);
requestCurrentWatchface(builder);
}
public void requestWatchfaces(final TransactionBuilder builder) {
write(builder, CMD_LIST_GET);
}

View File

@ -28,9 +28,12 @@ import java.util.Locale;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventUpdatePreferences;
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.GB;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
public class ZeppOsWifiService extends AbstractZeppOsService {
private static final Logger LOG = LoggerFactory.getLogger(ZeppOsWifiService.class);
@ -98,6 +101,41 @@ public class ZeppOsWifiService extends AbstractZeppOsService {
}
}
@Override
public boolean onSendConfiguration(final String config, final Prefs prefs) {
switch (config) {
case DeviceSettingsPreferenceConst.WIFI_HOTSPOT_START:
final String ssid = prefs.getString(DeviceSettingsPreferenceConst.WIFI_HOTSPOT_SSID, "");
if (StringUtils.isNullOrEmpty(ssid)) {
LOG.error("Wi-Fi hotspot SSID not specified");
return true;
}
final String password = prefs.getString(DeviceSettingsPreferenceConst.WIFI_HOTSPOT_PASSWORD, "");
if (StringUtils.isNullOrEmpty(password) || password.length() < 8) {
LOG.error("Wi-Fi hotspot password is not valid");
return true;
}
startWifiHotspot(ssid, password);
return true;
case DeviceSettingsPreferenceConst.WIFI_HOTSPOT_STOP:
stopWifiHotspot();
return true;
case DeviceSettingsPreferenceConst.WIFI_HOTSPOT_SSID:
case DeviceSettingsPreferenceConst.WIFI_HOTSPOT_PASSWORD:
case DeviceSettingsPreferenceConst.WIFI_HOTSPOT_STATUS:
// Ignore preferences that are not reloadable
return true;
}
return false;
}
@Override
public void initialize(final TransactionBuilder builder) {
}
public void setCallback(final Callback callback) {
mCallback = callback;
}