From c1e8a57a22cb63f0cf6e8b6cfd0f4ac1b434bda8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Rebelo?= Date: Thu, 25 Aug 2022 10:04:14 +0100 Subject: [PATCH] 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 --- .../devices/huami/Huami2021Support.java | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/Huami2021Support.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/Huami2021Support.java index edf98428a..1fa18ade0 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/Huami2021Support.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/Huami2021Support.java @@ -282,10 +282,10 @@ public abstract class Huami2021Support extends HuamiSupport { int length = 34; if (calendarEventSpec.title != null) { - length += calendarEventSpec.title.length(); + length += calendarEventSpec.title.getBytes(StandardCharsets.UTF_8).length; } if (calendarEventSpec.description != null) { - length += calendarEventSpec.description.length(); + length += calendarEventSpec.description.getBytes(StandardCharsets.UTF_8).length; } final ByteBuffer buf = ByteBuffer.allocate(length); @@ -691,7 +691,18 @@ public abstract class Huami2021Support extends HuamiSupport { 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); // 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.put((byte) 0x00); - if (reminder.getMessage().getBytes().length > coordinator.getMaximumReminderMessageLength()) { - 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(message.getBytes(StandardCharsets.UTF_8)); buf.put((byte) 0x00); writeToChunked2021(builder, CHUNKED2021_ENDPOINT_REMINDERS, buf.array(), false); @@ -805,14 +808,14 @@ public abstract class Huami2021Support extends HuamiSupport { cannedMessage = StringUtils.truncate(cannedMessage, 140); 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); buf.order(ByteOrder.LITTLE_ENDIAN); buf.put(CANNED_MESSAGES_CMD_SET); buf.putInt(i++); - buf.put((byte) cannedMessage.length()); + buf.put((byte) cannedMessage.getBytes(StandardCharsets.UTF_8).length); buf.put((byte) 0x00); - buf.put(cannedMessage.getBytes()); + buf.put(cannedMessage.getBytes(StandardCharsets.UTF_8)); writeToChunked2021(builder, CHUNKED2021_ENDPOINT_CANNED_MESSAGES, buf.array(), false); } 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); buf.order(ByteOrder.LITTLE_ENDIAN); - buf.put((byte) ICONS_CMD_SEND_REQUEST); + buf.put(ICONS_CMD_SEND_REQUEST); buf.put((byte) 0x00); buf.put(url.getBytes(StandardCharsets.UTF_8)); buf.put((byte) 0x00);