BangleApps/apps/swp2clk/boot.js

134 lines
4.1 KiB
JavaScript
Raw Normal View History

/**
* How does this work?
*
* Every *boot.js file is executed everytime any app is loaded, including this one.
* We just need to figure out which app is currently loaded, in case we are in the white list / black list mode,
* to figure out if the swipe handler should be attached or not.
* It does not seem to be the case that this can be done easily, but we can work around it.
* It seems that every app is loaded via the global "load" function, which takes a fileName as it's first parameter to load any app
* or the default clock when the fileName is undefined.
* To be able to use this for us, we wrap the global "load" function, and determine before loading the next app,
* whether the swipe handler should be added or not, since we now know which app will be loaded.
* Note: We cannot add the swipe handler inside the wrapped "load" function, because once the "load" function is complete
* everything is cleaned up. That's why we merely save a flag, whether the swipe handler should be attached or not,
* which is evaluated once this file is executed again, which will be right after the load function completes
* (since every *boot.js file is executed everytime any app is loaded).
*/
(function () {
2021-12-22 22:30:29 +00:00
var DEBUG = false;
var FILE = "swp2clk.data.json";
var swipeHandler = (dir) => {
log("swipe");
log(dir);
if (dir === 1) {
if (!Bangle.showClock) load();
else Bangle.showClock();
}
};
var main = () => {
var settings = readSettings();
if (settings.addSwipeHandler) {
Bangle.on("swipe", swipeHandler);
}
var global_load = global.load;
global.load = (fileName) => {
log("loading filename!");
log(fileName);
var settings = readSettings();
if (fileName && fileName != ".bootcde") {
// "Off"
if (settings.mode === 0) {
settings.addSwipeHandler = false;
}
// "White List"
if (settings.mode === 1) {
if (settings.whiteList.indexOf(fileName) >= 0) {
settings.addSwipeHandler = true;
} else {
settings.addSwipeHandler = false;
}
}
// "Black List"
if (settings.mode === 2) {
if (settings.blackList.indexOf(fileName) >= 0) {
settings.addSwipeHandler = false;
} else {
settings.addSwipeHandler = true;
}
}
// "Always"
if (settings.mode === 3) {
settings.addSwipeHandler = true;
}
} else {
// Clock will load
settings.addSwipeHandler = false;
}
writeSettings(settings);
global_load(fileName);
};
};
// lib functions
var log = (message) => {
if (DEBUG) {
console.log(JSON.stringify(message));
}
};
var readSettings = () => {
log("reading settings");
var settings = require("Storage").readJSON(FILE, 1) || {
mode: 0,
whiteList: [],
blackList: [],
addSwipeHandler: false,
};
log(settings);
return settings;
};
var writeSettings = (settings) => {
log("writing settings");
log(settings);
require("Storage").writeJSON(FILE, settings);
};
// start main function
2022-11-13 16:03:36 +00:00
if (!Bangle.load){
log("no Bangle.load available");
main();
2022-11-13 16:03:36 +00:00
} else {
log("Bangle.load available");
Bangle.load = (o => (fileName) => {
2022-11-13 16:03:36 +00:00
log("load " + fileName);
var settings = readSettings();
2022-11-13 16:03:36 +00:00
log("remove swipe handler");
Bangle.removeListener("swipe", swipeHandler);
if (fileName && fileName != ".bootcde") {
2022-11-13 16:03:36 +00:00
log("loading a not clock app " + fileName);
if ((settings.mode === 1 && settings.whiteList.includes(fileName)) || // "White List"
(settings.mode === 2 && !settings.blackList.includes(fileName)) || // "Black List"
settings.mode === 3) { // "Always"
2022-11-13 16:03:36 +00:00
log("register swipe handler");
// The handler must be added in the next idle time to prevent it executing directly iff Bangle.load was called in another swipe handler
setTimeout(()=>{Bangle.on("swipe", swipeHandler);},0);
}
}
o(fileName);
})(Bangle.load);
}
})();