Bangle.js: add save file to android functionality

Bangle.js:file handling LOG.warn -> info

Bangle.js: sync file can't escape device directory

Naïve solution. I wanted to use `Path.normalize()` but Android Studio
said it could not be used from the static context. This does not attempt
to normalize the path, but just remove the special names `..\` and `.\`.

Bangle.js:simpler hindering of escaping device dir
This commit is contained in:
Ganblejs 2024-01-31 23:27:04 +01:00 committed by José Rebelo
parent cef277261a
commit f8e15068ea
1 changed files with 34 additions and 0 deletions

View File

@ -580,6 +580,9 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
case "intent":
handleIntent(json);
break;
case "file":
handleFile(json);
break;
case "gps_power": {
boolean status = json.getBoolean("status");
LOG.info("Got gps power status: " + status);
@ -984,6 +987,37 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
return intent;
}
private void handleFile(JSONObject json) throws JSONException {
File dir;
try {
dir = new File(FileUtils.getExternalFilesDir() + "/" + FileUtils.makeValidFileName(getDevice().getName()));
if (!dir.isDirectory()) {
if (!dir.mkdir()) {
throw new IOException("Cannot create device specific directory for " + getDevice().getName());
}
}
} catch (IOException e) {
LOG.error("Could not get directory to write to with error: " + e);
return;
}
String filename = json.getString("n");
String filenameThatCantEscapeDir = filename.replaceAll("/","");
LOG.debug("Compare filename and filenameThatCantEscapeDir:\n" + filename + "\n" + filenameThatCantEscapeDir);
File outputFile = new File(dir, filenameThatCantEscapeDir);
String mode = "append";
if (json.getString("m").equals("w")) {
mode = "write";
}
try {
FileUtils.copyStringToFile(json.getString("c"), outputFile, mode);
LOG.info("Writing to "+outputFile);
} catch (IOException e) {
LOG.warn("Could not write to " + outputFile + "with error: " + e);
}
}
@Override
public void onSendConfiguration(final String config) {
switch (config) {