HPlus: Deprecate DeviceType constructor

This commit is contained in:
José Rebelo
2025-04-16 00:12:35 +01:00
parent 08732ffa68
commit e1e5cccf29
12 changed files with 151 additions and 48 deletions

View File

@@ -93,6 +93,7 @@
<w>lacross</w> <w>lacross</w>
<w>ladbsoft</w> <w>ladbsoft</w>
<w>ladera</w> <w>ladera</w>
<w>lemfo</w>
<w>lenovo</w> <w>lenovo</w>
<w>liebergeld</w> <w>liebergeld</w>
<w>lightforce</w> <w>lightforce</w>

View File

@@ -21,10 +21,13 @@ package nodomain.freeyourgadget.gadgetbridge.devices.hplus;
* @author Quallenauge &lt;Hamsi2k@freenet.de&gt; * @author Quallenauge &lt;Hamsi2k@freenet.de&gt;
*/ */
import androidx.annotation.NonNull;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import nodomain.freeyourgadget.gadgetbridge.R; 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 * 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() { public int getDeviceNameResource() {
return R.string.devicetype_exrizu_k8; return R.string.devicetype_exrizu_k8;
} }
@NonNull
@Override
public Class<? extends DeviceSupport> getDeviceSupportClass() {
return EXRIZUK8Support.class;
}
} }

View File

@@ -253,7 +253,6 @@ public class HPlusCoordinator extends AbstractBLEDeviceCoordinator {
return HPlusSupport.class; return HPlusSupport.class;
} }
@Override @Override
public int getDeviceNameResource() { public int getDeviceNameResource() {
return R.string.devicetype_hplus; return R.string.devicetype_hplus;
@@ -270,4 +269,3 @@ public class HPlusCoordinator extends AbstractBLEDeviceCoordinator {
return R.drawable.ic_device_hplus_disabled; return R.drawable.ic_device_hplus_disabled;
} }
} }

View File

@@ -21,27 +21,21 @@ package nodomain.freeyourgadget.gadgetbridge.devices.hplus;
* @author João Paulo Barraca &lt;jpbarraca@gmail.com&gt; * @author João Paulo Barraca &lt;jpbarraca@gmail.com&gt;
*/ */
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate; 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 * Pseudo Coordinator for the Makibes F68, a sub type of the HPLUS devices
*/ */
public class MakibesF68Coordinator extends HPlusCoordinator { public class MakibesF68Coordinator extends HPlusCoordinator {
@NonNull
@Override @Override
public boolean supports(GBDeviceCandidate candidate) { public boolean supports(GBDeviceCandidate candidate) {
String name = candidate.getName(); String name = candidate.getName();
if(name != null && name.startsWith("SPORT") && !name.startsWith("SPORTAGE")){ return name != null && name.startsWith("SPORT") && !name.startsWith("SPORTAGE");
return true;
}
return false;
} }
@Override @Override
@@ -53,4 +47,10 @@ public class MakibesF68Coordinator extends HPlusCoordinator {
public int getDeviceNameResource() { public int getDeviceNameResource() {
return R.string.devicetype_makibes_f68; return R.string.devicetype_makibes_f68;
} }
@NonNull
@Override
public Class<? extends DeviceSupport> getDeviceSupportClass() {
return MakibesF68Support.class;
}
} }

View File

@@ -21,14 +21,13 @@ package nodomain.freeyourgadget.gadgetbridge.devices.hplus;
* @author Alejandro Ladera Chamorro &lt;11555126+tiparega@users.noreply.github.com&gt; * @author Alejandro Ladera Chamorro &lt;11555126+tiparega@users.noreply.github.com&gt;
*/ */
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate; import nodomain.freeyourgadget.gadgetbridge.service.DeviceSupport;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; import nodomain.freeyourgadget.gadgetbridge.service.devices.hplus.Q8Support;
/** /**
* Pseudo Coordinator for the Q8, a sub type of the HPLUS devices * Pseudo Coordinator for the Q8, a sub type of the HPLUS devices
@@ -48,4 +47,10 @@ public class Q8Coordinator extends HPlusCoordinator {
public int getDeviceNameResource() { public int getDeviceNameResource() {
return R.string.devicetype_q8; return R.string.devicetype_q8;
} }
@NonNull
@Override
public Class<? extends DeviceSupport> getDeviceSupportClass() {
return Q8Support.class;
}
} }

View File

@@ -21,14 +21,13 @@ import androidx.annotation.NonNull;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate; 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 { public class SG2Coordinator extends HPlusCoordinator {
@NonNull
@Override @Override
public boolean supports(GBDeviceCandidate candidate) { public boolean supports(GBDeviceCandidate candidate) {
String name = candidate.getName(); String name = candidate.getName();
@@ -66,4 +65,10 @@ public class SG2Coordinator extends HPlusCoordinator {
public int getDeviceNameResource() { public int getDeviceNameResource() {
return R.string.devicetype_sg2; return R.string.devicetype_sg2;
} }
@NonNull
@Override
public Class<? extends DeviceSupport> getDeviceSupportClass() {
return SG2Support.class;
}
} }

View File

@@ -27,7 +27,6 @@ package nodomain.freeyourgadget.gadgetbridge.service;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.content.Context; import android.content.Context;
import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -36,15 +35,11 @@ import org.slf4j.LoggerFactory;
import nodomain.freeyourgadget.gadgetbridge.GBException; import nodomain.freeyourgadget.gadgetbridge.GBException;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator; import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.externalevents.BluetoothConnectReceiver;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleSupport; import nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleSupport;
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.GB;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.EnumSet; import java.util.EnumSet;
public class DeviceSupportFactory { public class DeviceSupportFactory {
@@ -108,22 +103,11 @@ public class DeviceSupportFactory {
} }
private ServiceDeviceSupport createServiceDeviceSupport(GBDevice device) throws GBException { private ServiceDeviceSupport createServiceDeviceSupport(GBDevice device) throws GBException {
DeviceCoordinator coordinator = device.getDeviceCoordinator(); final DeviceCoordinator coordinator = device.getDeviceCoordinator();
Class supportClass = coordinator.getDeviceSupportClass(); final Class<?> supportClass = coordinator.getDeviceSupportClass();
try { try {
Constructor supportConstructor = supportClass.getConstructor(DeviceType.class); final DeviceSupport supportInstance = (DeviceSupport) supportClass.newInstance();
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();
return new ServiceDeviceSupport(supportInstance, coordinator.getInitialFlags()); return new ServiceDeviceSupport(supportInstance, coordinator.getInitialFlags());
} catch (ReflectiveOperationException e) { } catch (ReflectiveOperationException e) {
LOG.error("error calling DeviceSupport constructor with zero arguments"); 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()) { if (mBtAdapter != null && mBtAdapter.isEnabled()) {
try { try {
DeviceSupport deviceSupport = createServiceDeviceSupport(gbDevice); final DeviceSupport deviceSupport = createServiceDeviceSupport(gbDevice);
if (deviceSupport != null) { deviceSupport.setContext(gbDevice, mBtAdapter, mContext);
deviceSupport.setContext(gbDevice, mBtAdapter, mContext); return deviceSupport;
return deviceSupport; } catch (final Exception e) {
}
} catch (Exception e) {
throw new GBException(mContext.getString(R.string.cannot_connect_bt_address_invalid_), 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 throw new GBException("cannot connect to " + gbDevice, e); // FIXME: localize
} }
} }
} }

View File

@@ -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 <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.service.devices.hplus;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
public class EXRIZUK8Support extends HPlusSupport {
public EXRIZUK8Support() {
super(DeviceType.EXRIZUK8);
}
}

View File

@@ -66,11 +66,15 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
public BluetoothGattCharacteristic ctrlCharacteristic = null; public BluetoothGattCharacteristic ctrlCharacteristic = null;
public BluetoothGattCharacteristic measureCharacteristic = null; public BluetoothGattCharacteristic measureCharacteristic = null;
private HPlusHandlerThread syncHelper; 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); super(LOG);
LOG.info("HPlusSupport Instance Created"); LOG.info("HPlusSupport Instance created for {}", type);
deviceType = type; deviceType = type;
addSupportedService(HPlusConstants.UUID_SERVICE_HP); addSupportedService(HPlusConstants.UUID_SERVICE_HP);

View File

@@ -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 <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.service.devices.hplus;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
public class MakibesF68Support extends HPlusSupport {
public MakibesF68Support() {
super(DeviceType.MAKIBESF68);
}
}

View File

@@ -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 <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.service.devices.hplus;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
public class Q8Support extends HPlusSupport {
public Q8Support() {
super(DeviceType.Q8);
}
}

View File

@@ -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 <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.service.devices.hplus;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
public class SG2Support extends HPlusSupport {
public SG2Support() {
super(DeviceType.SG2);
}
}