2022-09-14 19:11:38 +03:00
|
|
|
#include <furi.h>
|
|
|
|
#include <gui/gui.h>
|
2022-10-28 17:08:50 +03:00
|
|
|
#include <assets_icons.h>
|
2022-09-14 19:11:38 +03:00
|
|
|
#include <gui/view_dispatcher.h>
|
|
|
|
#include <storage/storage.h>
|
2022-10-06 18:37:53 +03:00
|
|
|
#include <gui/modules/loading.h>
|
2022-09-14 19:11:38 +03:00
|
|
|
#include <dialogs/dialogs.h>
|
2023-03-01 20:57:27 +03:00
|
|
|
#include <toolbox/path.h>
|
2022-09-14 19:11:38 +03:00
|
|
|
#include <flipper_application/flipper_application.h>
|
2023-03-14 17:29:28 +03:00
|
|
|
#include <loader/firmware_api/firmware_api.h>
|
2022-10-06 18:37:53 +03:00
|
|
|
#include "fap_loader_app.h"
|
2022-09-14 19:11:38 +03:00
|
|
|
|
|
|
|
#define TAG "fap_loader_app"
|
|
|
|
|
2022-10-06 18:37:53 +03:00
|
|
|
struct FapLoader {
|
2022-09-14 19:11:38 +03:00
|
|
|
FlipperApplication* app;
|
|
|
|
Storage* storage;
|
|
|
|
DialogsApp* dialogs;
|
|
|
|
Gui* gui;
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* fap_path;
|
2022-09-26 18:59:28 +03:00
|
|
|
ViewDispatcher* view_dispatcher;
|
|
|
|
Loading* loading;
|
2022-10-06 18:37:53 +03:00
|
|
|
};
|
2022-09-14 19:11:38 +03:00
|
|
|
|
2022-10-06 18:37:53 +03:00
|
|
|
bool fap_loader_load_name_and_icon(
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* path,
|
2022-10-06 18:37:53 +03:00
|
|
|
Storage* storage,
|
2022-10-05 18:15:23 +03:00
|
|
|
uint8_t** icon_ptr,
|
|
|
|
FuriString* item_name) {
|
2023-03-14 17:29:28 +03:00
|
|
|
FlipperApplication* app = flipper_application_alloc(storage, firmware_api_interface);
|
2022-09-14 19:11:38 +03:00
|
|
|
|
|
|
|
FlipperApplicationPreloadStatus preload_res =
|
2022-10-05 18:15:23 +03:00
|
|
|
flipper_application_preload_manifest(app, furi_string_get_cstr(path));
|
2022-09-14 19:11:38 +03:00
|
|
|
|
|
|
|
bool load_success = false;
|
|
|
|
|
|
|
|
if(preload_res == FlipperApplicationPreloadStatusSuccess) {
|
|
|
|
const FlipperApplicationManifest* manifest = flipper_application_get_manifest(app);
|
|
|
|
if(manifest->has_icon) {
|
|
|
|
memcpy(*icon_ptr, manifest->icon, FAP_MANIFEST_MAX_ICON_SIZE);
|
|
|
|
}
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_set(item_name, manifest->name);
|
2022-09-14 19:11:38 +03:00
|
|
|
load_success = true;
|
|
|
|
} else {
|
2022-10-05 18:15:23 +03:00
|
|
|
FURI_LOG_E(TAG, "FAP Loader failed to preload %s", furi_string_get_cstr(path));
|
2022-09-14 19:11:38 +03:00
|
|
|
load_success = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
flipper_application_free(app);
|
|
|
|
return load_success;
|
|
|
|
}
|
|
|
|
|
2022-10-06 18:37:53 +03:00
|
|
|
static bool fap_loader_item_callback(
|
|
|
|
FuriString* path,
|
|
|
|
void* context,
|
|
|
|
uint8_t** icon_ptr,
|
|
|
|
FuriString* item_name) {
|
|
|
|
FapLoader* fap_loader = context;
|
|
|
|
furi_assert(fap_loader);
|
|
|
|
return fap_loader_load_name_and_icon(path, fap_loader->storage, icon_ptr, item_name);
|
|
|
|
}
|
|
|
|
|
2022-09-14 19:11:38 +03:00
|
|
|
static bool fap_loader_run_selected_app(FapLoader* loader) {
|
|
|
|
furi_assert(loader);
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* error_message;
|
2022-09-14 19:11:38 +03:00
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
error_message = furi_string_alloc_set("unknown error");
|
2022-09-14 19:11:38 +03:00
|
|
|
|
|
|
|
bool file_selected = false;
|
|
|
|
bool show_error = true;
|
|
|
|
do {
|
|
|
|
file_selected = true;
|
2023-03-14 17:29:28 +03:00
|
|
|
loader->app = flipper_application_alloc(loader->storage, firmware_api_interface);
|
2022-10-07 20:06:29 +03:00
|
|
|
size_t start = furi_get_tick();
|
2022-09-14 19:11:38 +03:00
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
FURI_LOG_I(TAG, "FAP Loader is loading %s", furi_string_get_cstr(loader->fap_path));
|
2022-09-14 19:11:38 +03:00
|
|
|
|
|
|
|
FlipperApplicationPreloadStatus preload_res =
|
2022-10-05 18:15:23 +03:00
|
|
|
flipper_application_preload(loader->app, furi_string_get_cstr(loader->fap_path));
|
2022-09-14 19:11:38 +03:00
|
|
|
if(preload_res != FlipperApplicationPreloadStatusSuccess) {
|
|
|
|
const char* err_msg = flipper_application_preload_status_to_string(preload_res);
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(error_message, "Preload failed: %s", err_msg);
|
2022-09-14 19:11:38 +03:00
|
|
|
FURI_LOG_E(
|
|
|
|
TAG,
|
|
|
|
"FAP Loader failed to preload %s: %s",
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_get_cstr(loader->fap_path),
|
2022-09-14 19:11:38 +03:00
|
|
|
err_msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
FURI_LOG_I(TAG, "FAP Loader is mapping");
|
|
|
|
FlipperApplicationLoadStatus load_status = flipper_application_map_to_memory(loader->app);
|
|
|
|
if(load_status != FlipperApplicationLoadStatusSuccess) {
|
|
|
|
const char* err_msg = flipper_application_load_status_to_string(load_status);
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(error_message, "Load failed: %s", err_msg);
|
2022-09-14 19:11:38 +03:00
|
|
|
FURI_LOG_E(
|
|
|
|
TAG,
|
|
|
|
"FAP Loader failed to map to memory %s: %s",
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_get_cstr(loader->fap_path),
|
2022-09-14 19:11:38 +03:00
|
|
|
err_msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-10-07 20:06:29 +03:00
|
|
|
FURI_LOG_I(TAG, "Loaded in %ums", (size_t)(furi_get_tick() - start));
|
2022-10-14 19:54:52 +03:00
|
|
|
FURI_LOG_I(TAG, "FAP Loader is starting app");
|
2022-09-14 19:11:38 +03:00
|
|
|
|
|
|
|
FuriThread* thread = flipper_application_spawn(loader->app, NULL);
|
2023-03-01 20:57:27 +03:00
|
|
|
|
|
|
|
FuriString* app_name = furi_string_alloc();
|
|
|
|
path_extract_filename_no_ext(furi_string_get_cstr(loader->fap_path), app_name);
|
|
|
|
furi_thread_set_appid(thread, furi_string_get_cstr(app_name));
|
|
|
|
furi_string_free(app_name);
|
|
|
|
|
2022-09-14 19:11:38 +03:00
|
|
|
furi_thread_start(thread);
|
|
|
|
furi_thread_join(thread);
|
|
|
|
|
|
|
|
show_error = false;
|
|
|
|
int ret = furi_thread_get_return_code(thread);
|
|
|
|
|
|
|
|
FURI_LOG_I(TAG, "FAP app returned: %i", ret);
|
|
|
|
} while(0);
|
|
|
|
|
|
|
|
if(show_error) {
|
|
|
|
DialogMessage* message = dialog_message_alloc();
|
|
|
|
dialog_message_set_header(message, "Error", 64, 0, AlignCenter, AlignTop);
|
|
|
|
dialog_message_set_buttons(message, NULL, NULL, NULL);
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* buffer;
|
|
|
|
buffer = furi_string_alloc();
|
|
|
|
furi_string_printf(buffer, "%s", furi_string_get_cstr(error_message));
|
|
|
|
furi_string_replace(buffer, ":", "\n");
|
2022-09-14 19:11:38 +03:00
|
|
|
dialog_message_set_text(
|
2022-10-05 18:15:23 +03:00
|
|
|
message, furi_string_get_cstr(buffer), 64, 32, AlignCenter, AlignCenter);
|
2022-09-14 19:11:38 +03:00
|
|
|
|
|
|
|
dialog_message_show(loader->dialogs, message);
|
|
|
|
dialog_message_free(message);
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(buffer);
|
2022-09-14 19:11:38 +03:00
|
|
|
}
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(error_message);
|
2022-09-14 19:11:38 +03:00
|
|
|
|
|
|
|
if(file_selected) {
|
|
|
|
flipper_application_free(loader->app);
|
|
|
|
}
|
|
|
|
|
|
|
|
return file_selected;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool fap_loader_select_app(FapLoader* loader) {
|
|
|
|
const DialogsFileBrowserOptions browser_options = {
|
|
|
|
.extension = ".fap",
|
|
|
|
.skip_assets = true,
|
2022-10-06 18:37:53 +03:00
|
|
|
.icon = &I_unknown_10px,
|
2022-09-14 19:11:38 +03:00
|
|
|
.hide_ext = true,
|
|
|
|
.item_loader_callback = fap_loader_item_callback,
|
|
|
|
.item_loader_context = loader,
|
2022-12-07 17:17:41 +03:00
|
|
|
.base_path = EXT_PATH("apps"),
|
2022-09-14 19:11:38 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return dialog_file_browser_show(
|
|
|
|
loader->dialogs, loader->fap_path, loader->fap_path, &browser_options);
|
|
|
|
}
|
|
|
|
|
2022-10-06 18:37:53 +03:00
|
|
|
static FapLoader* fap_loader_alloc(const char* path) {
|
2022-12-26 15:13:30 +03:00
|
|
|
FapLoader* loader = malloc(sizeof(FapLoader)); //-V799
|
2022-10-06 18:37:53 +03:00
|
|
|
loader->fap_path = furi_string_alloc_set(path);
|
2022-09-14 19:11:38 +03:00
|
|
|
loader->storage = furi_record_open(RECORD_STORAGE);
|
|
|
|
loader->dialogs = furi_record_open(RECORD_DIALOGS);
|
|
|
|
loader->gui = furi_record_open(RECORD_GUI);
|
2022-09-26 18:59:28 +03:00
|
|
|
loader->view_dispatcher = view_dispatcher_alloc();
|
|
|
|
loader->loading = loading_alloc();
|
|
|
|
view_dispatcher_attach_to_gui(
|
|
|
|
loader->view_dispatcher, loader->gui, ViewDispatcherTypeFullscreen);
|
|
|
|
view_dispatcher_add_view(loader->view_dispatcher, 0, loading_get_view(loader->loading));
|
2022-10-06 18:37:53 +03:00
|
|
|
return loader;
|
2022-12-26 15:13:30 +03:00
|
|
|
} //-V773
|
2022-10-06 18:37:53 +03:00
|
|
|
|
|
|
|
static void fap_loader_free(FapLoader* loader) {
|
|
|
|
view_dispatcher_remove_view(loader->view_dispatcher, 0);
|
|
|
|
loading_free(loader->loading);
|
|
|
|
view_dispatcher_free(loader->view_dispatcher);
|
|
|
|
furi_string_free(loader->fap_path);
|
|
|
|
furi_record_close(RECORD_GUI);
|
|
|
|
furi_record_close(RECORD_DIALOGS);
|
|
|
|
furi_record_close(RECORD_STORAGE);
|
|
|
|
free(loader);
|
|
|
|
}
|
2022-09-14 19:11:38 +03:00
|
|
|
|
2022-10-06 18:37:53 +03:00
|
|
|
int32_t fap_loader_app(void* p) {
|
|
|
|
FapLoader* loader;
|
2022-09-14 19:11:38 +03:00
|
|
|
if(p) {
|
2022-10-06 18:37:53 +03:00
|
|
|
loader = fap_loader_alloc((const char*)p);
|
2022-10-13 21:09:37 +03:00
|
|
|
view_dispatcher_switch_to_view(loader->view_dispatcher, 0);
|
2022-09-14 19:11:38 +03:00
|
|
|
fap_loader_run_selected_app(loader);
|
|
|
|
} else {
|
2022-10-06 18:37:53 +03:00
|
|
|
loader = fap_loader_alloc(EXT_PATH("apps"));
|
2022-09-14 19:11:38 +03:00
|
|
|
while(fap_loader_select_app(loader)) {
|
2022-09-26 18:59:28 +03:00
|
|
|
view_dispatcher_switch_to_view(loader->view_dispatcher, 0);
|
2022-09-14 19:11:38 +03:00
|
|
|
fap_loader_run_selected_app(loader);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-06 18:37:53 +03:00
|
|
|
fap_loader_free(loader);
|
2022-09-14 19:11:38 +03:00
|
|
|
return 0;
|
2022-10-06 18:37:53 +03:00
|
|
|
}
|