gbmusic: Move event listener from widget to boot code

pull/1442/head
Richard de Boer 2022-02-13 15:19:39 +01:00
parent 405de6d6c0
commit 089055d948
No known key found for this signature in database
GPG Key ID: 8721727971871937
5 changed files with 47 additions and 74 deletions

View File

@ -6,3 +6,4 @@
0.06: Bangle.js 2 support
0.07: Fix "previous" button image
0.08: Fix scrolling title background color
0.09: Move event listener from widget to boot code, stops music from showing up in messages

View File

@ -175,10 +175,8 @@ function rIcon(l) {
}
let layout;
function makeUI() {
global.gbmusic_active = true; // we don't need our widget (needed for <2.09 devices)
Bangle.loadWidgets();
Bangle.drawWidgets();
delete (global.gbmusic_active);
const Layout = require("Layout");
layout = new Layout({
type: "v", c: [
@ -331,7 +329,7 @@ function formatNum(info) {
* Update music info
* @param {Object} info - Gadgetbridge musicinfo event
*/
function musicInfo(info) {
function info(info) {
scrollStop();
layout.title.label = info.track || "";
layout.album.label = info.album || "";
@ -360,7 +358,7 @@ let tPxt, tIxt; // Timeouts to eXiT when Paused/Inactive for too long
* Update music state
* @param {Object} e - Gadgetbridge musicstate event
*/
function musicState(e) {
function state(e) {
stat = e.state;
// if paused for five minutes, load the clock
// (but timeout resets if we get new info, even while paused)
@ -584,8 +582,8 @@ function startEmulator() {
println: (line) => {console.log("Bluetooth:", line);},
};
// some example info
GB({"t": "musicinfo", "artist": "Some Artist Name", "album": "The Album Name", "track": "The Track Title Goes Here", "dur": 241, "c": 2, "n": 2});
GB({"t": "musicstate", "state": "play", "position": 0, "shuffle": 1, "repeat": 1});
info({"t": "musicinfo", "artist": "Some Artist Name", "album": "The Album Name", "track": "The Track Title Goes Here", "dur": 241, "c": 2, "n": 2});
state({"t": "musicstate", "state": "play", "position": 0, "shuffle": 1, "repeat": 1});
}
}
function startWatches() {
@ -596,25 +594,6 @@ function startWatches() {
function start() {
makeUI();
// start listening for music updates
const _GB = global.GB;
global.GB = (event) => {
// we eat music events!
switch(event.t) {
case "musicinfo":
musicInfo(event);
break;
case "musicstate":
musicState(event);
break;
default:
// pass on other events
if (_GB) {
setTimeout(_GB, 0, event);
}
return;
}
};
startWatches();
tick();
startEmulator();
@ -625,11 +604,11 @@ function init() {
let saved = require("Storage").readJSON("gbmusic.load.json", true);
require("Storage").erase("gbmusic.load.json");
if (saved) {
// autoloaded: load state was saved by widget
// autoloaded: load state as saved by widget
auto = true;
start();
musicInfo(saved.info);
musicState(saved.state);
info(saved.info);
state(saved.state);
return;
}

37
apps/gbmusic/boot.js Normal file
View File

@ -0,0 +1,37 @@
setTimeout( // make other boot code run first, so we override e.g. android.boot.js GB
() => {
const APP = global.__FILE__==="gbmusic.app.js",
a = !!(require("Storage").readJSON("gbmusic.json", 1) || {}).autoStart;
let s, i; // state, info
/**
* Save current song and check if we want to load the gbmusic app
*
* Only runs while other apps are loaded
*/
function check() {
if (s!=="play" || !i || !a || !Bangle.CLOCK) return; // only launch app if we know which song we are playing, and autoLoad is enabled
delete (i.t);
// store info and launch music app
require("Storage").writeJSON("gbmusic.load.json", {
state: s,
info: i,
});
load("gbmusic.app.js");
}
global.GB = (_GB => e => {
// we eat music events!
switch(e.t) {
case "musicinfo":
i = e;
return APP ? info(e) : check();
case "musicstate":
s = e.state;
return APP ? state(e) : check();
default:
// pass on other events
if (_GB) setTimeout(_GB, 0, e);
}
})(global.GB);
}, 1);

View File

@ -2,7 +2,7 @@
"id": "gbmusic",
"name": "Gadgetbridge Music Controls",
"shortName": "Music Controls",
"version": "0.08",
"version": "0.09",
"description": "Control the music on your Gadgetbridge-connected phone",
"icon": "icon.png",
"screenshots": [{"url":"screenshot_v1.png"},{"url":"screenshot_v2.png"}],
@ -14,7 +14,7 @@
"storage": [
{"name":"gbmusic.app.js","url":"app.js"},
{"name":"gbmusic.settings.js","url":"settings.js"},
{"name":"gbmusic.wid.js","url":"widget.js"},
{"name":"gbmusic.boot.js","url":"boot.js"},
{"name":"gbmusic.img","url":"icon.js","evaluate":true}
],
"data": [{"name":"gbmusic.json"},{"name":"gbmusic.load.json"}]

View File

@ -1,44 +0,0 @@
(() => {
if (global.gbmusic_active || !(require("Storage").readJSON("gbmusic.json", 1) || {}).autoStart) {
return;
}
if (typeof __FILE__ === 'string') { // only exists since 2v09
const info = require("Storage").readJSON(__FILE__.split(".")[0]+".info", 1) || false;
if (info && info.type!=="clock") { // info can have no type (but then it isn't a clock)
return;
}
}
let state, info;
function checkMusic() {
if (state!=="play" || !info) {
return;
}
// playing music: launch music app
require("Storage").writeJSON("gbmusic.load.json", {
state: state,
info: info,
});
load("gbmusic.app.js");
}
const _GB = global.GB;
global.GB = (event) => {
// we eat music events!
switch(event.t) {
case "musicinfo":
info = event;
delete (info.t);
checkMusic();
break;
case "musicstate":
state = event.state;
checkMusic();
break;
default:
if (_GB) {
setTimeout(_GB, 0, event);
}
}
};
})();