From 23f47522961bb1b4e96b18194f47f9af138458c7 Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Wed, 29 Jan 2020 19:19:53 +0100 Subject: [PATCH] Fossil HR: Merge StringUtils in generic class, fix terminateNull() This fixes string truncation when using non ascii UTF-8 characters. Also fixes the Debug screen for Fossil HR --- .../notification/PlayNotificationRequest.java | 2 +- .../requests/fossil_hr/file/AssetFile.java | 3 +- .../fossil_hr/file/AssetFilePutRequest.java | 1 - .../NotificationFilterPutHRRequest.java | 3 +- .../NotificationImagePutRequest.java | 4 +-- .../PlayNotificationHRRequest.java | 3 +- .../devices/qhybrid/utils/StringUtils.java | 29 ------------------- .../gadgetbridge/util/StringUtils.java | 19 ++++++++++++ 8 files changed, 25 insertions(+), 39 deletions(-) delete mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/utils/StringUtils.java diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil/notification/PlayNotificationRequest.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil/notification/PlayNotificationRequest.java index 09eb389bc..4a624b7af 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil/notification/PlayNotificationRequest.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil/notification/PlayNotificationRequest.java @@ -23,7 +23,7 @@ import java.util.zip.CRC32; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.adapter.fossil.FossilWatchAdapter; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.file.FilePutRequest; -import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.utils.StringUtils; +import nodomain.freeyourgadget.gadgetbridge.util.StringUtils; public class PlayNotificationRequest extends FilePutRequest { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/file/AssetFile.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/file/AssetFile.java index 9973cae47..bf23584b9 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/file/AssetFile.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/file/AssetFile.java @@ -3,7 +3,8 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fo import java.nio.ByteBuffer; import java.util.zip.CRC32; -import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.utils.StringUtils; +import nodomain.freeyourgadget.gadgetbridge.util.StringUtils; + public class AssetFile { private String fileName; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/file/AssetFilePutRequest.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/file/AssetFilePutRequest.java index a0ffd9c92..e6709d971 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/file/AssetFilePutRequest.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/file/AssetFilePutRequest.java @@ -7,7 +7,6 @@ import java.nio.ByteOrder; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.adapter.fossil.FossilWatchAdapter; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.file.FilePutRequest; -import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.utils.StringUtils; public class AssetFilePutRequest extends FilePutRequest { public AssetFilePutRequest(AssetFile[] files, byte subHandle, FossilWatchAdapter adapter) throws IOException { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/notification/NotificationFilterPutHRRequest.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/notification/NotificationFilterPutHRRequest.java index 43ee03b9f..5b6bc25d3 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/notification/NotificationFilterPutHRRequest.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/notification/NotificationFilterPutHRRequest.java @@ -5,11 +5,10 @@ import java.nio.ByteOrder; import java.util.ArrayList; import java.util.zip.CRC32; -import nodomain.freeyourgadget.gadgetbridge.devices.qhybrid.NotificationConfiguration; import nodomain.freeyourgadget.gadgetbridge.devices.qhybrid.NotificationHRConfiguration; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.adapter.fossil.FossilWatchAdapter; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.file.FilePutRequest; -import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.utils.StringUtils; +import nodomain.freeyourgadget.gadgetbridge.util.StringUtils; public class NotificationFilterPutHRRequest extends FilePutRequest { public NotificationFilterPutHRRequest(NotificationHRConfiguration[] configs, FossilWatchAdapter adapter) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/notification/NotificationImagePutRequest.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/notification/NotificationImagePutRequest.java index f27d2d337..27850f7f1 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/notification/NotificationImagePutRequest.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/notification/NotificationImagePutRequest.java @@ -3,14 +3,12 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fo import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; -import java.util.ArrayList; -import java.util.stream.Stream; import java.util.zip.CRC32; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.adapter.fossil.FossilWatchAdapter; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.file.AssetFile; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.file.AssetFilePutRequest; -import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.utils.StringUtils; +import nodomain.freeyourgadget.gadgetbridge.util.StringUtils; public class NotificationImagePutRequest extends AssetFilePutRequest { private NotificationImagePutRequest(String packageName, AssetFile file, FossilWatchAdapter adapter) throws IOException { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/notification/PlayNotificationHRRequest.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/notification/PlayNotificationHRRequest.java index d04158982..43279574f 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/notification/PlayNotificationHRRequest.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil_hr/notification/PlayNotificationHRRequest.java @@ -8,8 +8,7 @@ import java.util.zip.CRC32; import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.adapter.fossil.FossilWatchAdapter; import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.file.FilePutRequest; -import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil.notification.PlayNotificationRequest; -import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.utils.StringUtils; +import nodomain.freeyourgadget.gadgetbridge.util.StringUtils; public class PlayNotificationHRRequest extends FilePutRequest { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/utils/StringUtils.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/utils/StringUtils.java deleted file mode 100644 index 4e9a94227..000000000 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/utils/StringUtils.java +++ /dev/null @@ -1,29 +0,0 @@ -package nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.utils; - -public class StringUtils extends nodomain.freeyourgadget.gadgetbridge.util.StringUtils { - public static String terminateNull(String input){ - if(input.length() == 0){ - return new String(new byte[]{(byte) 0}); - } - char lastChar = input.charAt(input.length() - 1); - if(lastChar == 0) return input; - - byte[] newArray = new byte[input.length() + 1]; - System.arraycopy(input.getBytes(), 0, newArray, 0, input.length()); - - newArray[newArray.length - 1] = 0; - - return new String(newArray); - } - - private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray(); - public static String bytesToHex(byte[] bytes) { - char[] hexChars = new char[bytes.length * 2]; - for (int j = bytes.length - 1; j >= 0; j--) { - int v = bytes[j] & 0xFF; - hexChars[j * 2] = HEX_ARRAY[v >>> 4]; - hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F]; - } - return new String(hexChars); - } -} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/StringUtils.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/StringUtils.java index d2f1ce0dd..0495dae01 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/StringUtils.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/StringUtils.java @@ -96,4 +96,23 @@ public class StringUtils { } return ""; } + + public static String terminateNull(String input) { + if (input == null || input.length() == 0) { + return new String(new byte[]{(byte) 0}); + } + char lastChar = input.charAt(input.length() - 1); + if (lastChar == 0) return input; + + byte[] newArray = new byte[input.getBytes().length + 1]; + System.arraycopy(input.getBytes(), 0, newArray, 0, input.getBytes().length); + + newArray[newArray.length - 1] = 0; + + return new String(newArray); + } + + public static String bytesToHex(byte[] array) { + return GB.hexdump(array, 0, -1); + } }