forked from espruino/BangleApps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaunch_utils.js
More file actions
79 lines (74 loc) · 3.14 KB
/
launch_utils.js
File metadata and controls
79 lines (74 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Utility code for launchers
/** Return a cache of launchable apps/clocks
{
hash : string // hash of .info files (used to figure out if we need to recreate it)
apps : [ ...., {
name, // app's name
type, // "app"/"clock"/etc
icon, // filename of icon
src, // filename to launch
wid // boolean - does the app load widgets? used for fast loading
}, ... ]
new : did we just rebuild the cache?
}
settings : {
showClocks:bool,
onNew : function(launchCache) // called if cache is new, before we save it
}
*/
exports.cache = function(settings) {
// cache app list so launcher loads more quickly
let s = require("Storage");
let launchCache = s.readJSON("launch.cache.json", true)||{};
let launchHash = require("Storage").hash(/\.info/);
if (launchCache.hash!=launchHash) {
launchCache = {
hash : launchHash,
apps : s.list(/\.info$/)
.map(app=>{
let a=s.readJSON(app,1);
return a&&{name:a.name,type:a.type,icon:a.icon,sortorder:a.sortorder,src:a.src};
}).filter(app=>app && (app.type=="app" || (app.type=="clock" && settings.showClocks) || (app.type=="launch" && settings.showLaunchers) || !app.type))
.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;
})
};
launchCache.apps.forEach(app => { delete app.sortorder; }); // no longer needed
if (settings.onNew) settings.onNew(launchCache);
s.writeJSON("launch.cache.json", launchCache);
launchCache.new = true;
}
return launchCache;
};
/** Call with app object from .cache() */
exports.loadApp = function(app) {
if (!app.src) return E.showMessage(/*LANG*/ "App Source\nNot found"); // sanity check
if (app.wid===undefined) { // If we hadn't stored whether the app uses widgets before, check now
let s = require("Storage");
let src = s.read(app.src)
app.wid = (src!==undefined)&&src.includes("Bangle.loadWidgets");
// Now update the launch cache to save having to do this again
let launchCache = s.readJSON("launch.cache.json", true)||{};
let a = launchCache.apps.find(a=>a.src===app.src);
if (a) {
a.wid = app.wid;
require("Storage").writeJSON("launch.cache.json", launchCache);
}
}
// TODO: If there's a load screen boot app, we could call it? Or maybe Bangle.load should do that?
if (app.wid || global.WIDGETS===undefined) Bangle.load(app.src) // if app uses widgets or we don't have any, we can fast load into it
else if (Object.keys(WIDGETS).every(w=>!!WIDGETS[w].remove)) { // are widgets unloadable? !! needed before 2v29 fw
Object.keys(WIDGETS).forEach(w=>WIDGETS[w].remove());
delete global.WIDGETS;
Bangle.load(app.src)
} else load(app.src); // otherwise default to slow load
};
/** delete the cache app list */
exports.clearCache = function() {
require("Storage").erase("launch.cache.json"); // delete the cache app list
};
// Should we have a helper function for loading/hiding widgets here?