gbmusic: Move event listener from widget to boot code
parent
405de6d6c0
commit
089055d948
|
|
@ -6,3 +6,4 @@
|
||||||
0.06: Bangle.js 2 support
|
0.06: Bangle.js 2 support
|
||||||
0.07: Fix "previous" button image
|
0.07: Fix "previous" button image
|
||||||
0.08: Fix scrolling title background color
|
0.08: Fix scrolling title background color
|
||||||
|
0.09: Move event listener from widget to boot code, stops music from showing up in messages
|
||||||
|
|
|
||||||
|
|
@ -175,10 +175,8 @@ function rIcon(l) {
|
||||||
}
|
}
|
||||||
let layout;
|
let layout;
|
||||||
function makeUI() {
|
function makeUI() {
|
||||||
global.gbmusic_active = true; // we don't need our widget (needed for <2.09 devices)
|
|
||||||
Bangle.loadWidgets();
|
Bangle.loadWidgets();
|
||||||
Bangle.drawWidgets();
|
Bangle.drawWidgets();
|
||||||
delete (global.gbmusic_active);
|
|
||||||
const Layout = require("Layout");
|
const Layout = require("Layout");
|
||||||
layout = new Layout({
|
layout = new Layout({
|
||||||
type: "v", c: [
|
type: "v", c: [
|
||||||
|
|
@ -331,7 +329,7 @@ function formatNum(info) {
|
||||||
* Update music info
|
* Update music info
|
||||||
* @param {Object} info - Gadgetbridge musicinfo event
|
* @param {Object} info - Gadgetbridge musicinfo event
|
||||||
*/
|
*/
|
||||||
function musicInfo(info) {
|
function info(info) {
|
||||||
scrollStop();
|
scrollStop();
|
||||||
layout.title.label = info.track || "";
|
layout.title.label = info.track || "";
|
||||||
layout.album.label = info.album || "";
|
layout.album.label = info.album || "";
|
||||||
|
|
@ -360,7 +358,7 @@ let tPxt, tIxt; // Timeouts to eXiT when Paused/Inactive for too long
|
||||||
* Update music state
|
* Update music state
|
||||||
* @param {Object} e - Gadgetbridge musicstate event
|
* @param {Object} e - Gadgetbridge musicstate event
|
||||||
*/
|
*/
|
||||||
function musicState(e) {
|
function state(e) {
|
||||||
stat = e.state;
|
stat = e.state;
|
||||||
// if paused for five minutes, load the clock
|
// if paused for five minutes, load the clock
|
||||||
// (but timeout resets if we get new info, even while paused)
|
// (but timeout resets if we get new info, even while paused)
|
||||||
|
|
@ -584,8 +582,8 @@ function startEmulator() {
|
||||||
println: (line) => {console.log("Bluetooth:", line);},
|
println: (line) => {console.log("Bluetooth:", line);},
|
||||||
};
|
};
|
||||||
// some example info
|
// 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});
|
info({"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});
|
state({"t": "musicstate", "state": "play", "position": 0, "shuffle": 1, "repeat": 1});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function startWatches() {
|
function startWatches() {
|
||||||
|
|
@ -596,25 +594,6 @@ function startWatches() {
|
||||||
|
|
||||||
function start() {
|
function start() {
|
||||||
makeUI();
|
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();
|
startWatches();
|
||||||
tick();
|
tick();
|
||||||
startEmulator();
|
startEmulator();
|
||||||
|
|
@ -625,11 +604,11 @@ function init() {
|
||||||
let saved = require("Storage").readJSON("gbmusic.load.json", true);
|
let saved = require("Storage").readJSON("gbmusic.load.json", true);
|
||||||
require("Storage").erase("gbmusic.load.json");
|
require("Storage").erase("gbmusic.load.json");
|
||||||
if (saved) {
|
if (saved) {
|
||||||
// autoloaded: load state was saved by widget
|
// autoloaded: load state as saved by widget
|
||||||
auto = true;
|
auto = true;
|
||||||
start();
|
start();
|
||||||
musicInfo(saved.info);
|
info(saved.info);
|
||||||
musicState(saved.state);
|
state(saved.state);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
"id": "gbmusic",
|
"id": "gbmusic",
|
||||||
"name": "Gadgetbridge Music Controls",
|
"name": "Gadgetbridge Music Controls",
|
||||||
"shortName": "Music Controls",
|
"shortName": "Music Controls",
|
||||||
"version": "0.08",
|
"version": "0.09",
|
||||||
"description": "Control the music on your Gadgetbridge-connected phone",
|
"description": "Control the music on your Gadgetbridge-connected phone",
|
||||||
"icon": "icon.png",
|
"icon": "icon.png",
|
||||||
"screenshots": [{"url":"screenshot_v1.png"},{"url":"screenshot_v2.png"}],
|
"screenshots": [{"url":"screenshot_v1.png"},{"url":"screenshot_v2.png"}],
|
||||||
|
|
@ -14,7 +14,7 @@
|
||||||
"storage": [
|
"storage": [
|
||||||
{"name":"gbmusic.app.js","url":"app.js"},
|
{"name":"gbmusic.app.js","url":"app.js"},
|
||||||
{"name":"gbmusic.settings.js","url":"settings.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}
|
{"name":"gbmusic.img","url":"icon.js","evaluate":true}
|
||||||
],
|
],
|
||||||
"data": [{"name":"gbmusic.json"},{"name":"gbmusic.load.json"}]
|
"data": [{"name":"gbmusic.json"},{"name":"gbmusic.load.json"}]
|
||||||
|
|
|
||||||
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
})();
|
|
||||||
Loading…
Reference in New Issue