mirror of
https://github.com/ByteWelder/Tactility.git
synced 2026-10-17 02:45:05 +00:00
303 lines
11 KiB
C++
303 lines
11 KiB
C++
#include <Tactility/app/alertdialog/AlertDialog.h>
|
|
#include <Tactility/app/apphubdetails/AppHubDetailsApp.h>
|
|
|
|
#include <app/event.h>
|
|
#include <app/manager.h>
|
|
#include <app/manifest.h>
|
|
#include <app/scheduler.h>
|
|
#include <app/start.h>
|
|
|
|
#include <app_hub/app_hub.h>
|
|
|
|
#include <lvgl_window_manager/window_manager.h>
|
|
|
|
#include <lvgl/lvgl.h>
|
|
#include <lvgl/widgets/toolbar.h>
|
|
|
|
#include <tactility/check.h>
|
|
#include <tactility/log.h>
|
|
|
|
#include <atomic>
|
|
#include <cstring>
|
|
#include <format>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace tt::app::apphubdetails {
|
|
|
|
constexpr auto* TAG = "AppHubDetails";
|
|
|
|
extern const ::AppManifest manifest;
|
|
|
|
namespace {
|
|
|
|
constexpr auto* CONFIRM_TEXT = "Confirm";
|
|
constexpr auto* CANCEL_TEXT = "Cancel";
|
|
constexpr int32_t CONFIRMATION_BUTTON_INDEX = 0;
|
|
constexpr size_t DESCRIPTION_MAX_SIZE = 2048;
|
|
|
|
struct Context {
|
|
uint32_t appInstanceId;
|
|
std::string appId;
|
|
|
|
lv_obj_t* toolbar = nullptr;
|
|
lv_obj_t* spinner = nullptr;
|
|
lv_obj_t* updateButton = nullptr;
|
|
lv_obj_t* updateLabel = nullptr;
|
|
|
|
// Set from the LVGL task (button press), read from this app's own thread (event loop) -
|
|
// both directions cross threads, hence atomic.
|
|
std::atomic<uint32_t> installDialogId = 0;
|
|
std::atomic<uint32_t> uninstallDialogId = 0;
|
|
std::atomic<uint32_t> updateDialogId = 0;
|
|
|
|
TaskEventGroup* eventGroup = nullptr;
|
|
AppHubEventSubscription appHubSub {};
|
|
};
|
|
|
|
AppHubApp getApp(Context* ctx) {
|
|
AppHubApp app {};
|
|
if (app_hub_get_app(ctx->appId.c_str(), &app) != ERROR_NONE) {
|
|
strncpy(app.id, ctx->appId.c_str(), sizeof(app.id) - 1);
|
|
strncpy(app.name, ctx->appId.c_str(), sizeof(app.name) - 1);
|
|
}
|
|
return app;
|
|
}
|
|
|
|
/** @return true when App Hub is installing or removing another app, which blocks this app's install and remove */
|
|
bool isAnotherAppBusy(Context* ctx) {
|
|
struct Search {
|
|
const char* appId;
|
|
bool busy;
|
|
} search { ctx->appId.c_str(), false };
|
|
app_hub_search(nullptr, [](const AppHubApp* app, void* context) {
|
|
auto* search = static_cast<Search*>(context);
|
|
if (app->operation_pending && strcmp(app->id, search->appId) != 0) {
|
|
search->busy = true;
|
|
return false;
|
|
}
|
|
return true;
|
|
}, &search);
|
|
return search.busy;
|
|
}
|
|
|
|
void updateViews(Context* ctx);
|
|
|
|
uint32_t showConfirmDialog(Context* ctx, const char* action) {
|
|
const auto message = std::format("{} {}?", action, getApp(ctx).name);
|
|
return alertdialog::start(ctx->appInstanceId, CONFIRM_TEXT, message, std::vector<std::string> { CONFIRM_TEXT, CANCEL_TEXT });
|
|
}
|
|
|
|
void onBackPressed(lv_event_t* e) {
|
|
auto* ctx = static_cast<Context*>(lv_event_get_user_data(e));
|
|
app_event_emit_close(ctx->appInstanceId);
|
|
}
|
|
|
|
void onInstallPressed(lv_event_t* e) {
|
|
auto* ctx = static_cast<Context*>(lv_event_get_user_data(e));
|
|
ctx->installDialogId = showConfirmDialog(ctx, "Install");
|
|
}
|
|
|
|
void onUninstallPressed(lv_event_t* e) {
|
|
auto* ctx = static_cast<Context*>(lv_event_get_user_data(e));
|
|
ctx->uninstallDialogId = showConfirmDialog(ctx, "Uninstall");
|
|
}
|
|
|
|
void onUpdatePressed(lv_event_t* e) {
|
|
auto* ctx = static_cast<Context*>(lv_event_get_user_data(e));
|
|
ctx->updateDialogId = showConfirmDialog(ctx, "Update");
|
|
}
|
|
|
|
void queueOperation(Context* ctx, error_t (*operation)(const char* id, uint32_t* operation_id), const char* failure) {
|
|
const error_t error = operation(ctx->appId.c_str(), nullptr);
|
|
if (error == ERROR_RESOURCE_BUSY) {
|
|
alertdialog::start(ctx->appInstanceId, "Busy", "Another app is being installed or removed. Try again when it's done.");
|
|
return;
|
|
} else if (error != ERROR_NONE) {
|
|
alertdialog::start(ctx->appInstanceId, "Error", failure);
|
|
return;
|
|
}
|
|
lvgl_lock();
|
|
updateViews(ctx);
|
|
lvgl_unlock();
|
|
}
|
|
|
|
void onOperationEvent(Context* ctx, const AppHubEvent& event) {
|
|
// Also another app's install or removal, as it blocks the buttons
|
|
const bool finished = event.type == APP_HUB_EVENT_INSTALLED || event.type == APP_HUB_EVENT_REMOVED;
|
|
if (finished && ctx->appId == event.app_id && event.result != ERROR_NONE) {
|
|
LOG_E(TAG, "Operation failed: %s", error_to_string(event.result));
|
|
const char* message = (event.type == APP_HUB_EVENT_REMOVED) ? "Failed to uninstall app" : "Failed to install app";
|
|
alertdialog::start(ctx->appInstanceId, "Error", message);
|
|
}
|
|
lvgl_lock();
|
|
updateViews(ctx);
|
|
lvgl_unlock();
|
|
}
|
|
|
|
void updateViews(Context* ctx) {
|
|
// Covered by another window (e.g. a dialog): createWidgets() updates the views when it's shown again
|
|
if (ctx->toolbar == nullptr) {
|
|
return;
|
|
}
|
|
lvgl_toolbar_clear_actions(ctx->toolbar);
|
|
ctx->spinner = lvgl_toolbar_add_spinner_action(ctx->toolbar);
|
|
lv_obj_set_hidden(ctx->updateLabel, true);
|
|
|
|
// This app's install or removal continues after the window closes, so it can already be running when the window opens.
|
|
// Another app's install or removal blocks this app's buttons until it's done.
|
|
const auto app = getApp(ctx);
|
|
const bool busy = app.operation_pending || ((app.installed || app.in_catalog) && isAnotherAppBusy(ctx));
|
|
lv_obj_set_hidden(ctx->spinner, !busy);
|
|
if (busy) {
|
|
return;
|
|
}
|
|
|
|
if (app.installed) {
|
|
if (app.update_available) {
|
|
ctx->updateButton = lvgl_toolbar_add_image_button_action(ctx->toolbar, LV_SYMBOL_DOWNLOAD, onUpdatePressed, ctx);
|
|
lv_obj_set_hidden(ctx->updateLabel, false);
|
|
}
|
|
lvgl_toolbar_add_image_button_action(ctx->toolbar, LV_SYMBOL_TRASH, onUninstallPressed, ctx);
|
|
} else if (app.in_catalog) {
|
|
lvgl_toolbar_add_image_button_action(ctx->toolbar, LV_SYMBOL_DOWNLOAD, onInstallPressed, ctx);
|
|
}
|
|
}
|
|
|
|
void createWidgets(lv_obj_t* parent, void* userData) {
|
|
auto* ctx = static_cast<Context*>(userData);
|
|
|
|
lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
|
|
lv_obj_set_style_pad_row(parent, 0, LV_STATE_DEFAULT);
|
|
|
|
const auto app = getApp(ctx);
|
|
ctx->toolbar = lvgl_toolbar_create(parent, app.name);
|
|
// The global toolbar nav callback only knows how to stop old-model apps.
|
|
lvgl_toolbar_set_nav_action(ctx->toolbar, LV_SYMBOL_CLOSE, onBackPressed, ctx);
|
|
auto* wrapper = lv_obj_create(parent);
|
|
lv_obj_set_style_border_width(wrapper, 0, LV_STATE_DEFAULT);
|
|
lv_obj_set_width(wrapper, LV_PCT(100));
|
|
lv_obj_set_flex_grow(wrapper, 1);
|
|
lv_obj_set_flex_flow(wrapper, LV_FLEX_FLOW_COLUMN);
|
|
|
|
ctx->updateLabel = lv_label_create(wrapper);
|
|
lv_label_set_text(ctx->updateLabel, "Update available!");
|
|
lv_obj_set_style_text_color(ctx->updateLabel, lv_color_make(0xff, 0xff, 00), LV_STATE_DEFAULT);
|
|
|
|
auto* description_label = lv_label_create(wrapper);
|
|
lv_obj_set_width(description_label, LV_PCT(100));
|
|
lv_label_set_long_mode(description_label, LV_LABEL_LONG_MODE_WRAP);
|
|
std::vector<char> description_buffer(DESCRIPTION_MAX_SIZE);
|
|
if (app_hub_get_description(ctx->appId.c_str(), description_buffer.data(), description_buffer.size()) == ERROR_NONE && description_buffer[0] != '\0') {
|
|
std::string description = description_buffer.data();
|
|
for (size_t pos = 0; (pos = description.find("\\n", pos)) != std::string::npos;) {
|
|
description.replace(pos, 2, "\n");
|
|
}
|
|
lv_label_set_text(description_label, description.c_str());
|
|
} else {
|
|
lv_label_set_text(description_label, "This app has no description yet.");
|
|
}
|
|
|
|
auto* version_label = lv_label_create(wrapper);
|
|
lv_label_set_text_fmt(version_label, "Version %s", app.in_catalog ? app.version_name : app.installed_version_name);
|
|
|
|
updateViews(ctx);
|
|
}
|
|
|
|
void destroyWidgets(void* userData) {
|
|
auto* ctx = static_cast<Context*>(userData);
|
|
ctx->toolbar = nullptr;
|
|
ctx->spinner = nullptr;
|
|
ctx->updateButton = nullptr;
|
|
ctx->updateLabel = nullptr;
|
|
}
|
|
|
|
int32_t appMain(int argc, char* argv[]) {
|
|
uint32_t appInstanceId = app_scheduler_current_app_id();
|
|
// argv layout: [0]=appId
|
|
|
|
Context ctx {};
|
|
ctx.appInstanceId = appInstanceId;
|
|
if (argc >= 1) {
|
|
ctx.appId = argv[0];
|
|
}
|
|
|
|
TaskEventGroup event_group {};
|
|
task_event_group_construct(&event_group);
|
|
ctx.eventGroup = &event_group;
|
|
|
|
AppEventSubscription sub {};
|
|
check(app_event_subscribe(&sub, &event_group) == ERROR_NONE);
|
|
check(app_hub_event_subscribe(&ctx.appHubSub, &event_group) == ERROR_NONE);
|
|
|
|
WindowId window = window_manager_create_ext(appInstanceId, createWidgets, destroyWidgets, &ctx);
|
|
|
|
bool shouldClose = false;
|
|
while (!shouldClose) {
|
|
task_event_group_wait_any(&event_group, nullptr, portMAX_DELAY);
|
|
|
|
AppEvent event {};
|
|
while (app_event_poll(&sub, &event) == ERROR_NONE) {
|
|
switch (event.type) {
|
|
case APP_EVENT_CLOSE:
|
|
shouldClose = true;
|
|
break;
|
|
case APP_EVENT_RESULT: {
|
|
bool confirmed = event.result.result == CONFIRMATION_BUTTON_INDEX;
|
|
if (event.result.launch_id == ctx.installDialogId && confirmed) {
|
|
queueOperation(&ctx, app_hub_install, "Failed to install app");
|
|
} else if (event.result.launch_id == ctx.uninstallDialogId && confirmed) {
|
|
queueOperation(&ctx, app_hub_remove, "Failed to uninstall app");
|
|
} else if (event.result.launch_id == ctx.updateDialogId && confirmed) {
|
|
queueOperation(&ctx, app_hub_install, "Failed to update app");
|
|
}
|
|
app_manager_stop(event.result.launch_id);
|
|
break;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
if (shouldClose) break;
|
|
}
|
|
|
|
AppHubEvent app_hub_event {};
|
|
while (!shouldClose && app_hub_event_poll(&ctx.appHubSub, &app_hub_event) == ERROR_NONE) {
|
|
if (app_hub_event.type != APP_HUB_EVENT_REFRESHING && app_hub_event.type != APP_HUB_EVENT_REFRESHED) {
|
|
onOperationEvent(&ctx, app_hub_event);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Queued operations continue in the background after the window closes
|
|
window_manager_remove(window);
|
|
check(app_hub_event_unsubscribe(&ctx.appHubSub) == ERROR_NONE);
|
|
check(app_event_unsubscribe(&sub) == ERROR_NONE);
|
|
task_event_group_destruct(&event_group);
|
|
|
|
return 0;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void start(const char* appId) {
|
|
// Fire-and-forget (parent_instance_id 0): AppHub's own multi-app browsing list isn't
|
|
// waiting on a result.
|
|
const char* argv[] = { appId };
|
|
uint32_t instanceId = 0;
|
|
AppStartContext context = app_start_context_for_manifest(&manifest);
|
|
app_start_context_set_arguments_ext(&context, 1, argv);
|
|
app_start_context_set_parent(&context, /*parent_instance_id=*/0);
|
|
app_start_with_context(&context, &instanceId);
|
|
}
|
|
|
|
extern const ::AppManifest manifest = {
|
|
.id = "tactility.apphubdetails",
|
|
.name = "App Details",
|
|
.category = APP_CATEGORY_SYSTEM,
|
|
.location = { APP_LOCATION_MEMORY, reinterpret_cast<void*>(appMain) },
|
|
.flags = APP_MANIFEST_FLAG_HIDDEN,
|
|
.stack = {}
|
|
};
|
|
|
|
} // namespace
|