From e1e5cccf29b6d2e1d323ed48e469999dab24775f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Rebelo?= Date: Wed, 16 Apr 2025 00:12:35 +0100 Subject: [PATCH] HPlus: Deprecate DeviceType constructor --- .idea/dictionaries/t.xml | 1 + .../devices/hplus/EXRIZUK8Coordinator.java | 9 +++++ .../devices/hplus/HPlusCoordinator.java | 2 -- .../devices/hplus/MakibesF68Coordinator.java | 18 +++++----- .../devices/hplus/Q8Coordinator.java | 11 ++++-- .../devices/hplus/SG2Coordinator.java | 13 ++++--- .../service/DeviceSupportFactory.java | 35 +++++-------------- .../devices/hplus/EXRIZUK8Support.java | 25 +++++++++++++ .../service/devices/hplus/HPlusSupport.java | 10 ++++-- .../devices/hplus/MakibesF68Support.java | 25 +++++++++++++ .../service/devices/hplus/Q8Support.java | 25 +++++++++++++ .../service/devices/hplus/SG2Support.java | 25 +++++++++++++ 12 files changed, 151 insertions(+), 48 deletions(-) create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/hplus/EXRIZUK8Support.java create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/hplus/MakibesF68Support.java create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/hplus/Q8Support.java create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/hplus/SG2Support.java diff --git a/.idea/dictionaries/t.xml b/.idea/dictionaries/t.xml index a9f3a021a..8d1672b97 100644 --- a/.idea/dictionaries/t.xml +++ b/.idea/dictionaries/t.xml @@ -93,6 +93,7 @@ lacross ladbsoft ladera + lemfo lenovo liebergeld lightforce diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/hplus/EXRIZUK8Coordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/hplus/EXRIZUK8Coordinator.java index ce7784fd1..b254171b2 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/hplus/EXRIZUK8Coordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/hplus/EXRIZUK8Coordinator.java @@ -21,10 +21,13 @@ package nodomain.freeyourgadget.gadgetbridge.devices.hplus; * @author Quallenauge <Hamsi2k@freenet.de> */ +import androidx.annotation.NonNull; import java.util.regex.Pattern; import nodomain.freeyourgadget.gadgetbridge.R; +import nodomain.freeyourgadget.gadgetbridge.service.DeviceSupport; +import nodomain.freeyourgadget.gadgetbridge.service.devices.hplus.EXRIZUK8Support; /** * Pseudo Coordinator for the EXRIZU K8, a sub type of the HPLUS devices @@ -44,4 +47,10 @@ public class EXRIZUK8Coordinator extends HPlusCoordinator { public int getDeviceNameResource() { return R.string.devicetype_exrizu_k8; } + + @NonNull + @Override + public Class getDeviceSupportClass() { + return EXRIZUK8Support.class; + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/hplus/HPlusCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/hplus/HPlusCoordinator.java index 5e06514c9..af3c1eb1f 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/hplus/HPlusCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/hplus/HPlusCoordinator.java @@ -253,7 +253,6 @@ public class HPlusCoordinator extends AbstractBLEDeviceCoordinator { return HPlusSupport.class; } - @Override public int getDeviceNameResource() { return R.string.devicetype_hplus; @@ -270,4 +269,3 @@ public class HPlusCoordinator extends AbstractBLEDeviceCoordinator { return R.drawable.ic_device_hplus_disabled; } } - diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/hplus/MakibesF68Coordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/hplus/MakibesF68Coordinator.java index e005a144c..263757c62 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/hplus/MakibesF68Coordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/hplus/MakibesF68Coordinator.java @@ -21,27 +21,21 @@ package nodomain.freeyourgadget.gadgetbridge.devices.hplus; * @author João Paulo Barraca <jpbarraca@gmail.com> */ - import androidx.annotation.NonNull; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate; -import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; +import nodomain.freeyourgadget.gadgetbridge.service.DeviceSupport; +import nodomain.freeyourgadget.gadgetbridge.service.devices.hplus.MakibesF68Support; /** * Pseudo Coordinator for the Makibes F68, a sub type of the HPLUS devices */ public class MakibesF68Coordinator extends HPlusCoordinator { - - @NonNull @Override public boolean supports(GBDeviceCandidate candidate) { String name = candidate.getName(); - if(name != null && name.startsWith("SPORT") && !name.startsWith("SPORTAGE")){ - return true; - } - - return false; + return name != null && name.startsWith("SPORT") && !name.startsWith("SPORTAGE"); } @Override @@ -53,4 +47,10 @@ public class MakibesF68Coordinator extends HPlusCoordinator { public int getDeviceNameResource() { return R.string.devicetype_makibes_f68; } + + @NonNull + @Override + public Class getDeviceSupportClass() { + return MakibesF68Support.class; + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/hplus/Q8Coordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/hplus/Q8Coordinator.java index 3ed48dbc1..6fb5765ed 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/hplus/Q8Coordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/hplus/Q8Coordinator.java @@ -21,14 +21,13 @@ package nodomain.freeyourgadget.gadgetbridge.devices.hplus; * @author Alejandro Ladera Chamorro <11555126+tiparega@users.noreply.github.com> */ - import androidx.annotation.NonNull; import java.util.regex.Pattern; import nodomain.freeyourgadget.gadgetbridge.R; -import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate; -import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; +import nodomain.freeyourgadget.gadgetbridge.service.DeviceSupport; +import nodomain.freeyourgadget.gadgetbridge.service.devices.hplus.Q8Support; /** * Pseudo Coordinator for the Q8, a sub type of the HPLUS devices @@ -48,4 +47,10 @@ public class Q8Coordinator extends HPlusCoordinator { public int getDeviceNameResource() { return R.string.devicetype_q8; } + + @NonNull + @Override + public Class getDeviceSupportClass() { + return Q8Support.class; + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/hplus/SG2Coordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/hplus/SG2Coordinator.java index 09e961520..36f340eb2 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/hplus/SG2Coordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/hplus/SG2Coordinator.java @@ -21,14 +21,13 @@ import androidx.annotation.NonNull; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate; -import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; +import nodomain.freeyourgadget.gadgetbridge.service.DeviceSupport; +import nodomain.freeyourgadget.gadgetbridge.service.devices.hplus.SG2Support; /** - * Pseudo Coordinator for the Lemfo SG2, a sub type of the HPLUS devices + * Pseudo Coordinator for the Lemfo SG2, a sub type of the HPLUS devices */ public class SG2Coordinator extends HPlusCoordinator { - - @NonNull @Override public boolean supports(GBDeviceCandidate candidate) { String name = candidate.getName(); @@ -66,4 +65,10 @@ public class SG2Coordinator extends HPlusCoordinator { public int getDeviceNameResource() { return R.string.devicetype_sg2; } + + @NonNull + @Override + public Class getDeviceSupportClass() { + return SG2Support.class; + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceSupportFactory.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceSupportFactory.java index 06488dc8a..33a981324 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceSupportFactory.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceSupportFactory.java @@ -27,7 +27,6 @@ package nodomain.freeyourgadget.gadgetbridge.service; import android.bluetooth.BluetoothAdapter; import android.content.Context; -import android.util.Log; import android.widget.Toast; import org.slf4j.Logger; @@ -36,15 +35,11 @@ import org.slf4j.LoggerFactory; import nodomain.freeyourgadget.gadgetbridge.GBException; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator; -import nodomain.freeyourgadget.gadgetbridge.externalevents.BluetoothConnectReceiver; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; -import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; import nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleSupport; -import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper; import nodomain.freeyourgadget.gadgetbridge.util.GB; import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; import java.util.EnumSet; public class DeviceSupportFactory { @@ -108,22 +103,11 @@ public class DeviceSupportFactory { } private ServiceDeviceSupport createServiceDeviceSupport(GBDevice device) throws GBException { - DeviceCoordinator coordinator = device.getDeviceCoordinator(); - Class supportClass = coordinator.getDeviceSupportClass(); + final DeviceCoordinator coordinator = device.getDeviceCoordinator(); + final Class supportClass = coordinator.getDeviceSupportClass(); try { - Constructor supportConstructor = supportClass.getConstructor(DeviceType.class); - DeviceSupport supportInstance = (DeviceSupport) supportConstructor.newInstance(device.getType()); - return new ServiceDeviceSupport(supportInstance, coordinator.getInitialFlags()); - } catch (NoSuchMethodException e) { - // ignore, let next call get the default, zero-argument constructor - } catch (ReflectiveOperationException e) { - LOG.error("error calling DeviceSupport constructor with argument 'DeviceType'"); - throw new GBException(e); - } - - try { - DeviceSupport supportInstance = (DeviceSupport) supportClass.newInstance(); + final DeviceSupport supportInstance = (DeviceSupport) supportClass.newInstance(); return new ServiceDeviceSupport(supportInstance, coordinator.getInitialFlags()); } catch (ReflectiveOperationException e) { LOG.error("error calling DeviceSupport constructor with zero arguments"); @@ -131,15 +115,13 @@ public class DeviceSupportFactory { } } - private DeviceSupport createBTDeviceSupport(GBDevice gbDevice) throws GBException { + private DeviceSupport createBTDeviceSupport(final GBDevice gbDevice) throws GBException { if (mBtAdapter != null && mBtAdapter.isEnabled()) { try { - DeviceSupport deviceSupport = createServiceDeviceSupport(gbDevice); - if (deviceSupport != null) { - deviceSupport.setContext(gbDevice, mBtAdapter, mContext); - return deviceSupport; - } - } catch (Exception e) { + final DeviceSupport deviceSupport = createServiceDeviceSupport(gbDevice); + deviceSupport.setContext(gbDevice, mBtAdapter, mContext); + return deviceSupport; + } catch (final Exception e) { throw new GBException(mContext.getString(R.string.cannot_connect_bt_address_invalid_), e); } } @@ -155,5 +137,4 @@ public class DeviceSupportFactory { throw new GBException("cannot connect to " + gbDevice, e); // FIXME: localize } } - } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/hplus/EXRIZUK8Support.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/hplus/EXRIZUK8Support.java new file mode 100644 index 000000000..5dd63b19f --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/hplus/EXRIZUK8Support.java @@ -0,0 +1,25 @@ +/* Copyright (C) 2025 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 . */ +package nodomain.freeyourgadget.gadgetbridge.service.devices.hplus; + +import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; + +public class EXRIZUK8Support extends HPlusSupport { + public EXRIZUK8Support() { + super(DeviceType.EXRIZUK8); + } +} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/hplus/HPlusSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/hplus/HPlusSupport.java index 591b7937f..863c55260 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/hplus/HPlusSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/hplus/HPlusSupport.java @@ -66,11 +66,15 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport { public BluetoothGattCharacteristic ctrlCharacteristic = null; public BluetoothGattCharacteristic measureCharacteristic = null; private HPlusHandlerThread syncHelper; - private DeviceType deviceType = DeviceType.UNKNOWN; + private final DeviceType deviceType; - public HPlusSupport(DeviceType type) { + public HPlusSupport() { + this(DeviceType.HPLUS); + } + + protected HPlusSupport(final DeviceType type) { super(LOG); - LOG.info("HPlusSupport Instance Created"); + LOG.info("HPlusSupport Instance created for {}", type); deviceType = type; addSupportedService(HPlusConstants.UUID_SERVICE_HP); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/hplus/MakibesF68Support.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/hplus/MakibesF68Support.java new file mode 100644 index 000000000..78377cd9f --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/hplus/MakibesF68Support.java @@ -0,0 +1,25 @@ +/* Copyright (C) 2025 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 . */ +package nodomain.freeyourgadget.gadgetbridge.service.devices.hplus; + +import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; + +public class MakibesF68Support extends HPlusSupport { + public MakibesF68Support() { + super(DeviceType.MAKIBESF68); + } +} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/hplus/Q8Support.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/hplus/Q8Support.java new file mode 100644 index 000000000..0f94b1584 --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/hplus/Q8Support.java @@ -0,0 +1,25 @@ +/* Copyright (C) 2025 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 . */ +package nodomain.freeyourgadget.gadgetbridge.service.devices.hplus; + +import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; + +public class Q8Support extends HPlusSupport { + public Q8Support() { + super(DeviceType.Q8); + } +} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/hplus/SG2Support.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/hplus/SG2Support.java new file mode 100644 index 000000000..740dfde1e --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/hplus/SG2Support.java @@ -0,0 +1,25 @@ +/* Copyright (C) 2025 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 . */ +package nodomain.freeyourgadget.gadgetbridge.service.devices.hplus; + +import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; + +public class SG2Support extends HPlusSupport { + public SG2Support() { + super(DeviceType.SG2); + } +}