mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2025-02-18 05:17:08 +01:00
Cleanup file handling a bit
This commit is contained in:
parent
0a4d8499fa
commit
f24dc0c1cb
@ -0,0 +1,5 @@
|
||||
package nodomain.freeyourgadget.gadgetbridge.devices.qhybrid;
|
||||
|
||||
public class FossilConstants {
|
||||
public static String QFILES_DIR = "qFiles";
|
||||
}
|
@ -68,6 +68,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.adapter.foss
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.adapter.fossil_hr.FossilHRWatchAdapter;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.misfit.DownloadFileRequest;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.misfit.PlayNotificationRequest;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||
|
||||
public class QHybridSupport extends QHybridBaseSupport {
|
||||
@ -544,23 +545,20 @@ public class QHybridSupport extends QHybridBaseSupport {
|
||||
|
||||
private void backupFile(DownloadFileRequest request) {
|
||||
try {
|
||||
File f = new File("/sdcard/qFiles/");
|
||||
if (!f.exists()) f.mkdir();
|
||||
|
||||
File file = new File("/sdcard/qFiles/" + request.timeStamp);
|
||||
File file = FileUtils.getExternalFile("qFiles/" + request.timeStamp);
|
||||
if (file.exists()) {
|
||||
throw new Exception("file " + file.getPath() + " exists");
|
||||
}
|
||||
logger.debug("Writing file " + file.getPath());
|
||||
file.createNewFile();
|
||||
FileOutputStream fos = new FileOutputStream(file);
|
||||
fos.write(request.file);
|
||||
fos.close();
|
||||
try (FileOutputStream fos = new FileOutputStream(file)) {
|
||||
fos.write(request.file);
|
||||
}
|
||||
logger.debug("file written.");
|
||||
|
||||
FileOutputStream fos2 = new FileOutputStream("/sdcard/qFiles/steps", true);
|
||||
fos2.write(("file " + request.timeStamp + " cut\n\n").getBytes());
|
||||
fos2.close();
|
||||
file = FileUtils.getExternalFile("qFiles/steps");
|
||||
try (FileOutputStream fos = new FileOutputStream(file, true)) {
|
||||
fos.write(("file " + request.timeStamp + " cut\n\n").getBytes());
|
||||
}
|
||||
|
||||
//TODO file stuff
|
||||
// queueWrite(new EraseFileRequest((short) request.fileHandle));
|
||||
|
@ -74,6 +74,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.mis
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.misfit.SetVibrationStrengthRequest;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.misfit.UploadFileRequest;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.misfit.VibrateRequest;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||
|
||||
import static nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.QHybridSupport.ITEM_ACTIVITY_POINT;
|
||||
@ -178,7 +179,7 @@ public class MisfitWatchAdapter extends WatchAdapter {
|
||||
default: {
|
||||
log("unknown shit on " + characteristic.getUuid().toString() + ": " + arrayToString(characteristic.getValue()));
|
||||
try {
|
||||
File charLog = new File("/sdcard/qFiles/charLog.txt");
|
||||
File charLog = FileUtils.getExternalFile("qFiles/charLog.txt");
|
||||
try (FileOutputStream fos = new FileOutputStream(charLog, true)) {
|
||||
fos.write((new Date().toString() + ": " + characteristic.getUuid().toString() + ": " + arrayToString(characteristic.getValue())).getBytes());
|
||||
}
|
||||
@ -242,10 +243,7 @@ public class MisfitWatchAdapter extends WatchAdapter {
|
||||
int steps = ((GetCurrentStepCountRequest) request).steps;
|
||||
logger.debug("get current steps: " + steps);
|
||||
try {
|
||||
File f = new File("/sdcard/qFiles/");
|
||||
if (!f.exists()) f.mkdir();
|
||||
|
||||
File file = new File("/sdcard/qFiles/steps");
|
||||
File file = FileUtils.getExternalFile("qFiles/steps");
|
||||
logger.debug("Writing file " + file.getPath());
|
||||
try (FileOutputStream fos = new FileOutputStream(file, true)) {
|
||||
fos.write((System.currentTimeMillis() + ": " + steps + "\n").getBytes());
|
||||
|
@ -184,6 +184,24 @@ public class FileUtils {
|
||||
throw new IOException("no writable external directory found");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a File object representing the "child" argument, but relative
|
||||
* to the Android "external files directory" (e.g. /sdcard).
|
||||
* It doesn't matter whether child shall represent a file or a directory.
|
||||
* The parent directory will automatically be created, if necessary.
|
||||
* @param child the path to become relative to the external files directory
|
||||
* @throws IOException
|
||||
* @see #getExternalFilesDir()
|
||||
*/
|
||||
public static File getExternalFile(String child) throws IOException {
|
||||
File file = new File(getExternalFilesDir(), child);
|
||||
if (!file.getParentFile().mkdirs()) {
|
||||
throw new IOException("Unable to create directory " + file.getParent());
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
|
||||
private static boolean canWriteTo(File dir) {
|
||||
File file = new File(dir, "gbtest");
|
||||
try {
|
||||
|
Loading…
x
Reference in New Issue
Block a user