1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-06-01 19:06:06 +02:00
Gadgetbridge/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/GarminByteBufferReader.java
Daniele Gobbetti bdfab59a81 Garmin: Add support for replying to notifications
This uses the (assumed) new method of passing multiple actions, instead of the (assumed) legacy accept/decline approach.
At the moment the preset messages stored on the watch firmware are used for replying, the code supports using custom messages already but those have to be updated to the watch somehow (probably by protobuf) and this is not supported yet. Using custom messages if they are not set will just do nothing.
The NotificationActionIconPosition values have been determined on a vĂ­vomove Style and might not work properly on other watches.
The evaluation of GBDeviceEvent have been moved in GarminSupport since the notification actions handling uses device events.

Also adds a method to read null terminated strings to GarminByteBufferReader.
Also adds a warning in NotificationListener if the wrong handle is used for replying to a notification.
2024-05-03 20:28:11 +02:00

83 lines
1.9 KiB
Java

package nodomain.freeyourgadget.gadgetbridge.service.devices.garmin;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
public class GarminByteBufferReader {
protected final ByteBuffer byteBuffer;
public GarminByteBufferReader(byte[] data) {
this.byteBuffer = ByteBuffer.wrap(data);
}
public int remaining() {
return byteBuffer.remaining();
}
public ByteBuffer asReadOnlyBuffer() {
return byteBuffer.asReadOnlyBuffer();
}
public void setByteOrder(ByteOrder byteOrder) {
this.byteBuffer.order(byteOrder);
}
public int readByte() {
return Byte.toUnsignedInt(byteBuffer.get());
}
public int getPosition() {
return byteBuffer.position();
}
public int readShort() {
return Short.toUnsignedInt(byteBuffer.getShort());
}
public int readInt() {
return byteBuffer.getInt();
}
public long readLong() {
return byteBuffer.getLong();
}
public float readFloat32() {
return byteBuffer.getFloat();
}
public double readFloat64() {
return byteBuffer.getDouble();
}
public String readString() {
final int size = readByte();
byte[] bytes = new byte[size];
byteBuffer.get(bytes);
return new String(bytes, StandardCharsets.UTF_8);
}
public String readNullTerminatedString() {
int position = byteBuffer.position();
int size = 0;
while (byteBuffer.hasRemaining()) {
if (byteBuffer.get() == 0)
break;
size++;
}
byteBuffer.position(position);
byte[] bytes = new byte[size];
byteBuffer.get(bytes);
return new String(bytes, StandardCharsets.UTF_8);
}
public byte[] readBytes(int size) {
byte[] bytes = new byte[size];
byteBuffer.get(bytes);
return bytes;
}
}