1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-25 18:15:49 +01:00

Introduce device specific writable directory (MAC address)

Also adds temporary method to move the fetched files from the legacy path to the new one which does not include the device name.
Also moves the FileIndex to the end of the cached files to allow for easier sorting.

Cherry-picked from 525b395c01ce57449ee9a8f74af663595223279e and adapted
This commit is contained in:
Daniele Gobbetti 2024-05-03 10:22:43 +02:00
parent 72f1d0c82c
commit 8c88cc992a
4 changed files with 53 additions and 9 deletions

View File

@ -80,6 +80,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.StressSample;
import nodomain.freeyourgadget.gadgetbridge.model.TemperatureSample;
import nodomain.freeyourgadget.gadgetbridge.service.ServiceDeviceSupport;
import nodomain.freeyourgadget.gadgetbridge.service.SleepAsAndroidSender;
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
@ -296,6 +297,18 @@ public abstract class AbstractDeviceCoordinator implements DeviceCoordinator {
return null;
}
@Override
public File getWritableExportDirectory(final GBDevice device) throws IOException {
File dir;
dir = new File(FileUtils.getExternalFilesDir() + File.separator + device.getAddress());
if (!dir.isDirectory()) {
if (!dir.mkdir()) {
throw new IOException("Cannot create device specific directory for " + device.getName());
}
}
return dir;
}
@Override
public String getAppCacheSortFilename() {
return null;

View File

@ -458,6 +458,11 @@ public interface DeviceCoordinator {
*/
File getAppCacheDir() throws IOException;
/**
* Returns the dedicated writable export directory for this device.
*/
File getWritableExportDirectory(GBDevice device) throws IOException;
/**
* Returns a String containing the device app sort order filename.
*/

View File

@ -338,7 +338,7 @@ 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() + "_" + getFileIndex() + "_" + dateString + (getFiletype().isFitFile() ? ".fit" : ".bin");
return getFiletype().name() + "_" + dateString + "_" + getFileIndex() + (getFiletype().isFitFile() ? ".fit" : ".bin");
}
@NonNull

View File

@ -445,6 +445,9 @@ public class GarminSupport extends AbstractBTLEDeviceSupport implements ICommuni
}
private void processDownloadQueue() {
moveFilesFromLegacyCache(); //TODO: remove before merging
if (!filesToDownload.isEmpty() && !fileTransferHandler.isDownloading()) {
if (!gbDevice.isBusy()) {
GB.updateTransferNotification(getContext().getString(R.string.busy_task_fetch_activity_data), "", true, 0, getContext());
@ -484,6 +487,36 @@ public class GarminSupport extends AbstractBTLEDeviceSupport implements ICommuni
}
}
private void moveFilesFromLegacyCache() { //TODO: remove before merging
File legacyDir;
try {
legacyDir = new File(FileUtils.getExternalFilesDir() + "/" + FileUtils.makeValidFileName(getDevice().getName() + "_" + getDevice().getAddress()));
if (legacyDir.isDirectory()) {
final File newDir = getWritableExportDirectory();
File[] files = legacyDir.listFiles();
for (File file : files) {
if (file.isFile()) {
File destFile = new File(newDir, file.getName());
boolean success = file.renameTo(destFile);
if (!success) {
LOG.error("Failed to move file {}", file.getName());
} else {
LOG.info("Moved file {} to new cache directory", file.getName());
}
}
}
boolean removed = legacyDir.delete();
if (!removed) {
LOG.error("Failed to remove legacy directory: {}", legacyDir);
}
}
} catch (IOException e) {
LOG.error(e.getMessage());
}
}
private void enableBatteryLevelUpdate() {
final ProtobufMessage batteryLevelProtobufRequest = protocolBufferHandler.prepareProtobufRequest(GdiSmartProto.Smart.newBuilder()
.setDeviceStatusService(
@ -597,14 +630,7 @@ public class GarminSupport extends AbstractBTLEDeviceSupport implements ICommuni
}
public File getWritableExportDirectory() throws IOException {
File dir;
dir = new File(FileUtils.getExternalFilesDir() + "/" + FileUtils.makeValidFileName(getDevice().getName() + "_" + getDevice().getAddress()));
if (!dir.isDirectory()) {
if (!dir.mkdir()) {
throw new IOException("Cannot create device specific directory for " + getDevice().getName());
}
}
return dir;
return getDevice().getDeviceCoordinator().getWritableExportDirectory(getDevice());
}
@Override