From 2493d4c7c9c955d29d03664d3a59091df3a9765a Mon Sep 17 00:00:00 2001 From: thyttan <6uuxstm66@mozmail.comā©> Date: Wed, 1 May 2024 23:22:34 +0200 Subject: [PATCH] autoreset: add settings - black-/whitelist - timeout length --- apps/autoreset/ChangeLog | 1 + apps/autoreset/README.md | 6 +- apps/autoreset/boot.js | 23 ++++++- apps/autoreset/metadata.json | 8 ++- apps/autoreset/settings.js | 115 +++++++++++++++++++++++++++++++++++ 5 files changed, 145 insertions(+), 8 deletions(-) create mode 100644 apps/autoreset/settings.js diff --git a/apps/autoreset/ChangeLog b/apps/autoreset/ChangeLog index 5560f00bc..cef136817 100644 --- a/apps/autoreset/ChangeLog +++ b/apps/autoreset/ChangeLog @@ -1 +1,2 @@ 0.01: New App! +0.02: Add black- and whitelist for apps. Configure the timout time. diff --git a/apps/autoreset/README.md b/apps/autoreset/README.md index 965b2e291..3d086ee0e 100644 --- a/apps/autoreset/README.md +++ b/apps/autoreset/README.md @@ -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 diff --git a/apps/autoreset/boot.js b/apps/autoreset/boot.js index 3302eca08..fc8d9ec0a 100644 --- a/apps/autoreset/boot.js +++ b/apps/autoreset/boot.js @@ -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); }; diff --git a/apps/autoreset/metadata.json b/apps/autoreset/metadata.json index ca35b4d7a..c8866924b 100644 --- a/apps/autoreset/metadata.json +++ b/apps/autoreset/metadata.json @@ -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"} ] } diff --git a/apps/autoreset/settings.js b/apps/autoreset/settings.js new file mode 100644 index 000000000..8cbccd6f0 --- /dev/null +++ b/apps/autoreset/settings.js @@ -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(); +})