mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-12-02 23:12:57 +01:00
Mi Band 7: Fix reminder, calendar and canned messages UTF-8 encoding
- Fixes a crash when calendar events have multibyte UTF-8 characters - Fixes canned message accented characters
This commit is contained in:
parent
5637a2bc45
commit
c1e8a57a22
@ -282,10 +282,10 @@ public abstract class Huami2021Support extends HuamiSupport {
|
|||||||
|
|
||||||
int length = 34;
|
int length = 34;
|
||||||
if (calendarEventSpec.title != null) {
|
if (calendarEventSpec.title != null) {
|
||||||
length += calendarEventSpec.title.length();
|
length += calendarEventSpec.title.getBytes(StandardCharsets.UTF_8).length;
|
||||||
}
|
}
|
||||||
if (calendarEventSpec.description != null) {
|
if (calendarEventSpec.description != null) {
|
||||||
length += calendarEventSpec.description.length();
|
length += calendarEventSpec.description.getBytes(StandardCharsets.UTF_8).length;
|
||||||
}
|
}
|
||||||
|
|
||||||
final ByteBuffer buf = ByteBuffer.allocate(length);
|
final ByteBuffer buf = ByteBuffer.allocate(length);
|
||||||
@ -691,7 +691,18 @@ public abstract class Huami2021Support extends HuamiSupport {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final ByteBuffer buf = ByteBuffer.allocate(1 + 10 + reminder.getMessage().getBytes().length + 1);
|
final String message;
|
||||||
|
if (reminder.getMessage().length() > coordinator.getMaximumReminderMessageLength()) {
|
||||||
|
LOG.warn("The reminder message length {} is longer than {}, will be truncated",
|
||||||
|
reminder.getMessage().length(),
|
||||||
|
coordinator.getMaximumReminderMessageLength()
|
||||||
|
);
|
||||||
|
message = StringUtils.truncate(reminder.getMessage(), coordinator.getMaximumReminderMessageLength());
|
||||||
|
} else {
|
||||||
|
message = reminder.getMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
final ByteBuffer buf = ByteBuffer.allocate(1 + 10 + message.getBytes(StandardCharsets.UTF_8).length + 1);
|
||||||
buf.order(ByteOrder.LITTLE_ENDIAN);
|
buf.order(ByteOrder.LITTLE_ENDIAN);
|
||||||
|
|
||||||
// Update does an upsert, so let's use it. If we call create twice on the same ID, it becomes weird
|
// Update does an upsert, so let's use it. If we call create twice on the same ID, it becomes weird
|
||||||
@ -729,15 +740,7 @@ public abstract class Huami2021Support extends HuamiSupport {
|
|||||||
buf.putInt((int) (cal.getTimeInMillis() / 1000L));
|
buf.putInt((int) (cal.getTimeInMillis() / 1000L));
|
||||||
buf.put((byte) 0x00);
|
buf.put((byte) 0x00);
|
||||||
|
|
||||||
if (reminder.getMessage().getBytes().length > coordinator.getMaximumReminderMessageLength()) {
|
buf.put(message.getBytes(StandardCharsets.UTF_8));
|
||||||
LOG.warn("The reminder message length {} is longer than {}, will be truncated",
|
|
||||||
reminder.getMessage().getBytes().length,
|
|
||||||
coordinator.getMaximumReminderMessageLength()
|
|
||||||
);
|
|
||||||
buf.put(Arrays.copyOf(reminder.getMessage().getBytes(), coordinator.getMaximumReminderMessageLength()));
|
|
||||||
} else {
|
|
||||||
buf.put(reminder.getMessage().getBytes());
|
|
||||||
}
|
|
||||||
buf.put((byte) 0x00);
|
buf.put((byte) 0x00);
|
||||||
|
|
||||||
writeToChunked2021(builder, CHUNKED2021_ENDPOINT_REMINDERS, buf.array(), false);
|
writeToChunked2021(builder, CHUNKED2021_ENDPOINT_REMINDERS, buf.array(), false);
|
||||||
@ -805,14 +808,14 @@ public abstract class Huami2021Support extends HuamiSupport {
|
|||||||
cannedMessage = StringUtils.truncate(cannedMessage, 140);
|
cannedMessage = StringUtils.truncate(cannedMessage, 140);
|
||||||
LOG.debug("Setting canned message {} = '{}'", i, cannedMessage);
|
LOG.debug("Setting canned message {} = '{}'", i, cannedMessage);
|
||||||
|
|
||||||
final int length = cannedMessage.getBytes().length + 7;
|
final int length = cannedMessage.getBytes(StandardCharsets.UTF_8).length + 7;
|
||||||
final ByteBuffer buf = ByteBuffer.allocate(length);
|
final ByteBuffer buf = ByteBuffer.allocate(length);
|
||||||
buf.order(ByteOrder.LITTLE_ENDIAN);
|
buf.order(ByteOrder.LITTLE_ENDIAN);
|
||||||
buf.put(CANNED_MESSAGES_CMD_SET);
|
buf.put(CANNED_MESSAGES_CMD_SET);
|
||||||
buf.putInt(i++);
|
buf.putInt(i++);
|
||||||
buf.put((byte) cannedMessage.length());
|
buf.put((byte) cannedMessage.getBytes(StandardCharsets.UTF_8).length);
|
||||||
buf.put((byte) 0x00);
|
buf.put((byte) 0x00);
|
||||||
buf.put(cannedMessage.getBytes());
|
buf.put(cannedMessage.getBytes(StandardCharsets.UTF_8));
|
||||||
writeToChunked2021(builder, CHUNKED2021_ENDPOINT_CANNED_MESSAGES, buf.array(), false);
|
writeToChunked2021(builder, CHUNKED2021_ENDPOINT_CANNED_MESSAGES, buf.array(), false);
|
||||||
}
|
}
|
||||||
builder.queue(getQueue());
|
builder.queue(getQueue());
|
||||||
@ -2584,7 +2587,7 @@ public abstract class Huami2021Support extends HuamiSupport {
|
|||||||
|
|
||||||
final ByteBuffer buf = ByteBuffer.allocate(2 + url.length() + 1 + filename.length() + 1 + 4 + 4);
|
final ByteBuffer buf = ByteBuffer.allocate(2 + url.length() + 1 + filename.length() + 1 + 4 + 4);
|
||||||
buf.order(ByteOrder.LITTLE_ENDIAN);
|
buf.order(ByteOrder.LITTLE_ENDIAN);
|
||||||
buf.put((byte) ICONS_CMD_SEND_REQUEST);
|
buf.put(ICONS_CMD_SEND_REQUEST);
|
||||||
buf.put((byte) 0x00);
|
buf.put((byte) 0x00);
|
||||||
buf.put(url.getBytes(StandardCharsets.UTF_8));
|
buf.put(url.getBytes(StandardCharsets.UTF_8));
|
||||||
buf.put((byte) 0x00);
|
buf.put((byte) 0x00);
|
||||||
|
Loading…
Reference in New Issue
Block a user