Introduce wrappers for SerialDeviceSupport, deprecate old implementation

This commit is contained in:
José Rebelo
2026-01-01 15:57:43 +00:00
parent c19ed15dfe
commit c4dc75e6f9
6 changed files with 387 additions and 4 deletions
@@ -22,7 +22,6 @@ import android.os.ParcelUuid;
import org.slf4j.Logger; import org.slf4j.Logger;
import java.io.IOException;
import java.util.UUID; import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.Logging; import nodomain.freeyourgadget.gadgetbridge.Logging;
@@ -67,8 +66,7 @@ public abstract class AbstractBTBRDeviceSupport extends AbstractDeviceSupport im
final UUID supportedService = getSupportedService(); final UUID supportedService = getSupportedService();
if (supportedService == null) { if (supportedService == null) {
// Before throwing the exception, list the available UUIDs // Before throwing the exception, list the available UUIDs
final BluetoothDevice btDevice = getBluetoothAdapter().getRemoteDevice(gbDevice.getAddress()); final ParcelUuid[] uuids = getBluetoothDeviceUuids();
@SuppressLint("MissingPermission") final ParcelUuid[] uuids = btDevice.getUuids();
if (uuids == null || uuids.length == 0) { if (uuids == null || uuids.length == 0) {
logger.warn("Device provided no UUIDs to connect to: {}", gbDevice); logger.warn("Device provided no UUIDs to connect to: {}", gbDevice);
} else { } else {
@@ -91,6 +89,12 @@ public abstract class AbstractBTBRDeviceSupport extends AbstractDeviceSupport im
} }
} }
@SuppressLint("MissingPermission")
protected ParcelUuid[] getBluetoothDeviceUuids() {
final BluetoothDevice btDevice = getBluetoothAdapter().getRemoteDevice(gbDevice.getAddress());
return btDevice.getUuids();
}
public void disconnect() { public void disconnect() {
synchronized (ConnectionMonitor) { synchronized (ConnectionMonitor) {
if (mQueue != null) { if (mQueue != null) {
@@ -0,0 +1,66 @@
/* 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.serial;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.CallSpec;
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
import nodomain.freeyourgadget.gadgetbridge.service.AbstractHeadphoneBTBRDeviceSupport;
import nodomain.freeyourgadget.gadgetbridge.service.HeadphoneHelper;
/**
* @deprecated Use {@link AbstractHeadphoneBTBRDeviceSupport}
*/
@Deprecated
public abstract class AbstractHeadphoneSerialDeviceSupportV2<T extends GBDeviceProtocol>
extends AbstractSerialDeviceSupportV2<T> implements HeadphoneHelper.Callback {
private HeadphoneHelper headphoneHelper;
@Override
public void onSetCallState(final CallSpec callSpec) {
headphoneHelper.onSetCallState(callSpec);
}
@Override
public void onNotification(final NotificationSpec notificationSpec) {
headphoneHelper.onNotification(notificationSpec);
}
@Override
public void setContext(final GBDevice gbDevice, final BluetoothAdapter btAdapter, final Context context) {
super.setContext(gbDevice, btAdapter, context);
headphoneHelper = new HeadphoneHelper(getContext(), getDevice(), this);
}
@Override
public void onSendConfiguration(final String config) {
if (!headphoneHelper.onSendConfiguration(config))
super.onSendConfiguration(config);
}
@Override
public void dispose() {
synchronized (ConnectionMonitor) {
if (headphoneHelper != null)
headphoneHelper.dispose();
super.dispose();
}
}
}
@@ -51,7 +51,9 @@ import nodomain.freeyourgadget.gadgetbridge.service.AbstractDeviceSupport;
* <p/> * <p/>
* This implementation implements all methods of {@link EventHandler}, calls the {@link GBDeviceProtocol device protocol} * This implementation implements all methods of {@link EventHandler}, calls the {@link GBDeviceProtocol device protocol}
* to create the device specific message for the respective events and sends them to the device via {@link #sendToDevice(byte[])}. * to create the device specific message for the respective events and sends them to the device via {@link #sendToDevice(byte[])}.
* @deprecated Use {@link nodomain.freeyourgadget.gadgetbridge.service.btbr.AbstractBTBRDeviceSupport}
*/ */
@Deprecated
public abstract class AbstractSerialDeviceSupport extends AbstractDeviceSupport { public abstract class AbstractSerialDeviceSupport extends AbstractDeviceSupport {
private static final Logger LOG = LoggerFactory.getLogger(AbstractSerialDeviceSupport.class); private static final Logger LOG = LoggerFactory.getLogger(AbstractSerialDeviceSupport.class);
@@ -0,0 +1,304 @@
package nodomain.freeyourgadget.gadgetbridge.service.serial;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.location.Location;
import android.os.ParcelUuid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.Alarm;
import nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec;
import nodomain.freeyourgadget.gadgetbridge.model.CallSpec;
import nodomain.freeyourgadget.gadgetbridge.model.CannedMessagesSpec;
import nodomain.freeyourgadget.gadgetbridge.model.MusicSpec;
import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec;
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
import nodomain.freeyourgadget.gadgetbridge.model.Reminder;
import nodomain.freeyourgadget.gadgetbridge.model.WorldClock;
import nodomain.freeyourgadget.gadgetbridge.service.btbr.AbstractBTBRDeviceSupport;
import nodomain.freeyourgadget.gadgetbridge.service.btbr.TransactionBuilder;
/**
* This class serves only to help existing implementations transition from AbstractSerialDeviceSupport,
* and should not be used for new implementations.
*
* @deprecated Use {@link nodomain.freeyourgadget.gadgetbridge.service.btbr.AbstractBTBRDeviceSupport}
*/
@Deprecated
public abstract class AbstractSerialDeviceSupportV2<T extends GBDeviceProtocol> extends AbstractBTBRDeviceSupport {
private static final Logger LOG = LoggerFactory.getLogger(AbstractSerialDeviceSupportV2.class);
protected T mDeviceProtocol;
public AbstractSerialDeviceSupportV2() {
this(2048);
}
public AbstractSerialDeviceSupportV2(final int bufferSize) {
super(LOG, bufferSize);
}
protected abstract T createDeviceProtocol();
@Override
protected UUID getSupportedService() {
// Preserve the behavior from legacy AbstractSerialDeviceSupport, and return the first UUID
final UUID supportedService = super.getSupportedService();
if (supportedService != null) {
return supportedService;
}
final ParcelUuid[] bluetoothDeviceUuids = getBluetoothDeviceUuids();
if (bluetoothDeviceUuids != null && bluetoothDeviceUuids.length > 0) {
return bluetoothDeviceUuids[0].getUuid();
}
return null;
}
@Override
public boolean useAutoConnect() {
return false;
}
@Override
public void setContext(final GBDevice gbDevice, final BluetoothAdapter btAdapter, final Context context) {
super.setContext(gbDevice, btAdapter, context);
this.mDeviceProtocol = createDeviceProtocol();
}
@Override
public void onSocketRead(final byte[] data) {
final GBDeviceEvent[] deviceEvents = mDeviceProtocol.decodeResponse(data);
if (deviceEvents == null) {
LOG.debug("unhandled message");
return;
}
for (GBDeviceEvent deviceEvent : deviceEvents) {
if (deviceEvent == null) {
continue;
}
evaluateGBDeviceEvent(deviceEvent);
}
}
private void sendToDevice(final byte[] bytes) {
if (bytes != null) {
final TransactionBuilder builder = createTransactionBuilder("send bytes");
builder.write(bytes);
builder.queue();
}
}
private void handleGBDeviceEvent(final GBDeviceEventSendBytes sendBytes) {
sendToDevice(sendBytes.encodedBytes);
}
@Override
public void evaluateGBDeviceEvent(final GBDeviceEvent deviceEvent) {
if (deviceEvent instanceof GBDeviceEventSendBytes) {
handleGBDeviceEvent((GBDeviceEventSendBytes) deviceEvent);
return;
}
super.evaluateGBDeviceEvent(deviceEvent);
}
@Override
public void onNotification(final NotificationSpec notificationSpec) {
byte[] bytes = mDeviceProtocol.encodeNotification(notificationSpec);
sendToDevice(bytes);
}
@Override
public void onDeleteNotification(final int id) {
byte[] bytes = mDeviceProtocol.encodeDeleteNotification(id);
sendToDevice(bytes);
}
@Override
public void onSetTime() {
byte[] bytes = mDeviceProtocol.encodeSetTime();
sendToDevice(bytes);
}
@Override
public void onSetCallState(final CallSpec callSpec) {
byte[] bytes = mDeviceProtocol.encodeSetCallState(callSpec.number, callSpec.name, callSpec.command);
sendToDevice(bytes);
}
@Override
public void onSetCannedMessages(final CannedMessagesSpec cannedMessagesSpec) {
byte[] bytes = mDeviceProtocol.encodeSetCannedMessages(cannedMessagesSpec);
sendToDevice(bytes);
}
@Override
public void onSetMusicState(final MusicStateSpec stateSpec) {
byte[] bytes = mDeviceProtocol.encodeSetMusicState(stateSpec.state, stateSpec.position, stateSpec.playRate, stateSpec.shuffle, stateSpec.repeat);
sendToDevice(bytes);
}
@Override
public void onSetMusicInfo(final MusicSpec musicSpec) {
byte[] bytes = mDeviceProtocol.encodeSetMusicInfo(musicSpec.artist, musicSpec.album, musicSpec.track, musicSpec.duration, musicSpec.trackCount, musicSpec.trackNr);
sendToDevice(bytes);
}
@Override
public void onSetPhoneVolume(final float volume) {
byte[] bytes = mDeviceProtocol.encodeVolume(volume);
sendToDevice(bytes);
}
@Override
public void onAppInfoReq() {
byte[] bytes = mDeviceProtocol.encodeAppInfoReq();
sendToDevice(bytes);
}
@Override
public void onAppStart(final UUID uuid, final boolean start) {
byte[] bytes = mDeviceProtocol.encodeAppStart(uuid, start);
sendToDevice(bytes);
}
@Override
public void onAppDelete(final UUID uuid) {
byte[] bytes = mDeviceProtocol.encodeAppDelete(uuid);
sendToDevice(bytes);
}
@Override
public void onAppReorder(final UUID[] uuids) {
byte[] bytes = mDeviceProtocol.encodeAppReorder(uuids);
sendToDevice(bytes);
}
@Override
public void onFetchRecordedData(final int dataTypes) {
byte[] bytes = mDeviceProtocol.encodeFetchRecordedData(dataTypes);
sendToDevice(bytes);
}
@Override
public void onReset(final int flags) {
byte[] bytes = mDeviceProtocol.encodeReset(flags);
sendToDevice(bytes);
}
@Override
public void onFindDevice(final boolean start) {
byte[] bytes = mDeviceProtocol.encodeFindDevice(start);
sendToDevice(bytes);
}
@Override
public void onFindPhone(final boolean start) {
byte[] bytes = mDeviceProtocol.encodeFindPhone(start);
sendToDevice(bytes);
}
@Override
public void onScreenshotReq() {
byte[] bytes = mDeviceProtocol.encodeScreenshotReq();
sendToDevice(bytes);
}
@Override
public void onEnableRealtimeSteps(final boolean enable) {
byte[] bytes = mDeviceProtocol.encodeEnableRealtimeSteps(enable);
sendToDevice(bytes);
}
@Override
public void onEnableHeartRateSleepSupport(final boolean enable) {
byte[] bytes = mDeviceProtocol.encodeEnableHeartRateSleepSupport(enable);
sendToDevice(bytes);
}
@Override
public void onEnableRealtimeHeartRateMeasurement(final boolean enable) {
byte[] bytes = mDeviceProtocol.encodeEnableRealtimeHeartRateMeasurement(enable);
sendToDevice(bytes);
}
@Override
public void onAddCalendarEvent(final CalendarEventSpec calendarEventSpec) {
byte[] bytes = mDeviceProtocol.encodeAddCalendarEvent(calendarEventSpec);
sendToDevice(bytes);
}
@Override
public void onDeleteCalendarEvent(final byte type, final long id) {
byte[] bytes = mDeviceProtocol.encodeDeleteCalendarEvent(type, id);
sendToDevice(bytes);
}
@Override
public void onSendConfiguration(final String config) {
byte[] bytes = mDeviceProtocol.encodeSendConfiguration(config);
sendToDevice(bytes);
}
@Override
public void onTestNewFunction() {
byte[] bytes = mDeviceProtocol.encodeTestNewFunction();
sendToDevice(bytes);
}
@Override
public void onSendWeather() {
byte[] bytes = mDeviceProtocol.encodeSendWeather();
sendToDevice(bytes);
}
@Override
public void onSetFmFrequency(final float frequency) {
byte[] bytes = mDeviceProtocol.encodeFmFrequency(frequency);
sendToDevice(bytes);
}
@Override
public void onSetLedColor(final int color) {
byte[] bytes = mDeviceProtocol.encodeLedColor(color);
sendToDevice(bytes);
}
@Override
public void onPowerOff() {
byte[] bytes = mDeviceProtocol.encodePowerOff();
sendToDevice(bytes);
}
@Override
public void onSetAlarms(final ArrayList<? extends Alarm> alarms) {
byte[] bytes = mDeviceProtocol.encodeSetAlarms(alarms);
sendToDevice(bytes);
}
@Override
public void onSetReminders(final ArrayList<? extends Reminder> reminders) {
byte[] bytes = mDeviceProtocol.encodeReminders(reminders);
sendToDevice(bytes);
}
@Override
public void onSetWorldClocks(final ArrayList<? extends WorldClock> clocks) {
byte[] bytes = mDeviceProtocol.encodeWorldClocks(clocks);
sendToDevice(bytes);
}
@Override
public void onSetGpsLocation(final Location location) {
byte[] bytes = mDeviceProtocol.encodeGpsLocation(location);
sendToDevice(bytes);
}
}
@@ -22,6 +22,10 @@ import java.util.concurrent.atomic.AtomicLong;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
/**
* @deprecated Use {@link nodomain.freeyourgadget.gadgetbridge.service.btbr.AbstractBTBRDeviceSupport}
*/
@Deprecated
public abstract class GBDeviceIoThread extends Thread { public abstract class GBDeviceIoThread extends Thread {
private static final AtomicLong THREAD_COUNTER = new AtomicLong(0L); private static final AtomicLong THREAD_COUNTER = new AtomicLong(0L);
@@ -32,10 +32,13 @@ import nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec;
import nodomain.freeyourgadget.gadgetbridge.model.CannedMessagesSpec; import nodomain.freeyourgadget.gadgetbridge.model.CannedMessagesSpec;
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec; import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
import nodomain.freeyourgadget.gadgetbridge.model.Reminder; import nodomain.freeyourgadget.gadgetbridge.model.Reminder;
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
import nodomain.freeyourgadget.gadgetbridge.model.WorldClock; import nodomain.freeyourgadget.gadgetbridge.model.WorldClock;
import nodomain.freeyourgadget.gadgetbridge.util.preferences.DevicePrefs; import nodomain.freeyourgadget.gadgetbridge.util.preferences.DevicePrefs;
/**
* @deprecated Use {@link nodomain.freeyourgadget.gadgetbridge.service.btbr.AbstractBTBRDeviceSupport}
*/
@Deprecated
public abstract class GBDeviceProtocol { public abstract class GBDeviceProtocol {
public static final int RESET_FLAGS_REBOOT = 1; public static final int RESET_FLAGS_REBOOT = 1;