From d93ef074c32799b2a0534b9e567c23e2c60ae0bd Mon Sep 17 00:00:00 2001 From: Ganblejs Date: Wed, 6 Mar 2024 05:57:09 +0100 Subject: [PATCH] Bangle.js:actTrk: try fix parsing after interrupt --- .../banglejs/BangleJSActivityTrack.java | 68 +++++++++++++------ 1 file changed, 48 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/banglejs/BangleJSActivityTrack.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/banglejs/BangleJSActivityTrack.java index 2d9582af1..37235c180 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/banglejs/BangleJSActivityTrack.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/banglejs/BangleJSActivityTrack.java @@ -6,6 +6,8 @@ import static java.lang.Math.sqrt; import android.content.Context; import android.widget.Toast; +import androidx.annotation.Nullable; + import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -89,9 +91,15 @@ class BangleJSActivityTrack extends BangleJSDeviceSupport { timeoutTask = new TimerTask() { public void run() { - signalFetchingEnded(device, context); - LOG.warn(context.getString(R.string.busy_task_fetch_sports_details_interrupted)); - GB.toast(context.getString(R.string.busy_task_fetch_sports_details_interrupted), Toast.LENGTH_LONG, GB.INFO); + + try { + parseFetchedRecorderCSVs(getDir(), tracksListIntactPrivate, device, context); + } catch (JSONException e) { + throw new RuntimeException(e); + } + signalFetchingEnded(device, context); + LOG.warn(context.getString(R.string.busy_task_fetch_sports_details_interrupted)); + GB.toast(context.getString(R.string.busy_task_fetch_sports_details_interrupted), Toast.LENGTH_LONG, GB.INFO); } }; } @@ -102,12 +110,8 @@ class BangleJSActivityTrack extends BangleJSDeviceSupport { } private static String getLatestFetchedRecorderLog() { - File dir; - try { - dir = FileUtils.getExternalFilesDir(); - } catch (IOException e) { - return null; - } + File dir = getDir(); + if (dir == null) return null; String filename = "latestFetchedRecorderLog.txt"; File inputFile = new File(dir, filename); String lastSyncedID = ""; @@ -123,6 +127,17 @@ class BangleJSActivityTrack extends BangleJSDeviceSupport { return lastSyncedID; } + @Nullable + private static File getDir() { + File dir; + try { + dir = FileUtils.getExternalFilesDir(); + } catch (IOException e) { + return null; + } + return dir; + } + private static void setLatestFetchedRecorderLog(File dir, String log) { String filenameLogID = "latestFetchedRecorderLog.txt"; @@ -137,8 +152,9 @@ class BangleJSActivityTrack extends BangleJSDeviceSupport { private static void writeToRecorderCSV(String lines, File dir, String filename) { String mode = "append"; - if (lines.equals("")) { + if (lines.equals("erase")) { mode = "write"; + lines = ""; } File outputFile = new File(dir, filename); @@ -195,9 +211,14 @@ class BangleJSActivityTrack extends BangleJSDeviceSupport { return o; } + private static JSONArray tracksListIntactPrivate; + private static String lastCompletelyFetchedLog = ""; + static JSONArray handleActTrk(JSONObject json, JSONArray tracksList, JSONArray tracksListIntact, int prevPacketCount, GBDevice device, Context context) throws JSONException { stopAndRestartTimeout(device, context); + tracksListIntactPrivate = tracksListIntact; + JSONArray returnArray; JSONObject stopObj = new JSONObject().put("t","fetchRec").put("id","stop"); @@ -231,21 +252,17 @@ class BangleJSActivityTrack extends BangleJSDeviceSupport { if (!json.has("lines")) { // if no lines were sent with this json object, it signifies that the whole recorder log has been transmitted. setLatestFetchedRecorderLog(dir, log); if (tracksList.length()==0) { // All data from all Recorder logs have been fetched. - LOG.warn("tracksListIntact:\n" + tracksListIntact); - for (int i = 0; i