From 87ccb9492bb570fdce8708b352b4af72cbb79437 Mon Sep 17 00:00:00 2001 From: thyttan <6uuxstm66@mozmail.comā©> Date: Tue, 13 Jun 2023 21:41:45 +0200 Subject: [PATCH] android: Add Activity Tracks sync support Lets Gadgetbridge fetch logs created with the `recorder` app. --- apps/android/ChangeLog | 1 + apps/android/boot.js | 45 ++++++++++++++++++++++++++++++++++++++ apps/android/metadata.json | 2 +- 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/apps/android/ChangeLog b/apps/android/ChangeLog index 108242825c..de5130a917 100644 --- a/apps/android/ChangeLog +++ b/apps/android/ChangeLog @@ -33,3 +33,4 @@ 0.31: Implement API for activity fetching 0.32: Added support for loyalty cards from gadgetbridge 0.33: Fix alarms created in Gadgetbridge not repeating +0.34: Implement API for activity tracks fetching (Recorder app logs). diff --git a/apps/android/boot.js b/apps/android/boot.js index 63f9b2883f..666cac6d37 100644 --- a/apps/android/boot.js +++ b/apps/android/boot.js @@ -16,10 +16,12 @@ if (settings.vibrate == undefined) settings.vibrate = ".."; require('Storage').writeJSON("android.settings.json", settings); var _GB = global.GB; + let fetchRecInterval; global.GB = (event) => { // feed a copy to other handlers if there were any if (_GB) setTimeout(_GB,0,Object.assign({},event)); + /* TODO: Call handling, fitness */ var HANDLERS = { // {t:"notify",id:int, src,title,subject,body,sender,tel:string} add @@ -230,6 +232,49 @@ } gbSend({t: "actfetch", state: "end", count: actCount}); }, + //{t:"listRecs", id:"20230616a"} + "listRecs": function() { + let recs = require("Storage").list(/^recorder\.log.*\.csv$/,{sf:true}).map(s => s.slice(12, 21)); + if (event.id.length > 2) { // Handle if there was no id supplied. Then we send a list all available recorder logs back. + let firstNonsyncedIdx = recs.findIndex((logId) => logId > event.id); + if (-1 == firstNonsyncedIdx) { + recs = [] + } else { + recs = recs.slice(firstNonsyncedIdx); + } + } + gbSend({t:"actTrksList", list: recs}); // TODO: split up in multiple transmissions? + }, + //{t:"fetchRec", id:"20230616a"} + "fetchRec": function() { + // TODO: Decide on what names keys should have. + if (fetchRecInterval) { + clearInterval(fetchRecInterval); + fetchRecInterval = undefined; + } + if (event.id=="stop") { + return + } else { + let log = require("Storage").open("recorder.log"+event.id+".csv","r"); + let lines = "init";// = log.readLine(); + let pkgcnt = 0; + gbSend({t:"actTrk", log:event.id, lines:"erase", cnt:pkgcnt}); // "erase" will prompt Gadgetbridge to erase the contents of a already fetched log so we can rewrite it without keeping lines from the previous (probably failed) fetch. + let sendlines = ()=>{ + lines = log.readLine(); + for (var i = 0; i < 3; i++) { + let line = log.readLine(); + if (line) lines += line; + } + pkgcnt++; + gbSend({t:"actTrk", log:event.id, lines:lines, cnt:pkgcnt}); + if (!lines && fetchRecInterval) { + clearInterval(fetchRecInterval); + fetchRecInterval = undefined; + } + } + fetchRecInterval = setInterval(sendlines, 50) + } + }, "nav": function() { event.id="nav"; if (event.instr) { diff --git a/apps/android/metadata.json b/apps/android/metadata.json index 5babc520b3..72055f5576 100644 --- a/apps/android/metadata.json +++ b/apps/android/metadata.json @@ -2,7 +2,7 @@ "id": "android", "name": "Android Integration", "shortName": "Android", - "version": "0.33", + "version": "0.34", "description": "Display notifications/music/etc sent from the Gadgetbridge app on Android. This replaces the old 'Gadgetbridge' Bangle.js widget.", "icon": "app.png", "tags": "tool,system,messages,notifications,gadgetbridge",