2022-12-15 10:34:40 +00:00
|
|
|
// Will calling Bangle.load reset everything? if false, we fast load
|
|
|
|
function loadWillReset() {
|
|
|
|
return Bangle.load === load || !Bangle.uiRemove;
|
|
|
|
/* FIXME: Maybe we need a better way of deciding if an app will
|
|
|
|
be fast loaded than just hard-coding a Bangle.uiRemove check.
|
|
|
|
Bangle.load could return a bool (as the load doesn't happen immediately). */
|
|
|
|
}
|
|
|
|
|
2022-11-20 14:41:02 +00:00
|
|
|
/**
|
|
|
|
* Listener set up in boot.js, calls into here to keep boot.js short
|
|
|
|
*/
|
|
|
|
exports.listener = function(type, msg) {
|
|
|
|
// Default handler: Launch the GUI for all unhandled messages (except music if disabled in settings)
|
|
|
|
if (msg.handled || (global.__FILE__ && __FILE__.startsWith('messagegui.'))) return; // already handled or app open
|
|
|
|
|
|
|
|
// if no new messages now, make sure we don't load the messages app
|
|
|
|
if (exports.messageTimeout && !msg.new && require("messages").status(msg) !== "new") {
|
|
|
|
clearTimeout(exports.messageTimeout);
|
|
|
|
delete exports.messageTimeout;
|
|
|
|
}
|
2022-12-11 17:34:07 +00:00
|
|
|
if (msg.t==="remove") {
|
|
|
|
// we won't open the UI for removed messages, so make sure to delete it from flash
|
|
|
|
if (Bangle.MESSAGES) {
|
|
|
|
// we were waiting for exports.messageTimeout
|
|
|
|
require("messages").apply(msg, Bangle.MESSAGES);
|
|
|
|
if (!Bangle.MESSAGES.length) delete Bangle.MESSAGES;
|
|
|
|
}
|
|
|
|
return require("messages").save(msg); // always write removal to flash
|
|
|
|
}
|
2022-11-20 14:41:02 +00:00
|
|
|
|
|
|
|
const appSettings = require("Storage").readJSON("messages.settings.json", 1) || {};
|
2023-02-23 11:19:24 +00:00
|
|
|
let loadMessages = (Bangle.CLOCK || msg.important); // should we load the messages app?
|
2022-11-20 14:41:02 +00:00
|
|
|
if (type==="music") {
|
2022-11-30 22:58:36 +00:00
|
|
|
if (Bangle.CLOCK && msg.state && msg.title && appSettings.openMusic) loadMessages = true;
|
2022-11-20 14:41:02 +00:00
|
|
|
else return;
|
|
|
|
}
|
2022-12-16 11:00:00 +00:00
|
|
|
// Write the message to Bangle.MESSAGES. We'll deal with it in messageTimeout below
|
|
|
|
if (!Bangle.MESSAGES) Bangle.MESSAGES = [];
|
|
|
|
require("messages").apply(msg, Bangle.MESSAGES);
|
|
|
|
if (!Bangle.MESSAGES.length) delete Bangle.MESSAGES;
|
2022-12-07 20:56:07 +00:00
|
|
|
const saveToFlash = () => {
|
2022-12-16 11:00:00 +00:00
|
|
|
// save messages from RAM to flash if we decide not to launch app
|
|
|
|
// We apply all of Bangle.MESSAGES here in one write
|
|
|
|
if (!Bangle.MESSAGES || !Bangle.MESSAGES.length) return;
|
|
|
|
let messages = require("messages").getMessages(msg);
|
|
|
|
(Bangle.MESSAGES || []).forEach(m => require("messages").apply(m, messages));
|
|
|
|
require("messages").write(messages);
|
2022-12-07 20:56:07 +00:00
|
|
|
delete Bangle.MESSAGES;
|
|
|
|
}
|
2022-11-20 14:41:02 +00:00
|
|
|
msg.handled = true;
|
2022-12-16 11:00:00 +00:00
|
|
|
if ((msg.t!=="add" || !msg.new) && (type!=="music")) // music always has t:"modify"
|
|
|
|
return saveToFlash();
|
2022-11-20 14:41:02 +00:00
|
|
|
|
|
|
|
const quiet = (require("Storage").readJSON("setting.json", 1) || {}).quiet;
|
|
|
|
const unlockWatch = appSettings.unlockWatch;
|
|
|
|
// don't auto-open messages in quiet mode if quietNoAutOpn is true
|
|
|
|
if ((quiet && appSettings.quietNoAutOpn) || appSettings.noAutOpn)
|
|
|
|
loadMessages = false;
|
|
|
|
// after a delay load the app, to ensure we have all the messages
|
|
|
|
if (exports.messageTimeout) clearTimeout(exports.messageTimeout);
|
|
|
|
exports.messageTimeout = setTimeout(function() {
|
|
|
|
delete exports.messageTimeout;
|
2022-12-11 17:34:07 +00:00
|
|
|
if (!Bangle.MESSAGES) return; // message was removed during the delay
|
2022-11-20 14:41:02 +00:00
|
|
|
if (type!=="music") {
|
2022-12-07 20:56:07 +00:00
|
|
|
if (!loadMessages) {
|
|
|
|
// not opening the app, just buzz
|
|
|
|
saveToFlash();
|
|
|
|
return require("messages").buzz(msg.src);
|
|
|
|
}
|
2022-11-20 14:41:02 +00:00
|
|
|
if (!quiet && unlockWatch) {
|
|
|
|
Bangle.setLocked(false);
|
|
|
|
Bangle.setLCDPower(1); // turn screen on
|
|
|
|
}
|
|
|
|
}
|
2022-12-16 11:00:00 +00:00
|
|
|
// if loading the gui would reload everything, we must save our messages
|
|
|
|
if (loadWillReset()) saveToFlash();
|
2022-11-20 14:41:02 +00:00
|
|
|
exports.open(msg);
|
|
|
|
}, 500);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Launch GUI app with given message
|
|
|
|
* @param {object} msg
|
|
|
|
*/
|
|
|
|
exports.open = function(msg) {
|
2022-12-15 10:34:40 +00:00
|
|
|
if (msg && msg.id) {
|
|
|
|
// force a display by setting it as new and ensuring it ends up at the beginning of messages list
|
|
|
|
msg.new = 1;
|
|
|
|
if (loadWillReset()) {
|
|
|
|
// no fast loading: store message to load in flash - `msg` will be put in first
|
2022-12-07 20:56:07 +00:00
|
|
|
require("messages").save(msg, {force: 1});
|
2022-12-15 10:34:40 +00:00
|
|
|
} else {
|
|
|
|
// fast load - putting it at the end of Bangle.MESSAGES ensures it goes at the start of messages list
|
|
|
|
if (!Bangle.MESSAGES) Bangle.MESSAGES=[];
|
|
|
|
Bangle.MESSAGES = Bangle.MESSAGES.filter(m => m.id!=msg.id)
|
|
|
|
Bangle.MESSAGES.push(msg); // putting at the
|
2022-12-07 20:56:07 +00:00
|
|
|
}
|
2022-11-20 14:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Bangle.load((msg && msg.new && msg.id!=="music") ? "messagegui.new.js" : "messagegui.app.js");
|
|
|
|
};
|