|
| 1 | +{ |
| 2 | +// Things that use `eval` instead of `load` or `Bangle.load` (e.g. auto displaying new messages - `messagegui.new.js`) can affect the app history record unexpectedly. They don't themselves get added to the history (since we don't override `eval`) and if they use one of the other two mentioned load functions they may remove the last recorded entry. |
| 3 | +// Seems like `apphist` must be placed before `Fastload Utils` in execution order at boot for compatibility. Otherwise the Bangle.js 2 craps out, freezed up and soon reboots extra hard before loading the clock face again. This is ensured by metadata placing apphist at order 4 and Fastload Utils at order 5 in the boot sequence. |
| 4 | + |
| 5 | +const s = require("Storage"); |
| 6 | +const SETTINGS = s.readJSON("apphist.settings.json") || {}; |
| 7 | + |
| 8 | +global._load = global.load; |
| 9 | +Bangle._load = Bangle.load; |
| 10 | + |
| 11 | +let appHistory = s.readJSON("apphist.json",true)||[]; |
| 12 | +const resetHistory = ()=>{appHistory=[];s.writeJSON("apphist.json",appHistory);}; |
| 13 | +const recordHistory = ()=>{s.writeJSON("apphist.json",appHistory);}; |
| 14 | + |
| 15 | +const traverseHistory = (name)=>{ |
| 16 | + if (name && name!=".bootcde" && !(name=="quicklaunch.app.js" && SETTINGS.disregardQuicklaunch)) { |
| 17 | + // store the name of the app to launch |
| 18 | + appHistory.push(name); |
| 19 | + } else if (name==".bootcde") { // when Bangle.showClock is called |
| 20 | + resetHistory(); |
| 21 | + } else if (name=="quicklaunch.app.js" && SETTINGS.disregardQuicklaunch) { |
| 22 | + // do nothing with history |
| 23 | + } else { |
| 24 | + // go back in history |
| 25 | + appHistory.pop(); |
| 26 | + name = appHistory[appHistory.length-1]; |
| 27 | + } |
| 28 | + return name; |
| 29 | +}; |
| 30 | + |
| 31 | +const addHistoryTraversal = (loadFn => (name) => { |
| 32 | + name = traverseHistory(name); |
| 33 | + loadFn(name); |
| 34 | +}); |
| 35 | + |
| 36 | +const addHistoryTraversalFastload = (loadFn => (name) => { |
| 37 | + global.load = global._load; // Only run through traverseHistory once, not both for Bangle.load and global.load. |
| 38 | + name = traverseHistory(name); |
| 39 | + loadFn(name); |
| 40 | + global.load = globalLoadWithHistory; |
| 41 | +}); |
| 42 | + |
| 43 | +const globalLoadWithHistory = addHistoryTraversal(global.load); |
| 44 | +global.load = globalLoadWithHistory; |
| 45 | +Bangle.load = addHistoryTraversalFastload(Bangle._load); |
| 46 | + |
| 47 | +E.on('kill', ()=>{ |
| 48 | + // Usually record history, but reset it if long press of HW button was used. |
| 49 | + // FIXME: May be tricky for user to release button quickly enough to not trigger resetHistory. Commented out for now. |
| 50 | + // if (!BTN.read()) recordHistory(); else resetHistory(); |
| 51 | + recordHistory(); |
| 52 | +}); |
| 53 | +} |
0 commit comments