TransactionBuilder: rename wait/WaitAction to sleep/SleepAction

Object.wait(long) and TransactionBuilder.wait(int) have too similar signatures that may result in unintentionally calling the wrong method.
This commit is contained in:
Thomas Kuehne
2026-06-05 03:47:59 +00:00
parent 7c309e9a5d
commit 7e6d96ae8c
25 changed files with 88 additions and 76 deletions
@@ -32,7 +32,7 @@ import java.util.function.Predicate;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.service.btbr.actions.FunctionAction;
import nodomain.freeyourgadget.gadgetbridge.service.btbr.actions.SetProgressAction;
import nodomain.freeyourgadget.gadgetbridge.service.btbr.actions.WaitAction;
import nodomain.freeyourgadget.gadgetbridge.service.btbr.actions.SleepAction;
import nodomain.freeyourgadget.gadgetbridge.service.btbr.actions.WriteAction;
import nodomain.freeyourgadget.gadgetbridge.service.btbr.actions.SetDeviceStateAction;
import nodomain.freeyourgadget.gadgetbridge.service.btbr.actions.SetDeviceBusyAction;
@@ -72,11 +72,17 @@ public class TransactionBuilder {
* @see Thread#sleep(long)
*/
@NonNull
public TransactionBuilder wait(@IntRange(from = 0L) int millis) {
WaitAction action = new WaitAction(millis);
public TransactionBuilder sleep(@IntRange(from = 0L) int millis) {
SleepAction action = new SleepAction(millis);
return add(action);
}
/// @deprecated use {@link #sleep(int)} instead
@Deprecated
public TransactionBuilder wait(@IntRange(from = 0L) int millis) {
return sleep(millis);
}
/// Causes the {@link BtBRQueue} to execute the {@link Predicate} and expect no {@link SocketCallback} result.
/// The {@link Transaction} is aborted if the predicate throws an {@link Exception} or returns {@code false}.
///
@@ -23,11 +23,11 @@ import android.bluetooth.BluetoothSocket;
* Note that this is usually a bad idea, since it will not be able to process messages
* during that time. It is also likely to cause race conditions.
*/
public class WaitAction extends PlainAction {
public class SleepAction extends PlainAction {
private final int mMillis;
public WaitAction(int millis) {
public SleepAction(int millis) {
mMillis = millis;
}
@@ -50,7 +50,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceBusyAc
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction;
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetPreferredPhyAction;
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetProgressAction;
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.WaitAction;
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SleepAction;
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.WriteAction;
public class TransactionBuilder {
@@ -275,11 +275,17 @@ public class TransactionBuilder {
* @see Thread#sleep(long)
*/
@NonNull
public TransactionBuilder wait(@IntRange(from = 0L) int millis) {
WaitAction action = new WaitAction(millis);
public TransactionBuilder sleep(@IntRange(from = 0L) int millis) {
SleepAction action = new SleepAction(millis);
return add(action);
}
/// @deprecated use {@link #sleep(int)} instead
@Deprecated
public TransactionBuilder wait(@IntRange(from = 0L) int millis) {
return sleep(millis);
}
/// Causes the {@link BtLEQueue} to execute the {@link Predicate} and expect no {@link GattCallback} result.
/// The {@link Transaction} is aborted if the predicate throws an {@link Exception} or returns {@code false}.
///
@@ -23,11 +23,11 @@ import android.bluetooth.BluetoothGatt;
* Note that this is usually a bad idea, since it will not be able to process messages
* during that time. It is also likely to cause race conditions.
*/
public class WaitAction extends PlainAction {
public class SleepAction extends PlainAction {
private final int mMillis;
public WaitAction(int millis) {
public SleepAction(int millis) {
mMillis = millis;
}
@@ -90,7 +90,7 @@ public class ATCBLEOEPLDeviceSupport extends AbstractBTLESingleDeviceSupport {
getDevice().setFirmwareVersion2("N/A");
builder.requestMtu(512);
builder.notify(UUID_CHARACTERISTIC_MAIN, true);
builder.wait(300);
builder.sleep(300);
builder.write(UUID_CHARACTERISTIC_MAIN, COMMAND_GET_CONFIGURATION);
return builder;
}
@@ -209,7 +209,7 @@ public class CasioGB6900DeviceSupport extends CasioSupport {
BluetoothGattService llService = mBtGatt.getService(CasioConstants.LINK_LOSS_SERVICE);
BluetoothGattCharacteristic charact = llService.getCharacteristic(CasioConstants.ALERT_LEVEL_CHARACTERISTIC_UUID);
builder.writeLegacy(charact, value);
builder.wait(mCasioSleepTime);
builder.sleep(mCasioSleepTime);
}
private void addCharacteristics() {
@@ -223,7 +223,7 @@ public class CasioGB6900DeviceSupport extends CasioSupport {
public boolean enableNotifications(TransactionBuilder builder, boolean enable) {
for(BluetoothGattCharacteristic charact : mCasioCharacteristics) {
builder.notify(charact, enable);
builder.wait(mCasioSleepTime);
builder.sleep(mCasioSleepTime);
}
return true;
}
@@ -73,7 +73,7 @@ public class SetAlarmOperation extends AbstractBTLEOperation<CasioGB6900DeviceSu
if (getDevice() != null) {
try {
TransactionBuilder builder = performInitialized("finished operation");
builder.wait(0);
builder.sleep(0);
builder.setCallback(null); // unset ourselves from being the queue's gatt callback
builder.queue();
} catch (IOException ex) {
@@ -421,7 +421,7 @@ public class FetchSportDataOperation extends AbstractBTLEOperation<CasioGBD200De
try {
TransactionBuilder b = performInitialized("finished");
b.setCallback(null);
b.wait(0);
b.sleep(0);
b.queue();
} catch (IOException ex) {
LOG.error("Error resetting Gatt callback", ex);
@@ -135,7 +135,7 @@ public class FetchStepCountDataOperation extends AbstractBTLEOperation<CasioGBD2
try {
TransactionBuilder builder = performInitialized("finished");
builder.setCallback(null);
builder.wait(0);
builder.sleep(0);
builder.queue();
} catch (IOException ex) {
LOG.error("Error resetting Gatt callback", ex);
@@ -407,7 +407,7 @@ public class InitOperation extends AbstractBTLEOperation<CasioGBD200DeviceSuppor
try {
TransactionBuilder b = createTransactionBuilder("init_done");
b.setCallback(null);
b.wait(0);
b.sleep(0);
b.queueImmediately();
} catch (Exception e) {
LOG.error("Failed to release GATT callback after init: {}", e.getMessage());
@@ -110,7 +110,7 @@ public class FetchStepCountDataOperation extends AbstractBTLEOperation<CasioGBX
try {
TransactionBuilder builder = performInitialized("finished operation");
builder.setCallback(null); // unset ourselves from being the queue's gatt callback
builder.wait(0);
builder.sleep(0);
builder.queue();
} catch (IOException ex) {
LOG.error("Error resetting Gatt callback", ex);
@@ -74,7 +74,7 @@ public class GetConfigurationOperation extends AbstractBTLEOperation<Casio2C2DSu
if (getDevice() != null) {
try {
TransactionBuilder builder = performInitialized("finished operation");
builder.wait(0);
builder.sleep(0);
builder.setCallback(null); // unset ourselves from being the queue's gatt callback
builder.queue();
} catch (IOException ex) {
@@ -262,7 +262,7 @@ public class SetConfigurationOperation extends AbstractBTLEOperation<Casio2C2DSu
try {
TransactionBuilder builder = performInitialized("finished operation");
builder.setCallback(null); // unset ourselves from being the queue's gatt callback
builder.wait(0);
builder.sleep(0);
builder.queue();
} catch (IOException ex) {
LOG.info("Error resetting Gatt callback: " + ex.getMessage());
@@ -168,7 +168,7 @@ public class DomyosT540Support extends AbstractBTLESingleDeviceSupport {
void writeChunked(TransactionBuilder builder, byte[] data) {
builder.writeChunkedData(getCharacteristic(UUUD_CHARACTERISTICS_WRITE), data, 20)
.wait(100);
.sleep(100);
}
private byte getChecksum(byte[] command) {
@@ -246,7 +246,7 @@ public class DomyosT540Support extends AbstractBTLESingleDeviceSupport {
start_time = (int) (System.currentTimeMillis() / 1000);
}
}
builder.wait(200);
builder.sleep(200);
writeChunked(builder, COMMAND_REQUEST_DATA);
int time = (int) (System.currentTimeMillis() / 1000);
@@ -215,23 +215,23 @@ public class FitProDeviceSupport extends AbstractBTLESingleDeviceSupport {
builder.write(writeCharacteristic, craftData(CMD_GROUP_GENERAL, FitProConstants.CMD_INIT1, (byte) 0x2));
setTime(builder);
builder.wait(200);
builder.sleep(200);
builder.write(writeCharacteristic, craftData(CMD_GROUP_REQUEST_DATA, FitProConstants.CMD_INIT1));
builder.wait(200);
builder.sleep(200);
builder.write(writeCharacteristic, craftData(CMD_GROUP_REQUEST_DATA, FitProConstants.CMD_INIT2));
builder.wait(200);
builder.sleep(200);
setLanguage(builder);
builder.wait(200);
builder.sleep(200);
builder.write(writeCharacteristic, craftData(CMD_GROUP_GENERAL, FitProConstants.CMD_INIT3, VALUE_ON));
builder.wait(200);
builder.sleep(200);
builder.write(writeCharacteristic, craftData(CMD_GROUP_REQUEST_DATA, VALUE_ON));
builder.wait(200);
builder.sleep(200);
builder.write(writeCharacteristic, craftData(CMD_GROUP_REQUEST_DATA, (byte) 0xf));
builder.wait(200);
builder.sleep(200);
builder.write(writeCharacteristic, craftData(CMD_GROUP_REQUEST_DATA, CMD_GET_HW_INFO));
builder.wait(200);
builder.sleep(200);
builder.write(writeCharacteristic, craftData(CMD_GROUP_BAND_INFO, CMD_RX_BAND_INFO));
builder.wait(200);
builder.sleep(200);
builder.setDeviceState(GBDevice.State.INITIALIZED);
return builder;
@@ -711,7 +711,7 @@ public class HPlusSupport extends AbstractBTLESingleDeviceSupport {
msg[0] = HPlusConstants.CMD_SET_INCOMING_CALL_NUMBER;
builder.wait(200);
builder.sleep(200);
builder.write(ctrlCharacteristic, msg);
}
@@ -52,7 +52,7 @@ public class Mi2NotificationStrategy extends V2NotificationStrategy<HuamiSupport
// Don't wait during an incoming call, otherwise we'll not be able to stop the call notification
if (simpleNotification == null || simpleNotification.getAlertCategory() != AlertCategory.IncomingCall) {
waitDuration = Math.max(waitDuration, 4000);
builder.wait(waitDuration);
builder.sleep(waitDuration);
}
if (extraAction != null) {
@@ -348,9 +348,9 @@ public class Request {
private void builderWait(int millis) {
if (!this.supportProvider.isBLE())
this.builderBr.wait(millis);
this.builderBr.sleep(millis);
else
this.builderLe.wait(millis);
this.builderLe.sleep(millis);
}
private void performConnected() {
@@ -241,47 +241,47 @@ public class C60DeviceSupport extends AbstractBTLESingleDeviceSupport {
getDevice().setFirmwareVersion2("N/A");
int wait = 100;
builder.wait(wait);
builder.sleep(wait);
getDeviceData(builder);
builder.wait(wait);
builder.sleep(wait);
getBatteryData(builder);
builder.wait(wait);
builder.sleep(wait);
setTime(builder);
builder.wait(wait);
builder.sleep(wait);
getDeviceState(builder);
builder.wait(wait);
builder.sleep(wait);
getDndState(builder);
builder.wait(wait);
builder.sleep(wait);
getInactivityState(builder);
builder.wait(wait);
builder.sleep(wait);
getTargetData(builder);
builder.wait(wait);
builder.sleep(wait);
getHydration(builder);
builder.wait(wait);
builder.sleep(wait);
// getSteps(builder);
// builder.wait(wait);
// builder.sleep(wait);
// getHeartrate(builder);
// builder.wait(wait);
// builder.sleep(wait);
// getBodytemp(builder);
// builder.wait(wait);
// builder.sleep(wait);
// setDeviceState(builder);
// builder.wait(wait);
// builder.sleep(wait);
// setUserInfo(builder);
// builder.wait(wait);
// builder.sleep(wait);
// getTargetData(builder);
// builder.wait(wait);
// builder.sleep(wait);
// getNotice(builder);
// builder.wait(wait);
// builder.sleep(wait);
// getOxygen(builder);
// builder.wait(wait);
// builder.sleep(wait);
// getHrSampling(builder);
// builder.wait(wait);
// builder.sleep(wait);
// getAlarm(builder);
// builder.wait(wait);
// builder.sleep(wait);
// getStepsHistory(builder);
// builder.wait(500);
// builder.sleep(500);
// getHeartrateHistory(builder);
// builder.wait(500);
// builder.sleep(500);
builder.setDeviceState(GBDevice.State.INITIALIZED);
@@ -711,7 +711,7 @@ public class C60DeviceSupport extends AbstractBTLESingleDeviceSupport {
BluetoothGattCharacteristic characteristic = getCharacteristic(C60Constants.CHARACTERISTIC_WRITE);
builder.writeChunkedData(characteristic, data, CHUNK_SIZE);
if (wait > 0) {
builder.wait(wait);
builder.sleep(wait);
}
}
@@ -212,21 +212,21 @@ public class LaxasFitDeviceSupport extends AbstractBTLESingleDeviceSupport {
builder.write(writeCharacteristic, craftData(CMD_GROUP_GENERAL, LaxasFitConstants.CMD_INIT1, (byte) 0x2));
setTime(builder);
//builder.wait(200);
//builder.sleep(200);
builder.write(writeCharacteristic, craftData(CMD_GROUP_REQUEST_DATA, LaxasFitConstants.CMD_INIT1));
//builder.wait(200);
//builder.sleep(200);
builder.write(writeCharacteristic, craftData(CMD_GROUP_REQUEST_DATA, LaxasFitConstants.CMD_INIT2));
//builder.wait(200);
//builder.sleep(200);
builder.write(writeCharacteristic, craftData(CMD_GROUP_GENERAL, LaxasFitConstants.CMD_INIT3, VALUE_ON));
//builder.wait(200);
//builder.sleep(200);
builder.write(writeCharacteristic, craftData(CMD_GROUP_REQUEST_DATA, VALUE_ON));
//builder.wait(200);
//builder.sleep(200);
builder.write(writeCharacteristic, craftData(CMD_GROUP_REQUEST_DATA, (byte) 0xf));
//builder.wait(200);
//builder.sleep(200);
builder.write(writeCharacteristic, craftData(CMD_GROUP_REQUEST_DATA, CMD_GET_HW_INFO));
//builder.wait(200);
//builder.sleep(200);
builder.write(writeCharacteristic, craftData(CMD_GROUP_BAND_INFO, CMD_RX_BAND_INFO));
//builder.wait(200);
//builder.sleep(200);
builder.setDeviceState(GBDevice.State.INITIALIZED);
return builder;
@@ -54,7 +54,7 @@ public abstract class MultiFetchRequest extends Request {
throw new IllegalStateException("Device is busy");
}
builder.setBusyTask(getOperationName());
builder.wait(1000); // Wait a bit (after previous operation), or device sometimes won't respond
builder.sleep(1000); // Wait a bit (after previous operation), or device sometimes won't respond
}
@Override
@@ -118,7 +118,7 @@ public class MarstekB2500DeviceSupport extends AbstractBTLESingleDeviceSupport {
getDevice().setFirmwareVersion2("N/A");
builder.requestMtu(512);
builder.notify(UUID_CHARACTERISTIC_MAIN, true);
builder.wait(3500);
builder.sleep(3500);
builder.write(UUID_CHARACTERISTIC_MAIN, COMMAND_GET_INFOS1);
return builder;
}
@@ -133,7 +133,7 @@ public class MarstekB2500DeviceSupport extends AbstractBTLESingleDeviceSupport {
BluetoothGattCharacteristic characteristic = getCharacteristic(UUID_CHARACTERISTIC_MAIN);
if (characteristic != null && contents != null) {
builder.write(characteristic, contents);
builder.wait(750);
builder.sleep(750);
builder.queue();
}
}
@@ -87,12 +87,12 @@ public class V1NotificationStrategy implements NotificationStrategy {
int on = onOffSequence[j];
on = Math.min(500, on); // longer than 500ms is not possible
builder.write(controlPoint, startVibrate);
builder.wait(on);
builder.sleep(on);
builder.write(controlPoint, stopVibrate);
if (++j < onOffSequence.length) {
int off = Math.max(onOffSequence[j], 25); // wait at least 25ms
builder.wait(off);
builder.sleep(off);
}
if (extraAction != null) {
@@ -112,10 +112,10 @@ public class V1NotificationStrategy implements NotificationStrategy {
// int vDuration = Math.min(500, vibrateDuration); // longer than 500ms is not possible
// for (int i = 0; i < vibrateTimes; i++) {
// builder.write(controlPoint, startVibrate);
// builder.wait(vDuration);
// builder.sleep(vDuration);
// builder.write(controlPoint, stopVibrate);
// if (pause > 0) {
// builder.wait(pause);
// builder.sleep(pause);
// }
// }
//
@@ -52,14 +52,14 @@ public class V2NotificationStrategy<T extends AbstractBTLESingleDeviceSupport> i
int on = onOffSequence[j];
on = Math.min(500, on); // longer than 500ms is not possible
builder.write(alert, new byte[]{GattCharacteristic.MILD_ALERT}); //MILD_ALERT lights up GREEN leds, HIGH_ALERT lights up RED leds
// builder.wait(on);
// builder.sleep(on);
// builder.write(alert, new byte[]{GattCharacteristic.HIGH_ALERT});
builder.wait(on);
builder.sleep(on);
builder.write(alert, new byte[]{GattCharacteristic.NO_ALERT});
if (++j < onOffSequence.length) {
int off = Math.max(onOffSequence[j], 25); // wait at least 25ms
builder.wait(off);
builder.sleep(off);
}
if (extraAction != null) {
@@ -573,7 +573,7 @@ public class ZeTimeDeviceSupport extends AbstractBTLESingleDeviceSupport {
byte[] message = encodeCalendarEvent(body, calendarEvent.getBeginSeconds(), opcode);
sendMsgToWatch(builder, message);
builder.wait(300); // Urgh, seems it is a general problem when sending data too fast
builder.sleep(300); // Urgh, seems it is a general problem when sending data too fast
if (eventCount++ == 16) { // limit this to 16 for now
break;