add local app discovery: scan apps folder, render on home screen
This commit is contained in:
151
src/main/assets/apps/home/home.lua
Normal file
151
src/main/assets/apps/home/home.lua
Normal file
@@ -0,0 +1,151 @@
|
||||
-- home.lua - Home screen dynamic app rendering
|
||||
-- Handles system apps and discovered third-party apps
|
||||
|
||||
-- System apps with their navigation keys and colors
|
||||
local system_apps = {
|
||||
-- Row 1
|
||||
{name = "Phone", icon = "phone", color = "#4CAF50", nav = "dialer"},
|
||||
{name = "Messages", icon = "message", color = "#2196F3", nav = "messages"},
|
||||
{name = "Contacts", icon = "contacts", color = "#FF9800", nav = "contacts"},
|
||||
{name = "Browser", icon = "browser", color = "#F44336", nav = "browser"},
|
||||
-- Row 2
|
||||
{name = "Gallery", icon = "gallery", color = "#9C27B0", nav = nil},
|
||||
{name = "Camera", icon = "camera", color = "#00BCD4", nav = "camera"},
|
||||
{name = "Settings", icon = "settings", color = "#607D8B", nav = "settings"},
|
||||
{name = "Music", icon = "music", color = "#E91E63", nav = "music"},
|
||||
-- Row 3
|
||||
{name = "Calendar", icon = "calendar", color = "#3F51B5", nav = nil},
|
||||
{name = "Clock", icon = "clock", color = "#009688", nav = nil},
|
||||
{name = "Notes", icon = "notes", color = "#795548", nav = nil},
|
||||
{name = "Maps", icon = "maps", color = "#FF5722", nav = nil},
|
||||
-- Row 4
|
||||
{name = "Store", icon = "store", color = "#8BC34A", nav = "store"},
|
||||
{name = "Files", icon = "files", color = "#CDDC39", nav = nil},
|
||||
{name = "Calculator", icon = "calculator", color = "#FFC107", nav = nil},
|
||||
{name = "Weather", icon = "weather", color = "#673AB7", nav = nil},
|
||||
}
|
||||
|
||||
-- State
|
||||
local installed_apps = {}
|
||||
local home_document = nil -- Store document reference
|
||||
|
||||
-- Initialize on load (receives document from onload event)
|
||||
function initHome(doc)
|
||||
print("[Home] Initializing home screen...")
|
||||
home_document = doc
|
||||
|
||||
-- Get installed third-party apps
|
||||
if mosis and mosis.apps then
|
||||
installed_apps = mosis.apps.getInstalled() or {}
|
||||
print("[Home] Found " .. #installed_apps .. " installed apps")
|
||||
|
||||
-- Filter to only third-party (non-system) apps
|
||||
local third_party = {}
|
||||
for _, app in ipairs(installed_apps) do
|
||||
if not app.is_system_app then
|
||||
table.insert(third_party, app)
|
||||
print("[Home] Third-party app: " .. app.name .. " (" .. app.package_id .. ")")
|
||||
end
|
||||
end
|
||||
installed_apps = third_party
|
||||
else
|
||||
print("[Home] Warning: mosis.apps API not available")
|
||||
installed_apps = {}
|
||||
end
|
||||
|
||||
-- Render dynamic apps
|
||||
renderThirdPartyApps()
|
||||
end
|
||||
|
||||
-- Generate a color based on package_id
|
||||
function getAppColor(package_id)
|
||||
local colors = {
|
||||
"#BB86FC", "#03DAC6", "#FF9800", "#2196F3",
|
||||
"#4CAF50", "#F44336", "#E91E63", "#3F51B5",
|
||||
"#009688", "#795548", "#FF5722", "#673AB7"
|
||||
}
|
||||
|
||||
-- Simple hash of package_id to pick a color
|
||||
local hash = 0
|
||||
for i = 1, #package_id do
|
||||
hash = hash + package_id:byte(i)
|
||||
end
|
||||
|
||||
return colors[(hash % #colors) + 1]
|
||||
end
|
||||
|
||||
-- Get first letter for placeholder icon
|
||||
function getAppInitial(name)
|
||||
return name:sub(1, 1):upper()
|
||||
end
|
||||
|
||||
-- Render third-party apps into the grid
|
||||
function renderThirdPartyApps()
|
||||
-- Use stored document reference
|
||||
if not home_document then
|
||||
print("[Home] Could not get document reference")
|
||||
return
|
||||
end
|
||||
|
||||
local grid = home_document:GetElementById("third-party-apps")
|
||||
if not grid then
|
||||
print("[Home] third-party-apps container not found")
|
||||
return
|
||||
end
|
||||
|
||||
-- Clear existing content
|
||||
grid.inner_rml = ""
|
||||
|
||||
if #installed_apps == 0 then
|
||||
print("[Home] No third-party apps to display")
|
||||
return
|
||||
end
|
||||
|
||||
-- Build HTML for each app
|
||||
local html = ""
|
||||
for _, app in ipairs(installed_apps) do
|
||||
local color = getAppColor(app.package_id)
|
||||
local initial = getAppInitial(app.name)
|
||||
local icon_html
|
||||
|
||||
-- Check if app has an icon
|
||||
if app.icon and app.icon ~= "" then
|
||||
-- Third-party app icon path would be in their install directory
|
||||
-- For now, use initial as we need file:// protocol support
|
||||
icon_html = '<span style="font-size: 28px; color: #000000;">' .. initial .. '</span>'
|
||||
else
|
||||
icon_html = '<span style="font-size: 28px; color: #000000;">' .. initial .. '</span>'
|
||||
end
|
||||
|
||||
html = html .. [[
|
||||
<div class="app-icon">
|
||||
<div class="app-icon-image" style="background-color: ]] .. color .. [[;"
|
||||
onclick="launchThirdPartyApp(']] .. app.package_id .. [[')">
|
||||
]] .. icon_html .. [[
|
||||
</div>
|
||||
<span class="app-icon-label">]] .. app.name .. [[</span>
|
||||
</div>
|
||||
]]
|
||||
end
|
||||
|
||||
grid.inner_rml = html
|
||||
print("[Home] Rendered " .. #installed_apps .. " third-party apps")
|
||||
end
|
||||
|
||||
-- Launch a third-party app
|
||||
function launchThirdPartyApp(package_id)
|
||||
print("[Home] Launching app: " .. package_id)
|
||||
|
||||
if mosis and mosis.apps then
|
||||
local success = mosis.apps.launch(package_id)
|
||||
if success then
|
||||
print("[Home] App launched: " .. package_id)
|
||||
else
|
||||
print("[Home] Failed to launch app: " .. package_id)
|
||||
end
|
||||
else
|
||||
print("[Home] Cannot launch app: mosis.apps not available")
|
||||
end
|
||||
end
|
||||
|
||||
-- initHome() is called via onload in home.rml
|
||||
@@ -4,6 +4,7 @@
|
||||
<link type="text/rcss" href="../../ui/theme.rcss"/>
|
||||
<link type="text/rcss" href="../../ui/components.rcss"/>
|
||||
<script src="../../scripts/navigation.lua"></script>
|
||||
<script src="home.lua"></script>
|
||||
<title>Virtual Smartphone - Home</title>
|
||||
<style>
|
||||
.home-screen {
|
||||
@@ -34,9 +35,43 @@
|
||||
.status-bar-icons img {
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
/* Third-party apps section */
|
||||
.app-grid-section {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
#third-party-apps .app-icon {
|
||||
width: 25%;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
#third-party-apps .app-icon-image {
|
||||
width: 56px;
|
||||
height: 56px;
|
||||
border-radius: 14px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
margin: 0 auto 8px auto;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
#third-party-apps .app-icon-image:hover {
|
||||
opacity: 0.8;
|
||||
}
|
||||
|
||||
#third-party-apps .app-icon-label {
|
||||
display: block;
|
||||
text-align: center;
|
||||
font-size: 12px;
|
||||
color: #FFFFFF;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body class="home-screen">
|
||||
<body class="home-screen" onload="initHome(document)">
|
||||
<!-- Status Bar -->
|
||||
<div class="status-bar">
|
||||
<span class="status-bar-time">12:30</span>
|
||||
@@ -121,6 +156,11 @@
|
||||
<div class="app-icon-image" style="background-color: #673AB7;"><img src="../../icons/weather.tga"/></div>
|
||||
<span class="app-icon-label">Weather</span>
|
||||
</div>
|
||||
|
||||
<!-- Third-party apps (dynamically populated by home.lua) -->
|
||||
<div id="third-party-apps" class="app-grid-section">
|
||||
<!-- Apps will be rendered here by home.lua -->
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -566,12 +566,93 @@ bool AppManager::DownloadFile(const std::string& url, const std::string& dest_pa
|
||||
return false;
|
||||
}
|
||||
|
||||
void AppManager::ScanAppsDirectory() {
|
||||
std::string apps_dir = m_data_root + "/apps";
|
||||
|
||||
if (!fs::exists(apps_dir)) {
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_INFO("Scanning apps directory: %s", apps_dir.c_str());
|
||||
|
||||
for (const auto& entry : fs::directory_iterator(apps_dir)) {
|
||||
if (!entry.is_directory()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
std::string package_id = entry.path().filename().string();
|
||||
|
||||
// Skip if already registered
|
||||
if (m_installed_apps.find(package_id) != m_installed_apps.end()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Look for manifest.json in direct folder or package/ subfolder
|
||||
std::string manifest_path = entry.path().string() + "/manifest.json";
|
||||
std::string manifest_path_pkg = entry.path().string() + "/package/manifest.json";
|
||||
|
||||
std::string actual_manifest;
|
||||
std::string app_base_path;
|
||||
|
||||
if (fs::exists(manifest_path)) {
|
||||
actual_manifest = manifest_path;
|
||||
app_base_path = entry.path().string();
|
||||
} else if (fs::exists(manifest_path_pkg)) {
|
||||
actual_manifest = manifest_path_pkg;
|
||||
app_base_path = entry.path().string() + "/package";
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Parse manifest
|
||||
std::ifstream mf(actual_manifest);
|
||||
if (!mf) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
json j;
|
||||
mf >> j;
|
||||
|
||||
InstalledApp app;
|
||||
app.package_id = j.value("id", package_id);
|
||||
app.name = j.value("name", package_id);
|
||||
app.version_name = j.value("version", "1.0.0");
|
||||
app.version_code = j.value("version_code", 1);
|
||||
app.install_path = entry.path().string();
|
||||
app.entry_point = j.value("entry", "main.rml");
|
||||
app.icon_path = j.value("icon", "");
|
||||
app.is_system_app = false;
|
||||
|
||||
if (j.contains("developer")) {
|
||||
app.developer_name = j["developer"].value("name", "");
|
||||
}
|
||||
|
||||
if (j.contains("permissions") && j["permissions"].is_array()) {
|
||||
for (const auto& perm : j["permissions"]) {
|
||||
app.permissions.push_back(perm.get<std::string>());
|
||||
}
|
||||
}
|
||||
|
||||
app.installed_at = std::chrono::system_clock::now();
|
||||
app.updated_at = std::chrono::system_clock::now();
|
||||
|
||||
m_installed_apps[app.package_id] = app;
|
||||
LOG_INFO("Discovered app: %s (%s)", app.name.c_str(), app.package_id.c_str());
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
LOG_WARN("Failed to parse manifest for %s: %s", package_id.c_str(), e.what());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AppManager::LoadInstalledApps() {
|
||||
std::string registry_path = m_data_root + "/config/apps.json";
|
||||
|
||||
std::ifstream file(registry_path);
|
||||
if (!file) {
|
||||
LOG_INFO("No existing app registry found");
|
||||
LOG_INFO("No existing app registry found, scanning directory...");
|
||||
ScanAppsDirectory();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -618,11 +699,15 @@ void AppManager::LoadInstalledApps() {
|
||||
}
|
||||
}
|
||||
|
||||
LOG_INFO("Loaded %zu installed apps", m_installed_apps.size());
|
||||
LOG_INFO("Loaded %zu apps from registry", m_installed_apps.size());
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
LOG_ERROR("Error loading app registry: %s", e.what());
|
||||
}
|
||||
|
||||
// Always scan directory to discover newly copied apps
|
||||
ScanAppsDirectory();
|
||||
LOG_INFO("Total installed apps: %zu", m_installed_apps.size());
|
||||
}
|
||||
|
||||
void AppManager::SaveInstalledApps() {
|
||||
|
||||
@@ -152,6 +152,9 @@ private:
|
||||
void LoadInstalledApps();
|
||||
void SaveInstalledApps();
|
||||
|
||||
// Directory scanning for app discovery
|
||||
void ScanAppsDirectory();
|
||||
|
||||
// Directory size calculation
|
||||
int64_t CalculateDirectorySize(const std::string& path) const;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user