BangleApps/apps/widbt_notify/widget.js

90 lines
2.8 KiB
JavaScript
Raw Normal View History

(function() {
// load settings
var settings = Object.assign({
showWidget: true,
buzzOnConnect: true,
buzzOnLoss: true,
hideConnected: true,
showMessage: true,
nextBuzz: 30000
}, require("Storage").readJSON("widbt_notify.json", true) || {});
2022-09-21 17:27:09 +00:00
// setup widget with to hide if connected and option set
var widWidth = settings.hideConnected && NRF.getSecurityStatus().connected ? 0 : 15;
// write widget with loaded settings
WIDGETS.bluetooth_notify = Object.assign(settings, {
2022-07-01 21:01:48 +00:00
// set area and width
area: "tr",
width: widWidth,
2022-07-01 21:01:48 +00:00
// setup warning status
warningEnabled: 1,
2022-07-01 21:01:48 +00:00
draw: function() {
if (this.showWidget) {
g.reset();
if (NRF.getSecurityStatus().connected) {
if (!this.hideConnected) {
g.setColor((g.getBPP() > 8) ? "#07f" : (g.theme.dark ? "#0ff" : "#00f"));
g.drawImage(atob("CxQBBgDgFgJgR4jZMawfAcA4D4NYybEYIwTAsBwDAA=="), 2 + this.x, 2 + this.y);
}
} else {
// g.setColor(g.theme.dark ? "#666" : "#999");
g.setColor("#f00"); // red is easier to distinguish from blue
g.drawImage(atob("CxQBBgDgFgJgR4jZMawfAcA4D4NYybEYIwTAsBwDAA=="), 2 + this.x, 2 + this.y);
}
}
},
redrawCurrentApp: function() {
if (typeof(draw) == 'function') {
2022-11-15 19:06:51 +00:00
g.reset().clear();
draw();
Bangle.loadWidgets();
Bangle.drawWidgets();
} else {
load(); // fallback. This might reset some variables
}
},
2022-07-01 21:01:48 +00:00
onNRF: function(connect) {
2022-09-21 17:27:09 +00:00
// setup widget with and reload widgets to show/hide if hideConnected is enabled
if (this.hideConnected) {
this.width = connect ? 0 : 15; // ensures correct redraw
Bangle.drawWidgets();
} else {
// redraw widget
this.draw();
}
if (this.warningEnabled) {
if (this.showMessage) {
E.showMessage( /*LANG*/ 'Connection\n' + (connect ? /*LANG*/ 'restored.' : /*LANG*/ 'lost.'), 'Bluetooth');
setTimeout(() => {
WIDGETS.bluetooth_notify.redrawCurrentApp();
}, 3000); // clear message - this will reload the widget, resetting 'warningEnabled'.
}
2022-07-01 21:01:48 +00:00
this.warningEnabled = 0;
setTimeout('WIDGETS.bluetooth_notify.warningEnabled = 1;', this.nextBuzz); // don't buzz for the next X seconds.
var quiet = (require('Storage').readJSON('setting.json', 1) || {}).quiet;
if (!quiet && (connect ? this.buzzOnConnect : this.buzzOnLoss)) {
Bangle.buzz(700, 1); // buzz on connection resume or loss
}
}
}
});
// clear variables
settings = undefined;
widWidth = undefined;
// setup bluetooth connection events
NRF.on('connect', (addr) => WIDGETS.bluetooth_notify.onNRF(addr));
NRF.on('disconnect', () => WIDGETS.bluetooth_notify.onNRF());
})()