1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-04 09:17:29 +01:00

Garmin: Overwrite files if local size is 0, omit date if equal to garmin epoch

This commit is contained in:
José Rebelo 2024-05-05 14:04:31 +01:00 committed by Daniele Gobbetti
parent aee42ec1be
commit 8be679d1fd
2 changed files with 41 additions and 15 deletions

View File

@ -13,6 +13,7 @@ import java.nio.ByteOrder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.deviceevents.FileDownloadedDeviceEvent;
@ -312,6 +313,8 @@ public class FileTransferHandler implements MessageHandler {
}
public static class DirectoryEntry {
private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.ROOT);
private final int fileIndex;
private final FileType.FILETYPE filetype;
private final int fileNumber;
@ -339,9 +342,12 @@ public class FileTransferHandler implements MessageHandler {
}
public String getFileName() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
String dateString = dateFormat.format(fileDate);
return getFiletype().name() + "_" + dateString + "_" + getFileIndex() + (getFiletype().isFitFile() ? ".fit" : ".bin");
final StringBuilder sb = new StringBuilder(getFiletype().name());
if (fileDate.getTime() != GarminTimeUtils.GARMIN_TIME_EPOCH * 1000L) {
sb.append("_").append(SDF.format(fileDate));
}
sb.append("_").append(getFileIndex()).append(getFiletype().isFitFile() ? ".fit" : ".bin");
return sb.toString();
}
public String getLegacyFileName() {

View File

@ -81,6 +81,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.messages.Syst
import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.messages.status.NotificationSubscriptionStatusMessage;
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.Optional;
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_ALLOW_HIGH_MTU;
@ -484,7 +485,7 @@ public class GarminSupport extends AbstractBTLEDeviceSupport implements ICommuni
while (!filesToDownload.isEmpty()) {
final FileTransferHandler.DirectoryEntry directoryEntry = filesToDownload.remove();
if (checkFileExists(directoryEntry.getFileName()) || checkFileExists(directoryEntry.getLegacyFileName())) {
if (alreadyDownloaded(directoryEntry)) {
LOG.debug("File: {} already downloaded, not downloading again.", directoryEntry.getFileName());
if (!getKeepActivityDataOnDevice()) { // delete file from watch if already downloaded
sendOutgoingMessage(new SetFileFlagsMessage(directoryEntry.getFileIndex(), SetFileFlagsMessage.FileFlags.ARCHIVE));
@ -493,12 +494,9 @@ public class GarminSupport extends AbstractBTLEDeviceSupport implements ICommuni
}
final DownloadRequestMessage downloadRequestMessage = fileTransferHandler.downloadDirectoryEntry(directoryEntry);
if (downloadRequestMessage != null) {
LOG.debug("Will download file: {}", directoryEntry.getFileName());
sendOutgoingMessage(downloadRequestMessage);
return;
} else {
LOG.debug("File: {} already downloaded, not downloading again, from inside.", directoryEntry.getFileName());
}
}
}
@ -698,17 +696,39 @@ public class GarminSupport extends AbstractBTLEDeviceSupport implements ICommuni
}
}
private boolean checkFileExists(String fileName) {
private boolean alreadyDownloaded(final FileTransferHandler.DirectoryEntry entry) {
final Optional<File> file = getFile(entry.getFileName());
if (file.isPresent()) {
if (file.get().length() == 0) {
LOG.warn("File {} is empty", entry.getFileName());
return false;
}
return true;
}
final Optional<File> legacyFile = getFile(entry.getLegacyFileName());
if (legacyFile.isPresent()) {
if (legacyFile.get().length() == 0) {
LOG.warn("Legacy file {} is empty", entry.getFileName());
return false;
}
return true;
}
return false;
}
private Optional<File> getFile(final String fileName) {
File dir;
try {
dir = getWritableExportDirectory();
File outputFile = new File(dir, fileName);
if (outputFile.exists()) //do not download again already downloaded file
return true;
if (outputFile.exists())
return Optional.of(outputFile);
} catch (IOException e) {
LOG.error("IOException: " + e);
LOG.error("Failed to get file", e);
}
return false;
return Optional.empty();
}
public File getWritableExportDirectory() throws IOException {