autoreset: add settings

- black-/whitelist
- timeout length
pull/3395/head
thyttan 2024-05-01 23:22:34 +02:00
parent 1528a42c09
commit 2493d4c7c9
5 changed files with 145 additions and 8 deletions

View File

@ -1 +1,2 @@
0.01: New App!
0.02: Add black- and whitelist for apps. Configure the timout time.

View File

@ -6,11 +6,11 @@ Sets a timeout to load the clock face. The timeout is stopped and started again
Install with app loader and Auto Reset will run in background. If you don't interact with the watch it will time out to the clock face after 10 minutes.
Through the settings apps can be black-/whitelisted and the timeout length can be configured.
## TODO
- Add settings page
- set how many minutes the timeout should count down.
- whitelist/blacklist for apps.
- per app specific timeout lengths?
## Requests

View File

@ -1,12 +1,29 @@
{
const DEFAULTS = {
mode: 0,
apps: [],
timeout: 10
};
const settings = require("Storage").readJSON("autoreset.json", 1) || DEFAULTS;
// Check if the back button should be enabled for the current app.
// app is the src file of the app.
// Derivative of the backswipe app's logic.
function enabledForApp(app) {
if (Bangle.CLOCK==1) return false;
if (!settings) return true;
let isListed = settings.apps.filter((a) => a.files.includes(app)).length > 0;
return settings.mode===0?!isListed:isListed;
}
let timeoutAutoreset;
let resetTimeoutAutoreset = (force)=>{
const resetTimeoutAutoreset = (force)=>{
if (timeoutAutoreset) clearTimeout(timeoutAutoreset);
setTimeout(()=>{ // Short outer timeout to make sure we have time to leave clock face before checking `Bangle.CLOCK!=1`.
if (Bangle.CLOCK!=1) { // Only add timeout if not already on clock face.
if (enabledForApp(global.__FILE__)) {
timeoutAutoreset = setTimeout(()=>{
if (Bangle.CLOCK!=1) Bangle.showClock();
}, 10*60*1000);
}, settings.timeout*60*1000);
}
},200);
};

View File

@ -1,6 +1,6 @@
{ "id": "autoreset",
"name": "Auto Reset",
"version":"0.01",
"version":"0.02",
"description": "Sets a timeout to load the clock face. The timeout is stopped and started again upon user input.",
"icon": "app.png",
"type": "bootloader",
@ -8,6 +8,10 @@
"supports" : ["BANGLEJS2"],
"readme": "README.md",
"storage": [
{"name":"autoreset.boot.js","url":"boot.js"}
{"name":"autoreset.boot.js","url":"boot.js"},
{"name":"autoreset.settings.js","url":"settings.js"}
],
"data":[
{"name":"autoreset.json"}
]
}

115
apps/autoreset/settings.js Normal file
View File

@ -0,0 +1,115 @@
(function(back) {
var FILE = 'autoreset.json';
// Mode can be 'blacklist' or 'whitelist'
// Apps is an array of app info objects, where all the apps that are there are either blocked or allowed, depending on the mode
var DEFAULTS = {
'mode': 0,
'apps': [],
'timeout': 10
};
var settings = {};
var loadSettings = function() {
settings = require('Storage').readJSON(FILE, 1) || DEFAULTS;
};
var saveSettings = function(settings) {
require('Storage').write(FILE, settings);
};
// Get all app info files
var getApps = function() {
var apps = require('Storage').list(/\.info$/).map(appInfoFileName => {
var appInfo = require('Storage').readJSON(appInfoFileName, 1);
return appInfo && {
'name': appInfo.name,
'sortorder': appInfo.sortorder,
'src': appInfo.src,
'files': appInfo.files
};
}).filter(app => app && !!app.src);
apps.sort((a, b) => {
var n = (0 | a.sortorder) - (0 | b.sortorder);
if (n) return n; // do sortorder first
if (a.name < b.name) return -1;
if (a.name > b.name) return 1;
return 0;
});
return apps;
};
var showMenu = function() {
var menu = {
'': { 'title': 'Auto Reset' },
/*LANG*/'< Back': () => {
back();
},
/*LANG*/'Mode': {
value: settings.mode,
min: 0,
max: 1,
format: v => ["Blacklist", "Whitelist"][v],
onchange: v => {
settings.mode = v;
saveSettings(settings);
},
},
/*LANG*/'App List': () => {
showAppSubMenu();
},
/*LANG*/'Timeout [min]': {
value: settings.timeout,
min: 0.25, max: 30, step : 0.25,
format: v => v,
onchange: v => {
settings.timeout = v;
saveSettings(settings);
},
},
};
E.showMenu(menu);
};
var showAppSubMenu = function() {
var menu = {
'': { 'title': 'Auto Reset' },
'< Back': () => {
showMenu();
},
'Add App': () => {
showAppList();
}
};
settings.apps.forEach(app => {
menu[app.name] = () => {
settings.apps.splice(settings.apps.indexOf(app), 1);
saveSettings(settings);
showAppSubMenu();
}
});
E.showMenu(menu);
}
var showAppList = function() {
var apps = getApps();
var menu = {
'': { 'title': 'Auto Reset' },
/*LANG*/'< Back': () => {
showMenu();
}
};
apps.forEach(app => {
menu[app.name] = () => {
settings.apps.push(app);
saveSettings(settings);
showAppSubMenu();
}
});
E.showMenu(menu);
}
loadSettings();
showMenu();
})