1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-29 05:16:51 +01:00

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
This commit is contained in:
Andreas Shimokawa 2020-01-29 19:19:53 +01:00
parent 5e8004756f
commit 23f4752296
8 changed files with 25 additions and 39 deletions

View File

@ -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 {

View File

@ -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;

View File

@ -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 {

View File

@ -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) {

View File

@ -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 {

View File

@ -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 {

View File

@ -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);
}
}

View File

@ -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);
}
}