From 320dde9a0ec1f98c384bd891dc9ea63e106d13d4 Mon Sep 17 00:00:00 2001 From: Arjan Schrijver Date: Fri, 19 Sep 2025 22:09:05 +0200 Subject: [PATCH] Fix compile errors (untested) --- .../pinetime/PineTimeInstallHandler.java | 2 - .../profiles/adablefs/AdaBleFsProfile.java | 203 +++++++++--------- .../devices/pinetime/PineTimeJFSupport.java | 11 +- .../util/RemoteFileSystemCache.java | 1 + app/src/main/res/values/strings.xml | 1 + 5 files changed, 113 insertions(+), 105 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pinetime/PineTimeInstallHandler.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pinetime/PineTimeInstallHandler.java index e1a4e99e2f..2ef19d7679 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pinetime/PineTimeInstallHandler.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pinetime/PineTimeInstallHandler.java @@ -17,8 +17,6 @@ package nodomain.freeyourgadget.gadgetbridge.devices.pinetime; import android.app.Activity; -import static java.nio.charset.StandardCharsets.UTF_8; - import android.content.Context; import android.net.Uri; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/profiles/adablefs/AdaBleFsProfile.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/profiles/adablefs/AdaBleFsProfile.java index 1060d88a86..fcb4fc2d36 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/profiles/adablefs/AdaBleFsProfile.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/profiles/adablefs/AdaBleFsProfile.java @@ -41,16 +41,17 @@ import java.util.Vector; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSupport; +import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLESingleDeviceSupport; import nodomain.freeyourgadget.gadgetbridge.service.btle.BtLEQueue; import nodomain.freeyourgadget.gadgetbridge.service.btle.GattCharacteristic; import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder; import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.AbstractBleProfile; +import nodomain.freeyourgadget.gadgetbridge.util.GBZipFile; import nodomain.freeyourgadget.gadgetbridge.util.RemoteFileSystemCache; import nodomain.freeyourgadget.gadgetbridge.util.UriHelper; -import nodomain.freeyourgadget.gadgetbridge.util.ZipFile; import nodomain.freeyourgadget.gadgetbridge.util.ZipFileException; -public class AdaBleFsProfile extends AbstractBleProfile { +public class AdaBleFsProfile extends AbstractBleProfile { final byte PADDING_BYTE = 0x00; final byte REQUEST_CONTINUED = 0x01; final byte REQUEST_WRITE_FILE_START = 0x20; @@ -84,7 +85,6 @@ public class AdaBleFsProfile extends Abstra int chunkSize; private RemoteFileSystemCache remoteFs; - List listingDirectory; private static final Logger LOG = LoggerFactory.getLogger(AdaBleFsProfile.class); @@ -101,7 +101,7 @@ public class AdaBleFsProfile extends Abstra // Unzip try { UriHelper uriHelper = UriHelper.get(uri, context); - ZipFile zipPackage = new ZipFile(uriHelper.openInputStream()); + GBZipFile zipPackage = new GBZipFile(uriHelper.openInputStream()); JSONObject resources_manifest = new JSONObject(new String(zipPackage.getFileFromZip(("resources.json")))); JSONArray resources = resources_manifest.getJSONArray("resources"); @@ -127,10 +127,14 @@ public class AdaBleFsProfile extends Abstra } catch (Exception e) { LOG.error("Unknown error occurred.", e); } - this.startNextAdaFsAction(); + try { + this.startNextAdaFsAction(); + } catch (IOException e) { + LOG.error("Error while loading resources: ", e); + } } - private void startNextAdaFsAction() { + private void startNextAdaFsAction() throws IOException { if (adaBleFsQueue.size() == 0) { return; } @@ -149,11 +153,16 @@ public class AdaBleFsProfile extends Abstra public void enableNotify(TransactionBuilder builder, boolean enable) { builder.notify(getCharacteristic(UUID_CHARACTERISTIC_FS_TRANSFER), enable); } + public boolean onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { UUID charUuid = characteristic.getUuid(); if (charUuid.equals(UUID_CHARACTERISTIC_FS_TRANSFER)) { - handleNextStatus(gatt, characteristic); + try { + handleNextStatus(gatt, characteristic); + } catch (IOException e) { + LOG.error("Error handling status: ", e); + } return true; } else { LOG.info("Unexpected onCharacteristicRead: " + GattCharacteristic.toString(characteristic)); @@ -164,7 +173,7 @@ public class AdaBleFsProfile extends Abstra return false; } - private void handleNextStatus(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { + private void handleNextStatus(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) throws IOException { // Determine what we are waiting on final byte[] returned = characteristic.getValue(); // if doing file upload @@ -233,55 +242,55 @@ public class AdaBleFsProfile extends Abstra for(int counter = 0; counter < length; counter++) { stringBytes[counter] = returned[28+counter]; } - Map relativeRoot = directoryTree; - for(String path: listingDirectory) { - relativeRoot = (Map) relativeRoot.get(path); - } +// Map relativeRoot = directoryTree; +// for(String path: listingDirectory) { +// relativeRoot = (Map) relativeRoot.get(path); +// } try { String path = new String(stringBytes, "UTF-8"); - Map here = directoryTree; // TODO Paths returned are relative to where we requested paths from so we need to alter this - // TODO Remove first / ? - // Split path on / - String soFar = ""; - final String[] paths = path.split("/"); - for(int counter = 0; counter < paths.length; counter++) { - final String dir = paths[counter]; - soFar = soFar + "/" + dir; - if (here.containsKey(dir)) { - Object entry = here.get(dir); - if (counter < (paths.length-1) || isDirectory) { - // If not at last entry in paths, or if the response says this is a directory - if (entry instanceof HashMap) { - // All good, continue; - } else { - // our map doesn't list directory, but this is? - LOG.warn("GadgetBridge cache thinks " + soFar + " is a file, but device thinks it's a directory."); - here.put(dir, new HashMap()); - } - } else { - // We are at the last string in paths, and response says this isn't a file - if (entry instanceof String) { - // All good, continue - } else { - LOG.warn("GadgetBridge cache thinks " + soFar + " is a directory, but device thinks it's a file."); - here.put(dir, dir); - } - } - } else { - // Our cache doesn't know of soFar - if (counter < (paths.length-1) || isDirectory) { - here.put(dir, new HashMap()); - } else { - here.put(dir, dir); - } - } - } +// Map here = directoryTree; // TODO Paths returned are relative to where we requested paths from so we need to alter this +// // TODO Remove first / ? +// // Split path on / +// String soFar = ""; +// final String[] paths = path.split("/"); +// for(int counter = 0; counter < paths.length; counter++) { +// final String dir = paths[counter]; +// soFar = soFar + "/" + dir; +// if (here.containsKey(dir)) { +// Object entry = here.get(dir); +// if (counter < (paths.length-1) || isDirectory) { +// // If not at last entry in paths, or if the response says this is a directory +// if (entry instanceof HashMap) { +// // All good, continue; +// } else { +// // our map doesn't list directory, but this is? +// LOG.warn("GadgetBridge cache thinks " + soFar + " is a file, but device thinks it's a directory."); +// here.put(dir, new HashMap()); +// } +// } else { +// // We are at the last string in paths, and response says this isn't a file +// if (entry instanceof String) { +// // All good, continue +// } else { +// LOG.warn("GadgetBridge cache thinks " + soFar + " is a directory, but device thinks it's a file."); +// here.put(dir, dir); +// } +// } +// } else { +// // Our cache doesn't know of soFar +// if (counter < (paths.length-1) || isDirectory) { +// here.put(dir, new HashMap()); +// } else { +// here.put(dir, dir); +// } +// } +// } } catch (UnsupportedEncodingException e) { // Pretty sure this branch will never happen } - if (entryNum == totalEntries) { - relativeRoot.put(".", "."); - } +// if (entryNum == totalEntries) { +// relativeRoot.put(".", "."); +// } return entryNum == totalEntries; } @@ -310,7 +319,7 @@ public class AdaBleFsProfile extends Abstra return true; } - private boolean checkContinueFileUpload(byte[] returned) { + private boolean checkContinueFileUpload(byte[] returned) throws IOException { /** * Check that a file upload command has completed * @@ -329,11 +338,11 @@ public class AdaBleFsProfile extends Abstra } @Override - public boolean onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { + public boolean onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, byte[] value, int status) { return onCharacteristicRead(gatt, characteristic, BluetoothGatt.GATT_SUCCESS); } - private void uploadNextFileChunk() { + private void uploadNextFileChunk() throws IOException { final AdaBleFsAction nextAction = adaBleFsQueue.getFirst(); final int toSendSize = Math.min(chunkSize, nextAction.data.length - bytesOfFileWritten); ArrayList command = new ArrayList<>(); @@ -356,10 +365,10 @@ public class AdaBleFsProfile extends Abstra for(int i = 0; i < command.size(); i++) { bytes[i] = command.get(i); } - TransactionBuilder builder = new TransactionBuilder("Upload file chunk"); + TransactionBuilder builder = performInitialized("Upload file chunk"); builder.write(getCharacteristic(UUID_CHARACTERISTIC_FS_TRANSFER), bytes); builder.read(getCharacteristic(UUID_CHARACTERISTIC_FS_TRANSFER)); - builder.queue(getQueue()); + builder.queue(); } private void uploadFileInitialise() { @@ -371,33 +380,33 @@ public class AdaBleFsProfile extends Abstra // then uploadFileStart() } - private TriState directoryExists(String[] directoryList) { - Map here = directoryTree; - String[] soFar; - String totalPath = "/"; - for(String directory: directoryList) { - if (here.containsKey(directory)) { - Object entry = here.get(directory); - if (entry instanceof HashMap) { - here = (Map) entry; - totalPath += directory + "/"; - } else { - // Not a HashMap, must be a string then and so this is a file, not a directory - return TriState.FALSE; - } - } else if (here.containsKey(".")) { - // We have listed this directory, and the requested path does not exist - return TriState.FALSE; - } - // Request directory listing for here - totalPath += directory; - requestListDirectory(totalPath); - return TriState.UNKNOWN; - } - return TriState.TRUE; - } +// private TriState directoryExists(String[] directoryList) throws IOException { +// Map here = directoryTree; +// String[] soFar; +// String totalPath = "/"; +// for(String directory: directoryList) { +// if (here.containsKey(directory)) { +// Object entry = here.get(directory); +// if (entry instanceof HashMap) { +// here = (Map) entry; +// totalPath += directory + "/"; +// } else { +// // Not a HashMap, must be a string then and so this is a file, not a directory +// return TriState.FALSE; +// } +// } else if (here.containsKey(".")) { +// // We have listed this directory, and the requested path does not exist +// return TriState.FALSE; +// } +// // Request directory listing for here +// totalPath += directory; +// requestListDirectory(totalPath); +// return TriState.UNKNOWN; +// } +// return TriState.TRUE; +// } - private void uploadFileStart() { + private void uploadFileStart() throws IOException { bytesOfFileWritten = 0; // TODO Should this be the time we upload, or should it somehow be the timestamp of the // resources.zip archive file @@ -429,14 +438,14 @@ public class AdaBleFsProfile extends Abstra for(int i = 0; i < command.size(); i++) { bytes[i] = command.get(i); } - TransactionBuilder builder = new TransactionBuilder("Upload file start"); + TransactionBuilder builder = performInitialized("Upload file start"); builder.write(getCharacteristic(UUID_CHARACTERISTIC_FS_TRANSFER), bytes); builder.read(getCharacteristic(UUID_CHARACTERISTIC_FS_TRANSFER)); - builder.queue(getQueue()); + builder.queue(); return; } - private void requestListDirectory(String path) { + private void requestListDirectory(String path) throws IOException { Vector command = new Vector(); command.add(REQUEST_LIST_DIRECTORY); command.add(PADDING_BYTE); @@ -451,14 +460,14 @@ public class AdaBleFsProfile extends Abstra for(int i = 0; i < command.size(); i++) { bytes[i] = command.get(i); } - TransactionBuilder builder = new TransactionBuilder("List directory"); + TransactionBuilder builder = performInitialized("List directory"); builder.write(getCharacteristic(UUID_CHARACTERISTIC_FS_TRANSFER), bytes); builder.read(getCharacteristic(UUID_CHARACTERISTIC_FS_TRANSFER)); - builder.queue(getQueue()); + builder.queue(); listingDirectory = Arrays.asList(path.split("/")); return; } - private void deleteFile() { + private void deleteFile() throws IOException { final AdaBleFsAction nextAction = adaBleFsQueue.getFirst(); Vector command = new Vector(); command.add(REQUEST_DELETE_FILE); @@ -474,14 +483,14 @@ public class AdaBleFsProfile extends Abstra for(int i = 0; i < command.size(); i++) { bytes[i] = command.get(i); } - TransactionBuilder builder = new TransactionBuilder("Delete file or directory"); + TransactionBuilder builder = performInitialized("Delete file or directory"); builder.write(getCharacteristic(UUID_CHARACTERISTIC_FS_TRANSFER), bytes); builder.read(getCharacteristic(UUID_CHARACTERISTIC_FS_TRANSFER)); - builder.queue(getQueue()); + builder.queue(); return; } - private void makeDirectory() { + private void makeDirectory() throws IOException { final AdaBleFsAction nextAction = adaBleFsQueue.getFirst(); long unixTime = System.currentTimeMillis() / 1000L; Vector command = new Vector(); @@ -510,14 +519,14 @@ public class AdaBleFsProfile extends Abstra for(int i = 0; i < command.size(); i++) { bytes[i] = command.get(i); } - TransactionBuilder builder = new TransactionBuilder("Create directory"); + TransactionBuilder builder = performInitialized("Create directory"); builder.write(getCharacteristic(UUID_CHARACTERISTIC_FS_TRANSFER), bytes); builder.read(getCharacteristic(UUID_CHARACTERISTIC_FS_TRANSFER)); - builder.queue(getQueue()); + builder.queue(); return; } - private void moveFileOrDirectory() { + private void moveFileOrDirectory() throws IOException { final AdaBleFsAction nextAction = adaBleFsQueue.getFirst(); long unixTime = System.currentTimeMillis() / 1000L; Vector command = new Vector(); @@ -540,10 +549,10 @@ public class AdaBleFsProfile extends Abstra for(int i = 0; i < command.size(); i++) { bytes[i] = command.get(i); } - TransactionBuilder builder = new TransactionBuilder("Move file or directory"); + TransactionBuilder builder = performInitialized("Move file or directory"); builder.write(getCharacteristic(UUID_CHARACTERISTIC_FS_TRANSFER), bytes); builder.read(getCharacteristic(UUID_CHARACTERISTIC_FS_TRANSFER)); - builder.queue(getQueue()); + builder.queue(); return; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pinetime/PineTimeJFSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pinetime/PineTimeJFSupport.java index d85b21bff6..44ba3aae52 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pinetime/PineTimeJFSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pinetime/PineTimeJFSupport.java @@ -62,6 +62,7 @@ import no.nordicsemi.android.dfu.DfuServiceInitiator; import no.nordicsemi.android.dfu.DfuServiceListenerHelper; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; +import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; import nodomain.freeyourgadget.gadgetbridge.database.DBHelper; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventBatteryInfo; @@ -91,15 +92,13 @@ import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec; import nodomain.freeyourgadget.gadgetbridge.model.WorldClock; import nodomain.freeyourgadget.gadgetbridge.model.weather.Weather; import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLESingleDeviceSupport; -import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSupport; -import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSupport; -import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.adablefs.AdaBleFsProfile; -import nodomain.freeyourgadget.gadgetbridge.service.btle.ResourceUploadProgressListener; import nodomain.freeyourgadget.gadgetbridge.service.btle.BLETypeConversions; import nodomain.freeyourgadget.gadgetbridge.service.btle.GattCharacteristic; import nodomain.freeyourgadget.gadgetbridge.service.btle.GattService; +import nodomain.freeyourgadget.gadgetbridge.service.btle.ResourceUploadProgressListener; import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder; import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.IntentListener; +import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.adablefs.AdaBleFsProfile; import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertCategory; import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertNotificationProfile; import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.NewAlert; @@ -563,7 +562,7 @@ public class PineTimeJFSupport extends AbstractBTLESingleDeviceSupport implement public void installResource(Uri uri) { try { - gbDevice.setBusyTask("resources upgrade"); + gbDevice.setBusyTask(R.string.uploading_resources, getContext()); adaBleFsProfile.loadResources(uri, getContext(), getQueue()); LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent(GB.ACTION_SET_PROGRESS_BAR) @@ -574,7 +573,7 @@ public class PineTimeJFSupport extends AbstractBTLESingleDeviceSupport implement ); } catch (Exception ex) { GB.toast(getContext(), getContext().getString(R.string.updatefirmwareoperation_write_failed) + ":" + ex.getMessage(), Toast.LENGTH_LONG, GB.ERROR, ex); - if (gbDevice.isBusy() && gbDevice.getBusyTask().equals("resources upgrade")) { + if (gbDevice.isBusy() && gbDevice.getBusyTask().equals(getContext().getString(R.string.uploading_resources))) { gbDevice.unsetBusyTask(); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/RemoteFileSystemCache.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/RemoteFileSystemCache.java index b17318b745..fabe164849 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/RemoteFileSystemCache.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/RemoteFileSystemCache.java @@ -20,6 +20,7 @@ package nodomain.freeyourgadget.gadgetbridge.util; import java.util.ArrayList; import java.util.List; +import java.util.Optional; public class RemoteFileSystemCache { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 836cd8dda4..99cba16dc4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1003,6 +1003,7 @@ Upload has been aborted! Upload has failed Upload is in progress\n%1$d%% at %2$.2fkbps (average %3$.2fkbps)\nPart %4$d of %5$d + Uploading resources… Flashing firmware… File cannot be installed, device not supported. File cannot be installed, device not ready.