2022-03-07 21:46:57 +00:00
|
|
|
function openMusic() {
|
|
|
|
// only read settings file for first music message
|
|
|
|
if ("undefined"==typeof exports._openMusic) {
|
|
|
|
exports._openMusic = !!((require('Storage').readJSON("messages.settings.json", true) || {}).openMusic);
|
|
|
|
}
|
|
|
|
return exports._openMusic;
|
|
|
|
}
|
2021-12-06 14:27:11 +00:00
|
|
|
/* Push a new message onto messages queue, event is:
|
2021-12-08 01:39:24 +00:00
|
|
|
{t:"add",id:int, src,title,subject,body,sender,tel, important:bool, new:bool}
|
2021-12-06 14:27:11 +00:00
|
|
|
{t:"add",id:int, id:"music", state, artist, track, etc} // add new
|
2022-09-24 12:48:59 +00:00
|
|
|
{t:"remove",id:int} // remove
|
2021-12-06 14:27:11 +00:00
|
|
|
{t:"modify",id:int, title:string} // modified
|
|
|
|
*/
|
2021-11-04 17:16:02 +00:00
|
|
|
exports.pushMessage = function(event) {
|
2022-09-24 12:24:49 +00:00
|
|
|
var messages = exports.getMessages();
|
2021-11-04 17:16:02 +00:00
|
|
|
// now modify/delete as appropriate
|
|
|
|
var mIdx = messages.findIndex(m=>m.id==event.id);
|
|
|
|
if (event.t=="remove") {
|
|
|
|
if (mIdx>=0) messages.splice(mIdx, 1); // remove item
|
|
|
|
mIdx=-1;
|
|
|
|
} else { // add/modify
|
2021-12-08 01:05:35 +00:00
|
|
|
if (event.t=="add"){
|
|
|
|
if(event.new === undefined ) { // If 'new' has not been set yet, set it
|
|
|
|
event.new=true; // Assume it should be new
|
|
|
|
}
|
|
|
|
}
|
2021-11-25 15:12:04 +00:00
|
|
|
if (mIdx<0) {
|
|
|
|
mIdx=0;
|
|
|
|
messages.unshift(event); // add new messages to the beginning
|
|
|
|
}
|
2021-11-04 17:16:02 +00:00
|
|
|
else Object.assign(messages[mIdx], event);
|
2022-03-07 21:46:57 +00:00
|
|
|
if (event.id=="music" && messages[mIdx].state=="play") {
|
|
|
|
messages[mIdx].new = true; // new track, or playback (re)started
|
2022-09-24 12:48:59 +00:00
|
|
|
type = 'music';
|
2022-03-07 21:46:57 +00:00
|
|
|
}
|
2021-11-04 17:16:02 +00:00
|
|
|
}
|
|
|
|
require("Storage").writeJSON("messages.json",messages);
|
2022-09-24 12:48:59 +00:00
|
|
|
var message = mIdx<0 ? {id:event.id, t:'remove'} : messages[mIdx];
|
2021-11-04 17:16:02 +00:00
|
|
|
// if in app, process immediately
|
2022-09-24 12:48:59 +00:00
|
|
|
if ("undefined"!=typeof MESSAGES) return onMessagesModified(message);
|
|
|
|
// emit message event
|
|
|
|
var type = 'text';
|
|
|
|
if (["call", "music", "map"].includes(message.id)) type = message.id;
|
|
|
|
if (message.src && message.src.toLowerCase().startsWith("alarm")) type = "alarm";
|
|
|
|
Bangle.emit("message", type, message);
|
2022-07-15 11:10:52 +00:00
|
|
|
// update the widget icons shown
|
|
|
|
if (global.WIDGETS && WIDGETS.messages) WIDGETS.messages.update(messages,true);
|
2022-09-24 14:17:21 +00:00
|
|
|
var handleMessage = () => {
|
2022-04-27 15:28:11 +00:00
|
|
|
// if no new messages now, make sure we don't load the messages app
|
2022-09-24 14:17:21 +00:00
|
|
|
if (event.t=="remove" && exports.messageTimeout && !messages.some(m => m.new)) {
|
|
|
|
clearTimeout(exports.messageTimeout);
|
|
|
|
delete exports.messageTimeout;
|
2022-03-28 20:08:16 +00:00
|
|
|
}
|
2022-09-24 14:17:21 +00:00
|
|
|
// ok, saved now
|
|
|
|
if (event.id=="music" && Bangle.CLOCK && messages[mIdx].new && openMusic()) {
|
|
|
|
// just load the app to display music: no buzzing
|
2022-11-15 10:48:09 +00:00
|
|
|
Bangle.load("messages.app.js");
|
2022-09-24 14:17:21 +00:00
|
|
|
} else if (event.t!="add") {
|
|
|
|
// we only care if it's new
|
|
|
|
return;
|
|
|
|
} else if (event.new==false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// otherwise load messages/show widget
|
|
|
|
var loadMessages = Bangle.CLOCK || event.important;
|
|
|
|
var quiet = (require('Storage').readJSON('setting.json', 1) || {}).quiet;
|
|
|
|
var appSettings = require('Storage').readJSON('messages.settings.json', 1) || {};
|
|
|
|
var unlockWatch = appSettings.unlockWatch;
|
|
|
|
// don't auto-open messages in quiet mode if quietNoAutOpn is true
|
|
|
|
if ((quiet && appSettings.quietNoAutOpn) || appSettings.noAutOpn)
|
|
|
|
loadMessages = false;
|
|
|
|
delete appSettings;
|
|
|
|
// after a delay load the app, to ensure we have all the messages
|
|
|
|
if (exports.messageTimeout) clearTimeout(exports.messageTimeout);
|
|
|
|
exports.messageTimeout = setTimeout(function() {
|
|
|
|
exports.messageTimeout = undefined;
|
|
|
|
// if we're in a clock or it's important, go straight to messages app
|
|
|
|
if (loadMessages) {
|
|
|
|
if (!quiet && unlockWatch) {
|
|
|
|
Bangle.setLocked(false);
|
|
|
|
Bangle.setLCDPower(1); // turn screen on
|
|
|
|
}
|
|
|
|
// we will buzz when we enter the messages app
|
2022-11-15 10:48:09 +00:00
|
|
|
return Bangle.load("messages.new.js");
|
2022-09-24 14:17:21 +00:00
|
|
|
}
|
|
|
|
if (global.WIDGETS && WIDGETS.messages) WIDGETS.messages.update(messages);
|
2022-09-24 14:46:59 +00:00
|
|
|
exports.buzz(message.src);
|
2022-09-24 14:17:21 +00:00
|
|
|
}, 500);
|
|
|
|
};
|
|
|
|
setTimeout(()=>{
|
|
|
|
if (!message.handled) handleMessage();
|
|
|
|
},0);
|
2021-11-04 17:16:02 +00:00
|
|
|
}
|
2021-12-06 14:27:11 +00:00
|
|
|
/// Remove all messages
|
2022-09-24 15:08:58 +00:00
|
|
|
exports.clearAll = function() {
|
|
|
|
if ("undefined"!= typeof MESSAGES) { // we're in a messages app, clear that as well
|
2021-11-23 16:28:34 +00:00
|
|
|
MESSAGES = [];
|
2022-09-24 15:08:58 +00:00
|
|
|
}
|
|
|
|
// Clear all messages
|
|
|
|
require("Storage").writeJSON("messages.json", []);
|
2021-11-23 16:28:34 +00:00
|
|
|
// if we have a widget, update it
|
|
|
|
if (global.WIDGETS && WIDGETS.messages)
|
2022-09-24 15:08:58 +00:00
|
|
|
WIDGETS.messages.update([]);
|
|
|
|
// let message listeners know
|
|
|
|
Bangle.emit("message", "clearAll", {}); // guarantee listeners an object as `message`
|
|
|
|
// clearAll cannot be marked as "handled"
|
|
|
|
// update app if in app
|
|
|
|
if ("function"== typeof onMessagesModified) onMessagesModified();
|
2021-11-23 16:28:34 +00:00
|
|
|
}
|
2022-05-25 08:58:02 +00:00
|
|
|
|
2022-08-09 13:48:04 +00:00
|
|
|
/**
|
|
|
|
* @returns {array} All messages
|
|
|
|
*/
|
|
|
|
exports.getMessages = function() {
|
|
|
|
if ("undefined"!=typeof MESSAGES) return MESSAGES; // loaded/managed by app
|
|
|
|
return require("Storage").readJSON("messages.json",1)||[];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if there are any messages
|
|
|
|
* @returns {string} "new"/"old"/"none"
|
|
|
|
*/
|
|
|
|
exports.status = function() {
|
|
|
|
try {
|
|
|
|
let status= "none";
|
|
|
|
for(const m of exports.getMessages()) {
|
|
|
|
if (["music", "map"].includes(m.id)) continue;
|
|
|
|
if (m.new) return "new";
|
|
|
|
status = "old";
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
} catch(e) {
|
|
|
|
return "none"; // don't bother e.g. the widget with errors
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-24 14:46:59 +00:00
|
|
|
/**
|
|
|
|
* Start buzzing for new message
|
|
|
|
* @param {string} msgSrc Message src to buzz for
|
|
|
|
* @return {Promise} Resolves when initial buzz finishes (there might be repeat buzzes later)
|
|
|
|
*/
|
|
|
|
exports.buzz = function(msgSrc) {
|
|
|
|
exports.stopBuzz(); // cancel any previous buzz timeouts
|
|
|
|
if ((require('Storage').readJSON('setting.json',1)||{}).quiet) return Promise.resolve(); // never buzz during Quiet Mode
|
2022-11-16 15:17:28 +00:00
|
|
|
var msgSettings = require('Storage').readJSON("messages.settings.json", true) || {};
|
2022-09-24 14:46:59 +00:00
|
|
|
var pattern;
|
|
|
|
if (msgSrc && msgSrc.toLowerCase() === "phone") {
|
|
|
|
// special vibration pattern for incoming calls
|
2022-11-16 15:17:28 +00:00
|
|
|
pattern = msgSettings.vibrateCalls;
|
2022-09-24 14:46:59 +00:00
|
|
|
} else {
|
2022-11-16 15:17:28 +00:00
|
|
|
pattern = msgSettings.vibrate;
|
2022-09-24 14:46:59 +00:00
|
|
|
}
|
|
|
|
if (pattern === undefined) { pattern = ":"; } // pattern may be "", so we can't use || ":" here
|
|
|
|
if (!pattern) return Promise.resolve();
|
|
|
|
|
2022-11-16 15:17:28 +00:00
|
|
|
var repeat = msgSettings.repeat;
|
2022-09-24 14:46:59 +00:00
|
|
|
if (repeat===undefined) repeat=4; // repeat may be zero
|
|
|
|
if (repeat) {
|
|
|
|
exports.buzzTimeout = setTimeout(()=>require("buzz").pattern(pattern), repeat*1000);
|
2022-11-16 15:17:28 +00:00
|
|
|
var vibrateTimeout = msgSettings.vibrateTimeout;
|
2022-09-24 14:46:59 +00:00
|
|
|
if (vibrateTimeout===undefined) vibrateTimeout=60;
|
2022-10-21 10:35:03 +00:00
|
|
|
if (vibrateTimeout && !exports.stopTimeout) exports.stopTimeout = setTimeout(exports.stopBuzz, vibrateTimeout*1000);
|
2022-09-24 14:46:59 +00:00
|
|
|
}
|
|
|
|
return require("buzz").pattern(pattern);
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* Stop buzzing
|
|
|
|
*/
|
|
|
|
exports.stopBuzz = function() {
|
|
|
|
if (exports.buzzTimeout) clearTimeout(exports.buzzTimeout);
|
|
|
|
delete exports.buzzTimeout;
|
|
|
|
if (exports.stopTimeout) clearTimeout(exports.stopTimeout);
|
|
|
|
delete exports.stopTimeout;
|
|
|
|
};
|