.pragma library .import "WallpaperPolicy.js" as WallpaperPolicy function modeFor(preferred, width, touch) { if (["phone", "tablet", "desktop"].indexOf(preferred) >= 0) return preferred; // Width is logical pixels; a narrow desktop output stays a desktop. if (!touch) return "desktop"; return width < 600 ? "phone" : width < 1000 ? "tablet" : "desktop"; } function validate(value) { if (!value || value.version !== 1) throw new Error("Unsupported preferences schema"); if (["auto", "phone", "tablet", "desktop"].indexOf(value.mode) < 0) throw new Error("Invalid layout mode"); if (["dark", "light", "amoled"].indexOf(value.theme) < 0) throw new Error("Invalid theme"); if (typeof value.reducedMotion !== "boolean" || typeof value.touch !== "boolean") throw new Error("Invalid input or motion preferences"); if (!value.outputs || Array.isArray(value.outputs) || typeof value.outputs !== "object") throw new Error("Invalid output preferences"); Object.keys(value.outputs).forEach(function(name) { if (["auto", "phone", "tablet", "desktop"].indexOf(value.outputs[name]) < 0) throw new Error("Invalid output mode"); }); ["notificationDnd", "notificationHistory"].forEach(function(key) { if (value[key] !== undefined && typeof value[key] !== "boolean") throw new Error("Invalid notification preference: " + key); }); ["favorites", "appHistory"].forEach(function(key) { if (value[key] === undefined) return; // Existing v1 files remain compatible. const list = value[key]; if (!Array.isArray(list) || list.length > 50 || list.some(id => typeof id !== "string" || id.length === 0 || id.length > 512)) throw new Error("Invalid application list: " + key); if (list.some((id, index) => list.indexOf(id) !== index)) throw new Error("Duplicate application id: " + key); }); if (value.wallpapers !== undefined) WallpaperPolicy.validate(value.wallpapers); return value; } function recordLaunch(history, id) { return [id].concat((history || []).filter(entry => entry !== id)).slice(0, 50); } function toggleFavorite(favorites, id) { const list = favorites || []; return list.indexOf(id) >= 0 ? list.filter(entry => entry !== id) : list.concat([id]).slice(0, 50); } function rankApps(apps, favorites, history) { favorites = favorites || []; history = history || []; return apps.slice().sort(function(a, b) { const af = favorites.indexOf(a.id), bf = favorites.indexOf(b.id); if (af >= 0 || bf >= 0) { if (af < 0) return 1; if (bf < 0) return -1; return af - bf; } const ah = history.indexOf(a.id), bh = history.indexOf(b.id); if (ah >= 0 || bh >= 0) { if (ah < 0) return 1; if (bh < 0) return -1; return ah - bh; } return a.name.localeCompare(b.name); }); } // Stable ordering for home-screen tiles; persist IDs rather than model indices. function moveFavorite(favorites, id, direction) { const list = (favorites || []).slice(); const from = list.indexOf(id); const to = from + direction; if (!Number.isInteger(direction) || Math.abs(direction) !== 1 || from < 0 || to < 0 || to >= list.length) return list; const tmp = list[to]; list[to] = list[from]; list[from] = tmp; return list; } function homePages(favorites, pageSize) { if (!Number.isInteger(pageSize) || pageSize < 1) throw new Error("Invalid page size"); const ids = favorites || [], pages = []; for (let i = 0; i < ids.length; i += pageSize) pages.push(ids.slice(i, i + pageSize)); return pages.length ? pages : [[]]; }