mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2025-02-04 13:04:08 +01:00
use ZipFile utility for reading the ZIP file instead of custom logic
merge conflict fix
This commit is contained in:
parent
56d087da2f
commit
c7841b4947
@ -20,17 +20,14 @@ import static java.nio.charset.StandardCharsets.UTF_8;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Build;
|
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.InputStream;
|
import java.io.IOException;
|
||||||
import java.util.zip.ZipEntry;
|
|
||||||
import java.util.zip.ZipInputStream;
|
|
||||||
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.R;
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.activities.InstallActivity;
|
import nodomain.freeyourgadget.gadgetbridge.activities.InstallActivity;
|
||||||
@ -39,6 +36,8 @@ import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
|||||||
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
|
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.model.GenericItem;
|
import nodomain.freeyourgadget.gadgetbridge.model.GenericItem;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.util.UriHelper;
|
import nodomain.freeyourgadget.gadgetbridge.util.UriHelper;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.util.ZipFile;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.util.ZipFileException;
|
||||||
|
|
||||||
public class PineTimeInstallHandler implements InstallHandler {
|
public class PineTimeInstallHandler implements InstallHandler {
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(PineTimeInstallHandler.class);
|
private static final Logger LOG = LoggerFactory.getLogger(PineTimeInstallHandler.class);
|
||||||
@ -51,46 +50,26 @@ public class PineTimeInstallHandler implements InstallHandler {
|
|||||||
this.context = context;
|
this.context = context;
|
||||||
|
|
||||||
UriHelper uriHelper;
|
UriHelper uriHelper;
|
||||||
InputStream inputStream;
|
|
||||||
ZipInputStream zipInputStream;
|
|
||||||
|
|
||||||
InfiniTimeDFUPackage metadata = null;
|
|
||||||
try {
|
try {
|
||||||
uriHelper = UriHelper.get(uri, this.context);
|
uriHelper = UriHelper.get(uri, this.context);
|
||||||
inputStream = new BufferedInputStream(uriHelper.openInputStream());
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
ZipFile dfuPackage = new ZipFile(uriHelper.openInputStream());
|
||||||
zipInputStream = new ZipInputStream(inputStream, UTF_8);
|
String manifest = new String(dfuPackage.getFileFromZip("manifest.json"));
|
||||||
} else {
|
dfuPackage.close();
|
||||||
zipInputStream = new ZipInputStream(inputStream);
|
|
||||||
|
if (!manifest.trim().isEmpty()) {
|
||||||
|
dfuPackageManifest = new Gson().fromJson(manifest.trim(), InfiniTimeDFUPackage.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
ZipEntry entry;
|
} catch (ZipFileException e) {
|
||||||
while ((entry = zipInputStream.getNextEntry()) != null) {
|
LOG.error("Unable to read manifest file.", e);
|
||||||
if (entry.isDirectory()) {
|
} catch (FileNotFoundException e) {
|
||||||
continue;
|
LOG.error("The DFU file was not found.", e);
|
||||||
}
|
} catch (IOException e) {
|
||||||
if (entry.getName().equals("manifest.json")) {
|
LOG.error("General IO error occurred.", e);
|
||||||
LOG.debug("Found manifest.json in DFU zip");
|
|
||||||
StringBuilder json = new StringBuilder();
|
|
||||||
|
|
||||||
final byte[] buffer = new byte[1024];
|
|
||||||
|
|
||||||
int read;
|
|
||||||
while ((read = zipInputStream.read(buffer, 0, buffer.length)) != -1) {
|
|
||||||
json.append(new String(buffer, 0, read));
|
|
||||||
}
|
|
||||||
|
|
||||||
Gson gson = new Gson();
|
|
||||||
metadata = gson.fromJson(json.toString().trim(), InfiniTimeDFUPackage.class);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
zipInputStream.close();
|
|
||||||
inputStream.close();
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
valid = false;
|
LOG.error("Unknown error occurred.", e);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (metadata != null &&
|
if (metadata != null &&
|
||||||
|
Loading…
Reference in New Issue
Block a user