1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-28 12:56:49 +01:00

Huami2021FirmwareInfo: use ZipFile functions where possible, separate firmware type handling to different functions for readability.

merge conflict fix
This commit is contained in:
MPeter 2022-09-01 15:10:45 +02:00
parent 81ef7698f6
commit e7bd1620fe

View File

@ -29,16 +29,12 @@ import java.util.Arrays;
import nodomain.freeyourgadget.gadgetbridge.service.btle.BLETypeConversions;
import nodomain.freeyourgadget.gadgetbridge.util.ArrayUtils;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.ZipUtils;
import nodomain.freeyourgadget.gadgetbridge.util.ZipFile;
public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo {
private static final Logger LOG = LoggerFactory.getLogger(Huami2021FirmwareInfo.class);
public static final byte[] ZIP_HEADER = new byte[]{
0x50, 0x4B, 0x03, 0x04
};
public Huami2021FirmwareInfo(final byte[] bytes) {
super(bytes);
}
@ -56,6 +52,15 @@ public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo {
@Override
protected HuamiFirmwareType determineFirmwareType(final byte[] bytes) {
if (ArrayUtils.equals(bytes, UIHHContainer.UIHH_HEADER, 0)) {
return handleUihhPackage(bytes);
} else if (ZipFile.isZipFile(bytes)) {
return handleZipPackage(bytes);
} else {
return HuamiFirmwareType.INVALID;
}
}
private HuamiFirmwareType handleUihhPackage(byte[] bytes) {
final UIHHContainer uihh = UIHHContainer.fromRawBytes(bytes);
if (uihh == null) {
LOG.warn("Invalid UIHH file");
@ -78,7 +83,7 @@ public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo {
}
if (uihhFirmwareZipFile != null && hasChangelog) {
final byte[] firmwareBin = ZipUtils.getFileFromZip(uihhFirmwareZipFile.getContent(), "META/firmware.bin");
byte[] firmwareBin = ZipFile.tryReadFileQuick(uihhFirmwareZipFile.getContent(), "META/firmware.bin");
if (isCompatibleFirmwareBin(firmwareBin)) {
return HuamiFirmwareType.FIRMWARE_UIHH_2021_ZIP_WITH_CHANGELOG;
@ -88,11 +93,8 @@ public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo {
return HuamiFirmwareType.INVALID;
}
if (!ArrayUtils.equals(bytes, ZIP_HEADER, 0)) {
return HuamiFirmwareType.INVALID;
}
final byte[] firmwareBin = ZipUtils.getFileFromZip(bytes, "META/firmware.bin");
private HuamiFirmwareType handleZipPackage(byte[] bytes) {
final byte[] firmwareBin = ZipFile.tryReadFileQuick(bytes, "META/firmware.bin");
if (isCompatibleFirmwareBin(firmwareBin)) {
return HuamiFirmwareType.FIRMWARE;
}
@ -191,7 +193,7 @@ public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo {
}
public String getFirmwareVersion(final byte[] fwbytes) {
final byte[] firmwareBin = ZipUtils.getFileFromZip(fwbytes, "META/firmware.bin");
final byte[] firmwareBin = ZipFile.tryReadFileQuick(fwbytes, "META/firmware.bin");
if (firmwareBin == null) {
LOG.warn("Failed to read firmware.bin");
@ -224,7 +226,7 @@ public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo {
}
public String getAppName() {
final byte[] appJsonBin = ZipUtils.getFileFromZip(getBytes(), "app.json");
final byte[] appJsonBin = ZipFile.tryReadFileQuick(getBytes(), "app.json");
if (appJsonBin == null) {
LOG.warn("Failed to get app.json from zip");
return null;
@ -248,7 +250,7 @@ public abstract class Huami2021FirmwareInfo extends AbstractHuamiFirmwareInfo {
}
public String getAppType() {
final byte[] appJsonBin = ZipUtils.getFileFromZip(getBytes(), "app.json");
final byte[] appJsonBin = ZipFile.tryReadFileQuick(getBytes(), "app.json");
if (appJsonBin == null) {
LOG.warn("Failed to get app.json from zip");
return null;