From 2ca3ccfaa0dba8dbf35274bd5c1627907c321d4b Mon Sep 17 00:00:00 2001 From: Richard de Boer Date: Sun, 25 Apr 2021 15:24:32 +0200 Subject: [PATCH] gbmusic: auto close after double song duration (or 1 hour) of inactivity Because some phones don't end an update when the music simply finished. --- apps.json | 2 +- apps/gbmusic/ChangeLog | 2 +- apps/gbmusic/app.js | 51 +++++++++++++++++++++++++++++++----------- 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/apps.json b/apps.json index 7ec30b819..d43f7b68b 100644 --- a/apps.json +++ b/apps.json @@ -3043,7 +3043,7 @@ "name": "Gadgetbridge Music Controls", "shortName":"Music Controls", "icon": "icon.png", - "version":"0.02", + "version":"0.03", "description": "Control the music on your Gadgetbridge-connected phone", "tags": "tools,bluetooth,gadgetbridge,music", "type":"app", diff --git a/apps/gbmusic/ChangeLog b/apps/gbmusic/ChangeLog index f456a4b16..0afcae268 100644 --- a/apps/gbmusic/ChangeLog +++ b/apps/gbmusic/ChangeLog @@ -1,3 +1,3 @@ 0.01: Initial version 0.02: Increase text brightness, improve controls, (try to) reduce memory usage -0.03: Only auto-start if active app is a clock \ No newline at end of file +0.03: Only auto-start if active app is a clock, auto close after 1 hour of inactivity diff --git a/apps/gbmusic/app.js b/apps/gbmusic/app.js index d54f449b1..ee50c1c17 100644 --- a/apps/gbmusic/app.js +++ b/apps/gbmusic/app.js @@ -11,7 +11,8 @@ let info = { n: 0, c: 0, }; -const TOUT = 300000; // auto close timeout: 5 minutes (in ms) +const POUT = 300000; // auto close timeout when paused: 5 minutes (in ms) +const IOUT = 3600000; // auto close timeout for inactivity: 1 hour (in ms) /////////////////////// // Self-repeating timeouts @@ -44,7 +45,7 @@ function brightness() { if (!fade) { return 1; } - return Math.max(0, 1-((Date.now()-fade)/TOUT)); + return Math.max(0, 1-((Date.now()-fade)/POUT)); } // Scroll long track names @@ -396,26 +397,50 @@ function musicInfo(e) { if (Bangle.isLCDOn()) { drawMusic(); } + if (tIxt) { + clearTimeout(tIxt); + tIxt = null; + } + if (auto && stat==="play") { + // if inactive for double song duration (or an hour if unknown), load the clock + // i.e. phone finished playing without bothering to notify the watch + tIxt = setTimeout(load, (info.dur*2000) || IOUT); + } } -let tXit; +let tPxt, tIxt; function musicState(e) { stat = e.state; // if paused for five minutes, load the clock // (but timeout resets if we get new info, even while paused) - if (tXit) { - clearTimeout(tXit); + if (tPxt) { + clearTimeout(tPxt); + tPxt = null; + } + if (tIxt) { + clearTimeout(tIxt); + tIxt = null; } - tXit = null; fade = null; delete info.track_color; - if (stat!=="play" && auto) { - if (stat==="stop") { // never actually happens with my phone :-( - load(); - } else { // also quit when paused for a long time - tXit = setTimeout(load, TOUT); - fade = Date.now(); - fadeOut(); + if (auto) { // auto opened -> auto close + switch(stat) { + case "stop": // never actually happens with my phone :-( + load(); + break; + case "play": + // if inactive for double song duration (or an hour if unknown), load the clock + // i.e. phone finished playing without bothering to notify the watch + tIxt = setTimeout(load, (info.dur*2000) || IOUT); + break; + case "pause": + default: + // quit when paused for a long time + // also fade out track info while waiting for this + tPxt = setTimeout(load, POUT); + fade = Date.now(); + fadeOut(); + break; } } if (Bangle.isLCDOn()) {